erc-autoaway.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. ;;; erc-autoaway.el --- Provides autoaway for ERC
  2. ;; Copyright (C) 2002-2004, 2006-2012 Free Software Foundation, Inc.
  3. ;; Author: Jorgen Schaefer <forcer@forcix.cx>
  4. ;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?ErcAutoAway
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; TODO:
  18. ;; - Legacy names: erc-auto-discard-away, erc-auto-set-away
  19. ;;; Code:
  20. (require 'erc)
  21. (defgroup erc-autoaway nil
  22. "Set yourself automatically away after some idletime and set
  23. yourself back when you type something."
  24. :group 'erc)
  25. (defvar erc-autoaway-idletimer nil
  26. "The Emacs idletimer.
  27. This is only used when `erc-autoaway-idle-method' is set to 'emacs.")
  28. (defvar erc-autoaway-last-sent-time (erc-current-time)
  29. "The last time the user sent something.")
  30. (defvar erc-autoaway-caused-away nil
  31. "Indicates whether this module was responsible for setting the
  32. user's away status.")
  33. (defvar erc-autoaway-idle-seconds)
  34. (defun erc-autoaway-reestablish-idletimer ()
  35. "Reestablish the Emacs idletimer.
  36. If `erc-autoaway-idle-method' is 'emacs, you must call this
  37. function each time you change `erc-autoaway-idle-seconds'."
  38. (interactive)
  39. (when erc-autoaway-idletimer
  40. (erc-cancel-timer erc-autoaway-idletimer))
  41. (setq erc-autoaway-idletimer
  42. (run-with-idle-timer erc-autoaway-idle-seconds
  43. t
  44. 'erc-autoaway-set-away
  45. erc-autoaway-idle-seconds)))
  46. (defun erc-autoaway-some-server-buffer ()
  47. "Return some ERC server buffer if its connection is alive.
  48. If none is found, return nil."
  49. (car (erc-buffer-list #'erc-open-server-buffer-p)))
  50. (defun erc-autoaway-insinuate-maybe (&optional server &rest ignored)
  51. "Add autoaway reset function to `post-command-hook' if at least one
  52. ERC process is alive.
  53. This is used when `erc-autoaway-idle-method' is 'user."
  54. (when (or server (erc-autoaway-some-server-buffer))
  55. (add-hook 'post-command-hook 'erc-autoaway-reset-idle-user)))
  56. (defun erc-autoaway-remove-maybe (&rest ignored)
  57. "Remove the autoaway reset function from `post-command-hook' if
  58. no ERC process is alive.
  59. This is used when `erc-autoaway-idle-method' is 'user."
  60. (unless (erc-autoaway-some-server-buffer)
  61. (remove-hook 'post-command-hook 'erc-autoaway-reset-idle-user)))
  62. ;;;###autoload (autoload 'erc-autoaway-mode "erc-autoaway")
  63. (define-erc-module autoaway nil
  64. "In ERC autoaway mode, you can be set away automatically.
  65. If `erc-auto-set-away' is set, then you will be set away after
  66. the number of seconds specified in `erc-autoaway-idle-seconds'.
  67. There are several kinds of being idle:
  68. User idle time measures how long you have not been sending any
  69. commands to Emacs. This is the default.
  70. Emacs idle time measures how long Emacs has been idle. This is
  71. currently not useful, since Emacs is non-idle when it handles
  72. ping-pong with IRC servers. See `erc-autoaway-idle-method'
  73. for more information.
  74. IRC idle time measures how long since you last sent something (see
  75. `erc-autoaway-last-sent-time').
  76. If `erc-auto-discard-away' is set, then typing anything, will
  77. set you no longer away.
  78. Related variables: `erc-public-away-p' and `erc-away-nickname'."
  79. ;; Enable:
  80. ((when (boundp 'erc-autoaway-idle-method)
  81. (add-hook 'erc-connect-pre-hook 'erc-autoaway-reset-indicators)
  82. (setq erc-autoaway-last-sent-time (erc-current-time))
  83. (cond
  84. ((eq erc-autoaway-idle-method 'irc)
  85. (add-hook 'erc-send-completed-hook 'erc-autoaway-reset-idle-irc)
  86. (add-hook 'erc-server-001-functions 'erc-autoaway-reset-idle-irc))
  87. ((eq erc-autoaway-idle-method 'user)
  88. (add-hook 'erc-after-connect 'erc-autoaway-insinuate-maybe)
  89. (add-hook 'erc-disconnected-hook 'erc-autoaway-remove-maybe)
  90. (erc-autoaway-insinuate-maybe))
  91. ((eq erc-autoaway-idle-method 'emacs)
  92. (erc-autoaway-reestablish-idletimer)))
  93. (add-hook 'erc-timer-hook 'erc-autoaway-possibly-set-away)
  94. (add-hook 'erc-server-305-functions 'erc-autoaway-reset-indicators)))
  95. ;; Disable:
  96. ((when (boundp 'erc-autoaway-idle-method)
  97. (remove-hook 'erc-connect-pre-hook 'erc-autoaway-reset-indicators)
  98. (cond
  99. ((eq erc-autoaway-idle-method 'irc)
  100. (remove-hook 'erc-send-completed-hook 'erc-autoaway-reset-idle-irc)
  101. (remove-hook 'erc-server-001-functions 'erc-autoaway-reset-idle-irc))
  102. ((eq erc-autoaway-idle-method 'user)
  103. (remove-hook 'post-command-hook 'erc-autoaway-reset-idle-user)
  104. (remove-hook 'erc-after-connect 'erc-autoaway-insinuate-maybe)
  105. (remove-hook 'erc-disconnected-hook 'erc-autoaway-remove-maybe))
  106. ((eq erc-autoaway-idle-method 'emacs)
  107. (erc-cancel-timer erc-autoaway-idletimer)
  108. (setq erc-autoaway-idletimer nil)))
  109. (remove-hook 'erc-timer-hook 'erc-autoaway-possibly-set-away)
  110. (remove-hook 'erc-server-305-functions 'erc-autoaway-reset-indicators))))
  111. (defcustom erc-autoaway-idle-method 'user
  112. "*The method used to determine how long you have been idle.
  113. If 'user, the time of the last command sent to Emacs is used.
  114. If 'emacs, the idle time in Emacs is used.
  115. If 'irc, the time of the last IRC command is used.
  116. The time itself is specified by `erc-autoaway-idle-seconds'.
  117. See `erc-autoaway-mode' for more information on the various
  118. definitions of being idle."
  119. :group 'erc-autoaway
  120. :type '(choice (const :tag "User idle time" user)
  121. (const :tag "Emacs idle time" emacs)
  122. (const :tag "Last IRC action" irc))
  123. :set (lambda (sym val)
  124. (if erc-autoaway-mode
  125. (progn
  126. (erc-autoaway-disable)
  127. (set sym val)
  128. (erc-autoaway-enable))
  129. (set sym val))))
  130. (defcustom erc-auto-set-away t
  131. "*If non-nil, set away after `erc-autoaway-idle-seconds' seconds of idling.
  132. ERC autoaway mode can set you away when you idle, and set you no
  133. longer away when you type something. This variable controls whether
  134. you will be set away when you idle. See `erc-auto-discard-away' for
  135. the other half."
  136. :group 'erc-autoaway
  137. :type 'boolean)
  138. (defcustom erc-auto-discard-away t
  139. "*If non-nil, sending anything when away automatically discards away state.
  140. ERC autoaway mode can set you away when you idle, and set you no
  141. longer away when you type something. This variable controls whether
  142. you will be set no longer away when you type something. See
  143. `erc-auto-set-away' for the other half.
  144. See also `erc-autoaway-no-auto-discard-regexp'."
  145. :group 'erc-autoaway
  146. :type 'boolean)
  147. (defcustom erc-autoaway-no-auto-discard-regexp "^/g?away.*$"
  148. "*Input that matches this will not automatically discard away status.
  149. See `erc-auto-discard-away'."
  150. :group 'erc-autoaway
  151. :type 'regexp)
  152. (defcustom erc-autoaway-idle-seconds 1800
  153. "*Number of seconds after which ERC will set you automatically away.
  154. If you are changing this variable using lisp instead of customizing it,
  155. you have to run `erc-autoaway-reestablish-idletimer' afterwards."
  156. :group 'erc-autoaway
  157. :set (lambda (sym val)
  158. (set-default sym val)
  159. (when (eq erc-autoaway-idle-method 'emacs)
  160. (erc-autoaway-reestablish-idletimer)))
  161. :type 'number)
  162. (defcustom erc-autoaway-message
  163. "I'm gone (autoaway after %i seconds of idletime)"
  164. "*Message ERC will use when setting you automatically away.
  165. It is used as a `format' string with the argument of the idletime
  166. in seconds."
  167. :group 'erc-autoaway
  168. :type 'string)
  169. (defun erc-autoaway-reset-idle-user (&rest stuff)
  170. "Reset the stored user idle time.
  171. This is one global variable since a user talking on one net can
  172. talk on another net too."
  173. (when erc-auto-discard-away
  174. (erc-autoaway-set-back #'erc-autoaway-remove-maybe))
  175. (setq erc-autoaway-last-sent-time (erc-current-time)))
  176. (defun erc-autoaway-reset-idle-irc (line &rest stuff)
  177. "Reset the stored IRC idle time.
  178. This is one global variable since a user talking on one net can
  179. talk on another net too."
  180. (when (and erc-auto-discard-away
  181. (stringp line)
  182. (not (string-match erc-autoaway-no-auto-discard-regexp line)))
  183. (erc-autoaway-set-back))
  184. (setq erc-autoaway-last-sent-time (erc-current-time)))
  185. (defun erc-autoaway-set-back (&optional none-alive-func)
  186. "Discard the away state globally.
  187. NONE-ALIVE-FUNC is the function to call if no ERC processes are alive."
  188. (let ((server-buffer (erc-autoaway-some-server-buffer)))
  189. (if (and erc-autoaway-caused-away
  190. (buffer-live-p server-buffer)
  191. (with-current-buffer server-buffer erc-away))
  192. (erc-cmd-GAWAY "")
  193. (when none-alive-func (funcall none-alive-func)))))
  194. (defun erc-autoaway-some-open-server-buffer ()
  195. "Return some ERC server buffer if its connection is alive and the
  196. user is not away.
  197. If none is found, return nil."
  198. (car (erc-buffer-list (lambda ()
  199. (and (erc-open-server-buffer-p)
  200. (not erc-away))))))
  201. (defun erc-autoaway-possibly-set-away (current-time)
  202. "Set autoaway when `erc-auto-set-away' is true and the idletime is
  203. exceeds `erc-autoaway-idle-seconds'."
  204. ;; A test for (erc-server-process-alive) is not necessary, because
  205. ;; this function is called from `erc-timer-hook', which is called
  206. ;; whenever the server sends something to the client.
  207. (when (and erc-server-connected
  208. erc-auto-set-away
  209. (not erc-autoaway-caused-away)
  210. (erc-autoaway-some-open-server-buffer))
  211. (let ((idle-time (erc-time-diff erc-autoaway-last-sent-time
  212. current-time)))
  213. (when (>= idle-time erc-autoaway-idle-seconds)
  214. (erc-display-message
  215. nil 'notice nil
  216. (format "Setting automatically away after %i seconds of idle-time"
  217. idle-time))
  218. (erc-autoaway-set-away idle-time t)))))
  219. (defun erc-autoaway-set-away (idle-time &optional notest)
  220. "Set the away state globally.
  221. If NOTEST is specified, do not check to see whether there is an
  222. active server buffer available."
  223. ;; Note that the idle timer runs, even when Emacs is inactive. In
  224. ;; order to prevent flooding when we connect, we test for an
  225. ;; existing process.
  226. (when (or notest (erc-autoaway-some-open-server-buffer))
  227. (setq erc-autoaway-caused-away t)
  228. (erc-cmd-GAWAY (format erc-autoaway-message idle-time))))
  229. (defun erc-autoaway-reset-indicators (&rest stuff)
  230. "Reset indicators used by the erc-autoaway module."
  231. (setq erc-autoaway-last-sent-time (erc-current-time))
  232. (setq erc-autoaway-caused-away nil))
  233. (provide 'erc-autoaway)
  234. ;;; erc-autoaway.el ends here
  235. ;;
  236. ;; Local Variables:
  237. ;; indent-tabs-mode: t
  238. ;; tab-width: 8
  239. ;; End: