From 8da6c4615f68a2988724e63386a79d60f4ae8a0c Mon Sep 17 00:00:00 2001 From: Denis Zheleztsov Date: Tue, 17 Oct 2017 14:09:25 +0300 Subject: [PATCH] Working answer post --- idec-answers.el | 137 ++++++++++++++++++++++------------------------ idec-customize.el | 2 +- idec-online.el | 5 ++ idec.el | 6 +- 4 files changed, 73 insertions(+), 77 deletions(-) diff --git a/idec-answers.el b/idec-answers.el index 36f5a72..fe22b46 100644 --- a/idec-answers.el +++ b/idec-answers.el @@ -27,11 +27,59 @@ ;;; Code: (require 'idec-mode) +(require 'web) -;; ANSWERS -(defun make-answer-header (id msg-hash) - "Make header with reto to ID from MSG-HASH." - (let (answer-hash subj p) +(defun point-url () + "Return url with `idec-primary-node' to send messages." + (concat idec-primary-node "u/point")) + +(defun request-is-done (result) + "Show message with RESULT code." + (message "IDEC: Sended. Result: %S" result)) + +(defun do-post-request (url data) + "Make POST request to URL with DATA." + (web-http-post + (lambda (con header data) + (request-is-done data)) + :url url + :data data)) + +(defun post-message (encoded-message) + "Do POST request to `idec-primary-node' with Base64 ENCODED-MESSAGE." + (let (json) + (setq json (make-hash-table :test 'equal)) + (puthash "pauth" idec-account-auth json) + (puthash "tmsg" encoded-message json) + (do-post-request (point-url) json)) + (message "Message sended")) + +(defun do-send-reply-post-request (message) + "Make IDEC compatible point MESSAGE and send it to `idec-primary-node'." + (message (gethash "body" message)) + (let (point-message) + (setq point-message (list + (gethash "echo" message) + (gethash "author" message) + (gethash "subj" message) + "" + (concat "@repto:" (gethash "id" message)) + (gethash "body" message))) + ;; Encode message in Base64 + (post-message (base64-encode-string (encode-coding-string (s-join "\n" point-message) 'utf-8))))) + +(defun send-message (msg) + "Send message MSG to `idec-primary-node'." + (switch-to-buffer (concat "*IDEC: answer to " (gethash "id" msg) "*")) + (puthash "body" + (s-join "\n" (-drop-last 1 (-drop 4 (split-string (buffer-string) "\n")))) + msg) + (message (gethash "body" msg)) + (do-send-reply-post-request msg)) + +(defun get-answers-hash (id msg-hash) + "Make answers hashtable from ID and MSG-HASH." + (let (answer-hash) (setq answer-hash (make-hash-table :test 'equal)) (puthash "id" id answer-hash) (puthash "echo" (get-message-field (gethash "content" msg-hash) "echo") answer-hash) @@ -44,6 +92,12 @@ (if (not (string-match "Re:" subj)) (puthash "subj" (concat "Re: " subj) answer-hash) (puthash "subj" subj answer-hash)) + answer-hash)) + +(defun make-answer-header (id msg-hash) + "Make header with reto to ID from MSG-HASH." + (let (answer-hash subj p) + (setq answer-hash (get-answers-hash id msg-hash)) (concat (concat "Answer to " id " in " (gethash "echo" answer-hash) "\n") @@ -56,84 +110,21 @@ (defun edit-answer-without-quote (id msg-hash) "Answer to message with ID MSG-HASH." - (let (answer-hash subj p) - (setq answer-hash (make-hash-table :test 'equal)) - (puthash "id" id answer-hash) - (puthash "echo" (get-message-field (gethash "content" msg-hash) "echo") answer-hash) - (puthash "author" (get-message-field (gethash "content" msg-hash) "author") answer-hash) - - (setq subj (get-message-field (gethash "content" msg-hash) "subj")) - - ;; Make `Re:' in subj if it not present. - (if (not (string-match "Re:" subj)) - (puthash "subj" (concat "Re: " subj) answer-hash) - (puthash "subj" subj answer-hash)) - + (let (answer-hash p) + (setq answer-hash (get-answers-hash id msg-hash)) (switch-to-buffer (get-buffer-create (concat "*IDEC: answer to " id "*"))) (insert (make-answer-header id msg-hash)) (forward-line) - (add-text-properties (point-min) (point) 'read-only) - - (forward-line) + (add-text-properties (point) (point-min) 'read-only) (setq p (point)) - (point-max) (insert "\n") (insert-text-button "[Send]" - 'action (lambda (x) (message "Send..."))) + 'action (lambda (x) (send-message (button-get x 'msg))) + 'msg answer-hash) (goto-char p) - ) - (idec-mode)) - -(defun edit-answer-without-quote (id msg-hash) - "Answer to message with ID MSG-HASH." - (let (answer-hash subj p) - (setq answer-hash (make-hash-table :test 'equal)) - (puthash "id" id answer-hash) - (puthash "echo" (get-message-field (gethash "content" msg-hash) "echo") answer-hash) - (puthash "author" (get-message-field (gethash "content" msg-hash) "author") answer-hash) - - (setq subj (get-message-field (gethash "content" msg-hash) "subj")) - - ;; Make `Re:' in subj if it not present. - (if (not (string-match "Re:" subj)) - (puthash "subj" (concat "Re: " subj) answer-hash) - (puthash "subj" subj answer-hash)) - - (switch-to-buffer (get-buffer-create (concat "*IDEC: answer to " id "*"))) - - (insert (concat "Answer to " id " in " (gethash "echo" answer-hash))) - ;; Write header - ;; (princ (concat "Answer to " id " in " (gethash "echo" answer-hash))) - - ;; Make it readonly - ;; (add-text-properties (point) (point-min) 'read-only) - - ;; Write author - (forward-line) - (insert (concat "\nAuthor: " (gethash "author" answer-hash) "\n")) - (add-text-properties (point-min) (point) 'read-only) - - ;; Write subj - (point-max) - (forward-line) - (insert (concat "Subj: "(gethash "subj" answer-hash))) - (forward-line) - - ;; Body - (insert "\n------- YOU MESSAGE BELLOW -------\n") - ;; (add-text-properties (beginning-of-line) (end-of-line) 'read-only) - (forward-line) - (setq p (point)) - - (point-max) - (insert "\n") - (insert-text-button "[Send]" - 'action (lambda (x) (message "Send..."))) - (goto-char p) - ) - (idec-mode)) + (idec-mode))) ;; END OF ANSWERS diff --git a/idec-customize.el b/idec-customize.el index a2f316f..0ca09d4 100644 --- a/idec-customize.el +++ b/idec-customize.el @@ -1,4 +1,4 @@ -;;; idec-customize.el --- GNU Emacs client for IDEC network +;;; idec-customize.el --- This file part of GNU Emacs client for IDEC network ;; Copyright (c) 2017 Denis Zheleztsov diff --git a/idec-online.el b/idec-online.el index 6333f69..7284ffe 100644 --- a/idec-online.el +++ b/idec-online.el @@ -114,6 +114,11 @@ (interactive) (idec-fetch-echo-list (concat idec-primary-node "list.txt"))) +(defun idec-online-browse-hidden () + "Browse hidden echo." + (interactive) + (load-echo-messages (read-string "Enter echo name: ") t)) + (provide 'idec-online) ;;; idec-online.el ends here diff --git a/idec.el b/idec.el index 259a2ea..a91bedf 100644 --- a/idec.el +++ b/idec.el @@ -97,17 +97,17 @@ Default to `idec-download-offset'" "IDEC accounts settings." :group 'idec) -(defcustom idec-account-nick nil +(defcustom idec-account-nick "" "Account nickname." :type 'string :group 'idec-accounts) -(defcustom idec-account-node nil +(defcustom idec-account-node "" "Node to send messages." :type 'string :group 'idec-accounts) -(defcustom idec-account-auth nil +(defcustom idec-account-auth "" "Account authstring." :type 'string :group 'idec-accounts)