Sync ERC 5.3 (devel) from upstream
[bpt/emacs.git] / lisp / erc / erc-log.el
CommitLineData
597993cf
MB
1;;; erc-log.el --- Logging facilities for ERC.
2
f0fa15c5 3;; Copyright (C) 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
597993cf
MB
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
e0085d62 15;; the Free Software Foundation; either version 3, or (at your option)
597993cf
MB
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;;
526dc846 34;; (require 'erc-log)
597993cf 35;; (setq erc-log-channels-directory "/path/to/logfiles") ; must be writable
526dc846 36;; (erc-log-enable)
597993cf 37;;
526dc846 38;; Or:
597993cf 39;;
526dc846 40;; M-x customize-variable erc-modules, and add "log".
597993cf 41;;
526dc846
MO
42;; There are two ways to setup logging. The first (default) method
43;; will save buffers on /part, /quit, or killing the channel
44;; buffer.
45;;
46;; The second will write to the log files on each incoming or outgoing
47;; line - this may not be optimal on a laptop HDD. To use this
48;; method, add the following to the above instructions.
49;;
50;; (setq erc-save-buffer-on-part nil
51;; erc-save-queries-on-quit nil
52;; erc-log-write-after-send t
53;; erc-log-write-after-insert t)
597993cf 54;;
597993cf
MB
55;; If you only want to save logs for some buffers, customise the
56;; variable `erc-enable-logging'.
57
58;; How it works:
59;;
60;; If logging is enabled, at some point, `erc-save-buffer-in-logs'
61;; will be called. The "end" of the buffer is taken from
62;; `erc-insert-marker', while `erc-last-saved-position' holds the
63;; position the buffer was last saved at (as a marker, or if the
64;; buffer hasn't been saved before, as the number 1 (point-min)).
65
66;; The region between `erc-last-saved-position' and
67;; `erc-insert-marker' is saved to the current buffer's logfile, and
68;; `erc-last-saved-position' is updated to reflect this.
69
70;;; History:
71;; 2003-04-26: logging code pulled out of erc.el. Switched to using
72;; markers.
73
74;;; TODO:
597993cf
MB
75;;
76;; * Really, we need to lock the logfiles somehow, so that if a user
77;; is running multiple emacsen and/or on the same channel as more
78;; than one user, only one process writes to the logfile. This is
79;; especially needed for those logfiles with no nick in them, as
80;; these would become corrupted.
81;; For a single emacs process, the problem could be solved using a
82;; variable which contained the names of buffers already being
83;; logged. This would require that logging be buffer-local,
84;; possibly not a bad thing anyway, since many people don't want to
85;; log the server buffer.
86;; For multiple emacsen the problem is trickier. On some systems,
87;; on could use the function `lock-buffer' and `unlock-buffer'.
88;; However, file locking isn't implemented on all platforms, for
89;; example, there is none on w32 systems.
90;; A third possibility might be to fake lockfiles. However, this
91;; might lead to problems if an emacs crashes, as the lockfile
92;; would be left lying around.
93
94;;; Code:
95
96(require 'erc)
ff59d266
MB
97(eval-when-compile
98 (require 'erc-networks)
99 (require 'cl))
597993cf
MB
100
101(defgroup erc-log nil
102 "Logging facilities for ERC."
103 :group 'erc)
104
105(defcustom erc-generate-log-file-name-function 'erc-generate-log-file-name-long
106 "*A function to generate a log filename.
107The function must take five arguments: BUFFER, TARGET, NICK, SERVER and PORT.
108BUFFER is the buffer to be saved,
109TARGET is the name of the channel, or the target of the query,
110NICK is the current nick,
526dc846
MO
111SERVER and PORT are the parameters that were used to connect to BUFFERs
112`erc-server-process'.
113
114If you want to write logs into different directories, make a
115custom function which returns the directory part and set
116`erc-log-channels-directory' to its name."
597993cf 117 :group 'erc-log
ff59d266
MB
118 :type '(choice (const :tag "Long style" erc-generate-log-file-name-long)
119 (const :tag "Long, but with network name rather than server"
120 erc-generate-log-file-name-network)
121 (const :tag "Short" erc-generate-log-file-name-short)
122 (const :tag "With date" erc-generate-log-file-name-with-date)
526dc846 123 (function :tag "Other function")))
597993cf 124
597993cf
MB
125(defcustom erc-truncate-buffer-on-save nil
126 "Truncate any ERC (channel, query, server) buffer when it is saved."
127 :group 'erc-log
128 :type 'boolean)
129
130(defcustom erc-enable-logging t
131 "If non-nil, ERC will log IRC conversations.
132This can either be a boolean value of nil or t, or a function.
133If the value is a function, it will be called with one argument, the
134name of the current ERC buffer. One possible function, which saves
135all but server buffers is `erc-log-all-but-server-buffers'.
136
137This variable is buffer local. Setting it via \\[customize] sets the
138default value.
139
140Log files are stored in `erc-log-channels-directory'."
141 :group 'erc-log
142 :type '(choice boolean
143 function))
144(make-variable-buffer-local 'erc-enable-logging)
145
146(defcustom erc-log-channels-directory "~/log"
147 "The directory to place log files for channels.
148Leave blank to disable logging. If not nil, all the channel
149buffers are logged in separate files in that directory. The
526dc846
MO
150directory should not end with a trailing slash.
151
152If this is the name of a function, the function will be called
153with the buffer, target, nick, server, and port arguments. See
154`erc-generate-log-file-name-function' for a description of these
155arguments."
597993cf
MB
156 :group 'erc-log
157 :type '(choice directory
526dc846
MO
158 (function "Function")
159 (const :tag "Disable logging" nil)))
597993cf 160
0b6bb130 161(defcustom erc-log-insert-log-on-open nil
597993cf
MB
162 "*Insert log file contents into the buffer if a log file exists."
163 :group 'erc-log
164 :type 'boolean)
165
0b6bb130
MB
166(defcustom erc-save-buffer-on-part t
167 "*Save the channel buffer content using `erc-save-buffer-in-logs' on PART.
168
169If you set this to nil, you may want to enable both
170`erc-log-write-after-send' and `erc-log-write-after-insert'."
171 :group 'erc-log
172 :type 'boolean)
173
174(defcustom erc-save-queries-on-quit t
175 "*Save all query (also channel) buffers of the server on QUIT.
176
177If you set this to nil, you may want to enable both
178`erc-log-write-after-send' and `erc-log-write-after-insert'."
179 :group 'erc-log
180 :type 'boolean)
181
182(defcustom erc-log-write-after-send nil
183 "*If non-nil, write to log file after every message you send.
184
185If you set this to nil, you may want to enable both
186`erc-save-buffer-on-part' and `erc-save-queries-on-quit'."
187 :group 'erc-log
188 :type 'boolean)
189
190(defcustom erc-log-write-after-insert nil
191 "*If non-nil, write to log file when new text is added to a
192logged ERC buffer.
193
194If you set this to nil, you may want to enable both
195`erc-save-buffer-on-part' and `erc-save-queries-on-quit'."
597993cf
MB
196 :group 'erc-log
197 :type 'boolean)
198
199(defcustom erc-log-file-coding-system (if (featurep 'xemacs)
200 'binary
201 'emacs-mule)
202 "*The coding system ERC should use for writing log files.
203
204This should ideally, be a \"catch-all\" coding system, like
205`emacs-mule', or `iso-2022-7bit'."
206 :group 'erc-log)
207
208;;;###autoload (autoload 'erc-log-mode "erc-log" nil t)
209(define-erc-module log nil
210 "Automatically logs things you receive on IRC into files.
211Files are stored in `erc-log-channels-directory'; file name
212format is defined through a formatting function on
213`erc-generate-log-file-name-function'.
214
215Since automatic logging is not always a Good Thing (especially if
216people say things in different coding systems), you can turn logging
217behaviour on and off with the variable `erc-enable-logging', which can
218also be a predicate function. To only log when you are not set away, use:
219
220\(setq erc-enable-logging
221 (lambda (buffer)
222 (with-current-buffer buffer
ff59d266 223 (null (erc-away-time)))))"
597993cf 224 ;; enable
0b6bb130
MB
225 ((when erc-log-write-after-insert
226 (add-hook 'erc-insert-post-hook 'erc-save-buffer-in-logs))
227 (when erc-log-write-after-send
228 (add-hook 'erc-send-post-hook 'erc-save-buffer-in-logs))
229 (add-hook 'erc-kill-buffer-hook 'erc-save-buffer-in-logs)
230 (add-hook 'erc-kill-channel-hook 'erc-save-buffer-in-logs)
83dc6995 231 (add-hook 'kill-emacs-hook 'erc-log-save-all-buffers)
0b6bb130
MB
232 (add-hook 'erc-quit-hook 'erc-conditional-save-queries)
233 (add-hook 'erc-part-hook 'erc-conditional-save-buffer)
234 ;; append, so that 'erc-initialize-log-marker runs first
2e3ef421
MB
235 (add-hook 'erc-connect-pre-hook 'erc-log-setup-logging 'append)
236 (dolist (buffer (erc-buffer-list))
ff59d266 237 (erc-log-setup-logging buffer)))
597993cf 238 ;; disable
0b6bb130
MB
239 ((remove-hook 'erc-insert-post-hook 'erc-save-buffer-in-logs)
240 (remove-hook 'erc-send-post-hook 'erc-save-buffer-in-logs)
241 (remove-hook 'erc-kill-buffer-hook 'erc-save-buffer-in-logs)
242 (remove-hook 'erc-kill-channel-hook 'erc-save-buffer-in-logs)
83dc6995 243 (remove-hook 'kill-emacs-hook 'erc-log-save-all-buffers)
0b6bb130
MB
244 (remove-hook 'erc-quit-hook 'erc-conditional-save-queries)
245 (remove-hook 'erc-part-hook 'erc-conditional-save-buffer)
2e3ef421
MB
246 (remove-hook 'erc-connect-pre-hook 'erc-log-setup-logging)
247 (dolist (buffer (erc-buffer-list))
ff59d266 248 (erc-log-disable-logging buffer))))
597993cf
MB
249
250(define-key erc-mode-map "\C-c\C-l" 'erc-save-buffer-in-logs)
251
0b6bb130 252;;; functionality referenced from erc.el
ff59d266 253(defun erc-log-setup-logging (buffer)
597993cf 254 "Setup the buffer-local logging variables in the current buffer.
ff59d266
MB
255This function is destined to be run from `erc-connect-pre-hook'.
256The current buffer is given by BUFFER."
257 (when (erc-logging-enabled buffer)
258 (with-current-buffer buffer
259 (auto-save-mode -1)
260 (setq buffer-file-name nil)
261 (cond ((boundp 'write-file-functions)
262 (set (make-local-variable 'write-file-functions)
263 '(erc-save-buffer-in-logs)))
264 ((boundp 'local-write-file-hooks)
265 (setq local-write-file-hooks '(erc-save-buffer-in-logs)))
266 (t
267 (set (make-local-variable 'write-file-hooks)
268 '(erc-save-buffer-in-logs))))
269 (when erc-log-insert-log-on-open
270 (ignore-errors (insert-file-contents (erc-current-logfile))
271 (move-marker erc-last-saved-position
272 (1- (point-max))))))))
273
274(defun erc-log-disable-logging (buffer)
275 "Disable logging in BUFFER."
276 (when (erc-logging-enabled buffer)
277 (with-current-buffer buffer
278 (setq buffer-offer-save nil
279 erc-enable-logging nil))))
2e3ef421 280
597993cf
MB
281(defun erc-log-all-but-server-buffers (buffer)
282 "Returns t if logging should be enabled in BUFFER.
81bb49ce 283Returns nil if `erc-server-buffer-p' returns t."
597993cf
MB
284 (save-excursion
285 (save-window-excursion
286 (set-buffer buffer)
287 (not (erc-server-buffer-p)))))
288
289(defun erc-save-query-buffers (process)
83dc6995 290 "Save all buffers of the given PROCESS."
597993cf
MB
291 (erc-with-all-buffers-of-server process
292 nil
293 (erc-save-buffer-in-logs)))
294
295(defun erc-conditional-save-buffer (buffer)
296 "Save Query BUFFER if `erc-save-queries-on-quit' is t."
297 (when erc-save-buffer-on-part
298 (erc-save-buffer-in-logs buffer)))
299
300(defun erc-conditional-save-queries (process)
301 "Save Query buffers of PROCESS if `erc-save-queries-on-quit' is t."
302 (when erc-save-queries-on-quit
303 (erc-save-query-buffers process)))
304
83dc6995
MB
305;; Make sure that logs get saved, even if someone overrides the active
306;; process prompt for a quick exit from Emacs
307(defun erc-log-save-all-buffers ()
308 (dolist (buffer (erc-buffer-list))
309 (erc-save-buffer-in-logs buffer)))
310
597993cf
MB
311;;;###autoload
312(defun erc-logging-enabled (&optional buffer)
313 "Return non-nil if logging is enabled for BUFFER.
314If BUFFER is nil, the value of `current-buffer' is used.
315Logging is enabled if `erc-log-channels-directory' is non-nil, the directory
316is writeable (it will be created as necessary) and
317`erc-enable-logging' returns a non-nil value."
318 (and erc-log-channels-directory
526dc846
MO
319 (or (functionp erc-log-channels-directory)
320 (erc-directory-writable-p erc-log-channels-directory))
597993cf
MB
321 (if (functionp erc-enable-logging)
322 (funcall erc-enable-logging (or buffer (current-buffer)))
323 erc-enable-logging)))
324
b5bc193f
MB
325(defun erc-log-standardize-name (filename)
326 "Make FILENAME safe to use as the name of an ERC log.
327This will not work with full paths, only names.
328
329Any unsafe characters in the name are replaced with \"!\". The
330filename is downcased."
331 (downcase (erc-replace-regexp-in-string
332 "[/\\]" "!" (convert-standard-filename filename))))
333
597993cf
MB
334(defun erc-current-logfile (&optional buffer)
335 "Return the logfile to use for BUFFER.
336If BUFFER is nil, the value of `current-buffer' is used.
337This is determined by `erc-generate-log-file-name-function'.
338The result is converted to lowercase, as IRC is case-insensitive"
526dc846
MO
339 (unless buffer (setq buffer (current-buffer)))
340 (let ((target (or (buffer-name buffer) (erc-default-target)))
341 (nick (erc-current-nick))
342 (server erc-session-server)
343 (port erc-session-port))
344 (expand-file-name
345 (erc-log-standardize-name
346 (funcall erc-generate-log-file-name-function
347 buffer target nick server port))
348 (if (functionp erc-log-channels-directory)
349 (funcall erc-log-channels-directory
350 buffer target nick server port)
351 erc-log-channels-directory))))
597993cf
MB
352
353(defun erc-generate-log-file-name-with-date (buffer &rest ignore)
354 "This function computes a short log file name.
355The name of the log file is composed of BUFFER and the current date.
356This function is a possible value for `erc-generate-log-file-name-function'."
357 (concat (buffer-name buffer) "-" (format-time-string "%Y-%m-%d") ".txt"))
358
359(defun erc-generate-log-file-name-short (buffer &rest ignore)
360 "This function computes a short log file name.
361In fact, it only uses the buffer name of the BUFFER argument, so
362you can affect that using `rename-buffer' and the-like. This
363function is a possible value for
364`erc-generate-log-file-name-function'."
365 (concat (buffer-name buffer) ".txt"))
366
367(defun erc-generate-log-file-name-long (buffer target nick server port)
368 "Generates a log-file name in the way ERC always did it.
369This results in a file name of the form #channel!nick@server:port.txt.
370This function is a possible value for `erc-generate-log-file-name-function'."
371 (let ((file (concat
372 (if target (concat target "!"))
373 nick "@" server ":" (cond ((stringp port) port)
374 ((numberp port)
375 (number-to-string port))) ".txt")))
376 ;; we need a make-safe-file-name function.
377 (convert-standard-filename file)))
378
ff59d266
MB
379(defun erc-generate-log-file-name-network (buffer target nick server port)
380 "Generates a log-file name using the network name rather than server name.
381This results in a file name of the form #channel!nick@network.txt.
382This function is a possible value for `erc-generate-log-file-name-function'."
383 (require 'erc-networks)
384 (let ((file (concat
385 (if target (concat target "!"))
386 nick "@"
387 (or (with-current-buffer buffer (erc-network-name)) server)
388 ".txt")))
389 ;; we need a make-safe-file-name function.
390 (convert-standard-filename file)))
391
597993cf
MB
392;;;###autoload
393(defun erc-save-buffer-in-logs (&optional buffer)
394 "Append BUFFER contents to the log file, if logging is enabled.
395If BUFFER is not provided, current buffer is used.
396Logging is enabled if `erc-logging-enabled' returns non-nil.
397
398This is normally done on exit, to save the unsaved portion of the
399buffer, since only the text that runs off the buffer limit is logged
400automatically.
401
402You can save every individual message by putting this function on
403`erc-insert-post-hook'."
404 (interactive)
405 (or buffer (setq buffer (current-buffer)))
406 (when (erc-logging-enabled buffer)
407 (let ((file (erc-current-logfile buffer))
408 (coding-system-for-write erc-log-file-coding-system))
409 (save-excursion
410 (with-current-buffer buffer
411 (save-restriction
412 (widen)
413 ;; early on in the initalisation, don't try and write the log out
414 (when (and (markerp erc-last-saved-position)
415 (> erc-insert-marker (1+ erc-last-saved-position)))
416 (write-region (1+ (marker-position erc-last-saved-position))
417 (marker-position erc-insert-marker)
418 file t 'nomessage)
419 (if (and erc-truncate-buffer-on-save (interactive-p))
420 (progn
421 (let ((inhibit-read-only t)) (erase-buffer))
422 (move-marker erc-last-saved-position (point-max))
423 (erc-display-prompt))
424 (move-marker erc-last-saved-position
425 ;; If we place erc-last-saved-position at
426 ;; erc-insert-marker, because text gets
427 ;; inserted /before/ erc-insert-marker,
428 ;; the log file will not be saved
429 ;; (erc-last-saved-position will always
430 ;; be equal to erc-insert-marker).
431 (1- (marker-position erc-insert-marker)))))
432 (set-buffer-modified-p nil))))))
433 t)
434
435(provide 'erc-log)
436
437;;; erc-log.el ends here
438;;
439;; Local Variables:
440;; indent-tabs-mode: t
441;; tab-width: 8
442;; End:
443
444;; arch-tag: 54072f99-9f0a-4846-8908-2ccde92221de