Fix byte-compile error
[riece] / lisp / riece-log.el
1 ;;; riece-log.el --- Save IRC logs -*- lexical-binding: t -*-
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
104                     (if (and (riece-message-private-p message)
105                              (not (riece-message-own-p message)))
106                         (riece-message-speaker message)
107                       (riece-message-target message))
108                     coding-system-for-write))
109              (file-name-coding-system 'no-conversion))
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                       riece-log-lock-file))))
116
117 (defun riece-log-make-file-name (identity coding-system)
118   (expand-file-name (if (and (featurep 'mule) coding-system)
119                         (format "%s.txt.%s"
120                                 (format-time-string "%Y%m%d")
121                                 coding-system)
122                       (format "%s.txt"
123                                 (format-time-string "%Y%m%d")))
124                     (riece-log-directory identity)))
125
126 (defun riece-log-list-files (identity time)
127   (let ((directory (riece-log-directory identity))
128         (time-prefix (format-time-string "%Y%m%d" (or time '(0 0))))
129         files)
130     (when (file-directory-p directory)
131       (setq files (nreverse (sort (directory-files
132                                    directory t
133                                    (concat "^" riece-log-file-name-regexp)
134                                    t)
135                                   #'string-lessp)))
136       (while (and files
137                   (string-lessp (file-name-nondirectory (car files))
138                                 time-prefix))
139         (setq files (cdr files)))
140       files)))
141
142 (defun riece-log-directory (identity)
143   (let ((prefix (riece-identity-canonicalize-prefix
144                  (riece-identity-prefix identity)))
145         (server (riece-identity-server identity))
146         (map (assoc (riece-format-identity identity) riece-log-directory-map)))
147     (if map
148         (expand-file-name (cdr map) riece-log-directory)
149       (expand-file-name (riece-log-encode-file-name prefix)
150                         (expand-file-name
151                          (concat "." (riece-log-encode-file-name server))
152                          riece-log-directory)))))
153
154 (defun riece-log-encode-file-name (file-name)
155   (if riece-log-file-name-coding-system
156       (setq file-name
157             (encode-coding-string file-name
158                                   riece-log-file-name-coding-system)))
159   (let ((index 0)
160         c)
161     (while (string-match "[^-0-9A-Za-z_\x80-\xFF]" file-name index)
162       (setq c (aref file-name (match-beginning 0)))
163       (if (eq c ?=)
164           (setq file-name (replace-match "==" nil t file-name)
165                 index (1+ (match-end 0)))
166         (setq file-name (replace-match (format "=%02X" c) nil t file-name)
167               index (+ 2 (match-end 0)))))
168     file-name))
169
170 (defun riece-log-decode-file-name (file-name)
171   (let ((index 0))
172     (while (string-match "==\\|=\\([0-7][0-9A-F]\\)" file-name index)
173       (setq file-name (replace-match
174                        (if (eq (aref file-name (1- (match-end 0))) ?=)
175                            "="
176                          (char-to-string
177                           (car (read-from-string
178                                 (concat "?\\x" (match-string 1 file-name))))))
179                        nil t file-name)
180             index (1+ (match-beginning 0))))
181     file-name)
182   (if riece-log-file-name-coding-system
183       (setq file-name
184             (decode-coding-string file-name
185                                   riece-log-file-name-coding-system)))
186   file-name)
187
188 (defun riece-log-insert (identity lines)
189   "Insert logs for IDENTITY at most LINES.
190 If LINES is t, insert today's logs entirely."
191   (let* ((file-name-coding-system 'no-conversion)
192          (files (riece-log-list-files identity
193                                       (if (eq lines t) (current-time))))
194          name coding-system date point)
195     (while (and (or (eq lines t) (> lines 0)) files)
196       (save-restriction
197         (narrow-to-region (point) (point))
198         (if (and (string-match
199                   (concat "^" riece-log-file-name-regexp)
200                   (setq name (file-name-nondirectory (car files))))
201                  (match-beginning 2))
202             (progn
203               (setq coding-system
204                     (intern (substring name (match-beginning 2))))
205               (if (featurep 'xemacs)
206                   (setq coding-system (find-coding-system coding-system))
207                 (unless (coding-system-p coding-system)
208                   (setq coding-system nil)))
209               (if coding-system
210                   (let ((coding-system-for-read coding-system))
211                     (insert-file-contents (car files)))
212                 ;;don't insert file contents if they use non
213                 ;;supported coding-system.
214                 ))
215           ;;if the filename has no coding-system suffix, decode with
216           ;;riece-log-coding-system.
217           (let ((coding-system-for-read riece-log-coding-system))
218             (insert-file-contents (car files))))
219         ;;lines in the file contents are in reversed order.
220         (unless (eq lines t)
221           (goto-char (point-max))
222           (setq lines (- (forward-line (- lines))))
223           (delete-region (point-min) (point)))
224         ;;add (YYYY/MM/dd) suffix on each line left in the current buffer.
225         (unless (equal (substring name 0 8) (format-time-string "%Y%m%d"))
226           (setq date (concat " (" (substring name 0 4) "/"
227                              (substring name 4 6) "/"
228                              (substring name 6 8) ")"))
229           (while (not (eobp))
230             (end-of-line)
231             (setq point (point))
232             (insert date)
233             (put-text-property point (point)
234                                'riece-overlay-face 'riece-log-date-face)
235             (forward-line))
236           (goto-char (point-min))))
237       (setq files (cdr files)))))
238
239 (defun riece-log-flashback (identity)
240   (when riece-log-flashback
241     (riece-insert-info (current-buffer)
242                        (if (eq riece-log-flashback t)
243                            (riece-mcat "Recent messages of the day:\n")
244                          (format (riece-mcat
245                                   "Recent messages up to %d lines:\n")
246                                  riece-log-flashback)))
247     (let (buffer-read-only
248           (point (goto-char (point-max))))
249       (insert (with-temp-buffer
250                 (riece-log-insert identity riece-log-flashback)
251                 (buffer-string)))
252       (goto-char point)
253       (while (re-search-forward
254               (concat "^" riece-time-prefix-regexp
255                        "\\(<[^>]+>\\|>[^<]+<\\|([^)]+)\\|{[^}]+}\\|=[^=]+=\\)")
256               nil t)
257         (put-text-property (1+ (match-beginning 1)) (1- (match-end 1))
258                            'riece-identity
259                            (riece-make-identity
260                             (buffer-substring (1+ (match-beginning 1))
261                                               (1- (match-end 1)))
262                             (riece-identity-server identity))))
263       (run-hook-with-args 'riece-after-insert-functions
264                           point (goto-char (point-max)))
265       (set-window-point (get-buffer-window (current-buffer))
266                         (point)))))
267
268 (defun riece-log-dired (&optional channel)
269   (interactive)
270   (let ((directory (riece-log-directory (or channel riece-current-channel))))
271     (if (file-directory-p directory)
272         (dired directory)
273       (error "No log directory"))))
274
275 (defun riece-log-requires ()
276   (if (memq 'riece-button riece-addons)
277       '(riece-button)))
278
279 (defun riece-log-insinuate ()
280   (make-directory riece-log-directory t)
281   (setq riece-log-lock-file
282         (expand-file-name (format "!%s-%d-%d"
283                                   (riece-log-encode-file-name (system-name))
284                                   (user-uid)
285                                   (emacs-pid))
286                           riece-log-directory))
287   ;; FIXME: Use `riece-after-insert-functions' for trapping change,
288   ;; notice, wallops and so on. But must add argument.
289   (add-hook 'riece-after-display-message-functions
290             'riece-log-display-message-function)
291   (add-hook 'riece-channel-buffer-create-functions
292             'riece-log-flashback))
293
294 (defun riece-log-uninstall ()
295   (setq riece-log-lock-file nil)
296   (remove-hook 'riece-after-display-message-functions
297                'riece-log-display-message-function)
298   (remove-hook 'riece-channel-buffer-create-functions
299                'riece-log-flashback))
300
301 (defvar riece-command-mode-map)
302 (defun riece-log-enable ()
303   (define-key riece-command-mode-map "\C-cd" 'riece-log-dired))
304
305 (defun riece-log-disable ()
306   (define-key riece-command-mode-map "\C-cd" nil))
307
308 (provide 'riece-log)
309
310 ;;; riece-log.el ends here