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 (defcustom riece-log-open-directory-function 'find-file
75 "*Function for opening a directory."
79 (defface riece-log-date-face
82 (:foreground "Gray70"))
85 (:foreground "DimGray"))
88 "Face used for displaying \"(YYYY/MM/dd)\" extent."
89 :group 'riece-highlight-faces)
90 (defvar riece-log-date-face 'riece-log-date-face)
92 (defvar riece-log-lock-file nil
93 "Lock file for riece-log.
94 It is created if there is at least one instance of Emacs running riece-log.")
96 (defconst riece-log-file-name-regexp
97 (concat (riece-make-interval-regexp "[0-9]" 8) "\\.txt\\(\\.\\(.*\\)\\)?$"))
99 (defvar riece-log-enabled nil)
101 (defconst riece-log-description
104 (defun riece-log-display-message-function (message)
105 (if riece-log-enabled
106 (let ((coding-system-for-write (or riece-log-coding-system
107 buffer-file-coding-system))
108 (file (riece-log-get-file (riece-message-target message)
109 coding-system-for-write))
110 file-name-coding-system
111 default-file-name-coding-system)
112 (unless (file-directory-p (file-name-directory file))
113 (make-directory (file-name-directory file) t))
114 (write-region (concat (format-time-string "%H:%M") " "
115 (riece-format-message message))
117 riece-log-lock-file))))
119 (defun riece-log-get-file (identity coding-system)
121 (format "%s.txt.%s" (format-time-string "%Y%m%d") coding-system)
122 (riece-log-get-directory identity)))
124 (defun riece-log-get-files (identity time)
125 (let ((directory (riece-log-get-directory identity))
126 (time-prefix (format-time-string "%Y%m%d" (or time '(0 0))))
128 (when (file-directory-p directory)
129 (setq files (nreverse (sort (directory-files
131 (concat "^" riece-log-file-name-regexp)
135 (string-lessp (file-name-nondirectory (car files))
137 (setq files (cdr files)))
140 (defun riece-log-get-directory (identity)
141 (let ((prefix (riece-identity-canonicalize-prefix
142 (riece-identity-prefix identity)))
143 (server (riece-identity-server identity))
144 (map (assoc (riece-format-identity identity) riece-log-directory-map)))
146 (expand-file-name (cdr map) riece-log-directory)
147 (expand-file-name (riece-log-encode-file-name prefix)
149 (concat "." (riece-log-encode-file-name server))
150 riece-log-directory)))))
152 (defun riece-log-encode-file-name (file-name)
153 (if riece-log-file-name-coding-system
155 (encode-coding-string file-name
156 riece-log-file-name-coding-system)))
159 (while (string-match "[^-0-9A-Za-z_\x80-\xFF]" file-name index)
160 (setq c (aref file-name (match-beginning 0)))
162 (setq file-name (replace-match "==" nil t file-name)
163 index (1+ (match-end 0)))
164 (setq file-name (replace-match (format "=%02X" c) nil t file-name)
165 index (+ 2 (match-end 0)))))
168 (defun riece-log-decode-file-name (file-name)
170 (while (string-match "==\\|=\\([0-7][0-9A-F]\\)" file-name index)
171 (setq file-name (replace-match
172 (if (eq (aref file-name (1- (match-end 0))) ?=)
175 (car (read-from-string
176 (concat "?\\x" (match-string 1 file-name))))))
178 index (1+ (match-beginning 0))))
180 (if riece-log-file-name-coding-system
182 (decode-coding-string file-name
183 riece-log-file-name-coding-system)))
186 (defun riece-log-insert (identity lines)
187 "Insert logs for IDENTITY at most LINES.
188 If LINES is t, insert today's logs entirely."
189 (let* (file-name-coding-system
190 default-file-name-coding-system
191 (files (riece-log-get-files identity
192 (if (eq lines t) (current-time))))
193 name coding-system date point)
194 (while (and (or (eq lines t) (> lines 0)) files)
196 (narrow-to-region (point) (point))
197 (if (and (string-match
198 (concat "^" riece-log-file-name-regexp)
199 (setq name (file-name-nondirectory (car files))))
203 (intern (substring name (match-beginning 2))))
204 (if (featurep 'xemacs)
205 (setq coding-system (find-coding-system coding-system))
206 (unless (coding-system-p coding-system)
207 (setq coding-system nil)))
209 (let ((coding-system-for-read coding-system))
210 (insert-file-contents (car files)))
211 ;;don't insert file contents if they use non
212 ;;supported coding-system.
214 ;;if the filename has no coding-system suffix, decode with
215 ;;riece-log-coding-system.
216 (let ((coding-system-for-read riece-log-coding-system))
217 (insert-file-contents (car files))))
218 ;;lines in the file contents are in reversed order.
220 (goto-char (point-max))
221 (setq lines (- (forward-line (- lines))))
222 (delete-region (point-min) (point)))
223 ;;add (YYYY/MM/dd) suffix on each line left in the current buffer.
224 (unless (equal (substring name 0 8) (format-time-string "%Y%m%d"))
225 (setq date (concat " (" (substring name 0 4) "/"
226 (substring name 4 6) "/"
227 (substring name 6 8) ")"))
232 (put-text-property point (point)
233 'riece-overlay-face 'riece-log-date-face)
235 (goto-char (point-min))))
236 (setq files (cdr files)))))
238 (defun riece-log-flashback (identity)
239 (when riece-log-flashback
240 (riece-insert-info (current-buffer)
241 (if (eq riece-log-flashback t)
242 "Recent messages of the day:\n"
243 (format "Recent messages up to %d lines:\n"
244 riece-log-flashback)))
245 (let (buffer-read-only
246 (point (goto-char (point-max))))
247 (insert (with-temp-buffer
248 (riece-log-insert identity riece-log-flashback)
251 (while (re-search-forward
252 (concat "^" riece-time-prefix-regexp
253 "\\(<[^>]+>\\|>[^<]+<\\|([^)]+)\\|{[^}]+}\\|=[^=]+=\\)")
255 (put-text-property (1+ (match-beginning 1)) (1- (match-end 1))
258 (buffer-substring (1+ (match-beginning 1))
260 (riece-identity-server identity))))
261 (run-hook-with-args 'riece-after-insert-functions
262 point (goto-char (point-max)))
263 (set-window-point (get-buffer-window (current-buffer))
266 (defun riece-log-open-directory (&optional channel)
268 (let ((directory (riece-log-get-directory
269 (or channel riece-current-channel))))
270 (if (file-directory-p directory)
271 (funcall riece-log-open-directory-function directory)
272 (error "No log directory"))))
274 (defun riece-log-requires ()
275 (if (memq 'riece-button riece-addons)
278 (defun riece-log-insinuate ()
279 (make-directory riece-log-directory t)
280 (setq riece-log-lock-file
281 (expand-file-name (format "!%s-%d-%d"
282 (riece-log-encode-file-name (system-name))
285 riece-log-directory))
286 ;; FIXME: Use `riece-after-insert-functions' for trapping change,
287 ;; notice, wallops and so on. But must add argument.
288 (add-hook 'riece-after-display-message-functions
289 'riece-log-display-message-function)
290 (add-hook 'riece-channel-buffer-create-functions
291 'riece-log-flashback))
293 (defvar riece-command-mode-map)
294 (defun riece-log-enable ()
295 (define-key riece-command-mode-map "\C-cd" 'riece-log-open-directory)
296 (setq riece-log-enabled t))
298 (defun riece-log-disable ()
299 (define-key riece-command-mode-map "\C-cd" nil)
300 (setq riece-log-enabled nil))
304 ;;; riece-log.el ends here