0dfd3ef739d17a324c27b5b4153e3f12f51e3375
[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 logs."
40   :prefix "riece-"
41   :group 'riece)
42
43 (defcustom riece-log-directory
44   (expand-file-name "log" riece-directory)
45   "*Where to look for log files."
46   :type 'directory
47   :group 'riece-log)
48
49 (defcustom riece-log-directory-map nil
50   "*The map of channel name and directory name."
51   :type '(repeat (cons (string :tag "Channel name")
52                        (string :tag "Directory name")))
53   :group 'riece-log)
54
55 (defcustom riece-log-flashback 10
56   "*If non-nil, irc messages flash back from log files.
57 If integer, flash back only this line numbers. t means all lines."
58   :type '(choice (integer :tag "line numbers")
59                  (const t :tag "of the day")
60                  (const nil :tag "no flashback"))
61   :group 'riece-log)
62
63 (defcustom riece-log-coding-system nil
64   "*Coding system used for log files."
65   :type 'symbol
66   :group 'riece-log)
67
68 (defcustom riece-log-file-name-coding-system
69   (if (boundp 'file-name-coding-system)
70       file-name-coding-system)
71   "*Coding system used for filenames of log files."
72   :type 'symbol
73   :group 'riece-log)
74
75 (defface riece-log-date-face
76   '((((class color)
77       (background dark))
78      (:foreground "Gray70"))
79     (((class color)
80       (background light))
81      (:foreground "DimGray"))
82     (t
83      (:bold t)))
84   "Face used for displaying \"(YYYY/MM/dd)\" extent."
85   :group 'riece-highlight-faces)
86 (defvar riece-log-date-face 'riece-log-date-face)
87
88 (defvar riece-log-lock-file nil
89   "Lock file for riece-log.
90 It is created if there is at least one instance of Emacs running riece-log.")
91
92 (defconst riece-log-file-name-regexp
93   (concat (riece-make-interval-regexp "[0-9]" 8) "\\.txt\\(\\.\\(.*\\)\\)?$"))
94
95 (defvar riece-log-enabled nil)
96
97 (defconst riece-log-description
98   "Saving IRC logs")
99
100 (defun riece-log-display-message-function (message)
101   (if riece-log-enabled
102       (let* ((coding-system-for-write
103               (if (featurep 'mule)
104                   (or riece-log-coding-system
105                       (car (get-language-info current-language-environment
106                                               'coding-system)))))
107              (file (riece-log-make-file-name (riece-message-target message)
108                                              coding-system-for-write))
109              file-name-coding-system
110              default-file-name-coding-system)
111         (unless (file-directory-p (file-name-directory file))
112           (make-directory (file-name-directory file) t))
113         (write-region (concat (format-time-string "%H:%M") " "
114                               (riece-format-message message))
115                       nil file t 0
116                       riece-log-lock-file))))
117
118 (defun riece-log-make-file-name (identity coding-system)
119   (expand-file-name (if (featurep 'mule)
120                         (format "%s.txt.%s"
121                                 (format-time-string "%Y%m%d")
122                                 coding-system)
123                       (format "%s.txt"
124                                 (format-time-string "%Y%m%d")))
125                     (riece-log-directory identity)))
126
127 (defun riece-log-list-files (identity time)
128   (let ((directory (riece-log-directory identity))
129         (time-prefix (format-time-string "%Y%m%d" (or time '(0 0))))
130         files)
131     (when (file-directory-p directory)
132       (setq files (nreverse (sort (directory-files
133                                    directory t
134                                    (concat "^" riece-log-file-name-regexp)
135                                    t)
136                                   #'string-lessp)))
137       (while (and files
138                   (string-lessp (file-name-nondirectory (car files))
139                                 time-prefix))
140         (setq files (cdr files)))
141       files)))
142
143 (defun riece-log-directory (identity)
144   (let ((prefix (riece-identity-canonicalize-prefix
145                  (riece-identity-prefix identity)))
146         (server (riece-identity-server identity))
147         (map (assoc (riece-format-identity identity) riece-log-directory-map)))
148     (if map
149         (expand-file-name (cdr map) riece-log-directory)
150       (expand-file-name (riece-log-encode-file-name prefix)
151                         (expand-file-name
152                          (concat "." (riece-log-encode-file-name server))
153                          riece-log-directory)))))
154
155 (defun riece-log-encode-file-name (file-name)
156   (if riece-log-file-name-coding-system
157       (setq file-name
158             (encode-coding-string file-name
159                                   riece-log-file-name-coding-system)))
160   (let ((index 0)
161         c)
162     (while (string-match "[^-0-9A-Za-z_\x80-\xFF]" file-name index)
163       (setq c (aref file-name (match-beginning 0)))
164       (if (eq c ?=)
165           (setq file-name (replace-match "==" nil t file-name)
166                 index (1+ (match-end 0)))
167         (setq file-name (replace-match (format "=%02X" c) nil t file-name)
168               index (+ 2 (match-end 0)))))
169     file-name))
170
171 (defun riece-log-decode-file-name (file-name)
172   (let ((index 0))
173     (while (string-match "==\\|=\\([0-7][0-9A-F]\\)" file-name index)
174       (setq file-name (replace-match
175                        (if (eq (aref file-name (1- (match-end 0))) ?=)
176                            "="
177                          (char-to-string
178                           (car (read-from-string
179                                 (concat "?\\x" (match-string 1 file-name))))))
180                        nil t file-name)
181             index (1+ (match-beginning 0))))
182     file-name)
183   (if riece-log-file-name-coding-system
184       (setq file-name
185             (decode-coding-string file-name
186                                   riece-log-file-name-coding-system)))
187   file-name)
188
189 (defun riece-log-insert (identity lines)
190   "Insert logs for IDENTITY at most LINES.
191 If LINES is t, insert today's logs entirely."
192   (let* (file-name-coding-system
193          default-file-name-coding-system
194          (files (riece-log-list-files identity
195                                       (if (eq lines t) (current-time))))
196          name coding-system date point)
197     (while (and (or (eq lines t) (> lines 0)) files)
198       (save-restriction
199         (narrow-to-region (point) (point))
200         (if (and (string-match
201                   (concat "^" riece-log-file-name-regexp)
202                   (setq name (file-name-nondirectory (car files))))
203                  (match-beginning 2))
204             (progn
205               (setq coding-system
206                     (intern (substring name (match-beginning 2))))
207               (if (featurep 'xemacs)
208                   (setq coding-system (find-coding-system coding-system))
209                 (unless (coding-system-p coding-system)
210                   (setq coding-system nil)))
211               (if coding-system
212                   (let ((coding-system-for-read coding-system))
213                     (insert-file-contents (car files)))
214                 ;;don't insert file contents if they use non
215                 ;;supported coding-system.
216                 ))
217           ;;if the filename has no coding-system suffix, decode with
218           ;;riece-log-coding-system.
219           (let ((coding-system-for-read riece-log-coding-system))
220             (insert-file-contents (car files))))
221         ;;lines in the file contents are in reversed order.
222         (unless (eq lines t)
223           (goto-char (point-max))
224           (setq lines (- (forward-line (- lines))))
225           (delete-region (point-min) (point)))
226         ;;add (YYYY/MM/dd) suffix on each line left in the current buffer.
227         (unless (equal (substring name 0 8) (format-time-string "%Y%m%d"))
228           (setq date (concat " (" (substring name 0 4) "/"
229                              (substring name 4 6) "/"
230                              (substring name 6 8) ")"))
231           (while (not (eobp))
232             (end-of-line)
233             (setq point (point))
234             (insert date)
235             (put-text-property point (point)
236                                'riece-overlay-face 'riece-log-date-face)
237             (forward-line))
238           (goto-char (point-min))))
239       (setq files (cdr files)))))
240
241 (defun riece-log-flashback (identity)
242   (when riece-log-flashback
243     (riece-insert-info (current-buffer)
244                        (if (eq riece-log-flashback t)
245                            "Recent messages of the day:\n"
246                          (format "Recent messages up to %d lines:\n"
247                                  riece-log-flashback)))
248     (let (buffer-read-only
249           (point (goto-char (point-max))))
250       (insert (with-temp-buffer
251                 (riece-log-insert identity riece-log-flashback)
252                 (buffer-string)))
253       (goto-char point)
254       (while (re-search-forward
255               (concat "^" riece-time-prefix-regexp
256                        "\\(<[^>]+>\\|>[^<]+<\\|([^)]+)\\|{[^}]+}\\|=[^=]+=\\)")
257               nil t)
258         (put-text-property (1+ (match-beginning 1)) (1- (match-end 1))
259                            'riece-identity
260                            (riece-make-identity
261                             (buffer-substring (1+ (match-beginning 1))
262                                               (1- (match-end 1)))
263                             (riece-identity-server identity))))
264       (run-hook-with-args 'riece-after-insert-functions
265                           point (goto-char (point-max)))
266       (set-window-point (get-buffer-window (current-buffer))
267                         (point)))))
268
269 (defun riece-log-dired (&optional channel)
270   (interactive)
271   (let ((directory (riece-log-directory (or channel riece-current-channel))))
272     (if (file-directory-p directory)
273         (dired directory)
274       (error "No log directory"))))
275
276 (defun riece-log-requires ()
277   (if (memq 'riece-button riece-addons)
278       '(riece-button)))
279
280 (defun riece-log-insinuate ()
281   (make-directory riece-log-directory t)
282   (setq riece-log-lock-file
283         (expand-file-name (format "!%s-%d-%d"
284                                   (riece-log-encode-file-name (system-name))
285                                   (user-uid)
286                                   (emacs-pid))
287                           riece-log-directory))
288   ;; FIXME: Use `riece-after-insert-functions' for trapping change,
289   ;; notice, wallops and so on. But must add argument.
290   (add-hook 'riece-after-display-message-functions
291             'riece-log-display-message-function)
292   (add-hook 'riece-channel-buffer-create-functions
293             'riece-log-flashback))
294
295 (defvar riece-command-mode-map)
296 (defun riece-log-enable ()
297   (define-key riece-command-mode-map "\C-cd" 'riece-log-dired)
298   (setq riece-log-enabled t))
299
300 (defun riece-log-disable ()
301   (define-key riece-command-mode-map "\C-cd" nil)
302   (setq riece-log-enabled nil))
303
304 (provide 'riece-log)
305
306 ;;; riece-log.el ends here