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 (boolean :tag "flash back or not"))
61 (defcustom riece-log-coding-system nil
62 "*Coding system used for log files."
66 (defcustom riece-log-file-name-coding-system
67 (if (boundp 'file-name-coding-system)
68 file-name-coding-system)
69 "*Coding system used for filenames of log files."
73 (defcustom riece-log-open-directory-function 'find-file
74 "*Function for opening a directory."
78 (defvar riece-log-enabled nil)
80 (defconst riece-log-description
83 (defun riece-log-display-message-function (message)
85 (let ((file (riece-log-get-file (riece-message-target message)))
86 (coding-system-for-write riece-log-coding-system))
87 (unless (file-directory-p (file-name-directory file))
88 (make-directory (file-name-directory file) t))
89 (write-region (concat (format-time-string "%H:%M") " "
90 (riece-format-message message))
93 (defun riece-log-get-file (identity)
95 (concat (format-time-string "%Y%m%d") ".txt")
96 (riece-log-get-directory identity)))
98 (defun riece-log-get-files (identity)
99 (let ((directory (riece-log-get-directory identity)))
100 (if (file-directory-p directory)
101 (nreverse (sort (directory-files directory t
103 (riece-make-interval-regexp "[0-9]" 8)
108 (defun riece-log-get-directory (identity)
109 (let ((prefix (riece-identity-canonicalize-prefix
110 (riece-identity-prefix identity)))
111 (server (riece-identity-server identity))
112 (map (assoc (riece-format-identity identity) riece-log-directory-map))
115 (setq name (cdr map))
116 (expand-file-name (riece-log-encode-file-name prefix)
118 (concat "." (riece-log-encode-file-name server))
119 riece-log-directory)))))
121 (defun riece-log-encode-file-name (file-name)
122 (if riece-log-file-name-coding-system
124 (encode-coding-string file-name
125 riece-log-file-name-coding-system)))
128 (while (string-match "[^-0-9A-Za-z_\x80-\xFF]" file-name index)
129 (setq c (aref file-name (match-beginning 0)))
131 (setq file-name (replace-match "==" nil t file-name)
132 index (1+ (match-end 0)))
133 (setq file-name (replace-match (format "=%02X" c) nil t file-name)
134 index (+ 2 (match-end 0)))))
137 (defun riece-log-decode-file-name (file-name)
139 (while (string-match "==\\|=\\([0-7][0-9A-F]\\)" file-name index)
140 (setq file-name (replace-match
141 (if (eq (aref file-name (1- (match-end 0))) ?=)
144 (car (read-from-string
145 (concat "?\\x" (match-string 1 file-name))))))
147 index (1+ (match-beginning 0))))
149 (if riece-log-file-name-coding-system
151 (decode-coding-string file-name
152 riece-log-file-name-coding-system)))
155 (defun riece-log-insert (identity lines)
156 "Insert logs for IDENTITY at most LINES.
157 If LINES is t, insert today's logs entirely."
159 (let ((file (riece-log-get-file identity)))
160 (if (file-exists-p file)
161 (insert-file-contents file)))
162 (let ((files (riece-log-get-files identity))
165 (while (and (< lines 0) files)
166 (if (and (file-exists-p (car files))
167 (string-match (concat "\\([0-9][0-9][0-9][0-9]\\)"
168 "\\([0-9][0-9]\\)\\([0-9][0-9]\\).txt$")
171 (narrow-to-region (point) (point))
172 (setq date (concat " (" (match-string 1 (car files)) "/"
173 (match-string 2 (car files)) "/"
174 (match-string 3 (car files)) ")"))
175 (insert-file-contents (car files))
176 (goto-char (point-max))
177 (setq lines (forward-line lines))
178 (delete-region (point-min) (point))
183 (goto-char (point-min))))
184 (setq files (cdr files))))))
186 (defun riece-log-flashback (identity)
187 (when riece-log-flashback
188 (let (buffer-read-only
189 (point (goto-char (point-max))))
190 (insert (with-temp-buffer
191 (riece-log-insert identity riece-log-flashback)
194 (while (re-search-forward
195 "^[0-9][0-9]:[0-9][0-9] [<>]\\([^<>]+\\)[<>] " nil t)
196 (put-text-property (match-beginning 1) (match-end 1)
199 (riece-match-string-no-properties 1)
200 (riece-identity-server identity))))
201 (when (and (memq 'riece-button riece-addons)
202 riece-button-enabled)
203 (riece-button-update-buffer))
204 (goto-char (point-max))
205 (set-window-point (get-buffer-window (current-buffer))
208 (defun riece-log-open-directory (&optional channel)
210 (let ((directory (riece-log-get-directory
211 (or channel riece-current-channel))))
212 (if (file-directory-p directory)
213 (funcall riece-log-open-directory-function directory)
214 (error "No log directory"))))
216 (defun riece-log-requires ()
217 (if (memq 'riece-button riece-addons)
220 (defun riece-log-insinuate ()
221 ;; FIXME: Use `riece-after-insert-functions' for trapping change,
222 ;; notice, wallops and so on. But must add argument.
223 (add-hook 'riece-after-display-message-functions
224 'riece-log-display-message-function)
225 (add-hook 'riece-channel-buffer-create-functions
226 'riece-log-flashback))
228 (defvar riece-command-mode-map)
229 (defun riece-log-enable ()
230 (define-key riece-command-mode-map "\C-cd" 'riece-log-open-directory)
231 (setq riece-log-enabled t))
233 (defun riece-log-disable ()
234 (define-key riece-command-mode-map "\C-cd" nil)
235 (setq riece-log-enabled nil))
239 ;;; riece-log.el ends here