1 ;;; riece-log.el --- saving irc logs add-on
2 ;; Copyright (C) 2003 OHASHI Akira
3 ;; Copyright (C) 2004 Daiki Ueno
5 ;; Author: OHASHI Akira <bg66@koka-in.org>
6 ;; Daiki Ueno <ueno@unixuser.org>
7 ;; Keywords: IRC, riece
9 ;; This file is part of Riece.
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)
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.
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.
28 ;; This add-on saves irc logs for every channel.
30 ;; To use, add the following line to your ~/.riece/init.el:
31 ;; (add-to-list 'riece-addons 'riece-log)
35 (require 'riece-message)
36 (require 'riece-button)
38 (defgroup riece-log nil
42 (defcustom riece-log-directory
43 (expand-file-name "log" riece-directory)
44 "*Where to look for log files."
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")))
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 (const t :tag "of the day")
59 (const nil :tag "no flashback"))
62 (defcustom riece-log-coding-system nil
63 "*Coding system used for log files."
67 (defcustom riece-log-file-name-coding-system
68 (if (boundp 'file-name-coding-system)
69 file-name-coding-system)
70 "*Coding system used for filenames of log files."
74 (defface riece-log-date-face
77 (:foreground "Gray70"))
80 (:foreground "DimGray"))
83 "Face used for displaying \"(YYYY/MM/dd)\" extent."
84 :group 'riece-highlight-faces)
85 (defvar riece-log-date-face 'riece-log-date-face)
87 (defvar riece-log-lock-file nil
88 "Lock file for riece-log.
89 It is created if there is at least one instance of Emacs running riece-log.")
91 (defconst riece-log-file-name-regexp
92 (concat (riece-make-interval-regexp "[0-9]" 8) "\\.txt\\(\\.\\(.*\\)\\)?$"))
94 (defvar riece-log-enabled nil)
96 (defconst riece-log-description
99 (defun riece-log-display-message-function (message)
100 (if riece-log-enabled
101 (let ((coding-system-for-write
103 (or riece-log-coding-system
104 (car (get-language-info current-language-environment
106 (file (riece-log-make-file-name (riece-message-target message)
107 coding-system-for-write))
108 file-name-coding-system
109 default-file-name-coding-system)
110 (unless (file-directory-p (file-name-directory file))
111 (make-directory (file-name-directory file) t))
112 (write-region (concat (format-time-string "%H:%M") " "
113 (riece-format-message message))
115 riece-log-lock-file))))
117 (defun riece-log-make-file-name (identity coding-system)
118 (expand-file-name (if (featurep 'mule)
120 (format-time-string "%Y%m%d")
123 (format-time-string "%Y%m%d")))
124 (riece-log-directory identity)))
126 (defun riece-log-list-files (identity time)
127 (let ((directory (riece-log-directory identity))
128 (time-prefix (format-time-string "%Y%m%d" (or time '(0 0))))
130 (when (file-directory-p directory)
131 (setq files (nreverse (sort (directory-files
133 (concat "^" riece-log-file-name-regexp)
137 (string-lessp (file-name-nondirectory (car files))
139 (setq files (cdr files)))
142 (defun riece-log-directory (identity)
143 (let ((prefix (riece-identity-canonicalize-prefix
144 (riece-identity-prefix identity)))
145 (server (riece-identity-server identity))
146 (map (assoc (riece-format-identity identity) riece-log-directory-map)))
148 (expand-file-name (cdr map) riece-log-directory)
149 (expand-file-name (riece-log-encode-file-name prefix)
151 (concat "." (riece-log-encode-file-name server))
152 riece-log-directory)))))
154 (defun riece-log-encode-file-name (file-name)
155 (if riece-log-file-name-coding-system
157 (encode-coding-string file-name
158 riece-log-file-name-coding-system)))
161 (while (string-match "[^-0-9A-Za-z_\x80-\xFF]" file-name index)
162 (setq c (aref file-name (match-beginning 0)))
164 (setq file-name (replace-match "==" nil t file-name)
165 index (1+ (match-end 0)))
166 (setq file-name (replace-match (format "=%02X" c) nil t file-name)
167 index (+ 2 (match-end 0)))))
170 (defun riece-log-decode-file-name (file-name)
172 (while (string-match "==\\|=\\([0-7][0-9A-F]\\)" file-name index)
173 (setq file-name (replace-match
174 (if (eq (aref file-name (1- (match-end 0))) ?=)
177 (car (read-from-string
178 (concat "?\\x" (match-string 1 file-name))))))
180 index (1+ (match-beginning 0))))
182 (if riece-log-file-name-coding-system
184 (decode-coding-string file-name
185 riece-log-file-name-coding-system)))
188 (defun riece-log-insert (identity lines)
189 "Insert logs for IDENTITY at most LINES.
190 If LINES is t, insert today's logs entirely."
191 (let* (file-name-coding-system
192 default-file-name-coding-system
193 (files (riece-log-list-files identity
194 (if (eq lines t) (current-time))))
195 name coding-system date point)
196 (while (and (or (eq lines t) (> lines 0)) files)
198 (narrow-to-region (point) (point))
199 (if (and (string-match
200 (concat "^" riece-log-file-name-regexp)
201 (setq name (file-name-nondirectory (car files))))
205 (intern (substring name (match-beginning 2))))
206 (if (featurep 'xemacs)
207 (setq coding-system (find-coding-system coding-system))
208 (unless (coding-system-p coding-system)
209 (setq coding-system nil)))
211 (let ((coding-system-for-read coding-system))
212 (insert-file-contents (car files)))
213 ;;don't insert file contents if they use non
214 ;;supported coding-system.
216 ;;if the filename has no coding-system suffix, decode with
217 ;;riece-log-coding-system.
218 (let ((coding-system-for-read riece-log-coding-system))
219 (insert-file-contents (car files))))
220 ;;lines in the file contents are in reversed order.
222 (goto-char (point-max))
223 (setq lines (- (forward-line (- lines))))
224 (delete-region (point-min) (point)))
225 ;;add (YYYY/MM/dd) suffix on each line left in the current buffer.
226 (unless (equal (substring name 0 8) (format-time-string "%Y%m%d"))
227 (setq date (concat " (" (substring name 0 4) "/"
228 (substring name 4 6) "/"
229 (substring name 6 8) ")"))
234 (put-text-property point (point)
235 'riece-overlay-face 'riece-log-date-face)
237 (goto-char (point-min))))
238 (setq files (cdr files)))))
240 (defun riece-log-flashback (identity)
241 (when riece-log-flashback
242 (riece-insert-info (current-buffer)
243 (if (eq riece-log-flashback t)
244 "Recent messages of the day:\n"
245 (format "Recent messages up to %d lines:\n"
246 riece-log-flashback)))
247 (let (buffer-read-only
248 (point (goto-char (point-max))))
249 (insert (with-temp-buffer
250 (riece-log-insert identity riece-log-flashback)
253 (while (re-search-forward
254 (concat "^" riece-time-prefix-regexp
255 "\\(<[^>]+>\\|>[^<]+<\\|([^)]+)\\|{[^}]+}\\|=[^=]+=\\)")
257 (put-text-property (1+ (match-beginning 1)) (1- (match-end 1))
260 (buffer-substring (1+ (match-beginning 1))
262 (riece-identity-server identity))))
263 (run-hook-with-args 'riece-after-insert-functions
264 point (goto-char (point-max)))
265 (set-window-point (get-buffer-window (current-buffer))
268 (defun riece-log-dired (&optional channel)
270 (let ((directory (riece-log-directory (or channel riece-current-channel))))
271 (if (file-directory-p directory)
273 (error "No log directory"))))
275 (defun riece-log-requires ()
276 (if (memq 'riece-button riece-addons)
279 (defun riece-log-insinuate ()
280 (make-directory riece-log-directory t)
281 (setq riece-log-lock-file
282 (expand-file-name (format "!%s-%d-%d"
283 (riece-log-encode-file-name (system-name))
286 riece-log-directory))
287 ;; FIXME: Use `riece-after-insert-functions' for trapping change,
288 ;; notice, wallops and so on. But must add argument.
289 (add-hook 'riece-after-display-message-functions
290 'riece-log-display-message-function)
291 (add-hook 'riece-channel-buffer-create-functions
292 'riece-log-flashback))
294 (defvar riece-command-mode-map)
295 (defun riece-log-enable ()
296 (define-key riece-command-mode-map "\C-cd" 'riece-log-dired)
297 (setq riece-log-enabled t))
299 (defun riece-log-disable ()
300 (define-key riece-command-mode-map "\C-cd" nil)
301 (setq riece-log-enabled nil))
305 ;;; riece-log.el ends here