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