rcirc.el 108 KB

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