Flatten transparent part.
[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-lock-directory
49   (expand-file-name "=lock" riece-log-directory)
50   "*Lock directory for riece-log.
51 It is created if there is at least one instance of Emacs running riece-log."
52   :type 'directory
53   :group 'riece-log)
54
55 (defcustom riece-log-directory-map nil
56   "*The map of channel name and directory name."
57   :type '(repeat (cons (string :tag "Channel name")
58                        (string :tag "Directory name")))
59   :group 'riece-log)
60
61 (defcustom riece-log-flashback 10
62   "*If non-nil, irc messages flash back from log files.
63 If integer, flash back only this line numbers. t means all lines."
64   :type '(choice (integer :tag "line numbers")
65                  (const t :tag "of the day")
66                  (const nil :tag "no flashback"))
67   :group 'riece-log)
68
69 (defcustom riece-log-coding-system nil
70   "*Coding system used for log files."
71   :type 'symbol
72   :group 'riece-log)
73
74 (defcustom riece-log-file-name-coding-system
75   (if (boundp 'file-name-coding-system)
76       file-name-coding-system)
77   "*Coding system used for filenames of log files."
78   :type 'symbol
79   :group 'riece-log)
80
81 (defcustom riece-log-open-directory-function 'find-file
82   "*Function for opening a directory."
83   :type 'function
84   :group 'riece-log)
85
86 (defface riece-log-date-face
87   '((((class color)
88       (background dark))
89      (:foreground "Gray70"))
90     (((class color)
91       (background light))
92      (:foreground "DimGray"))
93     (t
94      (:bold t)))
95   "Face used for displaying \"(YYYY/MM/dd)\" extent."
96   :group 'riece-highlight-faces)
97 (defvar riece-log-date-face 'riece-log-date-face)
98
99 (defvar riece-log-enabled nil)
100
101 (defconst riece-log-description
102   "Saving IRC logs")
103
104 (defun riece-log-display-message-function (message)
105   (if riece-log-enabled
106       (let ((file (riece-log-get-file (riece-message-target message)))
107             (coding-system-for-write riece-log-coding-system)
108             file-name-coding-system
109             default-file-name-coding-system)
110         (unless (file-directory-p (file-name-directory file))
111           (make-directory (file-name-directory file) t))
112         (write-region (concat (format-time-string "%H:%M") " "
113                               (riece-format-message message))
114                       nil file t 0))))
115
116 (defun riece-log-get-file (identity)
117   (expand-file-name
118    (concat (format-time-string "%Y%m%d") ".txt")
119    (riece-log-get-directory identity)))
120
121 (defun riece-log-get-files (identity)
122   (let ((directory (riece-log-get-directory identity)))
123     (if (file-directory-p directory)
124         (nreverse (sort (directory-files directory t
125                          (concat "^"
126                                  (riece-make-interval-regexp "[0-9]" 8)
127                                  "\\.txt$")
128                          t)
129                   #'string-lessp)))))
130
131 (defun riece-log-get-directory (identity)
132   (let ((prefix (riece-identity-canonicalize-prefix
133                  (riece-identity-prefix identity)))
134         (server (riece-identity-server identity))
135         (map (assoc (riece-format-identity identity) riece-log-directory-map)))
136     (if map
137         (expand-file-name (cdr map) riece-log-directory)
138       (expand-file-name (riece-log-encode-file-name prefix)
139                         (expand-file-name
140                          (concat "." (riece-log-encode-file-name server))
141                          riece-log-directory)))))
142
143 (defun riece-log-encode-file-name (file-name)
144   (if riece-log-file-name-coding-system
145       (setq file-name
146             (encode-coding-string file-name
147                                   riece-log-file-name-coding-system)))
148   (let ((index 0)
149         c)
150     (while (string-match "[^-0-9A-Za-z_\x80-\xFF]" file-name index)
151       (setq c (aref file-name (match-beginning 0)))
152       (if (eq c ?=)
153           (setq file-name (replace-match "==" nil t file-name)
154                 index (1+ (match-end 0)))
155         (setq file-name (replace-match (format "=%02X" c) nil t file-name)
156               index (+ 2 (match-end 0)))))
157     file-name))
158
159 (defun riece-log-decode-file-name (file-name)
160   (let ((index 0))
161     (while (string-match "==\\|=\\([0-7][0-9A-F]\\)" file-name index)
162       (setq file-name (replace-match
163                        (if (eq (aref file-name (1- (match-end 0))) ?=)
164                            "="
165                          (char-to-string
166                           (car (read-from-string
167                                 (concat "?\\x" (match-string 1 file-name))))))
168                        nil t file-name)
169             index (1+ (match-beginning 0))))
170     file-name)
171   (if riece-log-file-name-coding-system
172       (setq file-name
173             (decode-coding-string file-name
174                                   riece-log-file-name-coding-system)))
175   file-name)
176
177 (defun riece-log-insert (identity lines)
178   "Insert logs for IDENTITY at most LINES.
179 If LINES is t, insert today's logs entirely."
180   (if (eq lines t)
181       (let* (file-name-coding-system
182              default-file-name-coding-system
183              (file (riece-log-get-file identity)))
184         (if (file-exists-p file)
185             (insert-file-contents file)))
186     (let* (file-name-coding-system
187            default-file-name-coding-system
188            (files (riece-log-get-files identity))
189            (lines (- lines))
190            name date point)
191       (while (and (< lines 0) files)
192         (if (and (file-exists-p (car files))
193                  (string-match (concat (riece-make-interval-regexp "[0-9]" 8)
194                                        "\\.txt$")
195                                (setq name (file-name-nondirectory
196                                            (car files)))))
197             (save-restriction
198               (narrow-to-region (point) (point))
199               (insert-file-contents (car files))
200               (goto-char (point-max))
201               (setq lines (forward-line lines))
202               (delete-region (point-min) (point))
203               (unless (equal name (format-time-string "%Y%m%d.txt"))
204                 (setq date (concat " (" (substring name 0 4) "/"
205                                    (substring name 4 6) "/"
206                                    (substring name 6 8) ")"))
207                 (while (not (eobp))
208                   (end-of-line)
209                   (setq point (point))
210                   (insert date)
211                   (put-text-property point (point)
212                                      'riece-overlay-face 'riece-log-date-face)
213                   (forward-line))
214                 (goto-char (point-min)))))
215         (setq files (cdr files))))))
216
217 (defun riece-log-flashback (identity)
218   (when riece-log-flashback
219     (riece-insert-info (current-buffer)
220                        (if (eq riece-log-flashback t)
221                            "Recent messages of the day:\n"
222                          (format "Recent messages up to %d lines:\n"
223                                  riece-log-flashback)))
224     (let (buffer-read-only
225           (point (goto-char (point-max))))
226       (insert (with-temp-buffer
227                 (riece-log-insert identity riece-log-flashback)
228                 (buffer-string)))
229       (goto-char point)
230       (while (re-search-forward
231               (concat "^" riece-time-prefix-regexp
232                        "\\(<[^>]+>\\|>[^<]+<\\|([^)]+)\\|{[^}]+}\\|=[^=]+=\\)")
233               nil t)
234         (put-text-property (1+ (match-beginning 1)) (1- (match-end 1))
235                            'riece-identity
236                            (riece-make-identity
237                             (buffer-substring (1+ (match-beginning 1))
238                                               (1- (match-end 1)))
239                             (riece-identity-server identity))))
240       (run-hook-with-args 'riece-after-insert-functions
241                           point (goto-char (point-max)))
242       (set-window-point (get-buffer-window (current-buffer))
243                         (point)))))
244
245 (defun riece-log-open-directory (&optional channel)
246   (interactive)
247   (let ((directory (riece-log-get-directory
248                     (or channel riece-current-channel))))
249     (if (file-directory-p directory)
250         (funcall riece-log-open-directory-function directory)
251       (error "No log directory"))))
252
253 (defun riece-log-requires ()
254   (if (memq 'riece-button riece-addons)
255       '(riece-button)))
256
257 (defun riece-log-insinuate ()
258   ;; FIXME: Use `riece-after-insert-functions' for trapping change,
259   ;; notice, wallops and so on. But must add argument.
260   (add-hook 'riece-after-display-message-functions
261             'riece-log-display-message-function)
262   (add-hook 'riece-channel-buffer-create-functions
263             'riece-log-flashback))
264
265 (defvar riece-command-mode-map)
266 (defun riece-log-enable ()
267   (define-key riece-command-mode-map "\C-cd" 'riece-log-open-directory)
268   (make-directory riece-log-directory t)
269   (condition-case nil
270       (progn
271         (make-directory riece-log-lock-directory)
272         (add-hook 'riece-exit-hook
273                   (lambda ()
274                     (condition-case nil
275                         (delete-directory riece-log-lock-directory)
276                       (error))))
277         (setq riece-log-enabled t))
278     (error
279      (if riece-debug
280          (message "Can't create lock directory for riece-log")))))
281
282 (defun riece-log-disable ()
283   (define-key riece-command-mode-map "\C-cd" nil)
284   (setq riece-log-enabled nil))
285
286 (provide 'riece-log)
287
288 ;;; riece-log.el ends here