* riece-kakasi.el (riece-kakasi-convert-string): Suppress
[riece] / lisp / riece-log.el
1 ;;; riece-log.el --- Save IRC logs
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., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;;; NOTE: This is an add-on module for Riece.
29
30 ;;; Code:
31
32 (require 'riece-message)
33 (require 'riece-button)
34 (require 'riece-mcat)
35
36 (defgroup riece-log nil
37   "Save IRC logs."
38   :prefix "riece-"
39   :group 'riece)
40
41 (defcustom riece-log-directory
42   (expand-file-name "log" riece-directory)
43   "*Where to look for log files."
44   :type 'directory
45   :group 'riece-log)
46
47 (defcustom riece-log-directory-map nil
48   "*The map of channel name and directory name."
49   :type '(repeat (cons (string :tag "Channel name")
50                        (string :tag "Directory name")))
51   :group 'riece-log)
52
53 (defcustom riece-log-flashback 10
54   "*If non-nil, irc messages flash back from log files.
55 If integer, flash back only this line numbers. t means all lines."
56   :type '(choice (integer :tag "line numbers")
57                  (const t :tag "of the day")
58                  (const nil :tag "no flashback"))
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 (defface riece-log-date-face
74   '((((class color)
75       (background dark))
76      (:foreground "Gray70"))
77     (((class color)
78       (background light))
79      (:foreground "DimGray"))
80     (t
81      (:bold t)))
82   "Face used for displaying \"(YYYY/MM/dd)\" extent."
83   :group 'riece-highlight-faces)
84 (defvar riece-log-date-face 'riece-log-date-face)
85
86 (defvar riece-log-lock-file nil
87   "Lock file for riece-log.
88 It is created if there is at least one instance of Emacs running riece-log.")
89
90 (defconst riece-log-file-name-regexp
91   (concat (riece-make-interval-regexp "[0-9]" 8) "\\.txt\\(\\.\\(.*\\)\\)?$"))
92
93 (defconst riece-log-description
94   "Save IRC logs.")
95
96 (defun riece-log-display-message-function (message)
97   (if (get 'riece-log 'riece-addon-enabled)
98       (let* ((coding-system-for-write
99               (if (featurep 'mule)
100                   (or riece-log-coding-system
101                       (car (get-language-info current-language-environment
102                                               'coding-system)))))
103              (file (riece-log-make-file-name (riece-message-target message)
104                                              coding-system-for-write))
105              file-name-coding-system
106              default-file-name-coding-system)
107         (unless (file-directory-p (file-name-directory file))
108           (make-directory (file-name-directory file) t))
109         (write-region (concat (format-time-string "%H:%M") " "
110                               (riece-format-message message))
111                       nil file t 0
112                       riece-log-lock-file))))
113
114 (defun riece-log-make-file-name (identity coding-system)
115   (expand-file-name (if (featurep 'mule)
116                         (format "%s.txt.%s"
117                                 (format-time-string "%Y%m%d")
118                                 coding-system)
119                       (format "%s.txt"
120                                 (format-time-string "%Y%m%d")))
121                     (riece-log-directory identity)))
122
123 (defun riece-log-list-files (identity time)
124   (let ((directory (riece-log-directory identity))
125         (time-prefix (format-time-string "%Y%m%d" (or time '(0 0))))
126         files)
127     (when (file-directory-p directory)
128       (setq files (nreverse (sort (directory-files
129                                    directory t
130                                    (concat "^" riece-log-file-name-regexp)
131                                    t)
132                                   #'string-lessp)))
133       (while (and files
134                   (string-lessp (file-name-nondirectory (car files))
135                                 time-prefix))
136         (setq files (cdr files)))
137       files)))
138
139 (defun riece-log-directory (identity)
140   (let ((prefix (riece-identity-canonicalize-prefix
141                  (riece-identity-prefix identity)))
142         (server (riece-identity-server identity))
143         (map (assoc (riece-format-identity identity) riece-log-directory-map)))
144     (if map
145         (expand-file-name (cdr map) riece-log-directory)
146       (expand-file-name (riece-log-encode-file-name prefix)
147                         (expand-file-name
148                          (concat "." (riece-log-encode-file-name server))
149                          riece-log-directory)))))
150
151 (defun riece-log-encode-file-name (file-name)
152   (if riece-log-file-name-coding-system
153       (setq file-name
154             (encode-coding-string file-name
155                                   riece-log-file-name-coding-system)))
156   (let ((index 0)
157         c)
158     (while (string-match "[^-0-9A-Za-z_\x80-\xFF]" file-name index)
159       (setq c (aref file-name (match-beginning 0)))
160       (if (eq c ?=)
161           (setq file-name (replace-match "==" nil t file-name)
162                 index (1+ (match-end 0)))
163         (setq file-name (replace-match (format "=%02X" c) nil t file-name)
164               index (+ 2 (match-end 0)))))
165     file-name))
166
167 (defun riece-log-decode-file-name (file-name)
168   (let ((index 0))
169     (while (string-match "==\\|=\\([0-7][0-9A-F]\\)" file-name index)
170       (setq file-name (replace-match
171                        (if (eq (aref file-name (1- (match-end 0))) ?=)
172                            "="
173                          (char-to-string
174                           (car (read-from-string
175                                 (concat "?\\x" (match-string 1 file-name))))))
176                        nil t file-name)
177             index (1+ (match-beginning 0))))
178     file-name)
179   (if riece-log-file-name-coding-system
180       (setq file-name
181             (decode-coding-string file-name
182                                   riece-log-file-name-coding-system)))
183   file-name)
184
185 (defun riece-log-insert (identity lines)
186   "Insert logs for IDENTITY at most LINES.
187 If LINES is t, insert today's logs entirely."
188   (let* (file-name-coding-system
189          default-file-name-coding-system
190          (files (riece-log-list-files identity
191                                       (if (eq lines t) (current-time))))
192          name coding-system date point)
193     (while (and (or (eq lines t) (> lines 0)) files)
194       (save-restriction
195         (narrow-to-region (point) (point))
196         (if (and (string-match
197                   (concat "^" riece-log-file-name-regexp)
198                   (setq name (file-name-nondirectory (car files))))
199                  (match-beginning 2))
200             (progn
201               (setq coding-system
202                     (intern (substring name (match-beginning 2))))
203               (if (featurep 'xemacs)
204                   (setq coding-system (find-coding-system coding-system))
205                 (unless (coding-system-p coding-system)
206                   (setq coding-system nil)))
207               (if coding-system
208                   (let ((coding-system-for-read coding-system))
209                     (insert-file-contents (car files)))
210                 ;;don't insert file contents if they use non
211                 ;;supported coding-system.
212                 ))
213           ;;if the filename has no coding-system suffix, decode with
214           ;;riece-log-coding-system.
215           (let ((coding-system-for-read riece-log-coding-system))
216             (insert-file-contents (car files))))
217         ;;lines in the file contents are in reversed order.
218         (unless (eq lines t)
219           (goto-char (point-max))
220           (setq lines (- (forward-line (- lines))))
221           (delete-region (point-min) (point)))
222         ;;add (YYYY/MM/dd) suffix on each line left in the current buffer.
223         (unless (equal (substring name 0 8) (format-time-string "%Y%m%d"))
224           (setq date (concat " (" (substring name 0 4) "/"
225                              (substring name 4 6) "/"
226                              (substring name 6 8) ")"))
227           (while (not (eobp))
228             (end-of-line)
229             (setq point (point))
230             (insert date)
231             (put-text-property point (point)
232                                'riece-overlay-face 'riece-log-date-face)
233             (forward-line))
234           (goto-char (point-min))))
235       (setq files (cdr files)))))
236
237 (defun riece-log-flashback (identity)
238   (when riece-log-flashback
239     (riece-insert-info (current-buffer)
240                        (if (eq riece-log-flashback t)
241                            (riece-mcat "Recent messages of the day:\n")
242                          (format (riece-mcat
243                                   "Recent messages up to %d lines:\n")
244                                  riece-log-flashback)))
245     (let (buffer-read-only
246           (point (goto-char (point-max))))
247       (insert (with-temp-buffer
248                 (riece-log-insert identity riece-log-flashback)
249                 (buffer-string)))
250       (goto-char point)
251       (while (re-search-forward
252               (concat "^" riece-time-prefix-regexp
253                        "\\(<[^>]+>\\|>[^<]+<\\|([^)]+)\\|{[^}]+}\\|=[^=]+=\\)")
254               nil t)
255         (put-text-property (1+ (match-beginning 1)) (1- (match-end 1))
256                            'riece-identity
257                            (riece-make-identity
258                             (buffer-substring (1+ (match-beginning 1))
259                                               (1- (match-end 1)))
260                             (riece-identity-server identity))))
261       (run-hook-with-args 'riece-after-insert-functions
262                           point (goto-char (point-max)))
263       (set-window-point (get-buffer-window (current-buffer))
264                         (point)))))
265
266 (defun riece-log-dired (&optional channel)
267   (interactive)
268   (let ((directory (riece-log-directory (or channel riece-current-channel))))
269     (if (file-directory-p directory)
270         (dired directory)
271       (error "No log directory"))))
272
273 (defun riece-log-requires ()
274   (if (memq 'riece-button riece-addons)
275       '(riece-button)))
276
277 (defun riece-log-insinuate ()
278   (make-directory riece-log-directory t)
279   (setq riece-log-lock-file
280         (expand-file-name (format "!%s-%d-%d"
281                                   (riece-log-encode-file-name (system-name))
282                                   (user-uid)
283                                   (emacs-pid))
284                           riece-log-directory))
285   ;; FIXME: Use `riece-after-insert-functions' for trapping change,
286   ;; notice, wallops and so on. But must add argument.
287   (add-hook 'riece-after-display-message-functions
288             'riece-log-display-message-function)
289   (add-hook 'riece-channel-buffer-create-functions
290             'riece-log-flashback))
291
292 (defun riece-log-uninstall ()
293   (setq riece-log-lock-file nil)
294   (remove-hook 'riece-after-display-message-functions
295                'riece-log-display-message-function)
296   (remove-hook 'riece-channel-buffer-create-functions
297                'riece-log-flashback))
298
299 (defvar riece-command-mode-map)
300 (defun riece-log-enable ()
301   (define-key riece-command-mode-map "\C-cd" 'riece-log-dired))
302
303 (defun riece-log-disable ()
304   (define-key riece-command-mode-map "\C-cd" nil))
305
306 (provide 'riece-log)
307
308 ;;; riece-log.el ends here