erc-log.el 17 KB

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