forked from Difrex/idec.el
-
Notifications
You must be signed in to change notification settings - Fork 0
/
idec-online.el
138 lines (121 loc) · 6.62 KB
/
idec-online.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
;;; idec-online.el --- This file part of GNU Emacs client for IDEC network
;; Copyright (c) 2017 Denis Zheleztsov
;; Author: Denis Zheleztsov <[email protected]>
;; Keywords: lisp,network,IDEC
;; Version: 0.1
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; In active developent.
;; Fetched node must be support modern IDEC extensions like /list.txt, /x/c, etc.
;;; Code:
;; (require 'idec-mode)
(require 'idec-answers)
(defun display-echo-messages (messages)
"Display downloaded MESSAGES from echo."
(message (concat "RECEIVED MESSAGES: " messages))
(let (msgs echo echo-msg-hash)
(setq echo-msg-hash (make-hash-table :test 'equal))
(setq echo (nth 0 (split-string messages "\n")))
(setq msgs (split-string messages "\n"))
(dolist (id msgs)
(when (not (or
(string-match "\\." id)
(string= "" id)))
(puthash id echo echo-msg-hash)))
(with-output-to-temp-buffer (get-buffer-create (concat "*IDEC: online browse " echo "*" ))
(switch-to-buffer (concat "*IDEC: online browse " echo "*"))
(maphash (lambda (id msg-hash)
(when (equal (get-message-field (gethash "content" msg-hash) "echo") echo)
(princ "__________________________________\n")
(princ (concat "ID: " id "\n"))
(princ (concat "From: " (get-message-field (gethash "content" msg-hash) "author") "("
(get-message-field (gethash "content" msg-hash) "address") ")" "\n"))
(princ (concat "To: " (get-message-field (gethash "content" msg-hash) "recipient") "\n"))
(princ (concat "Echo: " (get-message-field (gethash "content" msg-hash) "echo") "\n"))
(princ (concat "At: " (get-message-field (gethash "content" msg-hash) "time") "\n"))
(let (subj)
(setq subj (concat "Subject: " (get-message-field (gethash "content" msg-hash) "subj")))
(princ (concat subj "\n")))
(princ (concat "__________________________________\n\n"
(replace-in-string "\r" ""
(s-join "\n"
(get-message-field
(gethash "content" msg-hash)
"body")))))
(princ "\n__________________________________\n")
(princ "[")
(insert-button "Answer"
'action (lambda (x) (edit-answer-without-quote (button-get x 'id) (button-get x 'msg-hash)))
'id id
'msg-hash msg-hash)
(princ "]")
(princ "\t [")
(insert-button "Answer with quote")
(princ "]\n\n")))
;; Plain messages hash proccesing
(get-messages-content echo-msg-hash))
;; (idec-mode)
(idec))))
(defun load-echo-messages (echo &optional online)
"Load messages from ECHO with ONLINE selector."
(when (not online)
(message (concat "Update counter of " echo))
(store-echo-counter echo))
(display-echo-messages (get-url-content (make-echo-url echo))))
(defun proccess-echo-message (msg echo)
"Download new message MSG in ECHO."
(with-output-to-temp-buffer (get-buffer-create "*IDEC: DEBUG*")
(switch-to-buffer "*IDEC: DEBUG*")
(princ msg)
(princ echo)))
(defun proccess-echo-list (raw-list)
"Parse RAW-LIST from HTTP response."
(with-output-to-temp-buffer (get-buffer-create "*IDEC: list.txt*")
(switch-to-buffer "*IDEC: list.txt*")
(let (len lst)
;; Calculate echo name
(setq lst (list))
(dolist (l (split-string (decode-coding-string raw-list 'utf-8) "\n"))
(add-to-list 'lst (nth 0 (split-string l ":")) t))
(setq len (get-longest-string lst))
(message (concat "Longest echo " (number-to-string len)))
(dolist (line (split-string (decode-coding-string raw-list 'utf-8) "\n"))
(when (not (equal line ""))
;; Define echo
(defvar current-echo nil)
(setq current-echo (nth 0 (split-string line ":")))
;; Create clickable button
(insert-text-button current-echo
'action (lambda (x) (load-echo-messages (button-get x 'echo) t))
'help-echo (concat "Go to echo " current-echo)
'echo current-echo)
(save-excursion
(let (s e)
(setq e (point))
(beginning-of-line)
(setq s (point))
(add-text-properties s e '(comment t face '(:foreground "light green")))))
(princ (make-string (+ 3 (- len (length current-echo))) ? ))
(princ (format "%s\n"
(nth 2 (split-string line ":"))))
(save-excursion
(let (s e)
(setq e (point))
(re-search-backward " ")
(setq s (point))
(add-text-properties s e '(comment t face '(:foreground "light blue")))))))))
(idec-mode))
(defun idec-fetch-echo-list (nodeurl)
"Fetch echoes list from remote NODEURL."
(proccess-echo-list (get-url-content nodeurl)))
(provide 'idec-online)
;;; idec-online.el ends here