Update credits.
[riece] / lisp / riece-log.el
1 ;;; riece-log.el --- saving irc logs add-on
2 ;; Copyright (C) 2003 OHASHI Akira
3 ;; Copyright (C) 2004 Daiki Ueno
4
5 ;; Author: OHASHI Akira <bg66@koka-in.org>
6 ;;      Daiki Ueno <ueno@unixuser.org>
7 ;; Keywords: IRC, riece
8
9 ;; This file is part of Riece.
10
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)
14 ;; any later version.
15
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.
20
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., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;; This add-on saves irc logs for every channel.
29
30 ;; To use, add the following line to your ~/.riece/init.el:
31 ;; (add-to-list 'riece-addons 'riece-log)
32
33 ;;; Code:
34
35 (require 'riece-message)
36 (require 'riece-button)
37
38 (defgroup riece-log nil
39   "Save irc log"
40   :group 'riece)
41
42 (defcustom riece-log-directory
43   (expand-file-name "log" riece-directory)
44   "*Where to look for log files."
45   :type 'directory
46   :group 'riece-log)
47
48 (defcustom riece-log-directory-map nil
49   "*The map of channel name and directory name."
50   :type '(repeat (cons (string :tag "Channel name")
51                        (string :tag "Directory name")))
52   :group 'riece-log)
53
54 (defcustom riece-log-flashback 10
55   "*If non-nil, irc messages flash back from log files.
56 If integer, flash back only this line numbers. t means all lines."
57   :type '(choice (integer :tag "line numbers")
58                  (boolean :tag "flash back or not"))
59   :group 'riece-log)
60
61 (defcustom riece-log-coding-system nil
62   "*Coding system used for log files."
63   :type 'symbol
64   :group 'riece-log)
65
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."
70   :type 'symbol
71   :group 'riece-log)
72
73 (defcustom riece-log-open-directory-function 'find-file
74   "*Function for opening a directory."
75   :type 'function
76   :group 'riece-log)
77
78 (defvar riece-log-enabled nil)
79
80 (defconst riece-log-description
81   "Saving IRC logs")
82
83 (defun riece-log-display-message-function (message)
84   (if riece-log-enabled
85       (let ((file (riece-log-get-file (riece-message-target message)))
86             (coding-system-for-write riece-log-coding-system))
87         (unless (file-directory-p (file-name-directory file))
88           (make-directory (file-name-directory file) t))
89         (write-region (concat (format-time-string "%H:%M") " "
90                               (riece-format-message message))
91                       nil file t 0))))
92
93 (defun riece-log-get-file (identity)
94   (expand-file-name
95    (concat (format-time-string "%Y%m%d") ".txt")
96    (riece-log-get-directory identity)))
97
98 (defun riece-log-get-files (identity)
99   (let ((directory (riece-log-get-directory identity)))
100     (if (file-directory-p directory)
101         (nreverse (sort (directory-files directory t
102                          (concat "^"
103                                  (riece-make-interval-regexp "[0-9]" 8)
104                                  "\\.txt$")
105                          t)
106                   #'string-lessp)))))
107
108 (defun riece-log-get-directory (identity)
109   (let ((prefix (riece-identity-canonicalize-prefix
110                  (riece-identity-prefix identity)))
111         (server (riece-identity-server identity))
112         (map (assoc (riece-format-identity identity) riece-log-directory-map))
113         name)
114     (if map
115         (setq name (cdr map))
116       (expand-file-name (riece-log-encode-file-name prefix)
117                         (expand-file-name
118                          (concat "." (riece-log-encode-file-name server))
119                          riece-log-directory)))))
120
121 (defun riece-log-encode-file-name (file-name)
122   (if riece-log-file-name-coding-system
123       (setq file-name
124             (encode-coding-string file-name
125                                   riece-log-file-name-coding-system)))
126   (let ((index 0)
127         c)
128     (while (string-match "[^-0-9A-Za-z_\x80-\xFF]" file-name index)
129       (setq c (aref file-name (match-beginning 0)))
130       (if (eq c ?=)
131           (setq file-name (replace-match "==" nil t file-name)
132                 index (1+ (match-end 0)))
133         (setq file-name (replace-match (format "=%02X" c) nil t file-name)
134               index (+ 2 (match-end 0)))))
135     file-name))
136
137 (defun riece-log-decode-file-name (file-name)
138   (let ((index 0))
139     (while (string-match "==\\|=\\([0-7][0-9A-F]\\)" file-name index)
140       (setq file-name (replace-match
141                        (if (eq (aref file-name (1- (match-end 0))) ?=)
142                            "="
143                          (char-to-string
144                           (car (read-from-string
145                                 (concat "?\\x" (match-string 1 file-name))))))
146                        nil t file-name)
147             index (1+ (match-beginning 0))))
148     file-name)
149   (if riece-log-file-name-coding-system
150       (setq file-name
151             (decode-coding-string file-name
152                                   riece-log-file-name-coding-system)))
153   file-name)
154
155 (defun riece-log-insert (identity lines)
156   "Insert logs for IDENTITY at most LINES.
157 If LINES is t, insert today's logs entirely."
158   (if (eq lines t)
159       (let ((file (riece-log-get-file identity)))
160         (if (file-exists-p file)
161             (insert-file-contents file)))
162     (let ((files (riece-log-get-files identity))
163           (lines (- lines))
164           date point)
165       (while (and (< lines 0) files)
166         (if (and (file-exists-p (car files))
167                  (string-match (concat "\\([0-9][0-9][0-9][0-9]\\)"
168                                        "\\([0-9][0-9]\\)\\([0-9][0-9]\\).txt$")
169                                (car files)))
170             (save-restriction
171               (narrow-to-region (point) (point))
172               (setq date (concat " (" (match-string 1 (car files)) "/"
173                                  (match-string 2 (car files)) "/"
174                                  (match-string 3 (car files)) ")"))
175               (insert-file-contents (car files))
176               (goto-char (point-max))
177               (setq lines (forward-line lines))
178               (delete-region (point-min) (point))
179               (while (not (eobp))
180                 (end-of-line)
181                 (insert date)
182                 (forward-line))
183               (goto-char (point-min))))
184         (setq files (cdr files))))))
185
186 (defun riece-log-flashback (identity)
187   (when riece-log-flashback
188     (let (buffer-read-only
189           (point (goto-char (point-max))))
190       (insert (with-temp-buffer
191                 (riece-log-insert identity riece-log-flashback)
192                 (buffer-string)))
193       (goto-char point)
194       (while (re-search-forward
195               "^[0-9][0-9]:[0-9][0-9] [<>]\\([^<>]+\\)[<>] " nil t)
196         (put-text-property (match-beginning 1) (match-end 1)
197                            'riece-identity
198                            (riece-make-identity
199                             (riece-match-string-no-properties 1)
200                             (riece-identity-server identity))))
201       (when (and (memq 'riece-button riece-addons)
202                  riece-button-enabled)
203         (riece-button-update-buffer))
204       (goto-char (point-max))
205       (set-window-point (get-buffer-window (current-buffer))
206                         (point)))))
207
208 (defun riece-log-open-directory (&optional channel)
209   (interactive)
210   (let ((directory (riece-log-get-directory
211                     (or channel riece-current-channel))))
212     (if (file-directory-p directory)
213         (funcall riece-log-open-directory-function directory)
214       (error "No log directory"))))
215
216 (defun riece-log-requires ()
217   (if (memq 'riece-button riece-addons)
218       '(riece-button)))
219
220 (defun riece-log-insinuate ()
221   ;; FIXME: Use `riece-after-insert-functions' for trapping change,
222   ;; notice, wallops and so on. But must add argument.
223   (add-hook 'riece-after-display-message-functions
224             'riece-log-display-message-function)
225   (add-hook 'riece-channel-buffer-create-functions
226             'riece-log-flashback))
227
228 (defvar riece-command-mode-map)
229 (defun riece-log-enable ()
230   (define-key riece-command-mode-map "\C-cd" 'riece-log-open-directory)
231   (setq riece-log-enabled t))
232
233 (defun riece-log-disable ()
234   (define-key riece-command-mode-map "\C-cd" nil)
235   (setq riece-log-enabled nil))
236
237 (provide 'riece-log)
238
239 ;;; riece-log.el ends here