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