a2573feb8b37f09ddf45aad18d7d1c0d4e99eaff
[riece] / lisp / riece-log.el
1 ;;; riece-log.el --- saving irc logs add-on
2 ;; Copyright (C) 2003 OHASHI Akira
3
4 ;; Author: OHASHI Akira <bg66@koka-in.org>
5 ;; Keywords: IRC, riece
6
7 ;; This file is part of Riece.
8
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)
12 ;; any later version.
13
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.
18
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.
23
24 ;;; Commentary:
25
26 ;; This add-on saves irc logs for every channel.
27
28 ;; To use, add the following line to your ~/.riece/init.el:
29 ;; (add-to-list 'riece-addons 'riece-log)
30
31 ;;; Code:
32
33 (eval-when-compile (require 'riece-message)
34                    (require 'riece-button))
35
36 (defgroup riece-log nil
37   "Save irc log"
38   :group 'riece)
39
40 (defcustom riece-log-directory
41   (expand-file-name "log" riece-directory)
42   "*Where to look for log files."
43   :type 'directory
44   :group 'riece-log)
45
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")))
50   :group 'riece-log)
51
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"))
57   :group 'riece-log)
58
59 (defcustom riece-log-coding-system nil
60   "*Coding system used for log files."
61   :type 'symbol
62   :group 'riece-log)
63
64 (defcustom riece-log-open-directory-function 'find-file
65   "*Function for opening a directory."
66   :type 'function
67   :group 'riece-log)
68
69 (defvar riece-log-enabled nil)
70
71 (defun riece-log-display-message-function (message)
72   (if riece-log-enabled
73       (let ((file (riece-log-get-file (riece-message-target message)))
74             (coding-system-for-write riece-log-coding-system))
75         (unless (file-directory-p (file-name-directory file))
76           (make-directory (file-name-directory file) t))
77         (write-region (concat (format-time-string "%H:%M") " "
78                               (riece-format-message message))
79                       nil file t 0))))
80
81 (defun riece-log-get-file (identity)
82   (expand-file-name
83    (concat (format-time-string "%Y%m%d") ".log")
84    (riece-log-get-directory identity)))
85
86 (defun riece-log-get-directory (identity)
87   (let ((channel (riece-identity-canonicalize-prefix
88                   (riece-identity-prefix identity)))
89         (server (riece-identity-server identity))
90         (map (assoc (riece-format-identity identity) riece-log-directory-map))
91         name)
92     (cond (map (setq name (cdr map)))
93           ((string-match riece-strict-channel-regexp channel)
94            (let ((suffix (match-string 2 channel)))
95              (setq name (substring channel (match-end 1) (match-beginning 2)))
96              (when (and (stringp suffix)
97                         (string-match "^:\\*\\.\\(.*\\)" suffix))
98                (setq name (concat name "-" (match-string 1 suffix))))))
99           (t (setq name "priv")))
100     (if server
101         (expand-file-name name (expand-file-name server riece-log-directory))
102       (expand-file-name name riece-log-directory))))
103
104 (defun riece-log-flashback (identity)
105   (when (and riece-log-enabled riece-log-flashback)
106     (let ((file (riece-log-get-file identity)))
107       (when (file-exists-p file)
108         (let (string)
109           (with-temp-buffer
110             (insert-file-contents file)
111             (if (not (integerp riece-log-flashback))
112                 (goto-char (point-min))
113               (goto-char (point-max))
114               (forward-line (- riece-log-flashback)))
115             (setq string (buffer-substring (point) (point-max))))
116           (let (buffer-read-only)
117             (goto-char (point-max))
118             (insert string)
119             (goto-char (point-min))
120             (while (re-search-forward
121                     "^[0-9][0-9]:[0-9][0-9] [<>]\\([^<>]+\\)[<>] " nil t)
122               (put-text-property (match-beginning 1) (match-end 1)
123                                  'riece-identity
124                                  (riece-make-identity
125                                   (riece-match-string-no-properties 1)
126                                   (riece-identity-server identity))))
127             (if (memq 'riece-button riece-addons)
128                 (riece-button-update-buffer))
129             (goto-char (point-max))
130             (set-window-point (get-buffer-window (current-buffer))
131                               (point))))))))
132
133 (defun riece-log-open-directory (&optional channel)
134   (interactive)
135   (let ((directory (riece-log-get-directory
136                     (or channel riece-current-channel))))
137     (if (file-directory-p directory)
138         (funcall riece-log-open-directory-function directory)
139       (error "No log directory"))))
140
141 (defun riece-log-requires ()
142   (if (memq 'riece-button riece-addons)
143       '(riece-button)))
144
145 (defun riece-log-insinuate ()
146   ;; FIXME: Use `riece-after-insert-functions' for trapping change,
147   ;; notice, wallops and so on. But must add argument.
148   (add-hook 'riece-after-display-message-functions
149             'riece-log-display-message-function)
150   (add-hook 'riece-channel-buffer-create-functions
151             'riece-log-flashback))
152
153 (defvar riece-command-mode-map)
154 (defun riece-log-enable ()
155   (define-key riece-command-mode-map "\C-cd" 'riece-log-open-directory)
156   (setq riece-log-enabled t))
157
158 (defun riece-log-disable ()
159   (define-key riece-command-mode-map "\C-cd" nil)
160   (setq riece-log-enabled nil))
161
162 (provide 'riece-log)
163
164 ;;; riece-log.el ends here