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 (or riece-log-coding-system
102 buffer-file-coding-system))
103 (file (riece-log-make-file-name (riece-message-target message)
104 coding-system-for-write))
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-make-file-name (identity coding-system)
115 (expand-file-name (format "%s.txt.%s"
116 (format-time-string "%Y%m%d")
118 (riece-log-directory identity)))
120 (defun riece-log-list-files (identity time)
121 (let ((directory (riece-log-directory identity))
122 (time-prefix (format-time-string "%Y%m%d" (or time '(0 0))))
124 (when (file-directory-p directory)
125 (setq files (nreverse (sort (directory-files
127 (concat "^" riece-log-file-name-regexp)
131 (string-lessp (file-name-nondirectory (car files))
133 (setq files (cdr files)))
136 (defun riece-log-directory (identity)
137 (let ((prefix (riece-identity-canonicalize-prefix
138 (riece-identity-prefix identity)))
139 (server (riece-identity-server identity))
140 (map (assoc (riece-format-identity identity) riece-log-directory-map)))
142 (expand-file-name (cdr map) riece-log-directory)
143 (expand-file-name (riece-log-encode-file-name prefix)
145 (concat "." (riece-log-encode-file-name server))
146 riece-log-directory)))))
148 (defun riece-log-encode-file-name (file-name)
149 (if riece-log-file-name-coding-system
151 (encode-coding-string file-name
152 riece-log-file-name-coding-system)))
155 (while (string-match "[^-0-9A-Za-z_\x80-\xFF]" file-name index)
156 (setq c (aref file-name (match-beginning 0)))
158 (setq file-name (replace-match "==" nil t file-name)
159 index (1+ (match-end 0)))
160 (setq file-name (replace-match (format "=%02X" c) nil t file-name)
161 index (+ 2 (match-end 0)))))
164 (defun riece-log-decode-file-name (file-name)
166 (while (string-match "==\\|=\\([0-7][0-9A-F]\\)" file-name index)
167 (setq file-name (replace-match
168 (if (eq (aref file-name (1- (match-end 0))) ?=)
171 (car (read-from-string
172 (concat "?\\x" (match-string 1 file-name))))))
174 index (1+ (match-beginning 0))))
176 (if riece-log-file-name-coding-system
178 (decode-coding-string file-name
179 riece-log-file-name-coding-system)))
182 (defun riece-log-insert (identity lines)
183 "Insert logs for IDENTITY at most LINES.
184 If LINES is t, insert today's logs entirely."
185 (let* (file-name-coding-system
186 default-file-name-coding-system
187 (files (riece-log-list-files identity
188 (if (eq lines t) (current-time))))
189 name coding-system date point)
190 (while (and (or (eq lines t) (> lines 0)) files)
192 (narrow-to-region (point) (point))
193 (if (and (string-match
194 (concat "^" riece-log-file-name-regexp)
195 (setq name (file-name-nondirectory (car files))))
199 (intern (substring name (match-beginning 2))))
200 (if (featurep 'xemacs)
201 (setq coding-system (find-coding-system coding-system))
202 (unless (coding-system-p coding-system)
203 (setq coding-system nil)))
205 (let ((coding-system-for-read coding-system))
206 (insert-file-contents (car files)))
207 ;;don't insert file contents if they use non
208 ;;supported coding-system.
210 ;;if the filename has no coding-system suffix, decode with
211 ;;riece-log-coding-system.
212 (let ((coding-system-for-read riece-log-coding-system))
213 (insert-file-contents (car files))))
214 ;;lines in the file contents are in reversed order.
216 (goto-char (point-max))
217 (setq lines (- (forward-line (- lines))))
218 (delete-region (point-min) (point)))
219 ;;add (YYYY/MM/dd) suffix on each line left in the current buffer.
220 (unless (equal (substring name 0 8) (format-time-string "%Y%m%d"))
221 (setq date (concat " (" (substring name 0 4) "/"
222 (substring name 4 6) "/"
223 (substring name 6 8) ")"))
228 (put-text-property point (point)
229 'riece-overlay-face 'riece-log-date-face)
231 (goto-char (point-min))))
232 (setq files (cdr files)))))
234 (defun riece-log-flashback (identity)
235 (when riece-log-flashback
236 (riece-insert-info (current-buffer)
237 (if (eq riece-log-flashback t)
238 "Recent messages of the day:\n"
239 (format "Recent messages up to %d lines:\n"
240 riece-log-flashback)))
241 (let (buffer-read-only
242 (point (goto-char (point-max))))
243 (insert (with-temp-buffer
244 (riece-log-insert identity riece-log-flashback)
247 (while (re-search-forward
248 (concat "^" riece-time-prefix-regexp
249 "\\(<[^>]+>\\|>[^<]+<\\|([^)]+)\\|{[^}]+}\\|=[^=]+=\\)")
251 (put-text-property (1+ (match-beginning 1)) (1- (match-end 1))
254 (buffer-substring (1+ (match-beginning 1))
256 (riece-identity-server identity))))
257 (run-hook-with-args 'riece-after-insert-functions
258 point (goto-char (point-max)))
259 (set-window-point (get-buffer-window (current-buffer))
262 (defun riece-log-dired (&optional channel)
264 (let ((directory (riece-log-directory (or channel riece-current-channel))))
265 (if (file-directory-p directory)
267 (error "No log directory"))))
269 (defun riece-log-requires ()
270 (if (memq 'riece-button riece-addons)
273 (defun riece-log-insinuate ()
274 (make-directory riece-log-directory t)
275 (setq riece-log-lock-file
276 (expand-file-name (format "!%s-%d-%d"
277 (riece-log-encode-file-name (system-name))
280 riece-log-directory))
281 ;; FIXME: Use `riece-after-insert-functions' for trapping change,
282 ;; notice, wallops and so on. But must add argument.
283 (add-hook 'riece-after-display-message-functions
284 'riece-log-display-message-function)
285 (add-hook 'riece-channel-buffer-create-functions
286 'riece-log-flashback))
288 (defvar riece-command-mode-map)
289 (defun riece-log-enable ()
290 (define-key riece-command-mode-map "\C-cd" 'riece-log-dired)
291 (setq riece-log-enabled t))
293 (defun riece-log-disable ()
294 (define-key riece-command-mode-map "\C-cd" nil)
295 (setq riece-log-enabled nil))
299 ;;; riece-log.el ends here