* test/test-riece-log.el: New test cases.
[riece] / lisp / riece-log.el
1 ;;; riece-log.el --- saving irc logs add-on
2 ;; Copyright (C) 2003 OHASHI Akira
3
4 ;; Author: OHASHI Akira <bg66@koka-in.org>
5 ;; Keywords: IRC, riece
6
7 ;; This file is part of Riece.
8
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;; This add-on saves irc logs for every channel.
27
28 ;; To use, add the following line to your ~/.riece/init.el:
29 ;; (add-to-list 'riece-addons 'riece-log)
30
31 ;;; Code:
32
33 (require 'riece-message)
34 (require 'riece-button)
35
36 (defgroup riece-log nil
37   "Save irc log"
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                  (boolean :tag "flash back or not"))
57   :group 'riece-log)
58
59 (defcustom riece-log-coding-system nil
60   "*Coding system used for log files."
61   :type 'symbol
62   :group 'riece-log)
63
64 (defcustom riece-log-open-directory-function 'find-file
65   "*Function for opening a directory."
66   :type 'function
67   :group 'riece-log)
68
69 (defvar riece-log-enabled nil)
70
71 (defconst riece-log-description
72   "Saving IRC logs")
73
74 (defun riece-log-display-message-function (message)
75   (if riece-log-enabled
76       (let ((file (riece-log-get-file (riece-message-target message)))
77             (coding-system-for-write riece-log-coding-system))
78         (unless (file-directory-p (file-name-directory file))
79           (make-directory (file-name-directory file) t))
80         (write-region (concat (format-time-string "%H:%M") " "
81                               (riece-format-message message))
82                       nil file t 0))))
83
84 (defun riece-log-get-file (identity)
85   (expand-file-name
86    (concat (format-time-string "%Y%m%d") ".log")
87    (riece-log-get-directory identity)))
88
89 (defun riece-log-get-files (identity)
90   (let ((files (directory-files (riece-log-get-directory identity) t
91                                 (concat "^"
92                                         (riece-make-interval-regexp "[0-9]" 8)
93                                         "\\.log$")
94                                 t)))
95     (nreverse (sort files #'string-lessp))))
96
97 (defun riece-log-get-directory (identity)
98   (let ((channel (riece-identity-canonicalize-prefix
99                   (riece-identity-prefix identity)))
100         (server (riece-identity-server identity))
101         (map (assoc (riece-format-identity identity) riece-log-directory-map))
102         name)
103     (cond (map (setq name (cdr map)))
104           ((string-match riece-strict-channel-regexp channel)
105            (let ((suffix (match-string 2 channel)))
106              (setq name (substring channel (match-end 1) (match-beginning 2)))
107              (when (and (stringp suffix)
108                         (string-match "^:\\*\\.\\(.*\\)" suffix))
109                (setq name (concat name "-" (match-string 1 suffix))))))
110           (t (setq name "priv")))
111     (if server
112         (expand-file-name name (expand-file-name server riece-log-directory))
113       (expand-file-name name riece-log-directory))))
114
115 (defun riece-log-flashback-1 (identity)
116   (if (eq riece-log-flashback t)
117       (let ((file (riece-log-get-file identity)))
118         (if (file-exists-p file)
119             (insert-file-contents file)))
120     (let ((files (riece-log-get-files identity))
121           (lines (- riece-log-flashback))
122           date point)
123       (while (and (< lines 0) files)
124         (if (and (file-exists-p (car files))
125                  (string-match (concat "\\("
126                                        (riece-make-interval-regexp "[0-9]" 4)
127                                        "\\)\\("
128                                        (riece-make-interval-regexp "[0-9]" 2)
129                                        "\\)\\("
130                                        (riece-make-interval-regexp "[0-9]" 2)
131                                        "\\).log$")
132                                (car files)))
133             (save-restriction
134               (narrow-to-region (point-min) (point-min))
135               (setq date (concat " (" (match-string 1 (car files)) "/"
136                                  (match-string 2 (car files)) "/"
137                                  (match-string 3 (car files)) ")"))
138               (insert-file-contents (car files))
139               (goto-char (point-max))
140               (setq lines (forward-line lines)
141                     point (point))
142               (while (not (eobp))
143                 (end-of-line)
144                 (insert date)
145                 (forward-line))
146               (goto-char point)))
147         (setq files (cdr files)))
148       (if (zerop lines)
149           (delete-region (point-min) (point))))))
150
151 (defun riece-log-flashback (identity)
152   (when riece-log-flashback
153     (let (buffer-read-only
154           (point (goto-char (point-max))))
155       (insert (with-temp-buffer
156                 (riece-log-flashback-1 identity)
157                 (buffer-string)))
158       (goto-char point)
159       (while (re-search-forward
160               "^[0-9][0-9]:[0-9][0-9] [<>]\\([^<>]+\\)[<>] " nil t)
161         (put-text-property (match-beginning 1) (match-end 1)
162                            'riece-identity
163                            (riece-make-identity
164                             (riece-match-string-no-properties 1)
165                             (riece-identity-server identity))))
166       (when (and (memq 'riece-button riece-addons)
167                  riece-button-enabled)
168         (riece-button-update-buffer))
169       (goto-char (point-max))
170       (set-window-point (get-buffer-window (current-buffer))
171                         (point)))))
172
173 (defun riece-log-open-directory (&optional channel)
174   (interactive)
175   (let ((directory (riece-log-get-directory
176                     (or channel riece-current-channel))))
177     (if (file-directory-p directory)
178         (funcall riece-log-open-directory-function directory)
179       (error "No log directory"))))
180
181 (defun riece-log-requires ()
182   (if (memq 'riece-button riece-addons)
183       '(riece-button)))
184
185 (defun riece-log-insinuate ()
186   ;; FIXME: Use `riece-after-insert-functions' for trapping change,
187   ;; notice, wallops and so on. But must add argument.
188   (add-hook 'riece-after-display-message-functions
189             'riece-log-display-message-function)
190   (add-hook 'riece-channel-buffer-create-functions
191             'riece-log-flashback))
192
193 (defvar riece-command-mode-map)
194 (defun riece-log-enable ()
195   (define-key riece-command-mode-map "\C-cd" 'riece-log-open-directory)
196   (setq riece-log-enabled t))
197
198 (defun riece-log-disable ()
199   (define-key riece-command-mode-map "\C-cd" nil)
200   (setq riece-log-enabled nil))
201
202 (provide 'riece-log)
203
204 ;;; riece-log.el ends here