* riece-log.el (riece-log-get-directory): Fix for server name and
[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
35 (defgroup riece-log nil
36   "Save irc log"
37   :group 'riece)
38
39 (defcustom riece-log-directory
40   (expand-file-name "log" riece-directory)
41   "*Where to look for log files."
42   :type 'directory
43   :group 'riece-log)
44
45 (defcustom riece-log-directory-map nil
46   "*The map of channel name and directory name."
47   :type '(repeat (cons (string :tag "Channel name")
48                        (string :tag "Directory name")))
49   :group 'riece-log)
50
51 (defcustom riece-log-flashback 10
52   "*If non-nil, irc messages flash back from log files.
53 If integer, flash back only this line numbers. t means all lines."
54   :type '(choice (integer :tag "line numbers")
55                  (boolean :tag "flash back or not"))
56   :group 'riece-log)
57
58 (defun riece-log-display-message-function (message)
59   (let ((open-bracket
60          (funcall riece-message-make-open-bracket-function message))
61         (close-bracket
62          (funcall riece-message-make-close-bracket-function message))
63         (name
64          (funcall riece-message-make-name-function message))
65         (file (riece-log-get-file (riece-message-target message))))
66     (unless (file-directory-p (file-name-directory file))
67       (make-directory (file-name-directory file) t))
68     (with-temp-buffer
69       (insert (concat (format-time-string "%H:%M") " "
70                       open-bracket name close-bracket
71                       " " (riece-message-text message) "\n"))
72       (write-region (point-min) (point-max) file t 0))))
73
74 (defun riece-log-get-file (identity)
75   (expand-file-name
76    (concat (format-time-string "%Y%m%d") ".log")
77    (riece-log-get-directory identity)))
78
79 (defun riece-log-get-directory (identity)
80   (let* ((channel (riece-identity-prefix identity))
81          (server (riece-identity-server identity))
82          (map (assoc channel riece-log-directory-map))
83          name)
84     (cond (map (setq name (cdr map)))
85           ((string-match riece-channel-regexp channel)
86            (let ((suffix (match-string 2 channel)))
87              (setq name (substring channel (match-end 1) (match-beginning 2)))
88              (when (and (stringp suffix)
89                         (string-match "^:\\*\\.\\(.*\\)" suffix))
90                (setq name (concat name "-" (match-string 1 suffix))))))
91           (t (setq name "priv")))
92     (if server
93         (expand-file-name name (expand-file-name server riece-log-directory))
94       (expand-file-name name riece-log-directory))))
95
96 (defun riece-log-flashback (identity)
97   (when riece-log-flashback
98     (let ((file (riece-log-get-file identity)))
99       (when (file-exists-p file)
100         (let (string)
101           (with-temp-buffer
102             (insert-file-contents file)
103             (if (not (integerp riece-log-flashback))
104                 (goto-char (point-min))
105               (goto-char (point-max))
106               (forward-line (- riece-log-flashback)))
107             (setq string (buffer-substring (point) (point-max))))
108           (let (buffer-read-only)
109             (goto-char (point-max))
110             (insert string)
111             (goto-char (point-max))
112             (set-window-point (get-buffer-window (current-buffer))
113                               (point))))))))
114
115 (defun riece-log-open-directory (&optional channel)
116   (interactive)
117   (if channel
118       (find-file (riece-log-get-directory channel))
119     (find-file riece-log-directory)))
120
121 (defun riece-log-insinuate ()
122   ;; FIXME: Use `riece-after-insert-functions' for trapping change,
123   ;; notice, wallops and so on. But must add argument.
124   (add-hook 'riece-after-display-message-functions
125             'riece-log-display-message-function)
126   (add-hook 'riece-channel-buffer-create-functions
127             'riece-log-flashback))
128
129 (provide 'riece-log)
130
131 ;;; riece-log.el ends here