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