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-lock-directory
49 (expand-file-name "=lock" riece-log-directory)
50 "*Lock directory for riece-log.
51 It is created if there is at least one instance of Emacs running riece-log."
55 (defcustom riece-log-directory-map nil
56 "*The map of channel name and directory name."
57 :type '(repeat (cons (string :tag "Channel name")
58 (string :tag "Directory name")))
61 (defcustom riece-log-flashback 10
62 "*If non-nil, irc messages flash back from log files.
63 If integer, flash back only this line numbers. t means all lines."
64 :type '(choice (integer :tag "line numbers")
65 (const t :tag "of the day")
66 (const nil :tag "no flashback"))
69 (defcustom riece-log-coding-system nil
70 "*Coding system used for log files."
74 (defcustom riece-log-file-name-coding-system
75 (if (boundp 'file-name-coding-system)
76 file-name-coding-system)
77 "*Coding system used for filenames of log files."
81 (defcustom riece-log-open-directory-function 'find-file
82 "*Function for opening a directory."
86 (defface riece-log-date-face
89 (:foreground "Gray70"))
92 (:foreground "DimGray"))
95 "Face used for displaying \"(YYYY/MM/dd)\" extent."
96 :group 'riece-highlight-faces)
97 (defvar riece-log-date-face 'riece-log-date-face)
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 ((file (riece-log-get-file (riece-message-target message)))
107 (coding-system-for-write riece-log-coding-system)
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))
116 (defun riece-log-get-file (identity)
118 (concat (format-time-string "%Y%m%d") ".txt")
119 (riece-log-get-directory identity)))
121 (defun riece-log-get-files (identity)
122 (let ((directory (riece-log-get-directory identity)))
123 (if (file-directory-p directory)
124 (nreverse (sort (directory-files directory t
126 (riece-make-interval-regexp "[0-9]" 8)
131 (defun riece-log-get-directory (identity)
132 (let ((prefix (riece-identity-canonicalize-prefix
133 (riece-identity-prefix identity)))
134 (server (riece-identity-server identity))
135 (map (assoc (riece-format-identity identity) riece-log-directory-map)))
137 (expand-file-name (cdr map) riece-log-directory)
138 (expand-file-name (riece-log-encode-file-name prefix)
140 (concat "." (riece-log-encode-file-name server))
141 riece-log-directory)))))
143 (defun riece-log-encode-file-name (file-name)
144 (if riece-log-file-name-coding-system
146 (encode-coding-string file-name
147 riece-log-file-name-coding-system)))
150 (while (string-match "[^-0-9A-Za-z_\x80-\xFF]" file-name index)
151 (setq c (aref file-name (match-beginning 0)))
153 (setq file-name (replace-match "==" nil t file-name)
154 index (1+ (match-end 0)))
155 (setq file-name (replace-match (format "=%02X" c) nil t file-name)
156 index (+ 2 (match-end 0)))))
159 (defun riece-log-decode-file-name (file-name)
161 (while (string-match "==\\|=\\([0-7][0-9A-F]\\)" file-name index)
162 (setq file-name (replace-match
163 (if (eq (aref file-name (1- (match-end 0))) ?=)
166 (car (read-from-string
167 (concat "?\\x" (match-string 1 file-name))))))
169 index (1+ (match-beginning 0))))
171 (if riece-log-file-name-coding-system
173 (decode-coding-string file-name
174 riece-log-file-name-coding-system)))
177 (defun riece-log-insert (identity lines)
178 "Insert logs for IDENTITY at most LINES.
179 If LINES is t, insert today's logs entirely."
181 (let* (file-name-coding-system
182 default-file-name-coding-system
183 (file (riece-log-get-file identity)))
184 (if (file-exists-p file)
185 (insert-file-contents file)))
186 (let* (file-name-coding-system
187 default-file-name-coding-system
188 (files (riece-log-get-files identity))
191 (while (and (< lines 0) files)
192 (if (and (file-exists-p (car files))
193 (string-match (concat (riece-make-interval-regexp "[0-9]" 8)
195 (setq name (file-name-nondirectory
198 (narrow-to-region (point) (point))
199 (insert-file-contents (car files))
200 (goto-char (point-max))
201 (setq lines (forward-line lines))
202 (delete-region (point-min) (point))
203 (unless (equal name (format-time-string "%Y%m%d.txt"))
204 (setq date (concat " (" (substring name 0 4) "/"
205 (substring name 4 6) "/"
206 (substring name 6 8) ")"))
211 (put-text-property point (point)
212 'riece-overlay-face 'riece-log-date-face)
214 (goto-char (point-min)))))
215 (setq files (cdr files))))))
217 (defun riece-log-flashback (identity)
218 (when riece-log-flashback
219 (riece-insert-info (current-buffer)
220 (if (eq riece-log-flashback t)
221 "Recent messages of the day:\n"
222 (format "Recent messages up to %d lines:\n"
223 riece-log-flashback)))
224 (let (buffer-read-only
225 (point (goto-char (point-max))))
226 (insert (with-temp-buffer
227 (riece-log-insert identity riece-log-flashback)
230 (while (re-search-forward
231 (concat "^" riece-time-prefix-regexp
232 "\\(<[^>]+>\\|>[^<]+<\\|([^)]+)\\|{[^}]+}\\|=[^=]+=\\)")
234 (put-text-property (1+ (match-beginning 1)) (1- (match-end 1))
237 (buffer-substring (1+ (match-beginning 1))
239 (riece-identity-server identity))))
240 (run-hook-with-args 'riece-after-insert-functions
241 point (goto-char (point-max)))
242 (set-window-point (get-buffer-window (current-buffer))
245 (defun riece-log-open-directory (&optional channel)
247 (let ((directory (riece-log-get-directory
248 (or channel riece-current-channel))))
249 (if (file-directory-p directory)
250 (funcall riece-log-open-directory-function directory)
251 (error "No log directory"))))
253 (defun riece-log-requires ()
254 (if (memq 'riece-button riece-addons)
257 (defun riece-log-insinuate ()
258 ;; FIXME: Use `riece-after-insert-functions' for trapping change,
259 ;; notice, wallops and so on. But must add argument.
260 (add-hook 'riece-after-display-message-functions
261 'riece-log-display-message-function)
262 (add-hook 'riece-channel-buffer-create-functions
263 'riece-log-flashback))
265 (defvar riece-command-mode-map)
266 (defun riece-log-enable ()
267 (define-key riece-command-mode-map "\C-cd" 'riece-log-open-directory)
268 (make-directory riece-log-directory t)
271 (make-directory riece-log-lock-directory)
272 (add-hook 'riece-exit-hook
275 (delete-directory riece-log-lock-directory)
277 (setq riece-log-enabled t))
280 (message "Can't create lock directory for riece-log")))))
282 (defun riece-log-disable ()
283 (define-key riece-command-mode-map "\C-cd" nil)
284 (setq riece-log-enabled nil))
288 ;;; riece-log.el ends here