viper -- Update and prettify package-info.in provides.
[packages] / xemacs-packages / erc / erc-log.el
1 ;;; erc-log.el --- Logging facilities for ERC.
2
3 ;; Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
4
5 ;; Author: Lawrence Mitchell <wence@gmx.li>
6 ;; Keywords: IRC, chat, client, Internet, logging
7
8 ;; Created 2003-04-26
9 ;; Logging code taken from erc.el and modified to use markers.
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
27
28 ;;; Commentary:
29
30 ;; This file implements log file writing support for ERC.
31
32 ;; Quick start:
33 ;;
34 ;; (setq erc-enable-logging t)
35 ;; (setq erc-log-channels-directory "/path/to/logfiles") ; must be writable
36 ;;
37 ;; There are two ways to setup logging. The first will write to the log files
38 ;; on each incoming or outgoing line - this may not be optimal on a laptop
39 ;; HDD. To do this, M-x customize-variable erc-modules, and add "log".
40 ;;
41 ;; The second method will save buffers on /part, /quit, or killing the
42 ;; channel buffer. To do this, add the following to your .emacs:
43 ;;
44 ;; (require 'erc-log)
45 ;;
46 ;; You may optionally want the following code, to save all ERC buffers
47 ;; without confirmation when exiting emacs:
48 ;;
49 ;; (defadvice save-buffers-kill-emacs (before save-logs (&rest args) activate)
50 ;;  (save-some-buffers t (lambda ()
51 ;;                         (when (and (eq major-mode 'erc-mode)
52 ;;                                    (not (null buffer-file-name))) t))))
53 ;;
54 ;; If you only want to save logs for some buffers, customise the
55 ;; variable `erc-enable-logging'.
56
57 ;; How it works:
58 ;;
59 ;; If logging is enabled, at some point, `erc-save-buffer-in-logs'
60 ;; will be called.  The "end" of the buffer is taken from
61 ;; `erc-insert-marker', while `erc-last-saved-position' holds the
62 ;; position the buffer was last saved at (as a marker, or if the
63 ;; buffer hasn't been saved before, as the number 1 (point-min)).
64
65 ;; The region between `erc-last-saved-position' and
66 ;; `erc-insert-marker' is saved to the current buffer's logfile, and
67 ;; `erc-last-saved-position' is updated to reflect this.
68
69 ;;; History:
70 ;; 2003-04-26: logging code pulled out of erc.el.  Switched to using
71 ;; markers.
72
73 ;;; TODO:
74 ;; * Erc needs a generalised make-safe-file-name function, so that
75 ;;   generated file names don't contain any invalid file characters.
76 ;;
77 ;; * Really, we need to lock the logfiles somehow, so that if a user
78 ;;   is running multiple emacsen and/or on the same channel as more
79 ;;   than one user, only one process writes to the logfile.  This is
80 ;;   especially needed for those logfiles with no nick in them, as
81 ;;   these would become corrupted.
82 ;;   For a single emacs process, the problem could be solved using a
83 ;;   variable which contained the names of buffers already being
84 ;;   logged.  This would require that logging be buffer-local,
85 ;;   possibly not a bad thing anyway, since many people don't want to
86 ;;   log the server buffer.
87 ;;   For multiple emacsen the problem is trickier.  On some systems,
88 ;;   on could use the function `lock-buffer' and `unlock-buffer'.
89 ;;   However, file locking isn't implemented on all platforms, for
90 ;;   example, there is none on w32 systems.
91 ;;   A third possibility might be to fake lockfiles.  However, this
92 ;;   might lead to problems if an emacs crashes, as the lockfile
93 ;;   would be left lying around.
94
95 ;;; Code:
96
97 (require 'erc)
98 (eval-when-compile (require 'cl))
99
100 (defconst erc-log-version "$Revision: 1.27.2.1 $"
101   "ERC log mode revision")
102
103 (defgroup erc-log nil
104   "Logging facilities for ERC."
105   :group 'erc)
106
107 (defcustom erc-generate-log-file-name-function 'erc-generate-log-file-name-long
108   "*A function to generate a log filename.
109 The function must take five arguments: BUFFER, TARGET, NICK, SERVER and PORT.
110 BUFFER is the buffer to be saved,
111 TARGET is the name of the channel, or the target of the query,
112 NICK is the current nick,
113 SERVER and PORT are the parameters used to connect BUFFERs
114 `erc-server-process'."
115   :group 'erc-log
116   :type '(choice (const erc-generate-log-file-name-long)
117                  (const erc-generate-log-file-name-short)
118                  (const erc-generate-log-file-name-with-date)
119                  (symbol)))
120
121 (defcustom erc-save-buffer-on-part nil
122   "*Save the channel buffer content using `erc-save-buffer-in-logs' on PART."
123   :group 'erc-log
124   :type 'boolean)
125
126 (defcustom erc-truncate-buffer-on-save nil
127   "Truncate any ERC (channel, query, server) buffer when it is saved."
128   :group 'erc-log
129   :type 'boolean)
130
131 (defcustom erc-enable-logging t
132   "If non-nil, ERC will log IRC conversations.
133 This can either be a boolean value of nil or t, or a function.
134 If the value is a function, it will be called with one argument, the
135 name of the current ERC buffer.  One possible function, which saves
136 all but server buffers is `erc-log-all-but-server-buffers'.
137
138 This variable is buffer local.  Setting it via \\[customize] sets the
139 default value.
140
141 Log files are stored in `erc-log-channels-directory'."
142   :group 'erc-log
143   :type '(choice boolean
144                  function))
145 (make-variable-buffer-local 'erc-enable-logging)
146
147 (defcustom erc-log-channels-directory "~/log"
148   "The directory to place log files for channels.
149 Leave blank to disable logging.  If not nil, all the channel
150 buffers are logged in separate files in that directory.  The
151 directory should not end with a trailing slash."
152   :group 'erc-log
153   :type '(choice directory
154                  (const nil)))
155
156 (defcustom erc-log-insert-log-on-open t
157   "*Insert log file contents into the buffer if a log file exists."
158   :group 'erc-log
159   :type 'boolean)
160
161 (defcustom erc-save-queries-on-quit nil
162   "Save all query (also channel) buffers of the server on QUIT.
163 See the variable `erc-save-buffer-on-part' for details."
164   :group 'erc-log
165   :type 'boolean)
166
167 (defcustom erc-log-file-coding-system (if (featurep 'xemacs)
168                                           'binary
169                                         'emacs-mule)
170   "*The coding system ERC should use for writing log files.
171
172 This should ideally, be a \"catch-all\" coding system, like
173 `emacs-mule', or `iso-2022-7bit'."
174   :group 'erc-log)
175
176 ;;;###autoload (autoload 'erc-log-mode "erc-log" nil t)
177 (define-erc-module log nil
178   "Automatically logs things you receive on IRC into files.
179 Files are stored in `erc-log-channels-directory'; file name
180 format is defined through a formatting function on
181 `erc-generate-log-file-name-function'.
182
183 Since automatic logging is not always a Good Thing (especially if
184 people say things in different coding systems), you can turn logging
185 behaviour on and off with the variable `erc-enable-logging', which can
186 also be a predicate function. To only log when you are not set away, use:
187
188 \(setq erc-enable-logging
189       (lambda (buffer)
190         (with-current-buffer buffer
191           (not erc-away))))"
192   ;; enable
193   ((add-hook 'erc-insert-post-hook
194              'erc-save-buffer-in-logs)
195    (add-hook 'erc-send-post-hook
196              'erc-save-buffer-in-logs))
197   ;; disable
198   ((remove-hook 'erc-insert-post-hook
199                 'erc-save-buffer-in-logs)
200    (remove-hook 'erc-send-post-hook
201                 'erc-save-buffer-in-logs)))
202
203 (when erc-enable-logging
204   (add-hook 'erc-kill-buffer-hook
205             'erc-save-buffer-in-logs)
206   (add-hook 'erc-kill-channel-hook
207             'erc-save-buffer-in-logs)
208   (add-hook 'erc-quit-hook
209             'erc-conditional-save-queries)
210   (add-hook 'erc-part-hook
211             'erc-conditional-save-buffer))
212
213 (define-key erc-mode-map "\C-c\C-l" 'erc-save-buffer-in-logs)
214
215 ;;;functionality referenced from erc.el
216 (defun erc-log-setup-logging ()
217   "Setup the buffer-local logging variables in the current buffer.
218 This function is destined to be run from `erc-connect-pre-hook'."
219   (when (erc-logging-enabled)
220     (auto-save-mode -1)
221     (setq buffer-offer-save t
222           buffer-file-name nil
223           buffer-file-truename nil)
224     (cond ((boundp 'write-file-functions)
225            (set (make-local-variable 'write-file-functions)
226                 '(erc-save-buffer-in-logs)))
227           ((boundp 'local-write-file-hooks)
228            (setq local-write-file-hooks '(erc-save-buffer-in-logs)))
229           (t
230            (set (make-local-variable 'write-file-hooks)
231                 '(erc-save-buffer-in-logs))))
232     (when erc-log-insert-log-on-open
233       (let* ((log-file (erc-current-logfile))
234              (log-file-size (nth 7 (file-attributes log-file)))
235              (start (if (and log-file-size
236                              (memq 'erc-truncate-buffer
237                                    erc-insert-post-hook))
238                         (max (- log-file-size erc-max-buffer-size) 0)
239                       0)))
240         (ignore-errors (insert-file-contents log-file nil start
241                                              log-file-size))
242         (move-marker erc-last-saved-position
243                       (1- (point-max)))))))
244
245 ;;; Append, so that 'erc-initialize-log-marker keeps running first.
246 (add-hook 'erc-connect-pre-hook 'erc-log-setup-logging 'append)
247
248 (defun erc-log-all-but-server-buffers (buffer)
249   "Returns t if logging should be enabled in BUFFER.
250 Returns nil iff `erc-server-buffer-p' returns t."
251   (save-excursion
252     (save-window-excursion
253       (set-buffer buffer)
254       (not (erc-server-buffer-p)))))
255
256 (defun erc-save-query-buffers (process)
257   "Save all buffers process."
258   (erc-with-all-buffers-of-server process
259                                   nil
260                                   (erc-save-buffer-in-logs)))
261
262 (defun erc-conditional-save-buffer (buffer)
263   "Save Query BUFFER if `erc-save-queries-on-quit' is t."
264   (when erc-save-buffer-on-part
265     (erc-save-buffer-in-logs buffer)))
266
267 (defun erc-conditional-save-queries (process)
268   "Save Query buffers of PROCESS if `erc-save-queries-on-quit' is t."
269   (when erc-save-queries-on-quit
270     (erc-save-query-buffers process)))
271
272 ;;;###autoload
273 (defun erc-logging-enabled (&optional buffer)
274   "Return non-nil if logging is enabled for BUFFER.
275 If BUFFER is nil, the value of `current-buffer' is used.
276 Logging is enabled if `erc-log-channels-directory' is non-nil, the directory
277 is writeable (it will be created as necessary) and
278 `erc-enable-logging' returns a non-nil value."
279   (and erc-log-channels-directory
280        (erc-directory-writable-p erc-log-channels-directory)
281        (if (functionp erc-enable-logging)
282            (funcall erc-enable-logging (or buffer (current-buffer)))
283          erc-enable-logging)))
284
285 (defun erc-current-logfile (&optional buffer)
286   "Return the logfile to use for BUFFER.
287 If BUFFER is nil, the value of `current-buffer' is used.
288 This is determined by `erc-generate-log-file-name-function'.
289 The result is converted to lowercase, as IRC is case-insensitive"
290   (expand-file-name
291    (downcase (funcall erc-generate-log-file-name-function
292                       (or buffer (current-buffer))
293                       (or (erc-default-target) (buffer-name buffer))
294                       (erc-current-nick)
295                       erc-session-server erc-session-port))
296    erc-log-channels-directory))
297
298 (defun erc-generate-log-file-name-with-date (buffer &rest ignore)
299   "This function computes a short log file name.
300 The name of the log file is composed of BUFFER and the current date.
301 This function is a possible value for `erc-generate-log-file-name-function'."
302   (concat (buffer-name buffer) "-" (format-time-string "%Y-%m-%d") ".txt"))
303
304 (defun erc-generate-log-file-name-short (buffer &rest ignore)
305   "This function computes a short log file name.
306 In fact, it only uses the buffer name of the BUFFER argument, so
307 you can affect that using `rename-buffer' and the-like.  This
308 function is a possible value for
309 `erc-generate-log-file-name-function'."
310   (concat (buffer-name buffer) ".txt"))
311
312 (defun erc-generate-log-file-name-long (buffer target nick server port)
313   "Generates a log-file name in the way ERC always did it.
314 This results in a file name of the form #channel!nick@server:port.txt.
315 This function is a possible value for `erc-generate-log-file-name-function'."
316   (let ((file (concat
317                (if target (concat target "!"))
318                nick "@" server ":" (cond ((stringp port) port)
319                                          ((numberp port)
320                                           (number-to-string port))) ".txt")))
321     ;; we need a make-safe-file-name function.
322     (convert-standard-filename file)))
323
324 ;;;###autoload
325 (defun erc-save-buffer-in-logs (&optional buffer)
326   "Append BUFFER contents to the log file, if logging is enabled.
327 If BUFFER is not provided, current buffer is used.
328 Logging is enabled if `erc-logging-enabled' returns non-nil.
329
330 This is normally done on exit, to save the unsaved portion of the
331 buffer, since only the text that runs off the buffer limit is logged
332 automatically.
333
334 You can save every individual message by putting this function on
335 `erc-insert-post-hook'."
336   (interactive)
337   (or buffer (setq buffer (current-buffer)))
338   (when (erc-logging-enabled buffer)
339     (let ((file (erc-current-logfile buffer))
340           (coding-system-for-write erc-log-file-coding-system))
341       (save-excursion
342         (with-current-buffer buffer
343           (save-restriction
344             (widen)
345             ;; early on in the initalisation, don't try and write the log out
346             (when (and (markerp erc-last-saved-position)
347                        (> erc-insert-marker (1+ erc-last-saved-position)))
348               (write-region (1+ (marker-position erc-last-saved-position))
349                             (marker-position erc-insert-marker)
350                             file t 'nomessage)
351               (if (and erc-truncate-buffer-on-save (interactive-p))
352                   (progn
353                     (let ((inhibit-read-only t)) (erase-buffer))
354                     (move-marker erc-last-saved-position (point-max))
355                     (erc-display-prompt))
356                 (move-marker erc-last-saved-position
357                              ;; If we place erc-last-saved-position at
358                              ;; erc-insert-marker, because text gets
359                              ;; inserted /before/ erc-insert-marker,
360                              ;; the log file will not be saved
361                              ;; (erc-last-saved-position will always
362                              ;; be equal to erc-insert-marker).
363                              (1- (marker-position erc-insert-marker)))))
364             (set-buffer-modified-p nil))))))
365   t)
366
367 (provide 'erc-log)
368
369 ;;; erc-log.el ends here
370 ;;
371 ;; Local Variables:
372 ;; indent-tabs-mode: t
373 ;; tab-width: 8
374 ;; End:
375
376 ;; arch-tag: 54072f99-9f0a-4846-8908-2ccde92221de