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