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 (defvar riece-log-enabled nil)
98 (defconst riece-log-description
101 (defun riece-log-display-message-function (message)
102 (if riece-log-enabled
103 (let ((file (riece-log-get-file (riece-message-target message)))
104 (coding-system-for-write riece-log-coding-system)
105 file-name-coding-system
106 default-file-name-coding-system)
107 (unless (file-directory-p (file-name-directory file))
108 (make-directory (file-name-directory file) t))
109 (write-region (concat (format-time-string "%H:%M") " "
110 (riece-format-message message))
112 riece-log-lock-file))))
114 (defun riece-log-get-file (identity)
116 (concat (format-time-string "%Y%m%d") ".txt")
117 (riece-log-get-directory identity)))
119 (defun riece-log-get-files (identity)
120 (let ((directory (riece-log-get-directory identity)))
121 (if (file-directory-p directory)
122 (nreverse (sort (directory-files directory t
124 (riece-make-interval-regexp "[0-9]" 8)
129 (defun riece-log-get-directory (identity)
130 (let ((prefix (riece-identity-canonicalize-prefix
131 (riece-identity-prefix identity)))
132 (server (riece-identity-server identity))
133 (map (assoc (riece-format-identity identity) riece-log-directory-map)))
135 (expand-file-name (cdr map) riece-log-directory)
136 (expand-file-name (riece-log-encode-file-name prefix)
138 (concat "." (riece-log-encode-file-name server))
139 riece-log-directory)))))
141 (defun riece-log-encode-file-name (file-name)
142 (if riece-log-file-name-coding-system
144 (encode-coding-string file-name
145 riece-log-file-name-coding-system)))
148 (while (string-match "[^-0-9A-Za-z_\x80-\xFF]" file-name index)
149 (setq c (aref file-name (match-beginning 0)))
151 (setq file-name (replace-match "==" nil t file-name)
152 index (1+ (match-end 0)))
153 (setq file-name (replace-match (format "=%02X" c) nil t file-name)
154 index (+ 2 (match-end 0)))))
157 (defun riece-log-decode-file-name (file-name)
159 (while (string-match "==\\|=\\([0-7][0-9A-F]\\)" file-name index)
160 (setq file-name (replace-match
161 (if (eq (aref file-name (1- (match-end 0))) ?=)
164 (car (read-from-string
165 (concat "?\\x" (match-string 1 file-name))))))
167 index (1+ (match-beginning 0))))
169 (if riece-log-file-name-coding-system
171 (decode-coding-string file-name
172 riece-log-file-name-coding-system)))
175 (defun riece-log-insert (identity lines)
176 "Insert logs for IDENTITY at most LINES.
177 If LINES is t, insert today's logs entirely."
179 (let* (file-name-coding-system
180 default-file-name-coding-system
181 (file (riece-log-get-file identity)))
182 (if (file-exists-p file)
183 (insert-file-contents file)))
184 (let* (file-name-coding-system
185 default-file-name-coding-system
186 (files (riece-log-get-files identity))
189 (while (and (< lines 0) files)
190 (if (and (file-exists-p (car files))
191 (string-match (concat (riece-make-interval-regexp "[0-9]" 8)
193 (setq name (file-name-nondirectory
196 (narrow-to-region (point) (point))
197 (insert-file-contents (car files))
198 (goto-char (point-max))
199 (setq lines (forward-line lines))
200 (delete-region (point-min) (point))
201 (unless (equal name (format-time-string "%Y%m%d.txt"))
202 (setq date (concat " (" (substring name 0 4) "/"
203 (substring name 4 6) "/"
204 (substring name 6 8) ")"))
209 (put-text-property point (point)
210 'riece-overlay-face 'riece-log-date-face)
212 (goto-char (point-min)))))
213 (setq files (cdr files))))))
215 (defun riece-log-flashback (identity)
216 (when riece-log-flashback
217 (riece-insert-info (current-buffer)
218 (if (eq riece-log-flashback t)
219 "Recent messages of the day:\n"
220 (format "Recent messages up to %d lines:\n"
221 riece-log-flashback)))
222 (let (buffer-read-only
223 (point (goto-char (point-max))))
224 (insert (with-temp-buffer
225 (riece-log-insert identity riece-log-flashback)
228 (while (re-search-forward
229 (concat "^" riece-time-prefix-regexp
230 "\\(<[^>]+>\\|>[^<]+<\\|([^)]+)\\|{[^}]+}\\|=[^=]+=\\)")
232 (put-text-property (1+ (match-beginning 1)) (1- (match-end 1))
235 (buffer-substring (1+ (match-beginning 1))
237 (riece-identity-server identity))))
238 (run-hook-with-args 'riece-after-insert-functions
239 point (goto-char (point-max)))
240 (set-window-point (get-buffer-window (current-buffer))
243 (defun riece-log-open-directory (&optional channel)
245 (let ((directory (riece-log-get-directory
246 (or channel riece-current-channel))))
247 (if (file-directory-p directory)
248 (funcall riece-log-open-directory-function directory)
249 (error "No log directory"))))
251 (defun riece-log-requires ()
252 (if (memq 'riece-button riece-addons)
255 (defun riece-log-insinuate ()
256 (make-directory riece-log-directory t)
257 (setq riece-log-lock-file
258 (expand-file-name (format "!%s-%d-%d"
259 (riece-log-encode-file-name (system-name))
262 riece-log-directory))
263 ;; FIXME: Use `riece-after-insert-functions' for trapping change,
264 ;; notice, wallops and so on. But must add argument.
265 (add-hook 'riece-after-display-message-functions
266 'riece-log-display-message-function)
267 (add-hook 'riece-channel-buffer-create-functions
268 'riece-log-flashback))
270 (defvar riece-command-mode-map)
271 (defun riece-log-enable ()
272 (define-key riece-command-mode-map "\C-cd" 'riece-log-open-directory)
273 (setq riece-log-enabled t))
275 (defun riece-log-disable ()
276 (define-key riece-command-mode-map "\C-cd" nil)
277 (setq riece-log-enabled nil))
281 ;;; riece-log.el ends here