rcirc.el 111 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091
  1. ;;; rcirc.el --- default, simple IRC client -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2005-2017 Free Software Foundation, Inc.
  3. ;; Author: Ryan Yeske <rcyeske@gmail.com>
  4. ;; Maintainers: Ryan Yeske <rcyeske@gmail.com>,
  5. ;; Leo Liu <sdl.web@gmail.com>
  6. ;; Keywords: comm
  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. ;; Internet Relay Chat (IRC) is a form of instant communication over
  20. ;; the Internet. It is mainly designed for group (many-to-many)
  21. ;; communication in discussion forums called channels, but also allows
  22. ;; one-to-one communication.
  23. ;; Rcirc has simple defaults and clear and consistent behavior.
  24. ;; Message arrival timestamps, activity notification on the mode line,
  25. ;; message filling, nick completion, and keepalive pings are all
  26. ;; enabled by default, but can easily be adjusted or turned off. Each
  27. ;; discussion takes place in its own buffer and there is a single
  28. ;; server buffer per connection.
  29. ;; Open a new irc connection with:
  30. ;; M-x irc RET
  31. ;;; Todo:
  32. ;;; Code:
  33. (require 'cl-lib)
  34. (require 'ring)
  35. (require 'time-date)
  36. (defgroup rcirc nil
  37. "Simple IRC client."
  38. :version "22.1"
  39. :prefix "rcirc-"
  40. :link '(custom-manual "(rcirc)")
  41. :group 'applications)
  42. (defcustom rcirc-server-alist
  43. '(("irc.freenode.net" :channels ("#rcirc")
  44. ;; Don't use the TLS port by default, in case gnutls is not available.
  45. ;; :port 7000 :encryption tls
  46. ))
  47. "An alist of IRC connections to establish when running `rcirc'.
  48. Each element looks like (SERVER-NAME PARAMETERS).
  49. SERVER-NAME is a string describing the server to connect
  50. to.
  51. The optional PARAMETERS come in pairs PARAMETER VALUE.
  52. The following parameters are recognized:
  53. `:nick'
  54. VALUE must be a string. If absent, `rcirc-default-nick' is used
  55. for this connection.
  56. `:port'
  57. VALUE must be a number or string. If absent,
  58. `rcirc-default-port' is used.
  59. `:user-name'
  60. VALUE must be a string. If absent, `rcirc-default-user-name' is
  61. used.
  62. `:password'
  63. VALUE must be a string. If absent, no PASS command will be sent
  64. to the server.
  65. `:full-name'
  66. VALUE must be a string. If absent, `rcirc-default-full-name' is
  67. used.
  68. `:channels'
  69. VALUE must be a list of strings describing which channels to join
  70. when connecting to this server. If absent, no channels will be
  71. connected to automatically.
  72. `:encryption'
  73. VALUE must be `plain' (the default) for unencrypted connections, or `tls'
  74. for connections using SSL/TLS.
  75. `:server-alias'
  76. VALUE must be a string that will be used instead of the server name for
  77. display purposes. If absent, the real server name will be displayed instead."
  78. :type '(alist :key-type string
  79. :value-type (plist :options
  80. ((:nick string)
  81. (:port integer)
  82. (:user-name string)
  83. (:password string)
  84. (:full-name string)
  85. (:channels (repeat string))
  86. (:encryption (choice (const tls)
  87. (const plain)))
  88. (:server-alias string))))
  89. :group 'rcirc)
  90. (defcustom rcirc-default-port 6667
  91. "The default port to connect to."
  92. :type 'integer
  93. :group 'rcirc)
  94. (defcustom rcirc-default-nick (user-login-name)
  95. "Your nick."
  96. :type 'string
  97. :group 'rcirc)
  98. (defcustom rcirc-default-user-name "user"
  99. "Your user name sent to the server when connecting."
  100. :version "24.1" ; changed default
  101. :type 'string
  102. :group 'rcirc)
  103. (defcustom rcirc-default-full-name "unknown"
  104. "The full name sent to the server when connecting."
  105. :version "24.1" ; changed default
  106. :type 'string
  107. :group 'rcirc)
  108. (defcustom rcirc-fill-flag t
  109. "Non-nil means line-wrap messages printed in channel buffers."
  110. :type 'boolean
  111. :group 'rcirc)
  112. (defcustom rcirc-fill-column nil
  113. "Column beyond which automatic line-wrapping should happen.
  114. If nil, use value of `fill-column'.
  115. If a function (e.g., `frame-text-width' or `window-text-width'),
  116. call it to compute the number of columns."
  117. :risky t ; can get funcalled
  118. :type '(choice (const :tag "Value of `fill-column'" nil)
  119. (integer :tag "Number of columns")
  120. (function :tag "Function returning the number of columns"))
  121. :group 'rcirc)
  122. (defcustom rcirc-fill-prefix nil
  123. "Text to insert before filled lines.
  124. If nil, calculate the prefix dynamically to line up text
  125. underneath each nick."
  126. :type '(choice (const :tag "Dynamic" nil)
  127. (string :tag "Prefix text"))
  128. :group 'rcirc)
  129. (defvar rcirc-ignore-buffer-activity-flag nil
  130. "If non-nil, ignore activity in this buffer.")
  131. (make-variable-buffer-local 'rcirc-ignore-buffer-activity-flag)
  132. (defvar rcirc-low-priority-flag nil
  133. "If non-nil, activity in this buffer is considered low priority.")
  134. (make-variable-buffer-local 'rcirc-low-priority-flag)
  135. (defcustom rcirc-omit-responses
  136. '("JOIN" "PART" "QUIT" "NICK")
  137. "Responses which will be hidden when `rcirc-omit-mode' is enabled."
  138. :type '(repeat string)
  139. :group 'rcirc)
  140. (define-minor-mode rcirc-omit-mode
  141. "Toggle the hiding of \"uninteresting\" lines.
  142. With a prefix argument ARG, enable Rcirc-Omit mode if ARG is
  143. positive, and disable it otherwise. If called from Lisp, enable
  144. the mode if ARG is omitted or nil.
  145. Uninteresting lines are those whose responses are listed in
  146. `rcirc-omit-responses'."
  147. nil " Omit" nil
  148. (if rcirc-omit-mode
  149. (progn
  150. (add-to-invisibility-spec '(rcirc-omit . nil))
  151. (message "Rcirc-Omit mode enabled"))
  152. (remove-from-invisibility-spec '(rcirc-omit . nil))
  153. (message "Rcirc-Omit mode disabled"))
  154. (dolist (window (get-buffer-window-list (current-buffer)))
  155. (with-selected-window window
  156. (recenter (when (> (point) rcirc-prompt-start-marker) -1)))))
  157. (defcustom rcirc-time-format "%H:%M "
  158. "Describes how timestamps are printed.
  159. Used as the first arg to `format-time-string'."
  160. :type 'string
  161. :group 'rcirc)
  162. (defcustom rcirc-input-ring-size 1024
  163. "Size of input history ring."
  164. :type 'integer
  165. :group 'rcirc)
  166. (defcustom rcirc-read-only-flag t
  167. "Non-nil means make text in IRC buffers read-only."
  168. :type 'boolean
  169. :group 'rcirc)
  170. (defcustom rcirc-buffer-maximum-lines nil
  171. "The maximum size in lines for rcirc buffers.
  172. Channel buffers are truncated from the top to be no greater than this
  173. number. If zero or nil, no truncating is done."
  174. :type '(choice (const :tag "No truncation" nil)
  175. (integer :tag "Number of lines"))
  176. :group 'rcirc)
  177. (defcustom rcirc-scroll-show-maximum-output t
  178. "If non-nil, scroll buffer to keep the point at the bottom of
  179. the window."
  180. :type 'boolean
  181. :group 'rcirc)
  182. (defcustom rcirc-authinfo nil
  183. "List of authentication passwords.
  184. Each element of the list is a list with a SERVER-REGEXP string
  185. and a method symbol followed by method specific arguments.
  186. The valid METHOD symbols are `nickserv', `chanserv' and
  187. `bitlbee'.
  188. The ARGUMENTS for each METHOD symbol are:
  189. `nickserv': NICK PASSWORD [NICKSERV-NICK]
  190. `chanserv': NICK CHANNEL PASSWORD
  191. `bitlbee': NICK PASSWORD
  192. `quakenet': ACCOUNT PASSWORD
  193. Examples:
  194. ((\"freenode\" nickserv \"bob\" \"p455w0rd\")
  195. (\"freenode\" chanserv \"bob\" \"#bobland\" \"passwd99\")
  196. (\"bitlbee\" bitlbee \"robert\" \"sekrit\")
  197. (\"dal.net\" nickserv \"bob\" \"sekrit\" \"NickServ@services.dal.net\")
  198. (\"quakenet.org\" quakenet \"bobby\" \"sekrit\"))"
  199. :type '(alist :key-type (string :tag "Server")
  200. :value-type (choice (list :tag "NickServ"
  201. (const nickserv)
  202. (string :tag "Nick")
  203. (string :tag "Password"))
  204. (list :tag "ChanServ"
  205. (const chanserv)
  206. (string :tag "Nick")
  207. (string :tag "Channel")
  208. (string :tag "Password"))
  209. (list :tag "BitlBee"
  210. (const bitlbee)
  211. (string :tag "Nick")
  212. (string :tag "Password"))
  213. (list :tag "QuakeNet"
  214. (const quakenet)
  215. (string :tag "Account")
  216. (string :tag "Password"))))
  217. :group 'rcirc)
  218. (defcustom rcirc-auto-authenticate-flag t
  219. "Non-nil means automatically send authentication string to server.
  220. See also `rcirc-authinfo'."
  221. :type 'boolean
  222. :group 'rcirc)
  223. (defcustom rcirc-authenticate-before-join t
  224. "Non-nil means authenticate to services before joining channels.
  225. Currently only works with NickServ on some networks."
  226. :version "24.1"
  227. :type 'boolean
  228. :group 'rcirc)
  229. (defcustom rcirc-prompt "> "
  230. "Prompt string to use in IRC buffers.
  231. The following replacements are made:
  232. %n is your nick.
  233. %s is the server.
  234. %t is the buffer target, a channel or a user.
  235. Setting this alone will not affect the prompt;
  236. use either M-x customize or also call `rcirc-update-prompt'."
  237. :type 'string
  238. :set 'rcirc-set-changed
  239. :initialize 'custom-initialize-default
  240. :group 'rcirc)
  241. (defcustom rcirc-keywords nil
  242. "List of keywords to highlight in message text."
  243. :type '(repeat string)
  244. :group 'rcirc)
  245. (defcustom rcirc-ignore-list ()
  246. "List of ignored nicks.
  247. Use /ignore to list them, use /ignore NICK to add or remove a nick."
  248. :type '(repeat string)
  249. :group 'rcirc)
  250. (defvar rcirc-ignore-list-automatic ()
  251. "List of ignored nicks added to `rcirc-ignore-list' because of renaming.
  252. When an ignored person renames, their nick is added to both lists.
  253. Nicks will be removed from the automatic list on follow-up renamings or
  254. parts.")
  255. (defcustom rcirc-bright-nicks nil
  256. "List of nicks to be emphasized.
  257. See `rcirc-bright-nick' face."
  258. :type '(repeat string)
  259. :group 'rcirc)
  260. (defcustom rcirc-dim-nicks nil
  261. "List of nicks to be deemphasized.
  262. See `rcirc-dim-nick' face."
  263. :type '(repeat string)
  264. :group 'rcirc)
  265. (define-obsolete-variable-alias 'rcirc-print-hooks
  266. 'rcirc-print-functions "24.3")
  267. (defcustom rcirc-print-functions nil
  268. "Hook run after text is printed.
  269. Called with 5 arguments, PROCESS, SENDER, RESPONSE, TARGET and TEXT."
  270. :type 'hook
  271. :group 'rcirc)
  272. (defvar rcirc-authenticated-hook nil
  273. "Hook run after successfully authenticated.")
  274. (defcustom rcirc-always-use-server-buffer-flag nil
  275. "Non-nil means messages without a channel target will go to the server buffer."
  276. :type 'boolean
  277. :group 'rcirc)
  278. (defcustom rcirc-decode-coding-system 'utf-8
  279. "Coding system used to decode incoming irc messages.
  280. Set to `undecided' if you want the encoding of the incoming
  281. messages autodetected."
  282. :type 'coding-system
  283. :group 'rcirc)
  284. (defcustom rcirc-encode-coding-system 'utf-8
  285. "Coding system used to encode outgoing irc messages."
  286. :type 'coding-system
  287. :group 'rcirc)
  288. (defcustom rcirc-coding-system-alist nil
  289. "Alist to decide a coding system to use for a channel I/O operation.
  290. The format is ((PATTERN . VAL) ...).
  291. PATTERN is either a string or a cons of strings.
  292. If PATTERN is a string, it is used to match a target.
  293. If PATTERN is a cons of strings, the car part is used to match a
  294. target, and the cdr part is used to match a server.
  295. VAL is either a coding system or a cons of coding systems.
  296. If VAL is a coding system, it is used for both decoding and encoding
  297. messages.
  298. If VAL is a cons of coding systems, the car part is used for decoding,
  299. and the cdr part is used for encoding."
  300. :type '(alist :key-type (choice (string :tag "Channel Regexp")
  301. (cons (string :tag "Channel Regexp")
  302. (string :tag "Server Regexp")))
  303. :value-type (choice coding-system
  304. (cons (coding-system :tag "Decode")
  305. (coding-system :tag "Encode"))))
  306. :group 'rcirc)
  307. (defcustom rcirc-multiline-major-mode 'fundamental-mode
  308. "Major-mode function to use in multiline edit buffers."
  309. :type 'function
  310. :group 'rcirc)
  311. (defcustom rcirc-nick-completion-format "%s: "
  312. "Format string to use in nick completions.
  313. The format string is only used when completing at the beginning
  314. of a line. The string is passed as the first argument to
  315. `format' with the nickname as the second argument."
  316. :version "24.1"
  317. :type 'string
  318. :group 'rcirc)
  319. (defcustom rcirc-kill-channel-buffers nil
  320. "When non-nil, kill channel buffers when the server buffer is killed.
  321. Only the channel buffers associated with the server in question
  322. will be killed."
  323. :version "24.3"
  324. :type 'boolean
  325. :group 'rcirc)
  326. (defvar rcirc-nick nil)
  327. (defvar rcirc-prompt-start-marker nil)
  328. (defvar rcirc-prompt-end-marker nil)
  329. (defvar rcirc-nick-table nil)
  330. (defvar rcirc-recent-quit-alist nil
  331. "Alist of nicks that have recently quit or parted the channel.")
  332. (defvar rcirc-nick-syntax-table
  333. (let ((table (make-syntax-table text-mode-syntax-table)))
  334. (mapc (lambda (c) (modify-syntax-entry c "w" table))
  335. "[]\\`_^{|}-")
  336. (modify-syntax-entry ?' "_" table)
  337. table)
  338. "Syntax table which includes all nick characters as word constituents.")
  339. ;; each process has an alist of (target . buffer) pairs
  340. (defvar rcirc-buffer-alist nil)
  341. (defvar rcirc-activity nil
  342. "List of buffers with unviewed activity.")
  343. (defvar rcirc-activity-string ""
  344. "String displayed in mode line representing `rcirc-activity'.")
  345. (put 'rcirc-activity-string 'risky-local-variable t)
  346. (defvar rcirc-server-buffer nil
  347. "The server buffer associated with this channel buffer.")
  348. (defvar rcirc-target nil
  349. "The channel or user associated with this buffer.")
  350. (defvar rcirc-urls nil
  351. "List of URLs seen in the current buffer and their start positions.")
  352. (put 'rcirc-urls 'permanent-local t)
  353. (defvar rcirc-timeout-seconds 600
  354. "Kill connection after this many seconds if there is no activity.")
  355. (defconst rcirc-id-string (concat "rcirc on GNU Emacs " emacs-version))
  356. (defvar rcirc-startup-channels nil)
  357. (defvar rcirc-server-name-history nil
  358. "History variable for \\[rcirc] call.")
  359. (defvar rcirc-server-port-history nil
  360. "History variable for \\[rcirc] call.")
  361. (defvar rcirc-nick-name-history nil
  362. "History variable for \\[rcirc] call.")
  363. (defvar rcirc-user-name-history nil
  364. "History variable for \\[rcirc] call.")
  365. ;;;###autoload
  366. (defun rcirc (arg)
  367. "Connect to all servers in `rcirc-server-alist'.
  368. Do not connect to a server if it is already connected.
  369. If ARG is non-nil, instead prompt for connection parameters."
  370. (interactive "P")
  371. (if arg
  372. (let* ((server (completing-read "IRC Server: "
  373. rcirc-server-alist
  374. nil nil
  375. (caar rcirc-server-alist)
  376. 'rcirc-server-name-history))
  377. (server-plist (cdr (assoc-string server rcirc-server-alist)))
  378. (port (read-string "IRC Port: "
  379. (number-to-string
  380. (or (plist-get server-plist :port)
  381. rcirc-default-port))
  382. 'rcirc-server-port-history))
  383. (nick (read-string "IRC Nick: "
  384. (or (plist-get server-plist :nick)
  385. rcirc-default-nick)
  386. 'rcirc-nick-name-history))
  387. (user-name (read-string "IRC Username: "
  388. (or (plist-get server-plist :user-name)
  389. rcirc-default-user-name)
  390. 'rcirc-user-name-history))
  391. (password (read-passwd "IRC Password: " nil
  392. (plist-get server-plist :password)))
  393. (channels (split-string
  394. (read-string "IRC Channels: "
  395. (mapconcat 'identity
  396. (plist-get server-plist
  397. :channels)
  398. " "))
  399. "[, ]+" t))
  400. (encryption (rcirc-prompt-for-encryption server-plist)))
  401. (rcirc-connect server port nick user-name
  402. rcirc-default-full-name
  403. channels password encryption))
  404. ;; connect to servers in `rcirc-server-alist'
  405. (let (connected-servers)
  406. (dolist (c rcirc-server-alist)
  407. (let ((server (car c))
  408. (nick (or (plist-get (cdr c) :nick) rcirc-default-nick))
  409. (port (or (plist-get (cdr c) :port) rcirc-default-port))
  410. (user-name (or (plist-get (cdr c) :user-name)
  411. rcirc-default-user-name))
  412. (full-name (or (plist-get (cdr c) :full-name)
  413. rcirc-default-full-name))
  414. (channels (plist-get (cdr c) :channels))
  415. (password (plist-get (cdr c) :password))
  416. (encryption (plist-get (cdr c) :encryption))
  417. (server-alias (plist-get (cdr c) :server-alias))
  418. contact)
  419. (when server
  420. (let (connected)
  421. (dolist (p (rcirc-process-list))
  422. (when (string= (or server-alias server) (process-name p))
  423. (setq connected p)))
  424. (if (not connected)
  425. (condition-case nil
  426. (rcirc-connect server port nick user-name
  427. full-name channels password encryption
  428. server-alias)
  429. (quit (message "Quit connecting to %s"
  430. (or server-alias server))))
  431. (with-current-buffer (process-buffer connected)
  432. (setq contact (process-contact
  433. (get-buffer-process (current-buffer)) :name))
  434. (setq connected-servers
  435. (cons (if (stringp contact)
  436. contact (or server-alias server))
  437. connected-servers))))))))
  438. (when connected-servers
  439. (message "Already connected to %s"
  440. (if (cdr connected-servers)
  441. (concat (mapconcat 'identity (butlast connected-servers) ", ")
  442. ", and "
  443. (car (last connected-servers)))
  444. (car connected-servers)))))))
  445. ;;;###autoload
  446. (defalias 'irc 'rcirc)
  447. (defvar rcirc-process-output nil)
  448. (defvar rcirc-topic nil)
  449. (defvar rcirc-keepalive-timer nil)
  450. (defvar rcirc-last-server-message-time nil)
  451. (defvar rcirc-server nil) ; server provided by server
  452. (defvar rcirc-server-name nil) ; server name given by 001 response
  453. (defvar rcirc-timeout-timer nil)
  454. (defvar rcirc-user-authenticated nil)
  455. (defvar rcirc-user-disconnect nil)
  456. (defvar rcirc-connecting nil)
  457. (defvar rcirc-connection-info nil)
  458. (defvar rcirc-process nil)
  459. ;;;###autoload
  460. (defun rcirc-connect (server &optional port nick user-name
  461. full-name startup-channels password encryption
  462. server-alias)
  463. (save-excursion
  464. (message "Connecting to %s..." (or server-alias server))
  465. (let* ((inhibit-eol-conversion)
  466. (port-number (if port
  467. (if (stringp port)
  468. (string-to-number port)
  469. port)
  470. rcirc-default-port))
  471. (nick (or nick rcirc-default-nick))
  472. (user-name (or user-name rcirc-default-user-name))
  473. (full-name (or full-name rcirc-default-full-name))
  474. (startup-channels startup-channels)
  475. (process (open-network-stream
  476. (or server-alias server) nil server port-number
  477. :type (or encryption 'plain))))
  478. ;; set up process
  479. (set-process-coding-system process 'raw-text 'raw-text)
  480. (switch-to-buffer (rcirc-generate-new-buffer-name process nil))
  481. (set-process-buffer process (current-buffer))
  482. (rcirc-mode process nil)
  483. (set-process-sentinel process 'rcirc-sentinel)
  484. (set-process-filter process 'rcirc-filter)
  485. (setq-local rcirc-connection-info
  486. (list server port nick user-name full-name startup-channels
  487. password encryption))
  488. (setq-local rcirc-process process)
  489. (setq-local rcirc-server server)
  490. (setq-local rcirc-server-name
  491. (or server-alias server)) ; Update when we get 001 response.
  492. (setq-local rcirc-buffer-alist nil)
  493. (setq-local rcirc-nick-table (make-hash-table :test 'equal))
  494. (setq-local rcirc-nick nick)
  495. (setq-local rcirc-process-output nil)
  496. (setq-local rcirc-startup-channels startup-channels)
  497. (setq-local rcirc-last-server-message-time (current-time))
  498. (setq-local rcirc-timeout-timer nil)
  499. (setq-local rcirc-user-disconnect nil)
  500. (setq-local rcirc-user-authenticated nil)
  501. (setq-local rcirc-connecting t)
  502. (add-hook 'auto-save-hook 'rcirc-log-write)
  503. ;; identify
  504. (unless (zerop (length password))
  505. (rcirc-send-string process (concat "PASS " password)))
  506. (rcirc-send-string process (concat "NICK " nick))
  507. (rcirc-send-string process (concat "USER " user-name
  508. " 0 * :" full-name))
  509. ;; setup ping timer if necessary
  510. (unless rcirc-keepalive-timer
  511. (setq rcirc-keepalive-timer
  512. (run-at-time 0 (/ rcirc-timeout-seconds 2) 'rcirc-keepalive)))
  513. (message "Connecting to %s...done" (or server-alias server))
  514. ;; return process object
  515. process)))
  516. (defmacro with-rcirc-process-buffer (process &rest body)
  517. (declare (indent 1) (debug t))
  518. `(with-current-buffer (process-buffer ,process)
  519. ,@body))
  520. (defmacro with-rcirc-server-buffer (&rest body)
  521. (declare (indent 0) (debug t))
  522. `(with-current-buffer rcirc-server-buffer
  523. ,@body))
  524. (define-obsolete-function-alias 'rcirc-float-time 'float-time "26.1")
  525. (defun rcirc-prompt-for-encryption (server-plist)
  526. "Prompt the user for the encryption method to use.
  527. SERVER-PLIST is the property list for the server."
  528. (let ((msg "Encryption (default %s): ")
  529. (choices '("plain" "tls"))
  530. (default (or (plist-get server-plist :encryption)
  531. 'plain)))
  532. (intern
  533. (completing-read (format msg default)
  534. choices nil t nil nil (symbol-name default)))))
  535. (defun rcirc-keepalive ()
  536. "Send keep alive pings to active rcirc processes.
  537. Kill processes that have not received a server message since the
  538. last ping."
  539. (if (rcirc-process-list)
  540. (mapc (lambda (process)
  541. (with-rcirc-process-buffer process
  542. (when (not rcirc-connecting)
  543. (rcirc-send-ctcp process
  544. rcirc-nick
  545. (format "KEEPALIVE %f"
  546. (float-time))))))
  547. (rcirc-process-list))
  548. ;; no processes, clean up timer
  549. (when (timerp rcirc-keepalive-timer)
  550. (cancel-timer rcirc-keepalive-timer))
  551. (setq rcirc-keepalive-timer nil)))
  552. (defun rcirc-handler-ctcp-KEEPALIVE (process _target _sender message)
  553. (with-rcirc-process-buffer process
  554. (setq header-line-format (format "%f" (- (float-time)
  555. (string-to-number message))))))
  556. (defvar rcirc-debug-buffer "*rcirc debug*")
  557. (defvar rcirc-debug-flag nil
  558. "If non-nil, write information to `rcirc-debug-buffer'.")
  559. (defun rcirc-debug (process text)
  560. "Add an entry to the debug log including PROCESS and TEXT.
  561. Debug text is written to `rcirc-debug-buffer' if `rcirc-debug-flag'
  562. is non-nil."
  563. (when rcirc-debug-flag
  564. (with-current-buffer (get-buffer-create rcirc-debug-buffer)
  565. (goto-char (point-max))
  566. (insert (concat
  567. "["
  568. (format-time-string "%Y-%m-%dT%T ") (process-name process)
  569. "] "
  570. text)))))
  571. (define-obsolete-variable-alias 'rcirc-sentinel-hooks
  572. 'rcirc-sentinel-functions "24.3")
  573. (defvar rcirc-sentinel-functions nil
  574. "Hook functions called when the process sentinel is called.
  575. Functions are called with PROCESS and SENTINEL arguments.")
  576. (defcustom rcirc-reconnect-delay 0
  577. "The minimum interval in seconds between reconnect attempts.
  578. When 0, do not auto-reconnect."
  579. :version "25.1"
  580. :type 'integer
  581. :group 'rcirc)
  582. (defvar rcirc-last-connect-time nil
  583. "The last time the buffer was connected.")
  584. (defun rcirc-sentinel (process sentinel)
  585. "Called when PROCESS receives SENTINEL."
  586. (let ((sentinel (replace-regexp-in-string "\n" "" sentinel)))
  587. (rcirc-debug process (format "SENTINEL: %S %S\n" process sentinel))
  588. (with-rcirc-process-buffer process
  589. (dolist (buffer (cons nil (mapcar 'cdr rcirc-buffer-alist)))
  590. (with-current-buffer (or buffer (current-buffer))
  591. (rcirc-print process "rcirc.el" "ERROR" rcirc-target
  592. (format "%s: %s (%S)"
  593. (process-name process)
  594. sentinel
  595. (process-status process))
  596. (not rcirc-target))
  597. (rcirc-disconnect-buffer)))
  598. (when (and (string= sentinel "deleted")
  599. (< 0 rcirc-reconnect-delay))
  600. (let ((now (current-time)))
  601. (when (or (null rcirc-last-connect-time)
  602. (< rcirc-reconnect-delay
  603. (float-time (time-subtract now rcirc-last-connect-time))))
  604. (setq rcirc-last-connect-time now)
  605. (rcirc-cmd-reconnect nil))))
  606. (run-hook-with-args 'rcirc-sentinel-functions process sentinel))))
  607. (defun rcirc-disconnect-buffer (&optional buffer)
  608. (with-current-buffer (or buffer (current-buffer))
  609. ;; set rcirc-target to nil for each channel so cleanup
  610. ;; doesn't happen when we reconnect
  611. (setq rcirc-target nil)
  612. (setq mode-line-process ":disconnected")))
  613. (defun rcirc-process-list ()
  614. "Return a list of rcirc processes."
  615. (let (ps)
  616. (mapc (lambda (p)
  617. (when (buffer-live-p (process-buffer p))
  618. (with-rcirc-process-buffer p
  619. (when (eq major-mode 'rcirc-mode)
  620. (setq ps (cons p ps))))))
  621. (process-list))
  622. ps))
  623. (define-obsolete-variable-alias 'rcirc-receive-message-hooks
  624. 'rcirc-receive-message-functions "24.3")
  625. (defvar rcirc-receive-message-functions nil
  626. "Hook functions run when a message is received from server.
  627. Function is called with PROCESS, COMMAND, SENDER, ARGS and LINE.")
  628. (defun rcirc-filter (process output)
  629. "Called when PROCESS receives OUTPUT."
  630. (rcirc-debug process output)
  631. (rcirc-reschedule-timeout process)
  632. (with-rcirc-process-buffer process
  633. (setq rcirc-last-server-message-time (current-time))
  634. (setq rcirc-process-output (concat rcirc-process-output output))
  635. (when (= (aref rcirc-process-output
  636. (1- (length rcirc-process-output))) ?\n)
  637. (mapc (lambda (line)
  638. (rcirc-process-server-response process line))
  639. (split-string rcirc-process-output "[\n\r]" t))
  640. (setq rcirc-process-output nil))))
  641. (defun rcirc-reschedule-timeout (process)
  642. (with-rcirc-process-buffer process
  643. (when (not rcirc-connecting)
  644. (with-rcirc-process-buffer process
  645. (when rcirc-timeout-timer (cancel-timer rcirc-timeout-timer))
  646. (setq rcirc-timeout-timer (run-at-time rcirc-timeout-seconds nil
  647. 'rcirc-delete-process
  648. process))))))
  649. (defun rcirc-delete-process (process)
  650. (delete-process process))
  651. (defvar rcirc-trap-errors-flag t)
  652. (defun rcirc-process-server-response (process text)
  653. (if rcirc-trap-errors-flag
  654. (condition-case err
  655. (rcirc-process-server-response-1 process text)
  656. (error
  657. (rcirc-print process "RCIRC" "ERROR" nil
  658. (format "\"%s\" %s" text err) t)))
  659. (rcirc-process-server-response-1 process text)))
  660. (defun rcirc-process-server-response-1 (process text)
  661. (if (string-match "^\\(:\\([^ ]+\\) \\)?\\([^ ]+\\) \\(.+\\)$" text)
  662. (let* ((user (match-string 2 text))
  663. (sender (rcirc-user-nick user))
  664. (cmd (match-string 3 text))
  665. (args (match-string 4 text))
  666. (handler (intern-soft (concat "rcirc-handler-" cmd))))
  667. (string-match "^\\([^:]*\\):?\\(.+\\)?$" args)
  668. (let* ((args1 (match-string 1 args))
  669. (args2 (match-string 2 args))
  670. (args (delq nil (append (split-string args1 " " t)
  671. (list args2)))))
  672. (if (not (fboundp handler))
  673. (rcirc-handler-generic process cmd sender args text)
  674. (funcall handler process sender args text))
  675. (run-hook-with-args 'rcirc-receive-message-functions
  676. process cmd sender args text)))
  677. (message "UNHANDLED: %s" text)))
  678. (defvar rcirc-responses-no-activity '("305" "306")
  679. "Responses that don't trigger activity in the mode-line indicator.")
  680. (defun rcirc-handler-generic (process response sender args _text)
  681. "Generic server response handler."
  682. (rcirc-print process sender response nil
  683. (mapconcat 'identity (cdr args) " ")
  684. (not (member response rcirc-responses-no-activity))))
  685. (defun rcirc--connection-open-p (process)
  686. (memq (process-status process) '(run open)))
  687. (defun rcirc-send-string (process string)
  688. "Send PROCESS a STRING plus a newline."
  689. (let ((string (concat (encode-coding-string string rcirc-encode-coding-system)
  690. "\n")))
  691. (unless (rcirc--connection-open-p process)
  692. (error "Network connection to %s is not open"
  693. (process-name process)))
  694. (rcirc-debug process string)
  695. (process-send-string process string)))
  696. (defun rcirc-send-privmsg (process target string)
  697. (rcirc-send-string process (format "PRIVMSG %s :%s" target string)))
  698. (defun rcirc-send-ctcp (process target request &optional args)
  699. (let ((args (if args (concat " " args) "")))
  700. (rcirc-send-privmsg process target
  701. (format "\C-a%s%s\C-a" request args))))
  702. (defun rcirc-buffer-process (&optional buffer)
  703. "Return the process associated with channel BUFFER.
  704. With no argument or nil as argument, use the current buffer."
  705. (let ((buffer (or buffer (and (buffer-live-p rcirc-server-buffer)
  706. rcirc-server-buffer))))
  707. (if buffer
  708. (with-current-buffer buffer rcirc-process)
  709. rcirc-process)))
  710. (defun rcirc-server-name (process)
  711. "Return PROCESS server name, given by the 001 response."
  712. (with-rcirc-process-buffer process
  713. (or rcirc-server-name
  714. (warn "server name for process %S unknown" process))))
  715. (defun rcirc-nick (process)
  716. "Return PROCESS nick."
  717. (with-rcirc-process-buffer process
  718. (or rcirc-nick rcirc-default-nick)))
  719. (defun rcirc-buffer-nick (&optional buffer)
  720. "Return the nick associated with BUFFER.
  721. With no argument or nil as argument, use the current buffer."
  722. (with-current-buffer (or buffer (current-buffer))
  723. (with-current-buffer rcirc-server-buffer
  724. (or rcirc-nick rcirc-default-nick))))
  725. (defvar rcirc-max-message-length 420
  726. "Messages longer than this value will be split.")
  727. (defun rcirc-split-message (message)
  728. "Split MESSAGE into chunks within `rcirc-max-message-length'."
  729. ;; `rcirc-encode-coding-system' can have buffer-local value.
  730. (let ((encoding rcirc-encode-coding-system))
  731. (with-temp-buffer
  732. (insert message)
  733. (goto-char (point-min))
  734. (let (result)
  735. (while (not (eobp))
  736. (goto-char (or (byte-to-position rcirc-max-message-length)
  737. (point-max)))
  738. ;; max message length is 512 including CRLF
  739. (while (and (not (bobp))
  740. (> (length (encode-coding-region
  741. (point-min) (point) encoding t))
  742. rcirc-max-message-length))
  743. (forward-char -1))
  744. (push (delete-and-extract-region (point-min) (point)) result))
  745. (nreverse result)))))
  746. (defun rcirc-send-message (process target message &optional noticep silent)
  747. "Send TARGET associated with PROCESS a privmsg with text MESSAGE.
  748. If NOTICEP is non-nil, send a notice instead of privmsg.
  749. If SILENT is non-nil, do not print the message in any irc buffer."
  750. (let ((response (if noticep "NOTICE" "PRIVMSG")))
  751. (rcirc-get-buffer-create process target)
  752. (dolist (msg (rcirc-split-message message))
  753. (rcirc-send-string process (concat response " " target " :" msg))
  754. (unless silent
  755. (rcirc-print process (rcirc-nick process) response target msg)))))
  756. (defvar rcirc-input-ring nil)
  757. (defvar rcirc-input-ring-index 0)
  758. (defun rcirc-prev-input-string (arg)
  759. (ring-ref rcirc-input-ring (+ rcirc-input-ring-index arg)))
  760. (defun rcirc-insert-prev-input ()
  761. (interactive)
  762. (when (<= rcirc-prompt-end-marker (point))
  763. (delete-region rcirc-prompt-end-marker (point-max))
  764. (insert (rcirc-prev-input-string 0))
  765. (setq rcirc-input-ring-index (1+ rcirc-input-ring-index))))
  766. (defun rcirc-insert-next-input ()
  767. (interactive)
  768. (when (<= rcirc-prompt-end-marker (point))
  769. (delete-region rcirc-prompt-end-marker (point-max))
  770. (setq rcirc-input-ring-index (1- rcirc-input-ring-index))
  771. (insert (rcirc-prev-input-string -1))))
  772. (defvar rcirc-server-commands
  773. '("/admin" "/away" "/connect" "/die" "/error" "/info"
  774. "/invite" "/ison" "/join" "/kick" "/kill" "/links"
  775. "/list" "/lusers" "/mode" "/motd" "/names" "/nick"
  776. "/notice" "/oper" "/part" "/pass" "/ping" "/pong"
  777. "/privmsg" "/quit" "/rehash" "/restart" "/service" "/servlist"
  778. "/server" "/squery" "/squit" "/stats" "/summon" "/time"
  779. "/topic" "/trace" "/user" "/userhost" "/users" "/version"
  780. "/wallops" "/who" "/whois" "/whowas")
  781. "A list of user commands by IRC server.
  782. The value defaults to RFCs 1459 and 2812.")
  783. ;; /me and /ctcp are not defined by `defun-rcirc-command'.
  784. (defvar rcirc-client-commands '("/me" "/ctcp")
  785. "A list of user commands defined by IRC client rcirc.
  786. The list is updated automatically by `defun-rcirc-command'.")
  787. (defun rcirc-completion-at-point ()
  788. "Function used for `completion-at-point-functions' in `rcirc-mode'."
  789. (and (rcirc-looking-at-input)
  790. (let* ((beg (save-excursion
  791. ;; On some networks it is common to message or
  792. ;; mention someone using @nick instead of just
  793. ;; nick.
  794. (if (re-search-backward "[[:space:]@]" rcirc-prompt-end-marker t)
  795. (1+ (point))
  796. rcirc-prompt-end-marker)))
  797. (table (if (and (= beg rcirc-prompt-end-marker)
  798. (eq (char-after beg) ?/))
  799. (delete-dups
  800. (nconc (sort (copy-sequence rcirc-client-commands)
  801. 'string-lessp)
  802. (sort (copy-sequence rcirc-server-commands)
  803. 'string-lessp)))
  804. (rcirc-channel-nicks (rcirc-buffer-process)
  805. rcirc-target))))
  806. (list beg (point) table))))
  807. (defvar rcirc-completions nil)
  808. (defvar rcirc-completion-start nil)
  809. (defun rcirc-complete ()
  810. "Cycle through completions from list of nicks in channel or IRC commands.
  811. IRC command completion is performed only if `/' is the first input char."
  812. (interactive)
  813. (unless (rcirc-looking-at-input)
  814. (error "Point not located after rcirc prompt"))
  815. (if (eq last-command this-command)
  816. (setq rcirc-completions
  817. (append (cdr rcirc-completions) (list (car rcirc-completions))))
  818. (let ((completion-ignore-case t)
  819. (table (rcirc-completion-at-point)))
  820. (setq rcirc-completion-start (car table))
  821. (setq rcirc-completions
  822. (and rcirc-completion-start
  823. (all-completions (buffer-substring rcirc-completion-start
  824. (cadr table))
  825. (nth 2 table))))))
  826. (let ((completion (car rcirc-completions)))
  827. (when completion
  828. (delete-region rcirc-completion-start (point))
  829. (insert
  830. (cond
  831. ((= (aref completion 0) ?/) (concat completion " "))
  832. ((= rcirc-completion-start rcirc-prompt-end-marker)
  833. (format rcirc-nick-completion-format completion))
  834. (t completion))))))
  835. (defun set-rcirc-decode-coding-system (coding-system)
  836. "Set the decode coding system used in this channel."
  837. (interactive "zCoding system for incoming messages: ")
  838. (setq-local rcirc-decode-coding-system coding-system))
  839. (defun set-rcirc-encode-coding-system (coding-system)
  840. "Set the encode coding system used in this channel."
  841. (interactive "zCoding system for outgoing messages: ")
  842. (setq-local rcirc-encode-coding-system coding-system))
  843. (defvar rcirc-mode-map
  844. (let ((map (make-sparse-keymap)))
  845. (define-key map (kbd "RET") 'rcirc-send-input)
  846. (define-key map (kbd "M-p") 'rcirc-insert-prev-input)
  847. (define-key map (kbd "M-n") 'rcirc-insert-next-input)
  848. (define-key map (kbd "TAB") 'rcirc-complete)
  849. (define-key map (kbd "C-c C-b") 'rcirc-browse-url)
  850. (define-key map (kbd "C-c C-c") 'rcirc-edit-multiline)
  851. (define-key map (kbd "C-c C-j") 'rcirc-cmd-join)
  852. (define-key map (kbd "C-c C-k") 'rcirc-cmd-kick)
  853. (define-key map (kbd "C-c C-l") 'rcirc-toggle-low-priority)
  854. (define-key map (kbd "C-c C-d") 'rcirc-cmd-mode)
  855. (define-key map (kbd "C-c C-m") 'rcirc-cmd-msg)
  856. (define-key map (kbd "C-c C-r") 'rcirc-cmd-nick) ; rename
  857. (define-key map (kbd "C-c C-o") 'rcirc-omit-mode)
  858. (define-key map (kbd "C-c C-p") 'rcirc-cmd-part)
  859. (define-key map (kbd "C-c C-q") 'rcirc-cmd-query)
  860. (define-key map (kbd "C-c C-t") 'rcirc-cmd-topic)
  861. (define-key map (kbd "C-c C-n") 'rcirc-cmd-names)
  862. (define-key map (kbd "C-c C-w") 'rcirc-cmd-whois)
  863. (define-key map (kbd "C-c C-x") 'rcirc-cmd-quit)
  864. (define-key map (kbd "C-c TAB") ; C-i
  865. 'rcirc-toggle-ignore-buffer-activity)
  866. (define-key map (kbd "C-c C-s") 'rcirc-switch-to-server-buffer)
  867. (define-key map (kbd "C-c C-a") 'rcirc-jump-to-first-unread-line)
  868. map)
  869. "Keymap for rcirc mode.")
  870. (defvar rcirc-short-buffer-name nil
  871. "Generated abbreviation to use to indicate buffer activity.")
  872. (defvar rcirc-mode-hook nil
  873. "Hook run when setting up rcirc buffer.")
  874. (defvar rcirc-last-post-time nil)
  875. (defvar rcirc-log-alist nil
  876. "Alist of lines to log to disk when `rcirc-log-flag' is non-nil.
  877. Each element looks like (FILENAME . TEXT).")
  878. (defvar rcirc-current-line 0
  879. "The current number of responses printed in this channel.
  880. This number is independent of the number of lines in the buffer.")
  881. (defun rcirc-mode (process target)
  882. ;; FIXME: Use define-derived-mode.
  883. "Major mode for IRC channel buffers.
  884. \\{rcirc-mode-map}"
  885. (kill-all-local-variables)
  886. (use-local-map rcirc-mode-map)
  887. (setq mode-name "rcirc")
  888. (setq major-mode 'rcirc-mode)
  889. (setq mode-line-process nil)
  890. (setq-local rcirc-input-ring
  891. ;; If rcirc-input-ring is already a ring with desired
  892. ;; size do not re-initialize.
  893. (if (and (ring-p rcirc-input-ring)
  894. (= (ring-size rcirc-input-ring)
  895. rcirc-input-ring-size))
  896. rcirc-input-ring
  897. (make-ring rcirc-input-ring-size)))
  898. (setq-local rcirc-server-buffer (process-buffer process))
  899. (setq-local rcirc-target target)
  900. (setq-local rcirc-topic nil)
  901. (setq-local rcirc-last-post-time (current-time))
  902. (setq-local fill-paragraph-function 'rcirc-fill-paragraph)
  903. (setq-local rcirc-recent-quit-alist nil)
  904. (setq-local rcirc-current-line 0)
  905. (setq-local rcirc-last-connect-time (current-time))
  906. (use-hard-newlines t)
  907. (setq-local rcirc-short-buffer-name nil)
  908. (setq-local rcirc-urls nil)
  909. ;; setup for omitting responses
  910. (setq buffer-invisibility-spec '())
  911. (setq buffer-display-table (make-display-table))
  912. (set-display-table-slot buffer-display-table 4
  913. (let ((glyph (make-glyph-code
  914. ?. 'font-lock-keyword-face)))
  915. (make-vector 3 glyph)))
  916. (dolist (i rcirc-coding-system-alist)
  917. (let ((chan (if (consp (car i)) (caar i) (car i)))
  918. (serv (if (consp (car i)) (cdar i) "")))
  919. (when (and (string-match chan (or target ""))
  920. (string-match serv (rcirc-server-name process)))
  921. (setq-local rcirc-decode-coding-system
  922. (if (consp (cdr i)) (cadr i) (cdr i)))
  923. (setq-local rcirc-encode-coding-system
  924. (if (consp (cdr i)) (cddr i) (cdr i))))))
  925. ;; setup the prompt and markers
  926. (setq-local rcirc-prompt-start-marker (point-max-marker))
  927. (setq-local rcirc-prompt-end-marker (point-max-marker))
  928. (rcirc-update-prompt)
  929. (goto-char rcirc-prompt-end-marker)
  930. (setq-local overlay-arrow-position (make-marker))
  931. ;; if the user changes the major mode or kills the buffer, there is
  932. ;; cleanup work to do
  933. (add-hook 'change-major-mode-hook 'rcirc-change-major-mode-hook nil t)
  934. (add-hook 'kill-buffer-hook 'rcirc-kill-buffer-hook nil t)
  935. ;; add to buffer list, and update buffer abbrevs
  936. (when target ; skip server buffer
  937. (let ((buffer (current-buffer)))
  938. (with-rcirc-process-buffer process
  939. (setq rcirc-buffer-alist (cons (cons target buffer)
  940. rcirc-buffer-alist))))
  941. (rcirc-update-short-buffer-names))
  942. (add-hook 'completion-at-point-functions
  943. 'rcirc-completion-at-point nil 'local)
  944. (run-mode-hooks 'rcirc-mode-hook))
  945. (defun rcirc-update-prompt (&optional all)
  946. "Reset the prompt string in the current buffer.
  947. If ALL is non-nil, update prompts in all IRC buffers."
  948. (if all
  949. (mapc (lambda (process)
  950. (mapc (lambda (buffer)
  951. (with-current-buffer buffer
  952. (rcirc-update-prompt)))
  953. (with-rcirc-process-buffer process
  954. (mapcar 'cdr rcirc-buffer-alist))))
  955. (rcirc-process-list))
  956. (let ((inhibit-read-only t)
  957. (prompt (or rcirc-prompt "")))
  958. (mapc (lambda (rep)
  959. (setq prompt
  960. (replace-regexp-in-string (car rep) (cdr rep) prompt)))
  961. (list (cons "%n" (rcirc-buffer-nick))
  962. (cons "%s" (with-rcirc-server-buffer rcirc-server-name))
  963. (cons "%t" (or rcirc-target ""))))
  964. (save-excursion
  965. (delete-region rcirc-prompt-start-marker rcirc-prompt-end-marker)
  966. (goto-char rcirc-prompt-start-marker)
  967. (let ((start (point)))
  968. (insert-before-markers prompt)
  969. (set-marker rcirc-prompt-start-marker start)
  970. (when (not (zerop (- rcirc-prompt-end-marker
  971. rcirc-prompt-start-marker)))
  972. (add-text-properties rcirc-prompt-start-marker
  973. rcirc-prompt-end-marker
  974. (list 'face 'rcirc-prompt
  975. 'read-only t 'field t
  976. 'front-sticky t 'rear-nonsticky t))))))))
  977. (defun rcirc-set-changed (option value)
  978. "Set OPTION to VALUE and do updates after a customization change."
  979. (set-default option value)
  980. (cond ((eq option 'rcirc-prompt)
  981. (rcirc-update-prompt 'all))
  982. (t
  983. (error "Bad option %s" option))))
  984. (defun rcirc-channel-p (target)
  985. "Return t if TARGET is a channel name."
  986. (and target
  987. (not (zerop (length target)))
  988. (or (eq (aref target 0) ?#)
  989. (eq (aref target 0) ?&))))
  990. (defcustom rcirc-log-directory "~/.emacs.d/rcirc-log"
  991. "Directory to keep IRC logfiles."
  992. :type 'directory
  993. :group 'rcirc)
  994. (defcustom rcirc-log-flag nil
  995. "Non-nil means log IRC activity to disk.
  996. Logfiles are kept in `rcirc-log-directory'."
  997. :type 'boolean
  998. :group 'rcirc)
  999. (defun rcirc-kill-buffer-hook ()
  1000. "Part the channel when killing an rcirc buffer.
  1001. If `rcirc-kill-channel-buffers' is non-nil and the killed buffer
  1002. is a server buffer, kills all of the channel buffers associated
  1003. with it."
  1004. (when (eq major-mode 'rcirc-mode)
  1005. (when (and rcirc-log-flag
  1006. rcirc-log-directory)
  1007. (rcirc-log-write))
  1008. (rcirc-clean-up-buffer "Killed buffer")
  1009. (when (and rcirc-buffer-alist ;; it's a server buffer
  1010. rcirc-kill-channel-buffers)
  1011. (dolist (channel rcirc-buffer-alist)
  1012. (kill-buffer (cdr channel))))))
  1013. (defun rcirc-change-major-mode-hook ()
  1014. "Part the channel when changing the major-mode."
  1015. (rcirc-clean-up-buffer "Changed major mode"))
  1016. (defun rcirc-clean-up-buffer (reason)
  1017. (let ((buffer (current-buffer)))
  1018. (rcirc-clear-activity buffer)
  1019. (when (and (rcirc-buffer-process)
  1020. (rcirc--connection-open-p (rcirc-buffer-process)))
  1021. (with-rcirc-server-buffer
  1022. (setq rcirc-buffer-alist
  1023. (rassq-delete-all buffer rcirc-buffer-alist)))
  1024. (rcirc-update-short-buffer-names)
  1025. (if (rcirc-channel-p rcirc-target)
  1026. (rcirc-send-string (rcirc-buffer-process)
  1027. (concat "PART " rcirc-target " :" reason))
  1028. (when rcirc-target
  1029. (rcirc-remove-nick-channel (rcirc-buffer-process)
  1030. (rcirc-buffer-nick)
  1031. rcirc-target))))
  1032. (setq rcirc-target nil)))
  1033. (defun rcirc-generate-new-buffer-name (process target)
  1034. "Return a buffer name based on PROCESS and TARGET.
  1035. This is used for the initial name given to IRC buffers."
  1036. (substring-no-properties
  1037. (if target
  1038. (concat target "@" (process-name process))
  1039. (concat "*" (process-name process) "*"))))
  1040. (defun rcirc-get-buffer (process target &optional server)
  1041. "Return the buffer associated with the PROCESS and TARGET.
  1042. If optional argument SERVER is non-nil, return the server buffer
  1043. if there is no existing buffer for TARGET, otherwise return nil."
  1044. (with-rcirc-process-buffer process
  1045. (if (null target)
  1046. (current-buffer)
  1047. (let ((buffer (cdr (assoc-string target rcirc-buffer-alist t))))
  1048. (or buffer (when server (current-buffer)))))))
  1049. (defun rcirc-get-buffer-create (process target)
  1050. "Return the buffer associated with the PROCESS and TARGET.
  1051. Create the buffer if it doesn't exist."
  1052. (let ((buffer (rcirc-get-buffer process target)))
  1053. (if (and buffer (buffer-live-p buffer))
  1054. (with-current-buffer buffer
  1055. (when (not rcirc-target)
  1056. (setq rcirc-target target))
  1057. buffer)
  1058. ;; create the buffer
  1059. (with-rcirc-process-buffer process
  1060. (let ((new-buffer (get-buffer-create
  1061. (rcirc-generate-new-buffer-name process target))))
  1062. (with-current-buffer new-buffer
  1063. (rcirc-mode process target)
  1064. (rcirc-put-nick-channel process (rcirc-nick process) target
  1065. rcirc-current-line))
  1066. new-buffer)))))
  1067. (defun rcirc-send-input ()
  1068. "Send input to target associated with the current buffer."
  1069. (interactive)
  1070. (if (< (point) rcirc-prompt-end-marker)
  1071. ;; copy the line down to the input area
  1072. (progn
  1073. (forward-line 0)
  1074. (let ((start (if (eq (point) (point-min))
  1075. (point)
  1076. (if (get-text-property (1- (point)) 'hard)
  1077. (point)
  1078. (previous-single-property-change (point) 'hard))))
  1079. (end (next-single-property-change (1+ (point)) 'hard)))
  1080. (goto-char (point-max))
  1081. (insert (replace-regexp-in-string
  1082. "\n\\s-+" " "
  1083. (buffer-substring-no-properties start end)))))
  1084. ;; process input
  1085. (goto-char (point-max))
  1086. (when (not (equal 0 (- (point) rcirc-prompt-end-marker)))
  1087. ;; delete a trailing newline
  1088. (when (eq (point) (point-at-bol))
  1089. (delete-char -1))
  1090. (let ((input (buffer-substring-no-properties
  1091. rcirc-prompt-end-marker (point))))
  1092. (dolist (line (split-string input "\n"))
  1093. (rcirc-process-input-line line))
  1094. ;; add to input-ring
  1095. (save-excursion
  1096. (ring-insert rcirc-input-ring input)
  1097. (setq rcirc-input-ring-index 0))))))
  1098. (defun rcirc-fill-paragraph (&optional justify)
  1099. (interactive "P")
  1100. (when (> (point) rcirc-prompt-end-marker)
  1101. (save-restriction
  1102. (narrow-to-region rcirc-prompt-end-marker (point-max))
  1103. (let ((fill-column rcirc-max-message-length))
  1104. (fill-region (point-min) (point-max) justify)))))
  1105. (defun rcirc-process-input-line (line)
  1106. (if (string-match "^/\\([^ ]+\\) ?\\(.*\\)$" line)
  1107. (rcirc-process-command (match-string 1 line)
  1108. (match-string 2 line)
  1109. line)
  1110. (rcirc-process-message line)))
  1111. (defun rcirc-process-message (line)
  1112. (if (not rcirc-target)
  1113. (message "Not joined (no target)")
  1114. (delete-region rcirc-prompt-end-marker (point))
  1115. (rcirc-send-message (rcirc-buffer-process) rcirc-target line)
  1116. (setq rcirc-last-post-time (current-time))))
  1117. (defun rcirc-process-command (command args line)
  1118. (if (eq (aref command 0) ?/)
  1119. ;; "//text" will send "/text" as a message
  1120. (rcirc-process-message (substring line 1))
  1121. (let ((fun (intern-soft (concat "rcirc-cmd-" command)))
  1122. (process (rcirc-buffer-process)))
  1123. (newline)
  1124. (with-current-buffer (current-buffer)
  1125. (delete-region rcirc-prompt-end-marker (point))
  1126. (if (string= command "me")
  1127. (rcirc-print process (rcirc-buffer-nick)
  1128. "ACTION" rcirc-target args)
  1129. (rcirc-print process (rcirc-buffer-nick)
  1130. "COMMAND" rcirc-target line))
  1131. (set-marker rcirc-prompt-end-marker (point))
  1132. (if (fboundp fun)
  1133. (funcall fun args process rcirc-target)
  1134. (rcirc-send-string process
  1135. (concat command " :" args)))))))
  1136. (defvar rcirc-parent-buffer nil)
  1137. (make-variable-buffer-local 'rcirc-parent-buffer)
  1138. (put 'rcirc-parent-buffer 'permanent-local t)
  1139. (defvar rcirc-window-configuration nil)
  1140. (defun rcirc-edit-multiline ()
  1141. "Move current edit to a dedicated buffer."
  1142. (interactive)
  1143. (let ((pos (1+ (- (point) rcirc-prompt-end-marker))))
  1144. (goto-char (point-max))
  1145. (let ((text (buffer-substring-no-properties rcirc-prompt-end-marker
  1146. (point)))
  1147. (parent (buffer-name)))
  1148. (delete-region rcirc-prompt-end-marker (point))
  1149. (setq rcirc-window-configuration (current-window-configuration))
  1150. (pop-to-buffer (concat "*multiline " parent "*"))
  1151. (funcall rcirc-multiline-major-mode)
  1152. (rcirc-multiline-minor-mode 1)
  1153. (setq rcirc-parent-buffer parent)
  1154. (insert text)
  1155. (and (> pos 0) (goto-char pos))
  1156. (message "Type C-c C-c to return text to %s, or C-c C-k to cancel" parent))))
  1157. (defvar rcirc-multiline-minor-mode-map
  1158. (let ((map (make-sparse-keymap)))
  1159. (define-key map (kbd "C-c C-c") 'rcirc-multiline-minor-submit)
  1160. (define-key map (kbd "C-x C-s") 'rcirc-multiline-minor-submit)
  1161. (define-key map (kbd "C-c C-k") 'rcirc-multiline-minor-cancel)
  1162. (define-key map (kbd "ESC ESC ESC") 'rcirc-multiline-minor-cancel)
  1163. map)
  1164. "Keymap for multiline mode in rcirc.")
  1165. (define-minor-mode rcirc-multiline-minor-mode
  1166. "Minor mode for editing multiple lines in rcirc.
  1167. With a prefix argument ARG, enable the mode if ARG is positive,
  1168. and disable it otherwise. If called from Lisp, enable the mode
  1169. if ARG is omitted or nil."
  1170. :init-value nil
  1171. :lighter " rcirc-mline"
  1172. :keymap rcirc-multiline-minor-mode-map
  1173. :global nil
  1174. :group 'rcirc
  1175. (setq fill-column rcirc-max-message-length))
  1176. (defun rcirc-multiline-minor-submit ()
  1177. "Send the text in buffer back to parent buffer."
  1178. (interactive)
  1179. (untabify (point-min) (point-max))
  1180. (let ((text (buffer-substring (point-min) (point-max)))
  1181. (buffer (current-buffer))
  1182. (pos (point)))
  1183. (set-buffer rcirc-parent-buffer)
  1184. (goto-char (point-max))
  1185. (insert text)
  1186. (kill-buffer buffer)
  1187. (set-window-configuration rcirc-window-configuration)
  1188. (goto-char (+ rcirc-prompt-end-marker (1- pos)))))
  1189. (defun rcirc-multiline-minor-cancel ()
  1190. "Cancel the multiline edit."
  1191. (interactive)
  1192. (kill-buffer (current-buffer))
  1193. (set-window-configuration rcirc-window-configuration))
  1194. (defun rcirc-any-buffer (process)
  1195. "Return a buffer for PROCESS, either the one selected or the process buffer."
  1196. (if rcirc-always-use-server-buffer-flag
  1197. (process-buffer process)
  1198. (let ((buffer (window-buffer)))
  1199. (if (and buffer
  1200. (with-current-buffer buffer
  1201. (and (eq major-mode 'rcirc-mode)
  1202. (eq (rcirc-buffer-process) process))))
  1203. buffer
  1204. (process-buffer process)))))
  1205. (defcustom rcirc-response-formats
  1206. '(("PRIVMSG" . "<%N> %m")
  1207. ("NOTICE" . "-%N- %m")
  1208. ("ACTION" . "[%N %m]")
  1209. ("COMMAND" . "%m")
  1210. ("ERROR" . "%fw!!! %m")
  1211. (t . "%fp*** %fs%n %r %m"))
  1212. "An alist of formats used for printing responses.
  1213. The format is looked up using the response-type as a key;
  1214. if no match is found, the default entry (with a key of t) is used.
  1215. The entry's value part should be a string, which is inserted with
  1216. the of the following escape sequences replaced by the described values:
  1217. %m The message text
  1218. %n The sender's nick
  1219. %N The sender's nick (with face `rcirc-my-nick' or `rcirc-other-nick')
  1220. %r The response-type
  1221. %t The target
  1222. %fw Following text uses the face `font-lock-warning-face'
  1223. %fp Following text uses the face `rcirc-server-prefix'
  1224. %fs Following text uses the face `rcirc-server'
  1225. %f[FACE] Following text uses the face FACE
  1226. %f- Following text uses the default face
  1227. %% A literal `%' character"
  1228. :type '(alist :key-type (choice (string :tag "Type")
  1229. (const :tag "Default" t))
  1230. :value-type string)
  1231. :group 'rcirc)
  1232. (defun rcirc-format-response-string (process sender response target text)
  1233. "Return a nicely-formatted response string, incorporating TEXT
  1234. \(and perhaps other arguments). The specific formatting used
  1235. is found by looking up RESPONSE in `rcirc-response-formats'."
  1236. (with-temp-buffer
  1237. (insert (or (cdr (assoc response rcirc-response-formats))
  1238. (cdr (assq t rcirc-response-formats))))
  1239. (goto-char (point-min))
  1240. (let ((start (point-min))
  1241. (sender (if (or (not sender)
  1242. (string= (rcirc-server-name process) sender))
  1243. ""
  1244. sender))
  1245. face)
  1246. (while (re-search-forward "%\\(\\(f\\(.\\)\\)\\|\\(.\\)\\)" nil t)
  1247. (rcirc-add-face start (match-beginning 0) face)
  1248. (setq start (match-beginning 0))
  1249. (replace-match
  1250. (cl-case (aref (match-string 1) 0)
  1251. (?f (setq face
  1252. (cl-case (string-to-char (match-string 3))
  1253. (?w 'font-lock-warning-face)
  1254. (?p 'rcirc-server-prefix)
  1255. (?s 'rcirc-server)
  1256. (t nil)))
  1257. "")
  1258. (?n sender)
  1259. (?N (let ((my-nick (rcirc-nick process)))
  1260. (save-match-data
  1261. (with-syntax-table rcirc-nick-syntax-table
  1262. (rcirc-facify sender
  1263. (cond ((string= sender my-nick)
  1264. 'rcirc-my-nick)
  1265. ((and rcirc-bright-nicks
  1266. (string-match
  1267. (regexp-opt rcirc-bright-nicks
  1268. 'words)
  1269. sender))
  1270. 'rcirc-bright-nick)
  1271. ((and rcirc-dim-nicks
  1272. (string-match
  1273. (regexp-opt rcirc-dim-nicks
  1274. 'words)
  1275. sender))
  1276. 'rcirc-dim-nick)
  1277. (t
  1278. 'rcirc-other-nick)))))))
  1279. (?m (propertize text 'rcirc-text text))
  1280. (?r response)
  1281. (?t (or target ""))
  1282. (t (concat "UNKNOWN CODE:" (match-string 0))))
  1283. t t nil 0)
  1284. (rcirc-add-face (match-beginning 0) (match-end 0) face))
  1285. (rcirc-add-face start (match-beginning 0) face))
  1286. (buffer-substring (point-min) (point-max))))
  1287. (defun rcirc-target-buffer (process sender response target _text)
  1288. "Return a buffer to print the server response."
  1289. (cl-assert (not (bufferp target)))
  1290. (with-rcirc-process-buffer process
  1291. (cond ((not target)
  1292. (rcirc-any-buffer process))
  1293. ((not (rcirc-channel-p target))
  1294. ;; message from another user
  1295. (if (or (string= response "PRIVMSG")
  1296. (string= response "ACTION"))
  1297. (rcirc-get-buffer-create process (if (string= sender rcirc-nick)
  1298. target
  1299. sender))
  1300. (rcirc-get-buffer process target t)))
  1301. ((or (rcirc-get-buffer process target)
  1302. (rcirc-any-buffer process))))))
  1303. (defvar rcirc-activity-types nil)
  1304. (make-variable-buffer-local 'rcirc-activity-types)
  1305. (defvar rcirc-last-sender nil)
  1306. (make-variable-buffer-local 'rcirc-last-sender)
  1307. (defcustom rcirc-omit-threshold 100
  1308. "Number of lines since last activity from a nick before `rcirc-omit-responses' are omitted."
  1309. :type 'integer
  1310. :group 'rcirc)
  1311. (defcustom rcirc-log-process-buffers nil
  1312. "Non-nil if rcirc process buffers should be logged to disk."
  1313. :group 'rcirc
  1314. :type 'boolean
  1315. :version "24.1")
  1316. (defun rcirc-last-quit-line (process nick target)
  1317. "Return the line number where NICK left TARGET.
  1318. Returns nil if the information is not recorded."
  1319. (let ((chanbuf (rcirc-get-buffer process target)))
  1320. (when chanbuf
  1321. (cdr (assoc-string nick (with-current-buffer chanbuf
  1322. rcirc-recent-quit-alist))))))
  1323. (defun rcirc-last-line (process nick target)
  1324. "Return the line from the last activity from NICK in TARGET."
  1325. (let ((line (or (cdr (assoc-string target
  1326. (gethash nick (with-rcirc-server-buffer
  1327. rcirc-nick-table)) t))
  1328. (rcirc-last-quit-line process nick target))))
  1329. (if line
  1330. line
  1331. ;;(message "line is nil for %s in %s" nick target)
  1332. nil)))
  1333. (defun rcirc-elapsed-lines (process nick target)
  1334. "Return the number of lines since activity from NICK in TARGET."
  1335. (let ((last-activity-line (rcirc-last-line process nick target)))
  1336. (when (and last-activity-line
  1337. (> last-activity-line 0))
  1338. (- rcirc-current-line last-activity-line))))
  1339. (defvar rcirc-markup-text-functions
  1340. '(rcirc-markup-attributes
  1341. rcirc-markup-my-nick
  1342. rcirc-markup-urls
  1343. rcirc-markup-keywords
  1344. rcirc-markup-bright-nicks)
  1345. "List of functions used to manipulate text before it is printed.
  1346. Each function takes two arguments, SENDER, and RESPONSE. The
  1347. buffer is narrowed with the text to be printed and the point is
  1348. at the beginning of the `rcirc-text' propertized text.")
  1349. (defun rcirc-print (process sender response target text &optional activity)
  1350. "Print TEXT in the buffer associated with TARGET.
  1351. Format based on SENDER and RESPONSE. If ACTIVITY is non-nil,
  1352. record activity."
  1353. (or text (setq text ""))
  1354. (unless (and (or (member sender rcirc-ignore-list)
  1355. (member (with-syntax-table rcirc-nick-syntax-table
  1356. (when (string-match "^\\([^/]\\w*\\)[:,]" text)
  1357. (match-string 1 text)))
  1358. rcirc-ignore-list))
  1359. ;; do not ignore if we sent the message
  1360. (not (string= sender (rcirc-nick process))))
  1361. (let* ((buffer (rcirc-target-buffer process sender response target text))
  1362. (inhibit-read-only t))
  1363. (with-current-buffer buffer
  1364. (let ((moving (= (point) rcirc-prompt-end-marker))
  1365. (old-point (point-marker))
  1366. (fill-start (marker-position rcirc-prompt-start-marker)))
  1367. (setq text (decode-coding-string text rcirc-decode-coding-system))
  1368. (unless (string= sender (rcirc-nick process))
  1369. ;; mark the line with overlay arrow
  1370. (unless (or (marker-position overlay-arrow-position)
  1371. (get-buffer-window (current-buffer))
  1372. (member response rcirc-omit-responses))
  1373. (set-marker overlay-arrow-position
  1374. (marker-position rcirc-prompt-start-marker))))
  1375. ;; temporarily set the marker insertion-type because
  1376. ;; insert-before-markers results in hidden text in new buffers
  1377. (goto-char rcirc-prompt-start-marker)
  1378. (set-marker-insertion-type rcirc-prompt-start-marker t)
  1379. (set-marker-insertion-type rcirc-prompt-end-marker t)
  1380. (let ((start (point)))
  1381. (insert (rcirc-format-response-string process sender response nil
  1382. text)
  1383. (propertize "\n" 'hard t))
  1384. ;; squeeze spaces out of text before rcirc-text
  1385. (fill-region fill-start
  1386. (1- (or (next-single-property-change fill-start
  1387. 'rcirc-text)
  1388. rcirc-prompt-end-marker)))
  1389. ;; run markup functions
  1390. (save-excursion
  1391. (save-restriction
  1392. (narrow-to-region start rcirc-prompt-start-marker)
  1393. (goto-char (or (next-single-property-change start 'rcirc-text)
  1394. (point)))
  1395. (when (rcirc-buffer-process)
  1396. (save-excursion (rcirc-markup-timestamp sender response))
  1397. (dolist (fn rcirc-markup-text-functions)
  1398. (save-excursion (funcall fn sender response)))
  1399. (when rcirc-fill-flag
  1400. (save-excursion (rcirc-markup-fill sender response))))
  1401. (when rcirc-read-only-flag
  1402. (add-text-properties (point-min) (point-max)
  1403. '(read-only t front-sticky t))))
  1404. ;; make text omittable
  1405. (let ((last-activity-lines (rcirc-elapsed-lines process sender target)))
  1406. (if (and (not (string= (rcirc-nick process) sender))
  1407. (member response rcirc-omit-responses)
  1408. (or (not last-activity-lines)
  1409. (< rcirc-omit-threshold last-activity-lines)))
  1410. (put-text-property (1- start) (1- rcirc-prompt-start-marker)
  1411. 'invisible 'rcirc-omit)
  1412. ;; otherwise increment the line count
  1413. (setq rcirc-current-line (1+ rcirc-current-line))))))
  1414. (set-marker-insertion-type rcirc-prompt-start-marker nil)
  1415. (set-marker-insertion-type rcirc-prompt-end-marker nil)
  1416. ;; truncate buffer if it is very long
  1417. (save-excursion
  1418. (when (and rcirc-buffer-maximum-lines
  1419. (> rcirc-buffer-maximum-lines 0)
  1420. (= (forward-line (- rcirc-buffer-maximum-lines)) 0))
  1421. (delete-region (point-min) (point))))
  1422. ;; set the window point for buffers show in windows
  1423. (walk-windows (lambda (w)
  1424. (when (and (not (eq (selected-window) w))
  1425. (eq (current-buffer)
  1426. (window-buffer w))
  1427. (>= (window-point w)
  1428. rcirc-prompt-end-marker))
  1429. (set-window-point w (point-max))))
  1430. nil t)
  1431. ;; restore the point
  1432. (goto-char (if moving rcirc-prompt-end-marker old-point))
  1433. ;; keep window on bottom line if it was already there
  1434. (when rcirc-scroll-show-maximum-output
  1435. (let ((window (get-buffer-window)))
  1436. (when window
  1437. (with-selected-window window
  1438. (when (eq major-mode 'rcirc-mode)
  1439. (when (<= (- (window-height)
  1440. (count-screen-lines (window-point)
  1441. (window-start))
  1442. 1)
  1443. 0)
  1444. (recenter -1)))))))
  1445. ;; flush undo (can we do something smarter here?)
  1446. (buffer-disable-undo)
  1447. (buffer-enable-undo))
  1448. ;; record mode line activity
  1449. (when (and activity
  1450. (not rcirc-ignore-buffer-activity-flag)
  1451. (not (and rcirc-dim-nicks sender
  1452. (string-match (regexp-opt rcirc-dim-nicks) sender)
  1453. (rcirc-channel-p target))))
  1454. (rcirc-record-activity (current-buffer)
  1455. (when (not (rcirc-channel-p rcirc-target))
  1456. 'nick)))
  1457. (when (and rcirc-log-flag
  1458. (or target
  1459. rcirc-log-process-buffers))
  1460. (rcirc-log process sender response target text))
  1461. (sit-for 0) ; displayed text before hook
  1462. (run-hook-with-args 'rcirc-print-functions
  1463. process sender response target text)))))
  1464. (defun rcirc-generate-log-filename (process target)
  1465. (if target
  1466. (rcirc-generate-new-buffer-name process target)
  1467. (process-name process)))
  1468. (defcustom rcirc-log-filename-function 'rcirc-generate-log-filename
  1469. "A function to generate the filename used by rcirc's logging facility.
  1470. It is called with two arguments, PROCESS and TARGET (see
  1471. `rcirc-generate-new-buffer-name' for their meaning), and should
  1472. return the filename, or nil if no logging is desired for this
  1473. session.
  1474. If the returned filename is absolute (`file-name-absolute-p'
  1475. returns t), then it is used as-is, otherwise the resulting file
  1476. is put into `rcirc-log-directory'.
  1477. The filename is then cleaned using `convert-standard-filename' to
  1478. guarantee valid filenames for the current OS."
  1479. :group 'rcirc
  1480. :type 'function)
  1481. (defun rcirc-log (process sender response target text)
  1482. "Record line in `rcirc-log', to be later written to disk."
  1483. (let ((filename (funcall rcirc-log-filename-function process target)))
  1484. (unless (null filename)
  1485. (let ((cell (assoc-string filename rcirc-log-alist))
  1486. (line (concat (format-time-string rcirc-time-format)
  1487. (substring-no-properties
  1488. (rcirc-format-response-string process sender
  1489. response target text))
  1490. "\n")))
  1491. (if cell
  1492. (setcdr cell (concat (cdr cell) line))
  1493. (setq rcirc-log-alist
  1494. (cons (cons filename line) rcirc-log-alist)))))))
  1495. (defun rcirc-log-write ()
  1496. "Flush `rcirc-log-alist' data to disk.
  1497. Log data is written to `rcirc-log-directory', except for
  1498. log-files with absolute names (see `rcirc-log-filename-function')."
  1499. (dolist (cell rcirc-log-alist)
  1500. (let ((filename (convert-standard-filename
  1501. (expand-file-name (car cell)
  1502. rcirc-log-directory)))
  1503. (coding-system-for-write 'utf-8))
  1504. (make-directory (file-name-directory filename) t)
  1505. (with-temp-buffer
  1506. (insert (cdr cell))
  1507. (write-region (point-min) (point-max) filename t 'quiet))))
  1508. (setq rcirc-log-alist nil))
  1509. (defun rcirc-view-log-file ()
  1510. "View logfile corresponding to the current buffer."
  1511. (interactive)
  1512. (find-file-other-window
  1513. (expand-file-name (funcall rcirc-log-filename-function
  1514. (rcirc-buffer-process) rcirc-target)
  1515. rcirc-log-directory)))
  1516. (defun rcirc-join-channels (process channels)
  1517. "Join CHANNELS."
  1518. (save-window-excursion
  1519. (dolist (channel channels)
  1520. (with-rcirc-process-buffer process
  1521. (rcirc-cmd-join channel process)))))
  1522. ;;; nick management
  1523. (defvar rcirc-nick-prefix-chars "~&@%+")
  1524. (defun rcirc-user-nick (user)
  1525. "Return the nick from USER. Remove any non-nick junk."
  1526. (save-match-data
  1527. (if (string-match (concat "^[" rcirc-nick-prefix-chars
  1528. "]?\\([^! ]+\\)!?") (or user ""))
  1529. (match-string 1 user)
  1530. user)))
  1531. (defun rcirc-nick-channels (process nick)
  1532. "Return list of channels for NICK."
  1533. (with-rcirc-process-buffer process
  1534. (mapcar (lambda (x) (car x))
  1535. (gethash nick rcirc-nick-table))))
  1536. (defun rcirc-put-nick-channel (process nick channel &optional line)
  1537. "Add CHANNEL to list associated with NICK.
  1538. Update the associated linestamp if LINE is non-nil.
  1539. If the record doesn't exist, and LINE is nil, set the linestamp
  1540. to zero."
  1541. (let ((nick (rcirc-user-nick nick)))
  1542. (with-rcirc-process-buffer process
  1543. (let* ((chans (gethash nick rcirc-nick-table))
  1544. (record (assoc-string channel chans t)))
  1545. (if record
  1546. (when line (setcdr record line))
  1547. (puthash nick (cons (cons channel (or line 0))
  1548. chans)
  1549. rcirc-nick-table))))))
  1550. (defun rcirc-nick-remove (process nick)
  1551. "Remove NICK from table."
  1552. (with-rcirc-process-buffer process
  1553. (remhash nick rcirc-nick-table)))
  1554. (defun rcirc-remove-nick-channel (process nick channel)
  1555. "Remove the CHANNEL from list associated with NICK."
  1556. (with-rcirc-process-buffer process
  1557. (let* ((chans (gethash nick rcirc-nick-table))
  1558. (newchans
  1559. ;; instead of assoc-string-delete-all:
  1560. (let ((record (assoc-string channel chans t)))
  1561. (when record
  1562. (setcar record 'delete)
  1563. (assq-delete-all 'delete chans)))))
  1564. (if newchans
  1565. (puthash nick newchans rcirc-nick-table)
  1566. (remhash nick rcirc-nick-table)))))
  1567. (defun rcirc-channel-nicks (process target)
  1568. "Return the list of nicks associated with TARGET sorted by last activity."
  1569. (when target
  1570. (if (rcirc-channel-p target)
  1571. (with-rcirc-process-buffer process
  1572. (let (nicks)
  1573. (maphash
  1574. (lambda (k v)
  1575. (let ((record (assoc-string target v t)))
  1576. (if record
  1577. (setq nicks (cons (cons k (cdr record)) nicks)))))
  1578. rcirc-nick-table)
  1579. (mapcar (lambda (x) (car x))
  1580. (sort nicks (lambda (x y)
  1581. (let ((lx (or (cdr x) 0))
  1582. (ly (or (cdr y) 0)))
  1583. (< ly lx)))))))
  1584. (list target))))
  1585. (defun rcirc-ignore-update-automatic (nick)
  1586. "Remove NICK from `rcirc-ignore-list'
  1587. if NICK is also on `rcirc-ignore-list-automatic'."
  1588. (when (member nick rcirc-ignore-list-automatic)
  1589. (setq rcirc-ignore-list-automatic
  1590. (delete nick rcirc-ignore-list-automatic)
  1591. rcirc-ignore-list
  1592. (delete nick rcirc-ignore-list))))
  1593. (defun rcirc-nickname< (s1 s2)
  1594. "Return t if IRC nickname S1 is less than S2, and nil otherwise.
  1595. Operator nicknames (@) are considered less than voiced
  1596. nicknames (+). Any other nicknames are greater than voiced
  1597. nicknames. The comparison is case-insensitive."
  1598. (setq s1 (downcase s1)
  1599. s2 (downcase s2))
  1600. (let* ((s1-op (eq ?@ (string-to-char s1)))
  1601. (s2-op (eq ?@ (string-to-char s2))))
  1602. (if s1-op
  1603. (if s2-op
  1604. (string< (substring s1 1) (substring s2 1))
  1605. t)
  1606. (if s2-op
  1607. nil
  1608. (string< s1 s2)))))
  1609. (defun rcirc-sort-nicknames-join (input sep)
  1610. "Return a string of sorted nicknames.
  1611. INPUT is a string containing nicknames separated by SEP.
  1612. This function does not alter the INPUT string."
  1613. (let* ((parts (split-string input sep t))
  1614. (sorted (sort parts 'rcirc-nickname<)))
  1615. (mapconcat 'identity sorted sep)))
  1616. ;;; activity tracking
  1617. (defvar rcirc-track-minor-mode-map
  1618. (let ((map (make-sparse-keymap)))
  1619. (define-key map (kbd "C-c C-@") 'rcirc-next-active-buffer)
  1620. (define-key map (kbd "C-c C-SPC") 'rcirc-next-active-buffer)
  1621. map)
  1622. "Keymap for rcirc track minor mode.")
  1623. ;;;###autoload
  1624. (define-minor-mode rcirc-track-minor-mode
  1625. "Global minor mode for tracking activity in rcirc buffers.
  1626. With a prefix argument ARG, enable the mode if ARG is positive,
  1627. and disable it otherwise. If called from Lisp, enable the mode
  1628. if ARG is omitted or nil."
  1629. :init-value nil
  1630. :lighter ""
  1631. :keymap rcirc-track-minor-mode-map
  1632. :global t
  1633. :group 'rcirc
  1634. (or global-mode-string (setq global-mode-string '("")))
  1635. ;; toggle the mode-line channel indicator
  1636. (if rcirc-track-minor-mode
  1637. (progn
  1638. (and (not (memq 'rcirc-activity-string global-mode-string))
  1639. (setq global-mode-string
  1640. (append global-mode-string '(rcirc-activity-string))))
  1641. (add-hook 'window-configuration-change-hook
  1642. 'rcirc-window-configuration-change))
  1643. (setq global-mode-string
  1644. (delete 'rcirc-activity-string global-mode-string))
  1645. (remove-hook 'window-configuration-change-hook
  1646. 'rcirc-window-configuration-change)))
  1647. (or (assq 'rcirc-ignore-buffer-activity-flag minor-mode-alist)
  1648. (setq minor-mode-alist
  1649. (cons '(rcirc-ignore-buffer-activity-flag " Ignore") minor-mode-alist)))
  1650. (or (assq 'rcirc-low-priority-flag minor-mode-alist)
  1651. (setq minor-mode-alist
  1652. (cons '(rcirc-low-priority-flag " LowPri") minor-mode-alist)))
  1653. (defun rcirc-toggle-ignore-buffer-activity ()
  1654. "Toggle the value of `rcirc-ignore-buffer-activity-flag'."
  1655. (interactive)
  1656. (setq rcirc-ignore-buffer-activity-flag
  1657. (not rcirc-ignore-buffer-activity-flag))
  1658. (message (if rcirc-ignore-buffer-activity-flag
  1659. "Ignore activity in this buffer"
  1660. "Notice activity in this buffer"))
  1661. (force-mode-line-update))
  1662. (defun rcirc-toggle-low-priority ()
  1663. "Toggle the value of `rcirc-low-priority-flag'."
  1664. (interactive)
  1665. (setq rcirc-low-priority-flag
  1666. (not rcirc-low-priority-flag))
  1667. (message (if rcirc-low-priority-flag
  1668. "Activity in this buffer is low priority"
  1669. "Activity in this buffer is normal priority"))
  1670. (force-mode-line-update))
  1671. (defun rcirc-switch-to-server-buffer ()
  1672. "Switch to the server buffer associated with current channel buffer."
  1673. (interactive)
  1674. (unless (buffer-live-p rcirc-server-buffer)
  1675. (error "No such buffer"))
  1676. (switch-to-buffer rcirc-server-buffer))
  1677. (defun rcirc-jump-to-first-unread-line ()
  1678. "Move the point to the first unread line in this buffer."
  1679. (interactive)
  1680. (if (marker-position overlay-arrow-position)
  1681. (goto-char overlay-arrow-position)
  1682. (message "No unread messages")))
  1683. (defun rcirc-bury-buffers ()
  1684. "Bury all RCIRC buffers."
  1685. (interactive)
  1686. (dolist (buf (buffer-list))
  1687. (when (eq 'rcirc-mode (with-current-buffer buf major-mode))
  1688. (bury-buffer buf) ; buffers not shown
  1689. (quit-windows-on buf)))) ; buffers shown in a window
  1690. (defun rcirc-next-active-buffer (arg)
  1691. "Switch to the next rcirc buffer with activity.
  1692. With prefix ARG, go to the next low priority buffer with activity."
  1693. (interactive "P")
  1694. (let* ((pair (rcirc-split-activity rcirc-activity))
  1695. (lopri (car pair))
  1696. (hipri (cdr pair)))
  1697. (if (or (and (not arg) hipri)
  1698. (and arg lopri))
  1699. (progn
  1700. (switch-to-buffer (car (if arg lopri hipri)))
  1701. (when (> (point) rcirc-prompt-start-marker)
  1702. (recenter -1)))
  1703. (rcirc-bury-buffers)
  1704. (message "No IRC activity.%s"
  1705. (if lopri
  1706. (concat
  1707. " Type C-u " (key-description (this-command-keys))
  1708. " for low priority activity.")
  1709. "")))))
  1710. (define-obsolete-variable-alias 'rcirc-activity-hooks
  1711. 'rcirc-activity-functions "24.3")
  1712. (defvar rcirc-activity-functions nil
  1713. "Hook to be run when there is channel activity.
  1714. Functions are called with a single argument, the buffer with the
  1715. activity. Only run if the buffer is not visible and
  1716. `rcirc-ignore-buffer-activity-flag' is non-nil.")
  1717. (defun rcirc-record-activity (buffer &optional type)
  1718. "Record BUFFER activity with TYPE."
  1719. (with-current-buffer buffer
  1720. (let ((old-activity rcirc-activity)
  1721. (old-types rcirc-activity-types))
  1722. (when (not (get-buffer-window (current-buffer) t))
  1723. (setq rcirc-activity
  1724. (sort (if (memq (current-buffer) rcirc-activity) rcirc-activity
  1725. (cons (current-buffer) rcirc-activity))
  1726. (lambda (b1 b2)
  1727. (let ((t1 (with-current-buffer b1 rcirc-last-post-time))
  1728. (t2 (with-current-buffer b2 rcirc-last-post-time)))
  1729. (time-less-p t2 t1)))))
  1730. (cl-pushnew type rcirc-activity-types)
  1731. (unless (and (equal rcirc-activity old-activity)
  1732. (member type old-types))
  1733. (rcirc-update-activity-string)))))
  1734. (run-hook-with-args 'rcirc-activity-functions buffer))
  1735. (defun rcirc-clear-activity (buffer)
  1736. "Clear the BUFFER activity."
  1737. (setq rcirc-activity (remove buffer rcirc-activity))
  1738. (with-current-buffer buffer
  1739. (setq rcirc-activity-types nil)))
  1740. (defun rcirc-clear-unread (buffer)
  1741. "Erase the last read message arrow from BUFFER."
  1742. (when (buffer-live-p buffer)
  1743. (with-current-buffer buffer
  1744. (set-marker overlay-arrow-position nil))))
  1745. (defun rcirc-split-activity (activity)
  1746. "Return a cons cell with ACTIVITY split into (lopri . hipri)."
  1747. (let (lopri hipri)
  1748. (dolist (buf activity)
  1749. (with-current-buffer buf
  1750. (if (and rcirc-low-priority-flag
  1751. (not (member 'nick rcirc-activity-types)))
  1752. (push buf lopri)
  1753. (push buf hipri))))
  1754. (cons (nreverse lopri) (nreverse hipri))))
  1755. (defvar rcirc-update-activity-string-hook nil
  1756. "Hook run whenever the activity string is updated.")
  1757. ;; TODO: add mouse properties
  1758. (defun rcirc-update-activity-string ()
  1759. "Update mode-line string."
  1760. (let* ((pair (rcirc-split-activity rcirc-activity))
  1761. (lopri (car pair))
  1762. (hipri (cdr pair)))
  1763. (setq rcirc-activity-string
  1764. (cond ((or hipri lopri)
  1765. (concat (and hipri "[")
  1766. (rcirc-activity-string hipri)
  1767. (and hipri lopri ",")
  1768. (and lopri
  1769. (concat "("
  1770. (rcirc-activity-string lopri)
  1771. ")"))
  1772. (and hipri "]")))
  1773. ((not (null (rcirc-process-list)))
  1774. "[]")
  1775. (t "[]")))
  1776. (run-hooks 'rcirc-update-activity-string-hook)))
  1777. (defun rcirc-activity-string (buffers)
  1778. (mapconcat (lambda (b)
  1779. (let ((s (substring-no-properties (rcirc-short-buffer-name b))))
  1780. (with-current-buffer b
  1781. (dolist (type rcirc-activity-types)
  1782. (rcirc-add-face 0 (length s)
  1783. (cl-case type
  1784. (nick 'rcirc-track-nick)
  1785. (keyword 'rcirc-track-keyword))
  1786. s)))
  1787. s))
  1788. buffers ","))
  1789. (defun rcirc-short-buffer-name (buffer)
  1790. "Return a short name for BUFFER to use in the mode line indicator."
  1791. (with-current-buffer buffer
  1792. (or rcirc-short-buffer-name (buffer-name))))
  1793. (defun rcirc-visible-buffers ()
  1794. "Return a list of the visible buffers that are in rcirc-mode."
  1795. (let (acc)
  1796. (walk-windows (lambda (w)
  1797. (with-current-buffer (window-buffer w)
  1798. (when (eq major-mode 'rcirc-mode)
  1799. (push (current-buffer) acc)))))
  1800. acc))
  1801. (defvar rcirc-visible-buffers nil)
  1802. (defun rcirc-window-configuration-change ()
  1803. (unless (minibuffer-window-active-p (minibuffer-window))
  1804. ;; delay this until command has finished to make sure window is
  1805. ;; actually visible before clearing activity
  1806. (add-hook 'post-command-hook 'rcirc-window-configuration-change-1)))
  1807. (defun rcirc-window-configuration-change-1 ()
  1808. ;; clear activity and overlay arrows
  1809. (let* ((old-activity rcirc-activity)
  1810. (hidden-buffers rcirc-visible-buffers))
  1811. (setq rcirc-visible-buffers (rcirc-visible-buffers))
  1812. (dolist (vbuf rcirc-visible-buffers)
  1813. (setq hidden-buffers (delq vbuf hidden-buffers))
  1814. ;; clear activity for all visible buffers
  1815. (rcirc-clear-activity vbuf))
  1816. ;; clear unread arrow from recently hidden buffers
  1817. (dolist (hbuf hidden-buffers)
  1818. (rcirc-clear-unread hbuf))
  1819. ;; remove any killed buffers from list
  1820. (setq rcirc-activity
  1821. (delq nil (mapcar (lambda (buf) (when (buffer-live-p buf) buf))
  1822. rcirc-activity)))
  1823. ;; update the mode-line string
  1824. (unless (equal old-activity rcirc-activity)
  1825. (rcirc-update-activity-string)))
  1826. (remove-hook 'post-command-hook 'rcirc-window-configuration-change-1))
  1827. ;;; buffer name abbreviation
  1828. (defun rcirc-update-short-buffer-names ()
  1829. (let ((bufalist
  1830. (apply 'append (mapcar (lambda (process)
  1831. (with-rcirc-process-buffer process
  1832. rcirc-buffer-alist))
  1833. (rcirc-process-list)))))
  1834. (dolist (i (rcirc-abbreviate bufalist))
  1835. (when (buffer-live-p (cdr i))
  1836. (with-current-buffer (cdr i)
  1837. (setq rcirc-short-buffer-name (car i)))))))
  1838. (defun rcirc-abbreviate (pairs)
  1839. (apply 'append (mapcar 'rcirc-rebuild-tree (rcirc-make-trees pairs))))
  1840. (defun rcirc-rebuild-tree (tree &optional acc)
  1841. (let ((ch (char-to-string (car tree))))
  1842. (dolist (x (cdr tree))
  1843. (if (listp x)
  1844. (setq acc (append acc
  1845. (mapcar (lambda (y)
  1846. (cons (concat ch (car y))
  1847. (cdr y)))
  1848. (rcirc-rebuild-tree x))))
  1849. (setq acc (cons (cons ch x) acc))))
  1850. acc))
  1851. (defun rcirc-make-trees (pairs)
  1852. (let (alist)
  1853. (mapc (lambda (pair)
  1854. (if (consp pair)
  1855. (let* ((str (car pair))
  1856. (data (cdr pair))
  1857. (char (unless (zerop (length str))
  1858. (aref str 0)))
  1859. (rest (unless (zerop (length str))
  1860. (substring str 1)))
  1861. (part (if char (assq char alist))))
  1862. (if part
  1863. ;; existing partition
  1864. (setcdr part (cons (cons rest data) (cdr part)))
  1865. ;; new partition
  1866. (setq alist (cons (if char
  1867. (list char (cons rest data))
  1868. data)
  1869. alist))))
  1870. (setq alist (cons pair alist))))
  1871. pairs)
  1872. ;; recurse into cdrs of alist
  1873. (mapc (lambda (x)
  1874. (when (and (listp x) (listp (cadr x)))
  1875. (setcdr x (if (> (length (cdr x)) 1)
  1876. (rcirc-make-trees (cdr x))
  1877. (setcdr x (list (cl-cdadr x)))))))
  1878. alist)))
  1879. ;;; /commands these are called with 3 args: PROCESS, TARGET, which is
  1880. ;; the current buffer/channel/user, and ARGS, which is a string
  1881. ;; containing the text following the /cmd.
  1882. (defmacro defun-rcirc-command (command argument docstring interactive-form
  1883. &rest body)
  1884. "Define a command."
  1885. `(progn
  1886. (add-to-list 'rcirc-client-commands ,(concat "/" (symbol-name command)))
  1887. (defun ,(intern (concat "rcirc-cmd-" (symbol-name command)))
  1888. (,@argument &optional process target)
  1889. ,(concat docstring "\n\nNote: If PROCESS or TARGET are nil, the values given"
  1890. "\nby `rcirc-buffer-process' and `rcirc-target' will be used.")
  1891. ,interactive-form
  1892. (let ((process (or process (rcirc-buffer-process)))
  1893. (target (or target rcirc-target)))
  1894. (ignore target) ; mark `target' variable as ignorable
  1895. ,@body))))
  1896. (defun-rcirc-command msg (message)
  1897. "Send private MESSAGE to TARGET."
  1898. (interactive "i")
  1899. (if (null message)
  1900. (progn
  1901. (setq target (completing-read "Message nick: "
  1902. (with-rcirc-server-buffer
  1903. rcirc-nick-table)))
  1904. (when (> (length target) 0)
  1905. (setq message (read-string (format "Message %s: " target)))
  1906. (when (> (length message) 0)
  1907. (rcirc-send-message process target message))))
  1908. (if (not (string-match "\\([^ ]+\\) \\(.+\\)" message))
  1909. (message "Not enough args, or something.")
  1910. (setq target (match-string 1 message)
  1911. message (match-string 2 message))
  1912. (rcirc-send-message process target message))))
  1913. (defun-rcirc-command query (nick)
  1914. "Open a private chat buffer to NICK."
  1915. (interactive (list (completing-read "Query nick: "
  1916. (with-rcirc-server-buffer rcirc-nick-table))))
  1917. (let ((existing-buffer (rcirc-get-buffer process nick)))
  1918. (switch-to-buffer (or existing-buffer
  1919. (rcirc-get-buffer-create process nick)))
  1920. (when (not existing-buffer)
  1921. (rcirc-cmd-whois nick))))
  1922. (defun-rcirc-command join (channels)
  1923. "Join CHANNELS.
  1924. CHANNELS is a comma- or space-separated string of channel names."
  1925. (interactive "sJoin channels: ")
  1926. (let* ((split-channels (split-string channels "[ ,]" t))
  1927. (buffers (mapcar (lambda (ch)
  1928. (rcirc-get-buffer-create process ch))
  1929. split-channels))
  1930. (channels (mapconcat 'identity split-channels ",")))
  1931. (rcirc-send-string process (concat "JOIN " channels))
  1932. (when (not (eq (selected-window) (minibuffer-window)))
  1933. (dolist (b buffers) ;; order the new channel buffers in the buffer list
  1934. (switch-to-buffer b)))))
  1935. (defun-rcirc-command invite (nick-channel)
  1936. "Invite NICK to CHANNEL."
  1937. (interactive (list
  1938. (concat
  1939. (completing-read "Invite nick: "
  1940. (with-rcirc-server-buffer rcirc-nick-table))
  1941. " "
  1942. (read-string "Channel: "))))
  1943. (rcirc-send-string process (concat "INVITE " nick-channel)))
  1944. ;; TODO: /part #channel reason, or consider removing #channel altogether
  1945. (defun-rcirc-command part (channel)
  1946. "Part CHANNEL."
  1947. (interactive "sPart channel: ")
  1948. (let ((channel (if (> (length channel) 0) channel target)))
  1949. (rcirc-send-string process (concat "PART " channel " :" rcirc-id-string))))
  1950. (defun-rcirc-command quit (reason)
  1951. "Send a quit message to server with REASON."
  1952. (interactive "sQuit reason: ")
  1953. (rcirc-send-string process (concat "QUIT :"
  1954. (if (not (zerop (length reason)))
  1955. reason
  1956. rcirc-id-string))))
  1957. (defun-rcirc-command reconnect (_)
  1958. "Reconnect to current server."
  1959. (interactive "i")
  1960. (with-rcirc-server-buffer
  1961. (cond
  1962. (rcirc-connecting (message "Already connecting"))
  1963. ((process-live-p process) (message "Server process is alive"))
  1964. (t (let ((conn-info rcirc-connection-info))
  1965. (setf (nth 5 conn-info)
  1966. (cl-remove-if-not #'rcirc-channel-p
  1967. (mapcar #'car rcirc-buffer-alist)))
  1968. (apply #'rcirc-connect conn-info))))))
  1969. (defun-rcirc-command nick (nick)
  1970. "Change nick to NICK."
  1971. (interactive "i")
  1972. (when (null nick)
  1973. (setq nick (read-string "New nick: " (rcirc-nick process))))
  1974. (rcirc-send-string process (concat "NICK " nick)))
  1975. (defun-rcirc-command names (channel)
  1976. "Display list of names in CHANNEL or in current channel if CHANNEL is nil.
  1977. If called interactively, prompt for a channel when prefix arg is supplied."
  1978. (interactive "P")
  1979. (if (called-interactively-p 'interactive)
  1980. (if channel
  1981. (setq channel (read-string "List names in channel: " target))))
  1982. (let ((channel (if (> (length channel) 0)
  1983. channel
  1984. target)))
  1985. (rcirc-send-string process (concat "NAMES " channel))))
  1986. (defun-rcirc-command topic (topic)
  1987. "List TOPIC for the TARGET channel.
  1988. With a prefix arg, prompt for new topic."
  1989. (interactive "P")
  1990. (if (and (called-interactively-p 'interactive) topic)
  1991. (setq topic (read-string "New Topic: " rcirc-topic)))
  1992. (rcirc-send-string process (concat "TOPIC " target
  1993. (when (> (length topic) 0)
  1994. (concat " :" topic)))))
  1995. (defun-rcirc-command whois (nick)
  1996. "Request information from server about NICK."
  1997. (interactive (list
  1998. (completing-read "Whois: "
  1999. (with-rcirc-server-buffer rcirc-nick-table))))
  2000. (rcirc-send-string process (concat "WHOIS " nick)))
  2001. (defun-rcirc-command mode (args)
  2002. "Set mode with ARGS."
  2003. (interactive (list (concat (read-string "Mode nick or channel: ")
  2004. " " (read-string "Mode: "))))
  2005. (rcirc-send-string process (concat "MODE " args)))
  2006. (defun-rcirc-command list (channels)
  2007. "Request information on CHANNELS from server."
  2008. (interactive "sList Channels: ")
  2009. (rcirc-send-string process (concat "LIST " channels)))
  2010. (defun-rcirc-command oper (args)
  2011. "Send operator command to server."
  2012. (interactive "sOper args: ")
  2013. (rcirc-send-string process (concat "OPER " args)))
  2014. (defun-rcirc-command quote (message)
  2015. "Send MESSAGE literally to server."
  2016. (interactive "sServer message: ")
  2017. (rcirc-send-string process message))
  2018. (defun-rcirc-command kick (arg)
  2019. "Kick NICK from current channel."
  2020. (interactive (list
  2021. (concat (completing-read "Kick nick: "
  2022. (rcirc-channel-nicks
  2023. (rcirc-buffer-process)
  2024. rcirc-target))
  2025. (read-from-minibuffer "Kick reason: "))))
  2026. (let* ((arglist (split-string arg))
  2027. (argstring (concat (car arglist) " :"
  2028. (mapconcat 'identity (cdr arglist) " "))))
  2029. (rcirc-send-string process (concat "KICK " target " " argstring))))
  2030. (defun rcirc-cmd-ctcp (args &optional process _target)
  2031. (if (string-match "^\\([^ ]+\\)\\s-+\\(.+\\)$" args)
  2032. (let* ((target (match-string 1 args))
  2033. (request (upcase (match-string 2 args)))
  2034. (function (intern-soft (concat "rcirc-ctcp-sender-" request))))
  2035. (if (fboundp function) ;; use special function if available
  2036. (funcall function process target request)
  2037. (rcirc-send-ctcp process target request)))
  2038. (rcirc-print process (rcirc-nick process) "ERROR" nil
  2039. "usage: /ctcp NICK REQUEST")))
  2040. (defun rcirc-ctcp-sender-PING (process target _request)
  2041. "Send a CTCP PING message to TARGET."
  2042. (let ((timestamp (format "%.0f" (float-time))))
  2043. (rcirc-send-ctcp process target "PING" timestamp)))
  2044. (defun rcirc-cmd-me (args &optional process target)
  2045. (rcirc-send-ctcp process target "ACTION" args))
  2046. (defun rcirc-add-or-remove (set &rest elements)
  2047. (dolist (elt elements)
  2048. (if (and elt (not (string= "" elt)))
  2049. (setq set (if (member-ignore-case elt set)
  2050. (delete elt set)
  2051. (cons elt set)))))
  2052. set)
  2053. (defun-rcirc-command ignore (nick)
  2054. "Manage the ignore list.
  2055. Ignore NICK, unignore NICK if already ignored, or list ignored
  2056. nicks when no NICK is given. When listing ignored nicks, the
  2057. ones added to the list automatically are marked with an asterisk."
  2058. (interactive "sToggle ignoring of nick: ")
  2059. (setq rcirc-ignore-list
  2060. (apply #'rcirc-add-or-remove rcirc-ignore-list
  2061. (split-string nick nil t)))
  2062. (rcirc-print process nil "IGNORE" target
  2063. (mapconcat
  2064. (lambda (nick)
  2065. (concat nick
  2066. (if (member nick rcirc-ignore-list-automatic)
  2067. "*" "")))
  2068. rcirc-ignore-list " ")))
  2069. (defun-rcirc-command bright (nick)
  2070. "Manage the bright nick list."
  2071. (interactive "sToggle emphasis of nick: ")
  2072. (setq rcirc-bright-nicks
  2073. (apply #'rcirc-add-or-remove rcirc-bright-nicks
  2074. (split-string nick nil t)))
  2075. (rcirc-print process nil "BRIGHT" target
  2076. (mapconcat 'identity rcirc-bright-nicks " ")))
  2077. (defun-rcirc-command dim (nick)
  2078. "Manage the dim nick list."
  2079. (interactive "sToggle deemphasis of nick: ")
  2080. (setq rcirc-dim-nicks
  2081. (apply #'rcirc-add-or-remove rcirc-dim-nicks
  2082. (split-string nick nil t)))
  2083. (rcirc-print process nil "DIM" target
  2084. (mapconcat 'identity rcirc-dim-nicks " ")))
  2085. (defun-rcirc-command keyword (keyword)
  2086. "Manage the keyword list.
  2087. Mark KEYWORD, unmark KEYWORD if already marked, or list marked
  2088. keywords when no KEYWORD is given."
  2089. (interactive "sToggle highlighting of keyword: ")
  2090. (setq rcirc-keywords
  2091. (apply #'rcirc-add-or-remove rcirc-keywords
  2092. (split-string keyword nil t)))
  2093. (rcirc-print process nil "KEYWORD" target
  2094. (mapconcat 'identity rcirc-keywords " ")))
  2095. (defun rcirc-add-face (start end name &optional object)
  2096. "Add face NAME to the face text property of the text from START to END."
  2097. (when name
  2098. (let ((pos start)
  2099. next prop)
  2100. (while (< pos end)
  2101. (setq prop (get-text-property pos 'font-lock-face object)
  2102. next (next-single-property-change pos 'font-lock-face object end))
  2103. (unless (member name (get-text-property pos 'font-lock-face object))
  2104. (add-text-properties pos next
  2105. (list 'font-lock-face (cons name prop)) object))
  2106. (setq pos next)))))
  2107. (defun rcirc-facify (string face)
  2108. "Return a copy of STRING with FACE property added."
  2109. (let ((string (or string "")))
  2110. (rcirc-add-face 0 (length string) face string)
  2111. string))
  2112. (defvar rcirc-url-regexp
  2113. (concat
  2114. "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|"
  2115. "nntp\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)"
  2116. "\\(//[-a-z0-9_.]+:[0-9]*\\)?"
  2117. (if (string-match "[[:digit:]]" "1") ;; Support POSIX?
  2118. (let ((chars "-a-z0-9_=#$@~%&*+\\/[:word:]")
  2119. (punct "!?:;.,"))
  2120. (concat
  2121. "\\(?:"
  2122. ;; Match paired parentheses, e.g. in Wikipedia URLs:
  2123. "[" chars punct "]+" "(" "[" chars punct "]+" "[" chars "]*)" "[" chars "]"
  2124. "\\|"
  2125. "[" chars punct "]+" "[" chars "]"
  2126. "\\)"))
  2127. (concat ;; XEmacs 21.4 doesn't support POSIX.
  2128. "\\([-a-z0-9_=!?#$@~%&*+\\/:;.,]\\|\\w\\)+"
  2129. "\\([-a-z0-9_=#$@~%&*+\\/]\\|\\w\\)"))
  2130. "\\)")
  2131. "Regexp matching URLs. Set to nil to disable URL features in rcirc.")
  2132. ;; cf cl-remove-if-not
  2133. (defun rcirc-condition-filter (condp lst)
  2134. "Remove all items not satisfying condition CONDP in list LST.
  2135. CONDP is a function that takes a list element as argument and returns
  2136. non-nil if that element should be included. Returns a new list."
  2137. (delq nil (mapcar (lambda (x) (and (funcall condp x) x)) lst)))
  2138. (defun rcirc-browse-url (&optional arg)
  2139. "Prompt for URL to browse based on URLs in buffer before point.
  2140. If ARG is given, opens the URL in a new browser window."
  2141. (interactive "P")
  2142. (let* ((point (point))
  2143. (filtered (rcirc-condition-filter
  2144. (lambda (x) (>= point (cdr x)))
  2145. rcirc-urls))
  2146. (completions (mapcar (lambda (x) (car x)) filtered))
  2147. (defaults (mapcar (lambda (x) (car x)) filtered)))
  2148. (browse-url (completing-read "Rcirc browse-url: "
  2149. completions nil nil (car defaults) nil defaults)
  2150. arg)))
  2151. (defun rcirc-markup-timestamp (_sender _response)
  2152. (goto-char (point-min))
  2153. (insert (rcirc-facify (format-time-string rcirc-time-format)
  2154. 'rcirc-timestamp)))
  2155. (defun rcirc-markup-attributes (_sender _response)
  2156. (while (re-search-forward "\\([\C-b\C-_\C-v]\\).*?\\(\\1\\|\C-o\\)" nil t)
  2157. (rcirc-add-face (match-beginning 0) (match-end 0)
  2158. (cl-case (char-after (match-beginning 1))
  2159. (?\C-b 'bold)
  2160. (?\C-v 'italic)
  2161. (?\C-_ 'underline)))
  2162. ;; keep the ^O since it could terminate other attributes
  2163. (when (not (eq ?\C-o (char-before (match-end 2))))
  2164. (delete-region (match-beginning 2) (match-end 2)))
  2165. (delete-region (match-beginning 1) (match-end 1))
  2166. (goto-char (match-beginning 1)))
  2167. ;; remove the ^O characters now
  2168. (goto-char (point-min))
  2169. (while (re-search-forward "\C-o+" nil t)
  2170. (delete-region (match-beginning 0) (match-end 0))))
  2171. (defun rcirc-markup-my-nick (_sender response)
  2172. (with-syntax-table rcirc-nick-syntax-table
  2173. (while (re-search-forward (concat "\\b"
  2174. (regexp-quote (rcirc-nick
  2175. (rcirc-buffer-process)))
  2176. "\\b")
  2177. nil t)
  2178. (rcirc-add-face (match-beginning 0) (match-end 0)
  2179. 'rcirc-nick-in-message)
  2180. (when (string= response "PRIVMSG")
  2181. (rcirc-add-face (point-min) (point-max)
  2182. 'rcirc-nick-in-message-full-line)
  2183. (rcirc-record-activity (current-buffer) 'nick)))))
  2184. (defun rcirc-markup-urls (_sender _response)
  2185. (while (and rcirc-url-regexp ;; nil means disable URL catching
  2186. (re-search-forward rcirc-url-regexp nil t))
  2187. (let* ((start (match-beginning 0))
  2188. (end (match-end 0))
  2189. (url (match-string-no-properties 0))
  2190. (link-text (buffer-substring-no-properties start end)))
  2191. (make-button start end
  2192. 'face 'rcirc-url
  2193. 'follow-link t
  2194. 'rcirc-url url
  2195. 'action (lambda (button)
  2196. (browse-url (button-get button 'rcirc-url))))
  2197. ;; record the url if it is not already the latest stored url
  2198. (when (not (string= link-text (caar rcirc-urls)))
  2199. (push (cons link-text start) rcirc-urls)))))
  2200. (defun rcirc-markup-keywords (sender response)
  2201. (when (and (string= response "PRIVMSG")
  2202. (not (string= sender (rcirc-nick (rcirc-buffer-process)))))
  2203. (let* ((target (or rcirc-target ""))
  2204. (keywords (delq nil (mapcar (lambda (keyword)
  2205. (when (not (string-match keyword
  2206. target))
  2207. keyword))
  2208. rcirc-keywords))))
  2209. (when keywords
  2210. (while (re-search-forward (regexp-opt keywords 'words) nil t)
  2211. (rcirc-add-face (match-beginning 0) (match-end 0) 'rcirc-keyword)
  2212. (rcirc-record-activity (current-buffer) 'keyword))))))
  2213. (defun rcirc-markup-bright-nicks (_sender response)
  2214. (when (and rcirc-bright-nicks
  2215. (string= response "NAMES"))
  2216. (with-syntax-table rcirc-nick-syntax-table
  2217. (while (re-search-forward (regexp-opt rcirc-bright-nicks 'words) nil t)
  2218. (rcirc-add-face (match-beginning 0) (match-end 0)
  2219. 'rcirc-bright-nick)))))
  2220. (defun rcirc-markup-fill (_sender response)
  2221. (when (not (string= response "372")) ; /motd
  2222. (let ((fill-prefix
  2223. (or rcirc-fill-prefix
  2224. (make-string (- (point) (line-beginning-position)) ?\s)))
  2225. (fill-column (- (cond ((null rcirc-fill-column) fill-column)
  2226. ((functionp rcirc-fill-column)
  2227. (funcall rcirc-fill-column))
  2228. (t rcirc-fill-column))
  2229. ;; make sure ... doesn't cause line wrapping
  2230. 3)))
  2231. (fill-region (point) (point-max) nil t))))
  2232. ;;; handlers
  2233. ;; these are called with the server PROCESS, the SENDER, which is a
  2234. ;; server or a user, depending on the command, the ARGS, which is a
  2235. ;; list of strings, and the TEXT, which is the original server text,
  2236. ;; verbatim
  2237. (defun rcirc-handler-001 (process sender args text)
  2238. (rcirc-handler-generic process "001" sender args text)
  2239. (with-rcirc-process-buffer process
  2240. (setq rcirc-connecting nil)
  2241. (rcirc-reschedule-timeout process)
  2242. (setq rcirc-server-name sender)
  2243. (setq rcirc-nick (car args))
  2244. (rcirc-update-prompt)
  2245. (if rcirc-auto-authenticate-flag
  2246. (if (and rcirc-authenticate-before-join
  2247. ;; We have to ensure that there's an authentication
  2248. ;; entry for that server. Else,
  2249. ;; rcirc-authenticated-hook won't be triggered, and
  2250. ;; autojoin won't happen at all.
  2251. (let (auth-required)
  2252. (dolist (s rcirc-authinfo auth-required)
  2253. (when (string-match (car s) rcirc-server-name)
  2254. (setq auth-required t)))))
  2255. (progn
  2256. (add-hook 'rcirc-authenticated-hook 'rcirc-join-channels-post-auth t t)
  2257. (rcirc-authenticate))
  2258. (rcirc-authenticate)
  2259. (rcirc-join-channels process rcirc-startup-channels))
  2260. (rcirc-join-channels process rcirc-startup-channels))))
  2261. (defun rcirc-join-channels-post-auth (process)
  2262. "Join `rcirc-startup-channels' after authenticating."
  2263. (with-rcirc-process-buffer process
  2264. (rcirc-join-channels process rcirc-startup-channels)))
  2265. (defun rcirc-handler-PRIVMSG (process sender args text)
  2266. (rcirc-check-auth-status process sender args text)
  2267. (let ((target (if (rcirc-channel-p (car args))
  2268. (car args)
  2269. sender))
  2270. (message (or (cadr args) "")))
  2271. (if (string-match "^\C-a\\(.*\\)\C-a$" message)
  2272. (rcirc-handler-CTCP process target sender (match-string 1 message))
  2273. (rcirc-print process sender "PRIVMSG" target message t))
  2274. ;; update nick linestamp
  2275. (with-current-buffer (rcirc-get-buffer process target t)
  2276. (rcirc-put-nick-channel process sender target rcirc-current-line))))
  2277. (defun rcirc-handler-NOTICE (process sender args text)
  2278. (rcirc-check-auth-status process sender args text)
  2279. (let ((target (car args))
  2280. (message (cadr args)))
  2281. (if (string-match "^\C-a\\(.*\\)\C-a$" message)
  2282. (rcirc-handler-CTCP-response process target sender
  2283. (match-string 1 message))
  2284. (rcirc-print process sender "NOTICE"
  2285. (cond ((rcirc-channel-p target)
  2286. target)
  2287. ;;; -ChanServ- [#gnu] Welcome...
  2288. ((string-match "\\[\\(#[^] ]+\\)\\]" message)
  2289. (match-string 1 message))
  2290. (sender
  2291. (if (string= sender (rcirc-server-name process))
  2292. nil ; server notice
  2293. sender)))
  2294. message t))))
  2295. (defun rcirc-check-auth-status (process sender args _text)
  2296. "Check if the user just authenticated.
  2297. If authenticated, runs `rcirc-authenticated-hook' with PROCESS as
  2298. the only argument."
  2299. (with-rcirc-process-buffer process
  2300. (when (and (not rcirc-user-authenticated)
  2301. rcirc-authenticate-before-join
  2302. rcirc-auto-authenticate-flag)
  2303. (let ((target (car args))
  2304. (message (cadr args)))
  2305. (when (or
  2306. (and ;; nickserv
  2307. (string= sender "NickServ")
  2308. (string= target rcirc-nick)
  2309. (member message
  2310. (list
  2311. (format "You are now identified for \C-b%s\C-b." rcirc-nick)
  2312. (format "You are successfully identified as \C-b%s\C-b." rcirc-nick)
  2313. "Password accepted - you are now recognized."
  2314. )))
  2315. (and ;; quakenet
  2316. (string= sender "Q")
  2317. (string= target rcirc-nick)
  2318. (string-match "\\`You are now logged in as .+\\.\\'" message)))
  2319. (setq rcirc-user-authenticated t)
  2320. (run-hook-with-args 'rcirc-authenticated-hook process)
  2321. (remove-hook 'rcirc-authenticated-hook 'rcirc-join-channels-post-auth t))))))
  2322. (defun rcirc-handler-WALLOPS (process sender args _text)
  2323. (rcirc-print process sender "WALLOPS" sender (car args) t))
  2324. (defun rcirc-handler-JOIN (process sender args _text)
  2325. (let ((channel (car args)))
  2326. (with-current-buffer (rcirc-get-buffer-create process channel)
  2327. ;; when recently rejoining, restore the linestamp
  2328. (rcirc-put-nick-channel process sender channel
  2329. (let ((last-activity-lines
  2330. (rcirc-elapsed-lines process sender channel)))
  2331. (when (and last-activity-lines
  2332. (< last-activity-lines rcirc-omit-threshold))
  2333. (rcirc-last-line process sender channel))))
  2334. ;; reset mode-line-process in case joining a channel with an
  2335. ;; already open buffer (after getting kicked e.g.)
  2336. (setq mode-line-process nil))
  2337. (rcirc-print process sender "JOIN" channel "")
  2338. ;; print in private chat buffer if it exists
  2339. (when (rcirc-get-buffer (rcirc-buffer-process) sender)
  2340. (rcirc-print process sender "JOIN" sender channel))))
  2341. ;; PART and KICK are handled the same way
  2342. (defun rcirc-handler-PART-or-KICK (process _response channel _sender nick _args)
  2343. (rcirc-ignore-update-automatic nick)
  2344. (if (not (string= nick (rcirc-nick process)))
  2345. ;; this is someone else leaving
  2346. (progn
  2347. (rcirc-maybe-remember-nick-quit process nick channel)
  2348. (rcirc-remove-nick-channel process nick channel))
  2349. ;; this is us leaving
  2350. (mapc (lambda (n)
  2351. (rcirc-remove-nick-channel process n channel))
  2352. (rcirc-channel-nicks process channel))
  2353. ;; if the buffer is still around, make it inactive
  2354. (let ((buffer (rcirc-get-buffer process channel)))
  2355. (when buffer
  2356. (rcirc-disconnect-buffer buffer)))))
  2357. (defun rcirc-handler-PART (process sender args _text)
  2358. (let* ((channel (car args))
  2359. (reason (cadr args))
  2360. (message (concat channel " " reason)))
  2361. (rcirc-print process sender "PART" channel message)
  2362. ;; print in private chat buffer if it exists
  2363. (when (rcirc-get-buffer (rcirc-buffer-process) sender)
  2364. (rcirc-print process sender "PART" sender message))
  2365. (rcirc-handler-PART-or-KICK process "PART" channel sender sender reason)))
  2366. (defun rcirc-handler-KICK (process sender args _text)
  2367. (let* ((channel (car args))
  2368. (nick (cadr args))
  2369. (reason (nth 2 args))
  2370. (message (concat nick " " channel " " reason)))
  2371. (rcirc-print process sender "KICK" channel message t)
  2372. ;; print in private chat buffer if it exists
  2373. (when (rcirc-get-buffer (rcirc-buffer-process) nick)
  2374. (rcirc-print process sender "KICK" nick message))
  2375. (rcirc-handler-PART-or-KICK process "KICK" channel sender nick reason)))
  2376. (defun rcirc-maybe-remember-nick-quit (process nick channel)
  2377. "Remember NICK as leaving CHANNEL if they recently spoke."
  2378. (let ((elapsed-lines (rcirc-elapsed-lines process nick channel)))
  2379. (when (and elapsed-lines
  2380. (< elapsed-lines rcirc-omit-threshold))
  2381. (let ((buffer (rcirc-get-buffer process channel)))
  2382. (when buffer
  2383. (with-current-buffer buffer
  2384. (let ((record (assoc-string nick rcirc-recent-quit-alist t))
  2385. (line (rcirc-last-line process nick channel)))
  2386. (if record
  2387. (setcdr record line)
  2388. (setq rcirc-recent-quit-alist
  2389. (cons (cons nick line)
  2390. rcirc-recent-quit-alist))))))))))
  2391. (defun rcirc-handler-QUIT (process sender args _text)
  2392. (rcirc-ignore-update-automatic sender)
  2393. (mapc (lambda (channel)
  2394. ;; broadcast quit message each channel
  2395. (rcirc-print process sender "QUIT" channel (apply 'concat args))
  2396. ;; record nick in quit table if they recently spoke
  2397. (rcirc-maybe-remember-nick-quit process sender channel))
  2398. (rcirc-nick-channels process sender))
  2399. (rcirc-nick-remove process sender))
  2400. (defun rcirc-handler-NICK (process sender args _text)
  2401. (let* ((old-nick sender)
  2402. (new-nick (car args))
  2403. (channels (rcirc-nick-channels process old-nick)))
  2404. ;; update list of ignored nicks
  2405. (rcirc-ignore-update-automatic old-nick)
  2406. (when (member old-nick rcirc-ignore-list)
  2407. (add-to-list 'rcirc-ignore-list new-nick)
  2408. (add-to-list 'rcirc-ignore-list-automatic new-nick))
  2409. ;; print message to nick's channels
  2410. (dolist (target channels)
  2411. (rcirc-print process sender "NICK" target new-nick))
  2412. ;; update private chat buffer, if it exists
  2413. (let ((chat-buffer (rcirc-get-buffer process old-nick)))
  2414. (when chat-buffer
  2415. (with-current-buffer chat-buffer
  2416. (rcirc-print process sender "NICK" old-nick new-nick)
  2417. (setq rcirc-target new-nick)
  2418. (rename-buffer (rcirc-generate-new-buffer-name process new-nick)))))
  2419. ;; remove old nick and add new one
  2420. (with-rcirc-process-buffer process
  2421. (let ((v (gethash old-nick rcirc-nick-table)))
  2422. (remhash old-nick rcirc-nick-table)
  2423. (puthash new-nick v rcirc-nick-table))
  2424. ;; if this is our nick...
  2425. (when (string= old-nick rcirc-nick)
  2426. (setq rcirc-nick new-nick)
  2427. (rcirc-update-prompt t)
  2428. ;; reauthenticate
  2429. (when rcirc-auto-authenticate-flag (rcirc-authenticate))))))
  2430. (defun rcirc-handler-PING (process _sender args _text)
  2431. (rcirc-send-string process (concat "PONG :" (car args))))
  2432. (defun rcirc-handler-PONG (_process _sender _args _text)
  2433. ;; do nothing
  2434. )
  2435. (defun rcirc-handler-TOPIC (process sender args _text)
  2436. (let ((topic (cadr args)))
  2437. (rcirc-print process sender "TOPIC" (car args) topic)
  2438. (with-current-buffer (rcirc-get-buffer process (car args))
  2439. (setq rcirc-topic topic))))
  2440. (defvar rcirc-nick-away-alist nil)
  2441. (defun rcirc-handler-301 (process _sender args text)
  2442. "RPL_AWAY"
  2443. (let* ((nick (cadr args))
  2444. (rec (assoc-string nick rcirc-nick-away-alist))
  2445. (away-message (nth 2 args)))
  2446. (when (or (not rec)
  2447. (not (string= (cdr rec) away-message)))
  2448. ;; away message has changed
  2449. (rcirc-handler-generic process "AWAY" nick (cdr args) text)
  2450. (if rec
  2451. (setcdr rec away-message)
  2452. (setq rcirc-nick-away-alist (cons (cons nick away-message)
  2453. rcirc-nick-away-alist))))))
  2454. (defun rcirc-handler-317 (process sender args _text)
  2455. "RPL_WHOISIDLE"
  2456. (let* ((nick (nth 1 args))
  2457. (idle-secs (string-to-number (nth 2 args)))
  2458. (idle-string
  2459. (if (< idle-secs most-positive-fixnum)
  2460. (format-seconds "%yy %dd %hh %mm %z%ss" idle-secs)
  2461. "a very long time"))
  2462. (signon-time (seconds-to-time (string-to-number (nth 3 args))))
  2463. (signon-string (format-time-string "%c" signon-time))
  2464. (message (format "%s idle for %s, signed on %s"
  2465. nick idle-string signon-string)))
  2466. (rcirc-print process sender "317" nil message t)))
  2467. (defun rcirc-handler-332 (process _sender args _text)
  2468. "RPL_TOPIC"
  2469. (let ((buffer (or (rcirc-get-buffer process (cadr args))
  2470. (rcirc-get-temp-buffer-create process (cadr args)))))
  2471. (with-current-buffer buffer
  2472. (setq rcirc-topic (nth 2 args)))))
  2473. (defun rcirc-handler-333 (process sender args _text)
  2474. "333 says who set the topic and when.
  2475. Not in rfc1459.txt"
  2476. (let ((buffer (or (rcirc-get-buffer process (cadr args))
  2477. (rcirc-get-temp-buffer-create process (cadr args)))))
  2478. (with-current-buffer buffer
  2479. (let ((setter (nth 2 args))
  2480. (time (current-time-string
  2481. (seconds-to-time
  2482. (string-to-number (cl-cadddr args))))))
  2483. (rcirc-print process sender "TOPIC" (cadr args)
  2484. (format "%s (%s on %s)" rcirc-topic setter time))))))
  2485. (defun rcirc-handler-477 (process sender args _text)
  2486. "ERR_NOCHANMODES"
  2487. (rcirc-print process sender "477" (cadr args) (nth 2 args)))
  2488. (defun rcirc-handler-MODE (process sender args _text)
  2489. (let ((target (car args))
  2490. (msg (mapconcat 'identity (cdr args) " ")))
  2491. (rcirc-print process sender "MODE"
  2492. (if (string= target (rcirc-nick process))
  2493. nil
  2494. target)
  2495. msg)
  2496. ;; print in private chat buffers if they exist
  2497. (mapc (lambda (nick)
  2498. (when (rcirc-get-buffer process nick)
  2499. (rcirc-print process sender "MODE" nick msg)))
  2500. (cddr args))))
  2501. (defun rcirc-get-temp-buffer-create (process channel)
  2502. "Return a buffer based on PROCESS and CHANNEL."
  2503. (let ((tmpnam (concat " " (downcase channel) "TMP" (process-name process))))
  2504. (get-buffer-create tmpnam)))
  2505. (defun rcirc-handler-353 (process _sender args _text)
  2506. "RPL_NAMREPLY"
  2507. (let ((channel (nth 2 args))
  2508. (names (or (nth 3 args) "")))
  2509. (mapc (lambda (nick)
  2510. (rcirc-put-nick-channel process nick channel))
  2511. (split-string names " " t))
  2512. ;; create a temporary buffer to insert the names into
  2513. ;; rcirc-handler-366 (RPL_ENDOFNAMES) will handle it
  2514. (with-current-buffer (rcirc-get-temp-buffer-create process channel)
  2515. (goto-char (point-max))
  2516. (insert (car (last args)) " "))))
  2517. (defun rcirc-handler-366 (process sender args _text)
  2518. "RPL_ENDOFNAMES"
  2519. (let* ((channel (cadr args))
  2520. (buffer (rcirc-get-temp-buffer-create process channel)))
  2521. (with-current-buffer buffer
  2522. (rcirc-print process sender "NAMES" channel
  2523. (let ((content (buffer-substring (point-min) (point-max))))
  2524. (rcirc-sort-nicknames-join content " "))))
  2525. (kill-buffer buffer)))
  2526. (defun rcirc-handler-433 (process sender args text)
  2527. "ERR_NICKNAMEINUSE"
  2528. (rcirc-handler-generic process "433" sender args text)
  2529. (let* ((new-nick (concat (cadr args) "`")))
  2530. (with-rcirc-process-buffer process
  2531. (rcirc-cmd-nick new-nick nil process))))
  2532. (defun rcirc-authenticate ()
  2533. "Send authentication to process associated with current buffer.
  2534. Passwords are stored in `rcirc-authinfo' (which see)."
  2535. (interactive)
  2536. (with-rcirc-server-buffer
  2537. (dolist (i rcirc-authinfo)
  2538. (let ((process (rcirc-buffer-process))
  2539. (server (car i))
  2540. (nick (nth 2 i))
  2541. (method (cadr i))
  2542. (args (cl-cdddr i)))
  2543. (when (and (string-match server rcirc-server))
  2544. (if (and (memq method '(nickserv chanserv bitlbee))
  2545. (string-match nick rcirc-nick))
  2546. ;; the following methods rely on the user's nickname.
  2547. (cl-case method
  2548. (nickserv
  2549. (rcirc-send-privmsg
  2550. process
  2551. (or (cadr args) "NickServ")
  2552. (concat "IDENTIFY " (car args))))
  2553. (chanserv
  2554. (rcirc-send-privmsg
  2555. process
  2556. "ChanServ"
  2557. (format "IDENTIFY %s %s" (car args) (cadr args))))
  2558. (bitlbee
  2559. (rcirc-send-privmsg
  2560. process
  2561. "&bitlbee"
  2562. (concat "IDENTIFY " (car args)))))
  2563. ;; quakenet authentication doesn't rely on the user's nickname.
  2564. ;; the variable `nick' here represents the Q account name.
  2565. (when (eq method 'quakenet)
  2566. (rcirc-send-privmsg
  2567. process
  2568. "Q@CServe.quakenet.org"
  2569. (format "AUTH %s %s" nick (car args))))))))))
  2570. (defun rcirc-handler-INVITE (process sender args _text)
  2571. (rcirc-print process sender "INVITE" nil (mapconcat 'identity args " ") t))
  2572. (defun rcirc-handler-ERROR (process sender args _text)
  2573. (rcirc-print process sender "ERROR" nil (mapconcat 'identity args " ")))
  2574. (defun rcirc-handler-CTCP (process target sender text)
  2575. (if (string-match "^\\([^ ]+\\) *\\(.*\\)$" text)
  2576. (let* ((request (upcase (match-string 1 text)))
  2577. (args (match-string 2 text))
  2578. (handler (intern-soft (concat "rcirc-handler-ctcp-" request))))
  2579. (if (not (fboundp handler))
  2580. (rcirc-print process sender "ERROR" target
  2581. (format "%s sent unsupported ctcp: %s" sender text)
  2582. t)
  2583. (funcall handler process target sender args)
  2584. (unless (or (string= request "ACTION")
  2585. (string= request "KEEPALIVE"))
  2586. (rcirc-print process sender "CTCP" target
  2587. (format "%s" text) t))))))
  2588. (defun rcirc-handler-ctcp-VERSION (process _target sender _args)
  2589. (rcirc-send-string process
  2590. (concat "NOTICE " sender
  2591. " :\C-aVERSION " rcirc-id-string
  2592. "\C-a")))
  2593. (defun rcirc-handler-ctcp-ACTION (process target sender args)
  2594. (rcirc-print process sender "ACTION" target args t))
  2595. (defun rcirc-handler-ctcp-TIME (process _target sender _args)
  2596. (rcirc-send-string process
  2597. (concat "NOTICE " sender
  2598. " :\C-aTIME " (current-time-string) "\C-a")))
  2599. (defun rcirc-handler-CTCP-response (process _target sender message)
  2600. (rcirc-print process sender "CTCP" nil message t))
  2601. (defgroup rcirc-faces nil
  2602. "Faces for rcirc."
  2603. :group 'rcirc
  2604. :group 'faces)
  2605. (defface rcirc-my-nick ; font-lock-function-name-face
  2606. '((((class color) (min-colors 88) (background light)) :foreground "Blue1")
  2607. (((class color) (min-colors 88) (background dark)) :foreground "LightSkyBlue")
  2608. (((class color) (min-colors 16) (background light)) :foreground "Blue")
  2609. (((class color) (min-colors 16) (background dark)) :foreground "LightSkyBlue")
  2610. (((class color) (min-colors 8)) :foreground "blue" :weight bold)
  2611. (t :inverse-video t :weight bold))
  2612. "Rcirc face for my messages."
  2613. :group 'rcirc-faces)
  2614. (defface rcirc-other-nick ; font-lock-variable-name-face
  2615. '((((class grayscale) (background light))
  2616. :foreground "Gray90" :weight bold :slant italic)
  2617. (((class grayscale) (background dark))
  2618. :foreground "DimGray" :weight bold :slant italic)
  2619. (((class color) (min-colors 88) (background light)) :foreground "DarkGoldenrod")
  2620. (((class color) (min-colors 88) (background dark)) :foreground "LightGoldenrod")
  2621. (((class color) (min-colors 16) (background light)) :foreground "DarkGoldenrod")
  2622. (((class color) (min-colors 16) (background dark)) :foreground "LightGoldenrod")
  2623. (((class color) (min-colors 8)) :foreground "yellow" :weight light)
  2624. (t :weight bold :slant italic))
  2625. "Rcirc face for other users' messages."
  2626. :group 'rcirc-faces)
  2627. (defface rcirc-bright-nick
  2628. '((((class grayscale) (background light))
  2629. :foreground "LightGray" :weight bold :underline t)
  2630. (((class grayscale) (background dark))
  2631. :foreground "Gray50" :weight bold :underline t)
  2632. (((class color) (min-colors 88) (background light)) :foreground "CadetBlue")
  2633. (((class color) (min-colors 88) (background dark)) :foreground "Aquamarine")
  2634. (((class color) (min-colors 16) (background light)) :foreground "CadetBlue")
  2635. (((class color) (min-colors 16) (background dark)) :foreground "Aquamarine")
  2636. (((class color) (min-colors 8)) :foreground "magenta")
  2637. (t :weight bold :underline t))
  2638. "Rcirc face for nicks matched by `rcirc-bright-nicks'."
  2639. :group 'rcirc-faces)
  2640. (defface rcirc-dim-nick
  2641. '((t :inherit default))
  2642. "Rcirc face for nicks in `rcirc-dim-nicks'."
  2643. :group 'rcirc-faces)
  2644. (defface rcirc-server ; font-lock-comment-face
  2645. '((((class grayscale) (background light))
  2646. :foreground "DimGray" :weight bold :slant italic)
  2647. (((class grayscale) (background dark))
  2648. :foreground "LightGray" :weight bold :slant italic)
  2649. (((class color) (min-colors 88) (background light))
  2650. :foreground "Firebrick")
  2651. (((class color) (min-colors 88) (background dark))
  2652. :foreground "chocolate1")
  2653. (((class color) (min-colors 16) (background light))
  2654. :foreground "red")
  2655. (((class color) (min-colors 16) (background dark))
  2656. :foreground "red1")
  2657. (((class color) (min-colors 8) (background light)))
  2658. (((class color) (min-colors 8) (background dark)))
  2659. (t :weight bold :slant italic))
  2660. "Rcirc face for server messages."
  2661. :group 'rcirc-faces)
  2662. (defface rcirc-server-prefix ; font-lock-comment-delimiter-face
  2663. '((default :inherit rcirc-server)
  2664. (((class grayscale)))
  2665. (((class color) (min-colors 16)))
  2666. (((class color) (min-colors 8) (background light))
  2667. :foreground "red")
  2668. (((class color) (min-colors 8) (background dark))
  2669. :foreground "red1"))
  2670. "Rcirc face for server prefixes."
  2671. :group 'rcirc-faces)
  2672. (defface rcirc-timestamp
  2673. '((t :inherit default))
  2674. "Rcirc face for timestamps."
  2675. :group 'rcirc-faces)
  2676. (defface rcirc-nick-in-message ; font-lock-keyword-face
  2677. '((((class grayscale) (background light)) :foreground "LightGray" :weight bold)
  2678. (((class grayscale) (background dark)) :foreground "DimGray" :weight bold)
  2679. (((class color) (min-colors 88) (background light)) :foreground "Purple")
  2680. (((class color) (min-colors 88) (background dark)) :foreground "Cyan1")
  2681. (((class color) (min-colors 16) (background light)) :foreground "Purple")
  2682. (((class color) (min-colors 16) (background dark)) :foreground "Cyan")
  2683. (((class color) (min-colors 8)) :foreground "cyan" :weight bold)
  2684. (t :weight bold))
  2685. "Rcirc face for instances of your nick within messages."
  2686. :group 'rcirc-faces)
  2687. (defface rcirc-nick-in-message-full-line '((t :weight bold))
  2688. "Rcirc face for emphasizing the entire message when your nick is mentioned."
  2689. :group 'rcirc-faces)
  2690. (defface rcirc-prompt ; comint-highlight-prompt
  2691. '((((min-colors 88) (background dark)) :foreground "cyan1")
  2692. (((background dark)) :foreground "cyan")
  2693. (t :foreground "dark blue"))
  2694. "Rcirc face for prompts."
  2695. :group 'rcirc-faces)
  2696. (defface rcirc-track-nick
  2697. '((((type tty)) :inherit default)
  2698. (t :inverse-video t))
  2699. "Rcirc face used in the mode-line when your nick is mentioned."
  2700. :group 'rcirc-faces)
  2701. (defface rcirc-track-keyword '((t :weight bold))
  2702. "Rcirc face used in the mode-line when keywords are mentioned."
  2703. :group 'rcirc-faces)
  2704. (defface rcirc-url '((t :weight bold))
  2705. "Rcirc face used to highlight urls."
  2706. :group 'rcirc-faces)
  2707. (defface rcirc-keyword '((t :inherit highlight))
  2708. "Rcirc face used to highlight keywords."
  2709. :group 'rcirc-faces)
  2710. ;; When using M-x flyspell-mode, only check words after the prompt
  2711. (put 'rcirc-mode 'flyspell-mode-predicate 'rcirc-looking-at-input)
  2712. (defun rcirc-looking-at-input ()
  2713. "Returns true if point is past the input marker."
  2714. (>= (point) rcirc-prompt-end-marker))
  2715. (provide 'rcirc)
  2716. ;;; rcirc.el ends here