1 ;;; riece-log.el --- Save IRC logs -*- lexical-binding: t -*-
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., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;;; NOTE: This is an add-on module for Riece.
32 (require 'riece-message)
33 (require 'riece-button)
36 (defgroup riece-log nil
41 (defcustom riece-log-directory
42 (expand-file-name "log" riece-directory)
43 "*Where to look for log files."
47 (defcustom riece-log-directory-map nil
48 "*The map of channel name and directory name."
49 :type '(repeat (cons (string :tag "Channel name")
50 (string :tag "Directory name")))
53 (defcustom riece-log-flashback 10
54 "*If non-nil, irc messages flash back from log files.
55 If integer, flash back only this line numbers. t means all lines."
56 :type '(choice (integer :tag "line numbers")
57 (const t :tag "of the day")
58 (const nil :tag "no flashback"))
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 (defface riece-log-date-face
76 (:foreground "Gray70"))
79 (:foreground "DimGray"))
82 "Face used for displaying \"(YYYY/MM/dd)\" extent."
83 :group 'riece-highlight-faces)
84 (defvar riece-log-date-face 'riece-log-date-face)
86 (defvar riece-log-lock-file nil
87 "Lock file for riece-log.
88 It is created if there is at least one instance of Emacs running riece-log.")
90 (defconst riece-log-file-name-regexp
91 (concat (riece-make-interval-regexp "[0-9]" 8) "\\.txt\\(\\.\\(.*\\)\\)?$"))
93 (defconst riece-log-description
96 (defun riece-log-display-message-function (message)
97 (if (get 'riece-log 'riece-addon-enabled)
98 (let* ((coding-system-for-write
100 (or riece-log-coding-system
101 (car (get-language-info current-language-environment
103 (file (riece-log-make-file-name
104 (if (and (riece-message-private-p message)
105 (not (riece-message-own-p message)))
106 (riece-message-speaker message)
107 (riece-message-target message))
108 coding-system-for-write))
109 (file-name-coding-system 'no-conversion))
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))
115 riece-log-lock-file))))
117 (defun riece-log-make-file-name (identity coding-system)
118 (expand-file-name (if (and (featurep 'mule) coding-system)
120 (format-time-string "%Y%m%d")
123 (format-time-string "%Y%m%d")))
124 (riece-log-directory identity)))
126 (defun riece-log-list-files (identity time)
127 (let ((directory (riece-log-directory identity))
128 (time-prefix (format-time-string "%Y%m%d" (or time '(0 0))))
130 (when (file-directory-p directory)
131 (setq files (nreverse (sort (directory-files
133 (concat "^" riece-log-file-name-regexp)
137 (string-lessp (file-name-nondirectory (car files))
139 (setq files (cdr files)))
142 (defun riece-log-directory (identity)
143 (let ((prefix (riece-identity-canonicalize-prefix
144 (riece-identity-prefix identity)))
145 (server (riece-identity-server identity))
146 (map (assoc (riece-format-identity identity) riece-log-directory-map)))
148 (expand-file-name (cdr map) riece-log-directory)
149 (expand-file-name (riece-log-encode-file-name prefix)
151 (concat "." (riece-log-encode-file-name server))
152 riece-log-directory)))))
154 (defun riece-log-encode-file-name (file-name)
155 (if riece-log-file-name-coding-system
157 (encode-coding-string file-name
158 riece-log-file-name-coding-system)))
161 (while (string-match "[^-0-9A-Za-z_\x80-\xFF]" file-name index)
162 (setq c (aref file-name (match-beginning 0)))
164 (setq file-name (replace-match "==" nil t file-name)
165 index (1+ (match-end 0)))
166 (setq file-name (replace-match (format "=%02X" c) nil t file-name)
167 index (+ 2 (match-end 0)))))
170 (defun riece-log-decode-file-name (file-name)
172 (while (string-match "==\\|=\\([0-7][0-9A-F]\\)" file-name index)
173 (setq file-name (replace-match
174 (if (eq (aref file-name (1- (match-end 0))) ?=)
177 (car (read-from-string
178 (concat "?\\x" (match-string 1 file-name))))))
180 index (1+ (match-beginning 0))))
182 (if riece-log-file-name-coding-system
184 (decode-coding-string file-name
185 riece-log-file-name-coding-system)))
188 (defun riece-log-insert (identity lines)
189 "Insert logs for IDENTITY at most LINES.
190 If LINES is t, insert today's logs entirely."
191 (let* ((file-name-coding-system 'no-conversion)
192 (files (riece-log-list-files identity
193 (if (eq lines t) (current-time))))
194 name coding-system date point)
195 (while (and (or (eq lines t) (> lines 0)) files)
197 (narrow-to-region (point) (point))
198 (if (and (string-match
199 (concat "^" riece-log-file-name-regexp)
200 (setq name (file-name-nondirectory (car files))))
204 (intern (substring name (match-beginning 2))))
205 (if (featurep 'xemacs)
206 (setq coding-system (find-coding-system coding-system))
207 (unless (coding-system-p coding-system)
208 (setq coding-system nil)))
210 (let ((coding-system-for-read coding-system))
211 (insert-file-contents (car files)))
212 ;;don't insert file contents if they use non
213 ;;supported coding-system.
215 ;;if the filename has no coding-system suffix, decode with
216 ;;riece-log-coding-system.
217 (let ((coding-system-for-read riece-log-coding-system))
218 (insert-file-contents (car files))))
219 ;;lines in the file contents are in reversed order.
221 (goto-char (point-max))
222 (setq lines (- (forward-line (- lines))))
223 (delete-region (point-min) (point)))
224 ;;add (YYYY/MM/dd) suffix on each line left in the current buffer.
225 (unless (equal (substring name 0 8) (format-time-string "%Y%m%d"))
226 (setq date (concat " (" (substring name 0 4) "/"
227 (substring name 4 6) "/"
228 (substring name 6 8) ")"))
233 (put-text-property point (point)
234 'riece-overlay-face 'riece-log-date-face)
236 (goto-char (point-min))))
237 (setq files (cdr files)))))
239 (defun riece-log-flashback (identity)
240 (when riece-log-flashback
241 (riece-insert-info (current-buffer)
242 (if (eq riece-log-flashback t)
243 (riece-mcat "Recent messages of the day:\n")
245 "Recent messages up to %d lines:\n")
246 riece-log-flashback)))
247 (let (buffer-read-only
248 (point (goto-char (point-max))))
249 (insert (with-temp-buffer
250 (riece-log-insert identity riece-log-flashback)
253 (while (re-search-forward
254 (concat "^" riece-time-prefix-regexp
255 "\\(<[^>]+>\\|>[^<]+<\\|([^)]+)\\|{[^}]+}\\|=[^=]+=\\)")
257 (put-text-property (1+ (match-beginning 1)) (1- (match-end 1))
260 (buffer-substring (1+ (match-beginning 1))
262 (riece-identity-server identity))))
263 (run-hook-with-args 'riece-after-insert-functions
264 point (goto-char (point-max)))
265 (set-window-point (get-buffer-window (current-buffer))
268 (defun riece-log-dired (&optional channel)
270 (let ((directory (riece-log-directory (or channel riece-current-channel))))
271 (if (file-directory-p directory)
273 (error "No log directory"))))
275 (defun riece-log-requires ()
276 (if (memq 'riece-button riece-addons)
279 (defun riece-log-insinuate ()
280 (make-directory riece-log-directory t)
281 (setq riece-log-lock-file
282 (expand-file-name (format "!%s-%d-%d"
283 (riece-log-encode-file-name (system-name))
286 riece-log-directory))
287 ;; FIXME: Use `riece-after-insert-functions' for trapping change,
288 ;; notice, wallops and so on. But must add argument.
289 (add-hook 'riece-after-display-message-functions
290 'riece-log-display-message-function)
291 (add-hook 'riece-channel-buffer-create-functions
292 'riece-log-flashback))
294 (defun riece-log-uninstall ()
295 (setq riece-log-lock-file nil)
296 (remove-hook 'riece-after-display-message-functions
297 'riece-log-display-message-function)
298 (remove-hook 'riece-channel-buffer-create-functions
299 'riece-log-flashback))
301 (defvar riece-command-mode-map)
302 (defun riece-log-enable ()
303 (define-key riece-command-mode-map "\C-cd" 'riece-log-dired))
305 (defun riece-log-disable ()
306 (define-key riece-command-mode-map "\C-cd" nil))
310 ;;; riece-log.el ends here