* riece-log.el (riece-log-flashback): Use
[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 (defun riece-log-display-message-function (message)
65   (let ((open-bracket
66          (funcall riece-message-make-open-bracket-function message))
67         (close-bracket
68          (funcall riece-message-make-close-bracket-function message))
69         (name
70          (funcall riece-message-make-name-function message))
71         (file (riece-log-get-file (riece-message-target message)))
72         (coding-system-for-write riece-log-coding-system))
73     (unless (file-directory-p (file-name-directory file))
74       (make-directory (file-name-directory file) t))
75     (write-region (concat (format-time-string "%H:%M") " "
76                           open-bracket name close-bracket
77                           " " (riece-message-text message) "\n")
78                   nil file t 0)))
79
80 (defun riece-log-get-file (identity)
81   (expand-file-name
82    (concat (format-time-string "%Y%m%d") ".log")
83    (riece-log-get-directory identity)))
84
85 (defun riece-log-get-directory (identity)
86   (let ((channel (riece-identity-prefix identity))
87         (server (riece-identity-server identity))
88         (map (assoc (riece-format-identity identity) riece-log-directory-map))
89         name)
90     (cond (map (setq name (cdr map)))
91           ((string-match riece-channel-regexp channel)
92            (let ((suffix (match-string 2 channel)))
93              (setq name (substring channel (match-end 1) (match-beginning 2)))
94              (when (and (stringp suffix)
95                         (string-match "^:\\*\\.\\(.*\\)" suffix))
96                (setq name (concat name "-" (match-string 1 suffix))))))
97           (t (setq name "priv")))
98     (if server
99         (expand-file-name name (expand-file-name server riece-log-directory))
100       (expand-file-name name riece-log-directory))))
101
102 (defun riece-log-flashback (identity)
103   (when riece-log-flashback
104     (let ((file (riece-log-get-file identity)))
105       (when (file-exists-p file)
106         (let (string)
107           (with-temp-buffer
108             (insert-file-contents file)
109             (if (not (integerp riece-log-flashback))
110                 (goto-char (point-min))
111               (goto-char (point-max))
112               (forward-line (- riece-log-flashback)))
113             (setq string (buffer-substring (point) (point-max))))
114           (let (buffer-read-only)
115             (goto-char (point-max))
116             (insert string)
117             (goto-char (point-min))
118             (while (re-search-forward
119                     "^[0-9][0-9]:[0-9][0-9] [<>]\\([^<>]+\\)[<>] " nil t)
120               (put-text-property (match-beginning 1) (match-end 1)
121                                  'riece-identity
122                                  (riece-make-identity
123                                   (riece-match-string-no-properties 1)
124                                   (riece-identity-server identity))))
125             (if (memq 'riece-button riece-addons)
126                 (riece-button-update-buffer))
127             (goto-char (point-max))
128             (set-window-point (get-buffer-window (current-buffer))
129                               (point))))))))
130
131 (defun riece-log-open-directory (&optional channel)
132   (interactive)
133   (if channel
134       (find-file (riece-log-get-directory channel))
135     (find-file riece-log-directory)))
136
137 (defun riece-log-requires ()
138   (if (memq 'riece-button riece-addons)
139       '(riece-button)))
140
141 (defun riece-log-insinuate ()
142   ;; FIXME: Use `riece-after-insert-functions' for trapping change,
143   ;; notice, wallops and so on. But must add argument.
144   (add-hook 'riece-after-display-message-functions
145             'riece-log-display-message-function)
146   (add-hook 'riece-channel-buffer-create-functions
147             'riece-log-flashback))
148
149 (provide 'riece-log)
150
151 ;;; riece-log.el ends here