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 (eval-when-compile (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-open-directory-function 'find-file
65 "*Function for opening a directory."
69 (defvar riece-log-enabled nil)
71 (defconst riece-log-description
74 (defun riece-log-display-message-function (message)
76 (let ((file (riece-log-get-file (riece-message-target message)))
77 (coding-system-for-write riece-log-coding-system))
78 (unless (file-directory-p (file-name-directory file))
79 (make-directory (file-name-directory file) t))
80 (write-region (concat (format-time-string "%H:%M") " "
81 (riece-format-message message))
84 (defun riece-log-get-file (identity)
86 (concat (format-time-string "%Y%m%d") ".log")
87 (riece-log-get-directory identity)))
89 (defun riece-log-get-directory (identity)
90 (let ((channel (riece-identity-canonicalize-prefix
91 (riece-identity-prefix identity)))
92 (server (riece-identity-server identity))
93 (map (assoc (riece-format-identity identity) riece-log-directory-map))
95 (cond (map (setq name (cdr map)))
96 ((string-match riece-strict-channel-regexp channel)
97 (let ((suffix (match-string 2 channel)))
98 (setq name (substring channel (match-end 1) (match-beginning 2)))
99 (when (and (stringp suffix)
100 (string-match "^:\\*\\.\\(.*\\)" suffix))
101 (setq name (concat name "-" (match-string 1 suffix))))))
102 (t (setq name "priv")))
104 (expand-file-name name (expand-file-name server riece-log-directory))
105 (expand-file-name name riece-log-directory))))
107 (defun riece-log-flashback (identity)
108 (when riece-log-flashback
109 (let ((file (riece-log-get-file identity)))
110 (when (file-exists-p file)
113 (insert-file-contents file)
114 (if (not (integerp riece-log-flashback))
115 (goto-char (point-min))
116 (goto-char (point-max))
117 (forward-line (- riece-log-flashback)))
118 (setq string (buffer-substring (point) (point-max))))
119 (let (buffer-read-only)
120 (goto-char (point-max))
122 (goto-char (point-min))
123 (while (re-search-forward
124 "^[0-9][0-9]:[0-9][0-9] [<>]\\([^<>]+\\)[<>] " nil t)
125 (put-text-property (match-beginning 1) (match-end 1)
128 (riece-match-string-no-properties 1)
129 (riece-identity-server identity))))
130 (if (memq 'riece-button riece-addons)
131 (riece-button-update-buffer))
132 (goto-char (point-max))
133 (set-window-point (get-buffer-window (current-buffer))
136 (defun riece-log-open-directory (&optional channel)
138 (let ((directory (riece-log-get-directory
139 (or channel riece-current-channel))))
140 (if (file-directory-p directory)
141 (funcall riece-log-open-directory-function directory)
142 (error "No log directory"))))
144 (defun riece-log-requires ()
145 (if (memq 'riece-button riece-addons)
148 (defun riece-log-insinuate ()
149 ;; FIXME: Use `riece-after-insert-functions' for trapping change,
150 ;; notice, wallops and so on. But must add argument.
151 (add-hook 'riece-after-display-message-functions
152 'riece-log-display-message-function)
153 (add-hook 'riece-channel-buffer-create-functions
154 'riece-log-flashback))
156 (defvar riece-command-mode-map)
157 (defun riece-log-enable ()
158 (define-key riece-command-mode-map "\C-cd" 'riece-log-open-directory)
159 (setq riece-log-enabled t))
161 (defun riece-log-disable ()
162 (define-key riece-command-mode-map "\C-cd" nil)
163 (setq riece-log-enabled nil))
167 ;;; riece-log.el ends here