|
27 | 27 | ;;; Code:
|
28 | 28 |
|
29 | 29 | (require 'idec-mode)
|
30 |
| - |
31 |
| -;; ANSWERS |
32 |
| -(defun make-answer-header (id msg-hash) |
33 |
| - "Make header with reto to ID from MSG-HASH." |
34 |
| - (let (answer-hash subj p) |
| 30 | +(require 'web) |
| 31 | + |
| 32 | +(defun point-url () |
| 33 | + "Return url with `idec-primary-node' to send messages." |
| 34 | + (concat idec-primary-node "u/point")) |
| 35 | + |
| 36 | +(defun request-is-done (result) |
| 37 | + "Show message with RESULT code." |
| 38 | + (message "IDEC: Sended. Result: %S" result)) |
| 39 | + |
| 40 | +(defun do-post-request (url data) |
| 41 | + "Make POST request to URL with DATA." |
| 42 | + (web-http-post |
| 43 | + (lambda (con header data) |
| 44 | + (request-is-done data)) |
| 45 | + :url url |
| 46 | + :data data)) |
| 47 | + |
| 48 | +(defun post-message (encoded-message) |
| 49 | + "Do POST request to `idec-primary-node' with Base64 ENCODED-MESSAGE." |
| 50 | + (let (json) |
| 51 | + (setq json (make-hash-table :test 'equal)) |
| 52 | + (puthash "pauth" idec-account-auth json) |
| 53 | + (puthash "tmsg" encoded-message json) |
| 54 | + (do-post-request (point-url) json)) |
| 55 | + (message "Message sended")) |
| 56 | + |
| 57 | +(defun do-send-reply-post-request (message) |
| 58 | + "Make IDEC compatible point MESSAGE and send it to `idec-primary-node'." |
| 59 | + (message (gethash "body" message)) |
| 60 | + (let (point-message) |
| 61 | + (setq point-message (list |
| 62 | + (gethash "echo" message) |
| 63 | + (gethash "author" message) |
| 64 | + (gethash "subj" message) |
| 65 | + "" |
| 66 | + (concat "@repto:" (gethash "id" message)) |
| 67 | + (gethash "body" message))) |
| 68 | + ;; Encode message in Base64 |
| 69 | + (post-message (base64-encode-string (encode-coding-string (s-join "\n" point-message) 'utf-8))))) |
| 70 | + |
| 71 | +(defun send-message (msg) |
| 72 | + "Send message MSG to `idec-primary-node'." |
| 73 | + (switch-to-buffer (concat "*IDEC: answer to " (gethash "id" msg) "*")) |
| 74 | + (puthash "body" |
| 75 | + (s-join "\n" (-drop-last 1 (-drop 4 (split-string (buffer-string) "\n")))) |
| 76 | + msg) |
| 77 | + (message (gethash "body" msg)) |
| 78 | + (do-send-reply-post-request msg)) |
| 79 | + |
| 80 | +(defun get-answers-hash (id msg-hash) |
| 81 | + "Make answers hashtable from ID and MSG-HASH." |
| 82 | + (let (answer-hash) |
35 | 83 | (setq answer-hash (make-hash-table :test 'equal))
|
36 | 84 | (puthash "id" id answer-hash)
|
37 | 85 | (puthash "echo" (get-message-field (gethash "content" msg-hash) "echo") answer-hash)
|
|
44 | 92 | (if (not (string-match "Re:" subj))
|
45 | 93 | (puthash "subj" (concat "Re: " subj) answer-hash)
|
46 | 94 | (puthash "subj" subj answer-hash))
|
| 95 | + answer-hash)) |
| 96 | + |
| 97 | +(defun make-answer-header (id msg-hash) |
| 98 | + "Make header with reto to ID from MSG-HASH." |
| 99 | + (let (answer-hash subj p) |
| 100 | + (setq answer-hash (get-answers-hash id msg-hash)) |
47 | 101 |
|
48 | 102 | (concat
|
49 | 103 | (concat "Answer to " id " in " (gethash "echo" answer-hash) "\n")
|
|
56 | 110 |
|
57 | 111 | (defun edit-answer-without-quote (id msg-hash)
|
58 | 112 | "Answer to message with ID MSG-HASH."
|
59 |
| - (let (answer-hash subj p) |
60 |
| - (setq answer-hash (make-hash-table :test 'equal)) |
61 |
| - (puthash "id" id answer-hash) |
62 |
| - (puthash "echo" (get-message-field (gethash "content" msg-hash) "echo") answer-hash) |
63 |
| - (puthash "author" (get-message-field (gethash "content" msg-hash) "author") answer-hash) |
64 |
| - |
65 |
| - (setq subj (get-message-field (gethash "content" msg-hash) "subj")) |
66 |
| - |
67 |
| - ;; Make `Re:' in subj if it not present. |
68 |
| - (if (not (string-match "Re:" subj)) |
69 |
| - (puthash "subj" (concat "Re: " subj) answer-hash) |
70 |
| - (puthash "subj" subj answer-hash)) |
71 |
| - |
| 113 | + (let (answer-hash p) |
| 114 | + (setq answer-hash (get-answers-hash id msg-hash)) |
72 | 115 | (switch-to-buffer (get-buffer-create (concat "*IDEC: answer to " id "*")))
|
73 | 116 |
|
74 | 117 | (insert (make-answer-header id msg-hash))
|
75 | 118 | (forward-line)
|
76 |
| - (add-text-properties (point-min) (point) 'read-only) |
77 |
| - |
78 |
| - (forward-line) |
79 |
| - (setq p (point)) |
80 |
| - |
81 |
| - (point-max) |
82 |
| - (insert "\n") |
83 |
| - (insert-text-button "[Send]" |
84 |
| - 'action (lambda (x) (message "Send..."))) |
85 |
| - (goto-char p) |
86 |
| - ) |
87 |
| - (idec-mode)) |
88 |
| - |
89 |
| -(defun edit-answer-without-quote (id msg-hash) |
90 |
| - "Answer to message with ID MSG-HASH." |
91 |
| - (let (answer-hash subj p) |
92 |
| - (setq answer-hash (make-hash-table :test 'equal)) |
93 |
| - (puthash "id" id answer-hash) |
94 |
| - (puthash "echo" (get-message-field (gethash "content" msg-hash) "echo") answer-hash) |
95 |
| - (puthash "author" (get-message-field (gethash "content" msg-hash) "author") answer-hash) |
96 |
| - |
97 |
| - (setq subj (get-message-field (gethash "content" msg-hash) "subj")) |
98 |
| - |
99 |
| - ;; Make `Re:' in subj if it not present. |
100 |
| - (if (not (string-match "Re:" subj)) |
101 |
| - (puthash "subj" (concat "Re: " subj) answer-hash) |
102 |
| - (puthash "subj" subj answer-hash)) |
103 |
| - |
104 |
| - (switch-to-buffer (get-buffer-create (concat "*IDEC: answer to " id "*"))) |
105 |
| - |
106 |
| - (insert (concat "Answer to " id " in " (gethash "echo" answer-hash))) |
107 |
| - ;; Write header |
108 |
| - ;; (princ (concat "Answer to " id " in " (gethash "echo" answer-hash))) |
109 |
| - |
110 |
| - ;; Make it readonly |
111 |
| - ;; (add-text-properties (point) (point-min) 'read-only) |
112 |
| - |
113 |
| - ;; Write author |
114 |
| - (forward-line) |
115 |
| - (insert (concat "\nAuthor: " (gethash "author" answer-hash) "\n")) |
116 |
| - (add-text-properties (point-min) (point) 'read-only) |
117 |
| - |
118 |
| - ;; Write subj |
119 |
| - (point-max) |
120 |
| - (forward-line) |
121 |
| - (insert (concat "Subj: "(gethash "subj" answer-hash))) |
122 |
| - (forward-line) |
123 |
| - |
124 |
| - ;; Body |
125 |
| - (insert "\n------- YOU MESSAGE BELLOW -------\n") |
126 |
| - ;; (add-text-properties (beginning-of-line) (end-of-line) 'read-only) |
127 |
| - (forward-line) |
| 119 | + (add-text-properties (point) (point-min) 'read-only) |
128 | 120 | (setq p (point))
|
129 | 121 |
|
130 |
| - (point-max) |
131 | 122 | (insert "\n")
|
132 | 123 | (insert-text-button "[Send]"
|
133 |
| - 'action (lambda (x) (message "Send..."))) |
| 124 | + 'action (lambda (x) (send-message (button-get x 'msg))) |
| 125 | + 'msg answer-hash) |
134 | 126 | (goto-char p)
|
135 |
| - ) |
136 |
| - (idec-mode)) |
| 127 | + (idec-mode))) |
137 | 128 |
|
138 | 129 | ;; END OF ANSWERS
|
139 | 130 |
|
|
0 commit comments