1 ;;; riece-log.el --- saving irc logs add-on
2 ;; Copyright (C) 2003 OHASHI Akira
4 ;; Author: OHASHI Akira <bg66@koka-in.org>
5 ;; Keywords: IRC, riece
7 ;; This file is part of Riece.
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
26 ;; This add-on saves irc logs for every channel.
28 ;; To use, add the following line to your ~/.riece/init.el:
29 ;; (add-to-list 'riece-addons 'riece-log)
33 (require 'riece-message)
34 (require 'riece-button)
36 (defgroup riece-log nil
40 (defcustom riece-log-directory
41 (expand-file-name "log" riece-directory)
42 "*Where to look for log files."
46 (defcustom riece-log-directory-map nil
47 "*The map of channel name and directory name."
48 :type '(repeat (cons (string :tag "Channel name")
49 (string :tag "Directory name")))
52 (defcustom riece-log-flashback 10
53 "*If non-nil, irc messages flash back from log files.
54 If integer, flash back only this line numbers. t means all lines."
55 :type '(choice (integer :tag "line numbers")
56 (boolean :tag "flash back or not"))
59 (defcustom riece-log-coding-system nil
60 "*Coding system used for log files."
64 (defcustom riece-log-file-name-coding-system
65 (if (boundp 'file-name-coding-system)
66 file-name-coding-system)
67 "*Coding system used for filenames of log files."
71 (defcustom riece-log-open-directory-function 'find-file
72 "*Function for opening a directory."
76 (defvar riece-log-enabled nil)
78 (defconst riece-log-description
81 (defun riece-log-display-message-function (message)
83 (let ((file (riece-log-get-file (riece-message-target message)))
84 (coding-system-for-write riece-log-coding-system))
85 (unless (file-directory-p (file-name-directory file))
86 (make-directory (file-name-directory file) t))
87 (write-region (concat (format-time-string "%H:%M") " "
88 (riece-format-message message))
91 (defun riece-log-get-file (identity)
93 (concat (format-time-string "%Y%m%d") ".log")
94 (riece-log-get-directory identity)))
96 (defun riece-log-get-files (identity)
97 (let ((files (directory-files (riece-log-get-directory identity) t
99 (riece-make-interval-regexp "[0-9]" 8)
102 (nreverse (sort files #'string-lessp))))
104 (defun riece-log-get-directory (identity)
105 (let ((channel (riece-identity-canonicalize-prefix
106 (riece-identity-prefix identity)))
107 (server (riece-identity-server identity))
108 (map (assoc (riece-format-identity identity) riece-log-directory-map))
110 (cond (map (setq name (cdr map)))
111 ((string-match riece-strict-channel-regexp channel)
112 (let ((suffix (match-string 2 channel)))
113 (setq name (substring channel (match-end 1) (match-beginning 2)))
114 (when (and (stringp suffix)
115 (string-match "^:\\*\\.\\(.*\\)" suffix))
116 (setq name (concat name "-" (match-string 1 suffix))))))
117 (t (setq name "priv")))
119 (expand-file-name name (expand-file-name server riece-log-directory))
120 (expand-file-name name riece-log-directory))))
122 (defun riece-log-get-directory-1 (identity)
123 (let ((prefix (riece-identity-canonicalize-prefix
124 (riece-identity-prefix identity)))
125 (server (riece-identity-server identity))
126 (map (assoc (riece-format-identity identity) riece-log-directory-map))
129 (setq name (cdr map))
130 (expand-file-name (riece-log-encode-file-name prefix)
132 (riece-log-encode-file-name (concat "." server))
133 riece-log-directory)))))
135 (defun riece-log-encode-file-name (file-name)
136 (if riece-log-file-name-coding-system
138 (encode-coding-string file-name
139 riece-log-file-name-coding-system)))
142 (while (string-match "[^-0-9A-Za-z=_\x80-\xFF]" file-name index)
143 (setq c (aref file-name (match-beginning 0))
144 file-name (replace-match
152 (defun riece-log-decode-file-name (file-name)
154 (while (string-match "==\\|=\\([0-7][0-9A-F]\\)" file-name index)
155 (setq file-name (replace-match
156 (if (eq (aref file-name (match-end 0)) ?=)
159 (car (read-from-string
160 (concat "?\\x" (match-string 1 file-name))))))
164 (if riece-log-file-name-coding-system
166 (decode-coding-string file-name
167 riece-log-file-name-coding-system)))
170 (defun riece-log-flashback-1 (identity)
171 (if (eq riece-log-flashback t)
172 (let ((file (riece-log-get-file identity)))
173 (if (file-exists-p file)
174 (insert-file-contents file)))
175 (let ((files (riece-log-get-files identity))
176 (lines (- riece-log-flashback))
178 (while (and (< lines 0) files)
179 (if (and (file-exists-p (car files))
180 (string-match (concat "\\("
181 (riece-make-interval-regexp "[0-9]" 4)
183 (riece-make-interval-regexp "[0-9]" 2)
185 (riece-make-interval-regexp "[0-9]" 2)
189 (narrow-to-region (point-min) (point-min))
190 (setq date (concat " (" (match-string 1 (car files)) "/"
191 (match-string 2 (car files)) "/"
192 (match-string 3 (car files)) ")"))
193 (insert-file-contents (car files))
194 (goto-char (point-max))
195 (setq lines (forward-line lines)
202 (setq files (cdr files)))
204 (delete-region (point-min) (point))))))
206 (defun riece-log-flashback (identity)
207 (when riece-log-flashback
208 (let (buffer-read-only
209 (point (goto-char (point-max))))
210 (insert (with-temp-buffer
211 (riece-log-flashback-1 identity)
214 (while (re-search-forward
215 "^[0-9][0-9]:[0-9][0-9] [<>]\\([^<>]+\\)[<>] " nil t)
216 (put-text-property (match-beginning 1) (match-end 1)
219 (riece-match-string-no-properties 1)
220 (riece-identity-server identity))))
221 (when (and (memq 'riece-button riece-addons)
222 riece-button-enabled)
223 (riece-button-update-buffer))
224 (goto-char (point-max))
225 (set-window-point (get-buffer-window (current-buffer))
228 (defun riece-log-open-directory (&optional channel)
230 (let ((directory (riece-log-get-directory
231 (or channel riece-current-channel))))
232 (if (file-directory-p directory)
233 (funcall riece-log-open-directory-function directory)
234 (error "No log directory"))))
236 (defun riece-log-requires ()
237 (if (memq 'riece-button riece-addons)
240 (defun riece-log-insinuate ()
241 ;; FIXME: Use `riece-after-insert-functions' for trapping change,
242 ;; notice, wallops and so on. But must add argument.
243 (add-hook 'riece-after-display-message-functions
244 'riece-log-display-message-function)
245 (add-hook 'riece-channel-buffer-create-functions
246 'riece-log-flashback))
248 (defvar riece-command-mode-map)
249 (defun riece-log-enable ()
250 (define-key riece-command-mode-map "\C-cd" 'riece-log-open-directory)
251 (setq riece-log-enabled t))
253 (defun riece-log-disable ()
254 (define-key riece-command-mode-map "\C-cd" nil)
255 (setq riece-log-enabled nil))
259 ;;; riece-log.el ends here