8e303b8fd0bec7a97ee60b9777107af23ae6111e
[riece] / lisp / riece-log.el
1 ;;; riece-log.el --- saving irc logs add-on
2 ;; Copyright (C) 2003 OHASHI Akira
3 ;; Copyright (C) 2004 Daiki Ueno
4
5 ;; Author: OHASHI Akira <bg66@koka-in.org>
6 ;;      Daiki Ueno <ueno@unixuser.org>
7 ;; Keywords: IRC, riece
8
9 ;; This file is part of Riece.
10
11 ;; This program is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;; This add-on saves irc logs for every channel.
29
30 ;; To use, add the following line to your ~/.riece/init.el:
31 ;; (add-to-list 'riece-addons 'riece-log)
32
33 ;;; Code:
34
35 (require 'riece-message)
36 (require 'riece-button)
37
38 (defgroup riece-log nil
39   "Save irc log"
40   :group 'riece)
41
42 (defcustom riece-log-directory
43   (expand-file-name "log" riece-directory)
44   "*Where to look for log files."
45   :type 'directory
46   :group 'riece-log)
47
48 (defcustom riece-log-directory-map nil
49   "*The map of channel name and directory name."
50   :type '(repeat (cons (string :tag "Channel name")
51                        (string :tag "Directory name")))
52   :group 'riece-log)
53
54 (defcustom riece-log-flashback 10
55   "*If non-nil, irc messages flash back from log files.
56 If integer, flash back only this line numbers. t means all lines."
57   :type '(choice (integer :tag "line numbers")
58                  (const t :tag "of the day")
59                  (const nil :tag "no flashback"))
60   :group 'riece-log)
61
62 (defcustom riece-log-coding-system nil
63   "*Coding system used for log files."
64   :type 'symbol
65   :group 'riece-log)
66
67 (defcustom riece-log-file-name-coding-system
68   (if (boundp 'file-name-coding-system)
69       file-name-coding-system)
70   "*Coding system used for filenames of log files."
71   :type 'symbol
72   :group 'riece-log)
73
74 (defcustom riece-log-open-directory-function 'find-file
75   "*Function for opening a directory."
76   :type 'function
77   :group 'riece-log)
78
79 (defface riece-log-date-face
80   '((((class color)
81       (background dark))
82      (:foreground "Gray70"))
83     (((class color)
84       (background light))
85      (:foreground "DimGray"))
86     (t
87      (:bold t)))
88   "Face used for displaying \"(YYYY/MM/dd)\" extent."
89   :group 'riece-highlight-faces)
90 (defvar riece-log-date-face 'riece-log-date-face)
91
92 (defvar riece-log-lock-file nil
93   "Lock file for riece-log.
94 It is created if there is at least one instance of Emacs running riece-log.")
95
96 (defconst riece-log-file-name-regexp
97   (concat (riece-make-interval-regexp "[0-9]" 8) "\\.txt\\(\\.\\(.*\\)\\)?$"))
98
99 (defvar riece-log-enabled nil)
100
101 (defconst riece-log-description
102   "Saving IRC logs")
103
104 (defun riece-log-display-message-function (message)
105   (if riece-log-enabled
106       (let ((coding-system-for-write (or riece-log-coding-system
107                                          buffer-file-coding-system))
108             (file (riece-log-get-file (riece-message-target message)
109                                       coding-system-for-write))
110             file-name-coding-system
111             default-file-name-coding-system)
112         (unless (file-directory-p (file-name-directory file))
113           (make-directory (file-name-directory file) t))
114         (write-region (concat (format-time-string "%H:%M") " "
115                               (riece-format-message message))
116                       nil file t 0
117                       riece-log-lock-file))))
118
119 (defun riece-log-get-file (identity coding-system)
120   (expand-file-name
121    (format "%s.txt.%s" (format-time-string "%Y%m%d") coding-system)
122    (riece-log-get-directory identity)))
123
124 (defun riece-log-get-files (identity time)
125   (let ((directory (riece-log-get-directory identity))
126         (time-prefix (format-time-string "%Y%m%d" (or time '(0 0))))
127         files)
128     (when (file-directory-p directory)
129       (setq files (nreverse (sort (directory-files
130                                    directory t
131                                    (concat "^" riece-log-file-name-regexp)
132                                    t)
133                                   #'string-lessp)))
134       (while (and files
135                   (string-lessp (file-name-nondirectory (car files))
136                                 time-prefix))
137         (setq files (cdr files)))
138       files)))
139
140 (defun riece-log-get-directory (identity)
141   (let ((prefix (riece-identity-canonicalize-prefix
142                  (riece-identity-prefix identity)))
143         (server (riece-identity-server identity))
144         (map (assoc (riece-format-identity identity) riece-log-directory-map)))
145     (if map
146         (expand-file-name (cdr map) riece-log-directory)
147       (expand-file-name (riece-log-encode-file-name prefix)
148                         (expand-file-name
149                          (concat "." (riece-log-encode-file-name server))
150                          riece-log-directory)))))
151
152 (defun riece-log-encode-file-name (file-name)
153   (if riece-log-file-name-coding-system
154       (setq file-name
155             (encode-coding-string file-name
156                                   riece-log-file-name-coding-system)))
157   (let ((index 0)
158         c)
159     (while (string-match "[^-0-9A-Za-z_\x80-\xFF]" file-name index)
160       (setq c (aref file-name (match-beginning 0)))
161       (if (eq c ?=)
162           (setq file-name (replace-match "==" nil t file-name)
163                 index (1+ (match-end 0)))
164         (setq file-name (replace-match (format "=%02X" c) nil t file-name)
165               index (+ 2 (match-end 0)))))
166     file-name))
167
168 (defun riece-log-decode-file-name (file-name)
169   (let ((index 0))
170     (while (string-match "==\\|=\\([0-7][0-9A-F]\\)" file-name index)
171       (setq file-name (replace-match
172                        (if (eq (aref file-name (1- (match-end 0))) ?=)
173                            "="
174                          (char-to-string
175                           (car (read-from-string
176                                 (concat "?\\x" (match-string 1 file-name))))))
177                        nil t file-name)
178             index (1+ (match-beginning 0))))
179     file-name)
180   (if riece-log-file-name-coding-system
181       (setq file-name
182             (decode-coding-string file-name
183                                   riece-log-file-name-coding-system)))
184   file-name)
185
186 (defun riece-log-insert (identity lines)
187   "Insert logs for IDENTITY at most LINES.
188 If LINES is t, insert today's logs entirely."
189   (let* (file-name-coding-system
190          default-file-name-coding-system
191          (files (riece-log-get-files identity
192                                      (if (eq lines t) (current-time))))
193          name coding-system date point)
194     (while (and (or (eq lines t) (> lines 0)) files)
195       (save-restriction
196         (narrow-to-region (point) (point))
197         (if (and (string-match
198                   (concat "^" riece-log-file-name-regexp)
199                   (setq name (file-name-nondirectory (car files))))
200                  (match-beginning 2))
201             (progn
202               (setq coding-system
203                     (intern (substring name (match-beginning 2))))
204               (if (featurep 'xemacs)
205                   (setq coding-system (find-coding-system coding-system))
206                 (unless (coding-system-p coding-system)
207                   (setq coding-system nil)))
208               (if coding-system
209                   (let ((coding-system-for-read coding-system))
210                     (insert-file-contents (car files)))
211                 ;;don't insert file contents if they use non
212                 ;;supported coding-system.
213                 ))
214           ;;if the filename has no coding-system suffix, decode with
215           ;;riece-log-coding-system.
216           (let ((coding-system-for-read riece-log-coding-system))
217             (insert-file-contents (car files))))
218         ;;lines in the file contents are in reversed order.
219         (unless (eq lines t)
220           (goto-char (point-max))
221           (setq lines (- (forward-line (- lines))))
222           (delete-region (point-min) (point)))
223         ;;add (YYYY/MM/dd) suffix on each line left in the current buffer.
224         (unless (equal (substring name 0 8) (format-time-string "%Y%m%d"))
225           (setq date (concat " (" (substring name 0 4) "/"
226                              (substring name 4 6) "/"
227                              (substring name 6 8) ")"))
228           (while (not (eobp))
229             (end-of-line)
230             (setq point (point))
231             (insert date)
232             (put-text-property point (point)
233                                'riece-overlay-face 'riece-log-date-face)
234             (forward-line))
235           (goto-char (point-min))))
236       (setq files (cdr files)))))
237
238 (defun riece-log-flashback (identity)
239   (when riece-log-flashback
240     (riece-insert-info (current-buffer)
241                        (if (eq riece-log-flashback t)
242                            "Recent messages of the day:\n"
243                          (format "Recent messages up to %d lines:\n"
244                                  riece-log-flashback)))
245     (let (buffer-read-only
246           (point (goto-char (point-max))))
247       (insert (with-temp-buffer
248                 (riece-log-insert identity riece-log-flashback)
249                 (buffer-string)))
250       (goto-char point)
251       (while (re-search-forward
252               (concat "^" riece-time-prefix-regexp
253                        "\\(<[^>]+>\\|>[^<]+<\\|([^)]+)\\|{[^}]+}\\|=[^=]+=\\)")
254               nil t)
255         (put-text-property (1+ (match-beginning 1)) (1- (match-end 1))
256                            'riece-identity
257                            (riece-make-identity
258                             (buffer-substring (1+ (match-beginning 1))
259                                               (1- (match-end 1)))
260                             (riece-identity-server identity))))
261       (run-hook-with-args 'riece-after-insert-functions
262                           point (goto-char (point-max)))
263       (set-window-point (get-buffer-window (current-buffer))
264                         (point)))))
265
266 (defun riece-log-open-directory (&optional channel)
267   (interactive)
268   (let ((directory (riece-log-get-directory
269                     (or channel riece-current-channel))))
270     (if (file-directory-p directory)
271         (funcall riece-log-open-directory-function directory)
272       (error "No log directory"))))
273
274 (defun riece-log-requires ()
275   (if (memq 'riece-button riece-addons)
276       '(riece-button)))
277
278 (defun riece-log-insinuate ()
279   (make-directory riece-log-directory t)
280   (setq riece-log-lock-file
281         (expand-file-name (format "!%s-%d-%d"
282                                   (riece-log-encode-file-name (system-name))
283                                   (user-uid)
284                                   (emacs-pid))
285                           riece-log-directory))
286   ;; FIXME: Use `riece-after-insert-functions' for trapping change,
287   ;; notice, wallops and so on. But must add argument.
288   (add-hook 'riece-after-display-message-functions
289             'riece-log-display-message-function)
290   (add-hook 'riece-channel-buffer-create-functions
291             'riece-log-flashback))
292
293 (defvar riece-command-mode-map)
294 (defun riece-log-enable ()
295   (define-key riece-command-mode-map "\C-cd" 'riece-log-open-directory)
296   (setq riece-log-enabled t))
297
298 (defun riece-log-disable ()
299   (define-key riece-command-mode-map "\C-cd" nil)
300   (setq riece-log-enabled nil))
301
302 (provide 'riece-log)
303
304 ;;; riece-log.el ends here