ddedc99428865598208ce4b4c1c801b5b046bffd
[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                  (boolean :tag "flash back or not"))
59   :group 'riece-log)
60
61 (defcustom riece-log-coding-system nil
62   "*Coding system used for log files."
63   :type 'symbol
64   :group 'riece-log)
65
66 (defcustom riece-log-file-name-coding-system
67   (if (boundp 'file-name-coding-system)
68       file-name-coding-system)
69   "*Coding system used for filenames of log files."
70   :type 'symbol
71   :group 'riece-log)
72
73 (defcustom riece-log-open-directory-function 'find-file
74   "*Function for opening a directory."
75   :type 'function
76   :group 'riece-log)
77
78 (defvar riece-log-enabled nil)
79
80 (defconst riece-log-description
81   "Saving IRC logs")
82
83 (defun riece-log-display-message-function (message)
84   (if riece-log-enabled
85       (let ((file (riece-log-get-file (riece-message-target message)))
86             (coding-system-for-write riece-log-coding-system)
87             file-name-coding-system
88             default-file-name-coding-system)
89         (unless (file-directory-p (file-name-directory file))
90           (make-directory (file-name-directory file) t))
91         (write-region (concat (format-time-string "%H:%M") " "
92                               (riece-format-message message))
93                       nil file t 0))))
94
95 (defun riece-log-get-file (identity)
96   (expand-file-name
97    (concat (format-time-string "%Y%m%d") ".txt")
98    (riece-log-get-directory identity)))
99
100 (defun riece-log-get-files (identity)
101   (let ((directory (riece-log-get-directory identity)))
102     (if (file-directory-p directory)
103         (nreverse (sort (directory-files directory t
104                          (concat "^"
105                                  (riece-make-interval-regexp "[0-9]" 8)
106                                  "\\.txt$")
107                          t)
108                   #'string-lessp)))))
109
110 (defun riece-log-get-directory (identity)
111   (let ((prefix (riece-identity-canonicalize-prefix
112                  (riece-identity-prefix identity)))
113         (server (riece-identity-server identity))
114         (map (assoc (riece-format-identity identity) riece-log-directory-map))
115         name)
116     (if map
117         (setq name (cdr map))
118       (expand-file-name (riece-log-encode-file-name prefix)
119                         (expand-file-name
120                          (concat "." (riece-log-encode-file-name server))
121                          riece-log-directory)))))
122
123 (defun riece-log-encode-file-name (file-name)
124   (if riece-log-file-name-coding-system
125       (setq file-name
126             (encode-coding-string file-name
127                                   riece-log-file-name-coding-system)))
128   (let ((index 0)
129         c)
130     (while (string-match "[^-0-9A-Za-z_\x80-\xFF]" file-name index)
131       (setq c (aref file-name (match-beginning 0)))
132       (if (eq c ?=)
133           (setq file-name (replace-match "==" nil t file-name)
134                 index (1+ (match-end 0)))
135         (setq file-name (replace-match (format "=%02X" c) nil t file-name)
136               index (+ 2 (match-end 0)))))
137     file-name))
138
139 (defun riece-log-decode-file-name (file-name)
140   (let ((index 0))
141     (while (string-match "==\\|=\\([0-7][0-9A-F]\\)" file-name index)
142       (setq file-name (replace-match
143                        (if (eq (aref file-name (1- (match-end 0))) ?=)
144                            "="
145                          (char-to-string
146                           (car (read-from-string
147                                 (concat "?\\x" (match-string 1 file-name))))))
148                        nil t file-name)
149             index (1+ (match-beginning 0))))
150     file-name)
151   (if riece-log-file-name-coding-system
152       (setq file-name
153             (decode-coding-string file-name
154                                   riece-log-file-name-coding-system)))
155   file-name)
156
157 (defun riece-log-insert (identity lines)
158   "Insert logs for IDENTITY at most LINES.
159 If LINES is t, insert today's logs entirely."
160   (if (eq lines t)
161       (let ((file (riece-log-get-file identity)))
162         (if (file-exists-p file)
163             (insert-file-contents file)))
164     (let ((files (riece-log-get-files identity))
165           (lines (- lines))
166           date point)
167       (while (and (< lines 0) files)
168         (if (and (file-exists-p (car files))
169                  (string-match (concat "\\([0-9][0-9][0-9][0-9]\\)"
170                                        "\\([0-9][0-9]\\)\\([0-9][0-9]\\).txt$")
171                                (car files)))
172             (save-restriction
173               (narrow-to-region (point) (point))
174               (setq date (concat " (" (match-string 1 (car files)) "/"
175                                  (match-string 2 (car files)) "/"
176                                  (match-string 3 (car files)) ")"))
177               (insert-file-contents (car files))
178               (goto-char (point-max))
179               (setq lines (forward-line lines))
180               (delete-region (point-min) (point))
181               (while (not (eobp))
182                 (end-of-line)
183                 (insert date)
184                 (forward-line))
185               (goto-char (point-min))))
186         (setq files (cdr files))))))
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-insert identity riece-log-flashback)
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