erc-log.el 17 KB

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