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