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