idec.el/idec.el

311 lines
14 KiB
EmacsLisp
Raw Permalink Normal View History

2017-09-29 16:06:46 +03:00
;;; idec.el --- GNU Emacs client for IDEC network
2017-09-28 10:28:09 +03:00
;; Copyright (c) 2017 Denis Zheleztsov
;; Author: Denis Zheleztsov <difrex.punk@gmail.com>
2017-09-28 11:53:39 +03:00
;; Keywords: lisp,network,IDEC
2017-09-28 10:28:09 +03:00
;; 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:
2017-09-28 17:12:09 +03:00
;; In active developent.
2017-09-28 11:53:39 +03:00
;; Fetched node must be support modern IDEC extensions like /list.txt, /x/c, etc.
2017-09-28 10:28:09 +03:00
;;; Code:
2017-10-17 10:34:56 +03:00
;; (require 'idec-answers)
(require 'idec-parser)
2017-11-03 10:55:04 +03:00
;; (require 'idec-online)
2017-10-18 14:14:33 +03:00
(require 'idec-db)
2017-09-28 15:53:38 +03:00
2017-09-28 11:53:39 +03:00
;; FUNCTIONS
;; ;;;;;;;;;
2017-09-29 16:06:46 +03:00
2017-10-01 01:35:29 +03:00
(defun get-url-content (url)
"Get URL content and return it without headers."
2017-10-02 10:27:20 +03:00
(with-current-buffer
2017-10-01 01:35:29 +03:00
(url-retrieve-synchronously url)
2017-10-02 10:27:20 +03:00
(goto-char (point-min))
(re-search-forward "^$")
(forward-line)
(delete-region (point) (point-min))
2017-10-01 01:35:29 +03:00
(buffer-string)))
2017-10-02 10:27:20 +03:00
2017-10-05 16:22:35 +03:00
;; LOCAL MAIL FUNCTIONS
;; ;;;;;;;;;;;;;;;;;;;;
(defun dots (echo)
"Get dots from ECHO list."
(make-string
(+ (- (get-longest-string (get-local-echoes))
(length echo))
3)
2018-06-27 15:08:32 +03:00
? )) ; `? ' - Space character
(defun longest-local-echo-subj (echo)
"Get longest subj from local ECHO."
(get-longest-string (get-echo-subjects echo)))
(defun mark-all-as-read (echo &optional checkpoint)
"Mark all messages in ECHO as read;
put cursor to CHECKPOINT."
(mark-all-messages-as-read echo)
(idec-local-browse checkpoint))
2017-10-05 16:22:35 +03:00
;; END OF LOCAL MAIL FUNCTIONS
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;
2017-10-02 10:27:20 +03:00
2018-06-26 14:22:32 +03:00
(defun make-plain-text-message-from-hash(msg)
"Make standard plain text message from MSG hash."
(concat
(gethash "tags" msg) "\n"
(gethash "echo" msg) "\n"
2018-06-26 15:37:27 +03:00
(nth 0 (split-string (number-to-string (time-to-seconds (date-to-time (gethash "time" msg)))) "\\.")) "\n"
2018-06-26 14:22:32 +03:00
(gethash "author" msg) "\n"
(gethash "address" msg) "\n"
(gethash "recipient" msg) "\n"
(gethash "subj" msg) "\n"
"\n"
(gethash "body" msg) "\n"))
2017-11-12 11:15:55 +03:00
(defun make-message-header (msg)
"Make message header from MSG hash."
2018-08-11 15:08:05 +03:00
(insert (concat "ID: " (gethash "id" msg) "\n"))
(insert "From: " )
(if (not (string-equal (gethash "repto" msg) ""))
(insert-button (concat (gethash "author" msg) "[" (gethash "address" msg) "]")
'action (lambda (x) (display-message-hash
(idec-db-get-message-by-id
(gethash "repto" (button-get x 'msg))
(gethash "echo" (button-get x 'msg)))))
2018-08-12 12:36:30 +03:00
'msg msg
'help-echo (concat "Jump to " (gethash "repto" msg)))
2018-08-11 15:08:05 +03:00
(insert (concat (gethash "author" msg) "[" (gethash "address" msg) "]")))
(insert "\n")
(insert (concat "To: " (gethash "recipient" msg) "\n"))
(insert (concat "Echo: " (gethash "echo" msg) "\n"))
(insert (concat "At: " (gethash "time" msg) "\n"))
(insert (concat "Subject: " (gethash "subj" msg) "\n")))
2017-11-12 11:15:55 +03:00
(defun display-message-hash (msg)
"Disaply message MSG in new buffer."
(mark-message-read (gethash "id" msg) (gethash "echo" msg))
(with-output-to-temp-buffer (get-buffer-create (concat "*IDEC: " (gethash "subj" msg) "*"))
(switch-to-buffer (concat "*IDEC: " (gethash "subj" msg) "*"))
(setq start (point))
2018-08-11 15:08:05 +03:00
(make-message-header msg)
2017-11-12 11:15:55 +03:00
(princ (concat "__________________________________\n\n"
2018-06-26 12:13:23 +03:00
(replace-in-string "\r" "" (gethash "body" msg))))
2017-11-12 11:15:55 +03:00
(princ "\n__________________________________\n")
(princ "[")
(let (answer-hash)
(setq answer-hash (make-hash-table :test 'equal))
2018-06-26 14:22:32 +03:00
(puthash "content" (make-plain-text-message-from-hash msg) answer-hash)
2017-11-12 11:15:55 +03:00
(insert-button "Answer"
'action (lambda (x) (edit-answer-without-quote (button-get x 'id) (button-get x 'msg-hash)))
'id (gethash "id" msg)
2018-08-09 22:53:49 +03:00
'msg-hash answer-hash)
2017-11-12 11:15:55 +03:00
(princ "]")
2018-08-10 15:18:02 +03:00
(princ (concat (make-string 11 ? ) "["))
2018-08-09 22:53:49 +03:00
(insert-button "Quoted answer"
'action (lambda (x) (idec-answers-edit-answer-with-quote
(button-get x 'id)
(button-get x 'msg-hash)))
'id (gethash "id" msg)
'msg-hash answer-hash))
2017-11-12 11:15:55 +03:00
(princ "]")
(add-text-properties start (point) 'read-only))
2018-06-29 09:44:31 +03:00
(idec))
2017-11-12 11:15:55 +03:00
(defun display-message (msg)
"Display message MSG in new buffer in idec-mode."
(mark-message-read (gethash "id" msg) (get-message-field (gethash "msg" msg) "echo"))
2018-06-28 10:46:15 +03:00
(defvar line "__________________________________")
2017-10-04 17:35:24 +03:00
(with-output-to-temp-buffer (get-buffer-create (concat "*IDEC: "
2018-06-26 15:37:27 +03:00
(decode-coding-string
(get-message-field
(gethash "msg" msg) "subj")
'utf-8)
"*"))
2017-10-02 17:56:07 +03:00
;; Run in IDEC mode
2017-10-04 17:35:24 +03:00
(switch-to-buffer (concat "*IDEC: " (decode-coding-string (get-message-field
(gethash "msg" msg) "subj")
'utf-8)
"*"))
(princ (concat "ID: " (gethash "id" msg) "\n"))
(princ (concat "From: " (get-message-field (gethash "msg" msg) "author") "("
(get-message-field (gethash "msg" msg) "address") ")" "\n"))
(princ (concat "To: " (get-message-field (gethash "msg" msg) "recipient") "\n"))
(princ (concat "Echo: " (get-message-field (gethash "msg" msg) "echo") "\n"))
(princ (concat "At: " (get-message-field (gethash "msg" msg) "time") "\n"))
(princ (concat "Subject: " (get-message-field (gethash "msg" msg) "subj") "\n"))
2018-06-28 10:46:15 +03:00
(princ (concat line "\n\n"
2018-06-26 12:13:23 +03:00
(replace-in-string "\r" ""
(s-join "\n" (get-message-field (gethash "msg" msg)
"body")))))
2018-06-28 10:46:15 +03:00
(princ (concat "\n" line "\n"))
2017-10-03 14:35:58 +03:00
(princ "[")
2017-10-18 14:14:33 +03:00
(let (answer-hash)
(setq answer-hash (make-hash-table :test 'equal))
(puthash "content" (gethash "msg" msg) answer-hash)
(insert-button "Answer"
'action (lambda (x) (edit-answer-without-quote (button-get x 'id) (button-get x 'msg-hash)))
'id (gethash "id" msg)
2018-08-09 22:53:49 +03:00
'msg-hash answer-hash)
2017-10-03 14:35:58 +03:00
(princ "]")
2018-08-09 22:53:49 +03:00
(princ (concat (make-string 9 ? ) "["))
(insert-button "Quoted answer"
'action (lambda (x) (idec-answers-edit-answer-with-quote (button-get x 'id) (button-get x 'msg-hash)))
'id (gethash "id" msg)
'msg-hash answer-hash))
2017-10-03 14:35:58 +03:00
(princ "]")
2017-10-02 17:56:07 +03:00
(add-text-properties (point-min) (point-max) 'read-only))
2017-10-03 14:35:58 +03:00
(point-max)
2018-08-09 22:53:49 +03:00
(idec))
2017-10-01 22:05:40 +03:00
(defun display-new-messages ()
"Display new fetched messages from `new-messages-list'."
2017-10-04 17:58:27 +03:00
(if (= (hash-table-count new-messages-list) 0)
(message "IDEC: No new messages.")
(with-output-to-temp-buffer (get-buffer-create "*IDEC: New messages*")
(switch-to-buffer "*IDEC: New messages*")
2017-10-04 00:37:20 +03:00
(maphash (lambda (id msg)
2017-10-04 17:35:24 +03:00
(let (m)
(setq m (make-hash-table :test 'equal))
(puthash "id" id m)
(puthash "msg" msg m)
;; Write message subj
(insert-text-button (concat (get-message-field msg "subj")
(make-string
(- (get-longest-field "subj" new-messages-list)
(length (get-message-field msg "subj")))
? ))
'help-echo "Read message"
'msg-hash m
'action (lambda (x) (display-message (button-get x 'msg-hash)))))
;; Write message time and echo
(princ (format " %s(%s)%s%s\t%s\n"
(get-message-field msg "author")
(get-message-field msg "address")
(make-string (-
(+
(get-longest-field "author" new-messages-list)
(get-longest-field "address" new-messages-list)
1)
(+
(length (get-message-field msg "author"))
(length (get-message-field msg "address")))
)
? )
(get-message-field msg "echo")
(get-message-field msg "time"))))
2017-10-04 17:58:27 +03:00
new-messages-list))
(idec-mode)))
2017-10-01 13:20:44 +03:00
2017-10-16 17:38:35 +03:00
(defun hash-table-keys (hash-table)
"Get list of keys from HASH-TABLE."
(let ((keys ()))
(maphash (lambda (k v) (push k keys)) hash-table)
keys))
2017-10-04 17:35:24 +03:00
(defun get-messages-content (messages)
"Get MESSAGES content from `idec-primary-node'."
(let (new-hash)
(setq new-hash (make-hash-table :test 'equal))
2017-10-16 17:38:35 +03:00
;; (message (get-url-content (make-messages-url (hash-table-keys messages))))
(dolist (line (split-string (get-url-content (make-messages-url (hash-table-keys messages))) "\n"))
2017-10-04 17:35:24 +03:00
(when (not (string= "" line))
(let (msgid content mes)
(setq mes (make-hash-table :test 'equal))
(setq msgid (nth 0 (split-string line ":")))
(setq content
(decode-coding-string
(base64-decode-string
(nth 1 (split-string line ":")))
'utf-8))
;; Populate message hash: {"echo": "echo name", "content": "message content"}
(puthash "echo" (get-message-field content "echo") mes)
(puthash "content" content mes)
(puthash msgid mes new-hash))))
new-hash))
(defun download-message (ids)
"Download messages with IDS to `idec-mail-dir'."
(if (= (hash-table-count ids) 0)
nil
(maphash (lambda (id msg)
(insert-message-to-db (gethash "content" msg) id)
;; (store-message (gethash "content" msg) (gethash "echo" msg) id)
2017-10-04 17:35:24 +03:00
(puthash id (gethash "content" msg) new-messages-list))
(get-messages-content ids))))
2017-10-01 01:35:29 +03:00
(defun download-subscriptions ()
"Download messages from echoes defined in `idec-echo-subscriptions' from `idec-primary-node'."
2017-10-04 17:35:24 +03:00
(message (make-echo-url (split-string idec-echo-subscriptions ",")))
(message idec-echo-subscriptions)
2017-10-01 01:35:29 +03:00
(get-url-content
(make-echo-url (split-string idec-echo-subscriptions ","))))
2017-09-28 10:28:09 +03:00
2017-09-28 11:53:39 +03:00
;; ECHOES FUNCTIONS
;; ;;;;;;;;;;;;;;;;
2017-10-02 10:27:20 +03:00
(defun make-echo-url (echoes &optional online)
2017-09-29 23:56:09 +03:00
"Make ECHOES url to retreive messages from `idec-primary-node';
with `idec-download-offset' and `idec-download-limit';
If ONLINE is t uses `idec-online-download-limit' and `idec-online-download-offset'."
2017-09-29 16:06:46 +03:00
;; Check ECHOES is list
(let (limit offset)
(if online
(and (setq limit idec-online-download-limit)
(setq offset idec-online-download-offset))
(and (setq limit idec-download-limit)
(setq offset idec-download-offset)))
(if (listp echoes)
;; Required GNU Emacs >= 25.3
(message (concat idec-primary-node "u/e/"
(s-join "/" echoes) "/" offset ":" limit))
(message (concat idec-primary-node "u/e/" echoes "/" offset ":" limit)))))
2017-09-29 16:06:46 +03:00
2017-10-01 13:20:44 +03:00
(defun make-messages-url (messages)
"Make MESSAGES url to retreive messages from `idec-primary-node'."
2017-10-04 17:58:27 +03:00
;; Check MESSAGES is list
2017-10-01 13:20:44 +03:00
(if (listp messages)
2017-09-29 16:06:46 +03:00
;; Required GNU Emacs >= 25.3
2017-10-16 17:38:35 +03:00
(concat idec-primary-node "u/m/" (s-join "/" messages))
(concat idec-primary-node "u/m/" messages)))
2017-10-01 13:20:44 +03:00
(defun make-count-url (echo)
"Return messages count url in `idec-primary-node' from ECHO."
(concat idec-primary-node "/x/c/" echo))
(defun echo-messages-count (echo)
"Get messages count in ECHO."
(nth 1 (split-string
(get-url-content (make-count-url echo)) ":")))
2017-09-28 11:53:39 +03:00
;; END OF ECHOES FUNCTIONS
;; ;;;;;;;;;;;;;;;;;;;;;;;
;; END OF FUNCTIONS
;; ;;;;;;;;;;;;;;;;
2017-09-28 10:28:09 +03:00
(provide 'idec)
;;; idec.el ends here