Skip to content

Commit 8da6c46

Browse files
author
Denis Zheleztsov
committed
Working answer post
1 parent b9ee92e commit 8da6c46

File tree

4 files changed

+74
-78
lines changed

4 files changed

+74
-78
lines changed

idec-answers.el

Lines changed: 65 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,59 @@
2727
;;; Code:
2828

2929
(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)
3583
(setq answer-hash (make-hash-table :test 'equal))
3684
(puthash "id" id answer-hash)
3785
(puthash "echo" (get-message-field (gethash "content" msg-hash) "echo") answer-hash)
@@ -44,6 +92,12 @@
4492
(if (not (string-match "Re:" subj))
4593
(puthash "subj" (concat "Re: " subj) answer-hash)
4694
(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))
47101

48102
(concat
49103
(concat "Answer to " id " in " (gethash "echo" answer-hash) "\n")
@@ -56,84 +110,21 @@
56110

57111
(defun edit-answer-without-quote (id msg-hash)
58112
"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))
72115
(switch-to-buffer (get-buffer-create (concat "*IDEC: answer to " id "*")))
73116

74117
(insert (make-answer-header id msg-hash))
75118
(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)
128120
(setq p (point))
129121

130-
(point-max)
131122
(insert "\n")
132123
(insert-text-button "[Send]"
133-
'action (lambda (x) (message "Send...")))
124+
'action (lambda (x) (send-message (button-get x 'msg)))
125+
'msg answer-hash)
134126
(goto-char p)
135-
)
136-
(idec-mode))
127+
(idec-mode)))
137128

138129
;; END OF ANSWERS
139130

idec-customize.el

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
;;; idec-customize.el --- GNU Emacs client for IDEC network
1+
;;; idec-customize.el --- This file part of GNU Emacs client for IDEC network
22

33
;; Copyright (c) 2017 Denis Zheleztsov
44

idec-online.el

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@
114114
(interactive)
115115
(idec-fetch-echo-list (concat idec-primary-node "list.txt")))
116116

117+
(defun idec-online-browse-hidden ()
118+
"Browse hidden echo."
119+
(interactive)
120+
(load-echo-messages (read-string "Enter echo name: ") t))
121+
117122
(provide 'idec-online)
118123

119124
;;; idec-online.el ends here

idec.el

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,17 @@ Default to `idec-download-offset'"
9797
"IDEC accounts settings."
9898
:group 'idec)
9999

100-
(defcustom idec-account-nick nil
100+
(defcustom idec-account-nick ""
101101
"Account nickname."
102102
:type 'string
103103
:group 'idec-accounts)
104104

105-
(defcustom idec-account-node nil
105+
(defcustom idec-account-node ""
106106
"Node to send messages."
107107
:type 'string
108108
:group 'idec-accounts)
109109

110-
(defcustom idec-account-auth nil
110+
(defcustom idec-account-auth ""
111111
"Account authstring."
112112
:type 'string
113113
:group 'idec-accounts)

0 commit comments

Comments
 (0)