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