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