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