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