f9c9fe38783b779b5088e0036b0084950e6c04f7
[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-file-name-coding-system
65   (if (boundp 'file-name-coding-system)
66       file-name-coding-system)
67   "*Coding system used for filenames of log files."
68   :type 'symbol
69   :group 'riece-log)
70
71 (defcustom riece-log-open-directory-function 'find-file
72   "*Function for opening a directory."
73   :type 'function
74   :group 'riece-log)
75
76 (defvar riece-log-enabled nil)
77
78 (defconst riece-log-description
79   "Saving IRC logs")
80
81 (defun riece-log-display-message-function (message)
82   (if riece-log-enabled
83       (let ((file (riece-log-get-file (riece-message-target message)))
84             (coding-system-for-write riece-log-coding-system))
85         (unless (file-directory-p (file-name-directory file))
86           (make-directory (file-name-directory file) t))
87         (write-region (concat (format-time-string "%H:%M") " "
88                               (riece-format-message message))
89                       nil file t 0))))
90
91 (defun riece-log-get-file (identity)
92   (expand-file-name
93    (concat (format-time-string "%Y%m%d") ".log")
94    (riece-log-get-directory identity)))
95
96 (defun riece-log-get-files (identity)
97   (let ((files (directory-files (riece-log-get-directory identity) t
98                                 (concat "^"
99                                         (riece-make-interval-regexp "[0-9]" 8)
100                                         "\\.log$")
101                                 t)))
102     (nreverse (sort files #'string-lessp))))
103
104 (defun riece-log-get-directory (identity)
105   (let ((channel (riece-identity-canonicalize-prefix
106                   (riece-identity-prefix identity)))
107         (server (riece-identity-server identity))
108         (map (assoc (riece-format-identity identity) riece-log-directory-map))
109         name)
110     (cond (map (setq name (cdr map)))
111           ((string-match riece-strict-channel-regexp channel)
112            (let ((suffix (match-string 2 channel)))
113              (setq name (substring channel (match-end 1) (match-beginning 2)))
114              (when (and (stringp suffix)
115                         (string-match "^:\\*\\.\\(.*\\)" suffix))
116                (setq name (concat name "-" (match-string 1 suffix))))))
117           (t (setq name "priv")))
118     (if server
119         (expand-file-name name (expand-file-name server riece-log-directory))
120       (expand-file-name name riece-log-directory))))
121
122 (defun riece-log-encode-file-name (file-name)
123   (if riece-log-file-name-coding-system
124       (setq file-name
125             (encode-coding-string file-name
126                                   riece-log-file-name-coding-system)))
127   (let ((index 0))
128     (while (string-match "[^-0-9A-Za-z=_\x80-\xFF]" file-name index)
129       (setq file-name (replace-match
130                        (format "=%02X"
131                                (aref file-name (match-beginning 0)))
132                        nil t file-name)
133             index (+ 3 index)))
134     file-name))
135
136 (defun riece-log-decode-file-name (file-name)
137   (let ((index 0))
138     (while (string-match "=\\([0-7][0-9A-F]\\)" file-name index)
139       (setq file-name (replace-match
140                        (char-to-string
141                         (car (read-from-string
142                               (concat "?\\x" (match-string 1 file-name)))))
143                        nil t file-name)
144             index (1+ index)))
145     file-name)
146   (if riece-log-file-name-coding-system
147       (setq file-name
148             (decode-coding-string file-name
149                                   riece-log-file-name-coding-system)))
150   file-name)
151
152 (defun riece-log-flashback-1 (identity)
153   (if (eq riece-log-flashback t)
154       (let ((file (riece-log-get-file identity)))
155         (if (file-exists-p file)
156             (insert-file-contents file)))
157     (let ((files (riece-log-get-files identity))
158           (lines (- riece-log-flashback))
159           date point)
160       (while (and (< lines 0) files)
161         (if (and (file-exists-p (car files))
162                  (string-match (concat "\\("
163                                        (riece-make-interval-regexp "[0-9]" 4)
164                                        "\\)\\("
165                                        (riece-make-interval-regexp "[0-9]" 2)
166                                        "\\)\\("
167                                        (riece-make-interval-regexp "[0-9]" 2)
168                                        "\\).log$")
169                                (car files)))
170             (save-restriction
171               (narrow-to-region (point-min) (point-min))
172               (setq date (concat " (" (match-string 1 (car files)) "/"
173                                  (match-string 2 (car files)) "/"
174                                  (match-string 3 (car files)) ")"))
175               (insert-file-contents (car files))
176               (goto-char (point-max))
177               (setq lines (forward-line lines)
178                     point (point))
179               (while (not (eobp))
180                 (end-of-line)
181                 (insert date)
182                 (forward-line))
183               (goto-char point)))
184         (setq files (cdr files)))
185       (if (zerop lines)
186           (delete-region (point-min) (point))))))
187
188 (defun riece-log-flashback (identity)
189   (when riece-log-flashback
190     (let (buffer-read-only
191           (point (goto-char (point-max))))
192       (insert (with-temp-buffer
193                 (riece-log-flashback-1 identity)
194                 (buffer-string)))
195       (goto-char point)
196       (while (re-search-forward
197               "^[0-9][0-9]:[0-9][0-9] [<>]\\([^<>]+\\)[<>] " nil t)
198         (put-text-property (match-beginning 1) (match-end 1)
199                            'riece-identity
200                            (riece-make-identity
201                             (riece-match-string-no-properties 1)
202                             (riece-identity-server identity))))
203       (when (and (memq 'riece-button riece-addons)
204                  riece-button-enabled)
205         (riece-button-update-buffer))
206       (goto-char (point-max))
207       (set-window-point (get-buffer-window (current-buffer))
208                         (point)))))
209
210 (defun riece-log-open-directory (&optional channel)
211   (interactive)
212   (let ((directory (riece-log-get-directory
213                     (or channel riece-current-channel))))
214     (if (file-directory-p directory)
215         (funcall riece-log-open-directory-function directory)
216       (error "No log directory"))))
217
218 (defun riece-log-requires ()
219   (if (memq 'riece-button riece-addons)
220       '(riece-button)))
221
222 (defun riece-log-insinuate ()
223   ;; FIXME: Use `riece-after-insert-functions' for trapping change,
224   ;; notice, wallops and so on. But must add argument.
225   (add-hook 'riece-after-display-message-functions
226             'riece-log-display-message-function)
227   (add-hook 'riece-channel-buffer-create-functions
228             'riece-log-flashback))
229
230 (defvar riece-command-mode-map)
231 (defun riece-log-enable ()
232   (define-key riece-command-mode-map "\C-cd" 'riece-log-open-directory)
233   (setq riece-log-enabled t))
234
235 (defun riece-log-disable ()
236   (define-key riece-command-mode-map "\C-cd" nil)
237   (setq riece-log-enabled nil))
238
239 (provide 'riece-log)
240
241 ;;; riece-log.el ends here