irc.py 123 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074
  1. # -*- test-case-name: twisted.words.test.test_irc -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Internet Relay Chat protocol for client and server.
  6. Future Plans
  7. ============
  8. The way the IRCClient class works here encourages people to implement
  9. IRC clients by subclassing the ephemeral protocol class, and it tends
  10. to end up with way more state than it should for an object which will
  11. be destroyed as soon as the TCP transport drops. Someone oughta do
  12. something about that, ya know?
  13. The DCC support needs to have more hooks for the client for it to be
  14. able to ask the user things like "Do you want to accept this session?"
  15. and "Transfer #2 is 67% done." and otherwise manage the DCC sessions.
  16. Test coverage needs to be better.
  17. @var MAX_COMMAND_LENGTH: The maximum length of a command, as defined by RFC
  18. 2812 section 2.3.
  19. @var attributes: Singleton instance of L{_CharacterAttributes}, used for
  20. constructing formatted text information.
  21. @author: Kevin Turner
  22. @see: RFC 1459: Internet Relay Chat Protocol
  23. @see: RFC 2812: Internet Relay Chat: Client Protocol
  24. @see: U{The Client-To-Client-Protocol
  25. <http://www.irchelp.org/irchelp/rfc/ctcpspec.html>}
  26. """
  27. import errno, os, random, re, stat, struct, sys, time, traceback
  28. import operator
  29. import string, socket
  30. import textwrap
  31. import shlex
  32. from functools import reduce
  33. from os import path
  34. from twisted.internet import reactor, protocol, task
  35. from twisted.persisted import styles
  36. from twisted.protocols import basic
  37. from twisted.python import log, reflect, _textattributes
  38. from twisted.python.compat import unicode, range
  39. NUL = chr(0)
  40. CR = chr(0o15)
  41. NL = chr(0o12)
  42. LF = NL
  43. SPC = chr(0o40)
  44. # This includes the CRLF terminator characters.
  45. MAX_COMMAND_LENGTH = 512
  46. CHANNEL_PREFIXES = '&#!+'
  47. class IRCBadMessage(Exception):
  48. pass
  49. class IRCPasswordMismatch(Exception):
  50. pass
  51. class IRCBadModes(ValueError):
  52. """
  53. A malformed mode was encountered while attempting to parse a mode string.
  54. """
  55. def parsemsg(s):
  56. """
  57. Breaks a message from an IRC server into its prefix, command, and
  58. arguments.
  59. @param s: The message to break.
  60. @type s: L{bytes}
  61. @return: A tuple of (prefix, command, args).
  62. @rtype: L{tuple}
  63. """
  64. prefix = ''
  65. trailing = []
  66. if not s:
  67. raise IRCBadMessage("Empty line.")
  68. if s[0:1] == ':':
  69. prefix, s = s[1:].split(' ', 1)
  70. if s.find(' :') != -1:
  71. s, trailing = s.split(' :', 1)
  72. args = s.split()
  73. args.append(trailing)
  74. else:
  75. args = s.split()
  76. command = args.pop(0)
  77. return prefix, command, args
  78. def split(str, length=80):
  79. """
  80. Split a string into multiple lines.
  81. Whitespace near C{str[length]} will be preferred as a breaking point.
  82. C{"\\n"} will also be used as a breaking point.
  83. @param str: The string to split.
  84. @type str: C{str}
  85. @param length: The maximum length which will be allowed for any string in
  86. the result.
  87. @type length: C{int}
  88. @return: C{list} of C{str}
  89. """
  90. return [chunk
  91. for line in str.split('\n')
  92. for chunk in textwrap.wrap(line, length)]
  93. def _intOrDefault(value, default=None):
  94. """
  95. Convert a value to an integer if possible.
  96. @rtype: C{int} or type of L{default}
  97. @return: An integer when C{value} can be converted to an integer,
  98. otherwise return C{default}
  99. """
  100. if value:
  101. try:
  102. return int(value)
  103. except (TypeError, ValueError):
  104. pass
  105. return default
  106. class UnhandledCommand(RuntimeError):
  107. """
  108. A command dispatcher could not locate an appropriate command handler.
  109. """
  110. class _CommandDispatcherMixin(object):
  111. """
  112. Dispatch commands to handlers based on their name.
  113. Command handler names should be of the form C{prefix_commandName},
  114. where C{prefix} is the value specified by L{prefix}, and must
  115. accept the parameters as given to L{dispatch}.
  116. Attempting to mix this in more than once for a single class will cause
  117. strange behaviour, due to L{prefix} being overwritten.
  118. @type prefix: C{str}
  119. @ivar prefix: Command handler prefix, used to locate handler attributes
  120. """
  121. prefix = None
  122. def dispatch(self, commandName, *args):
  123. """
  124. Perform actual command dispatch.
  125. """
  126. def _getMethodName(command):
  127. return '%s_%s' % (self.prefix, command)
  128. def _getMethod(name):
  129. return getattr(self, _getMethodName(name), None)
  130. method = _getMethod(commandName)
  131. if method is not None:
  132. return method(*args)
  133. method = _getMethod('unknown')
  134. if method is None:
  135. raise UnhandledCommand("No handler for %r could be found" % (_getMethodName(commandName),))
  136. return method(commandName, *args)
  137. def parseModes(modes, params, paramModes=('', '')):
  138. """
  139. Parse an IRC mode string.
  140. The mode string is parsed into two lists of mode changes (added and
  141. removed), with each mode change represented as C{(mode, param)} where mode
  142. is the mode character, and param is the parameter passed for that mode, or
  143. L{None} if no parameter is required.
  144. @type modes: C{str}
  145. @param modes: Modes string to parse.
  146. @type params: C{list}
  147. @param params: Parameters specified along with L{modes}.
  148. @type paramModes: C{(str, str)}
  149. @param paramModes: A pair of strings (C{(add, remove)}) that indicate which modes take
  150. parameters when added or removed.
  151. @returns: Two lists of mode changes, one for modes added and the other for
  152. modes removed respectively, mode changes in each list are represented as
  153. C{(mode, param)}.
  154. """
  155. if len(modes) == 0:
  156. raise IRCBadModes('Empty mode string')
  157. if modes[0] not in '+-':
  158. raise IRCBadModes('Malformed modes string: %r' % (modes,))
  159. changes = ([], [])
  160. direction = None
  161. count = -1
  162. for ch in modes:
  163. if ch in '+-':
  164. if count == 0:
  165. raise IRCBadModes('Empty mode sequence: %r' % (modes,))
  166. direction = '+-'.index(ch)
  167. count = 0
  168. else:
  169. param = None
  170. if ch in paramModes[direction]:
  171. try:
  172. param = params.pop(0)
  173. except IndexError:
  174. raise IRCBadModes('Not enough parameters: %r' % (ch,))
  175. changes[direction].append((ch, param))
  176. count += 1
  177. if len(params) > 0:
  178. raise IRCBadModes('Too many parameters: %r %r' % (modes, params))
  179. if count == 0:
  180. raise IRCBadModes('Empty mode sequence: %r' % (modes,))
  181. return changes
  182. class IRC(protocol.Protocol):
  183. """
  184. Internet Relay Chat server protocol.
  185. """
  186. buffer = ""
  187. hostname = None
  188. encoding = None
  189. def connectionMade(self):
  190. self.channels = []
  191. if self.hostname is None:
  192. self.hostname = socket.getfqdn()
  193. def sendLine(self, line):
  194. line = line + CR + LF
  195. if isinstance(line, unicode):
  196. useEncoding = self.encoding if self.encoding else "utf-8"
  197. line = line.encode(useEncoding)
  198. self.transport.write(line)
  199. def sendMessage(self, command, *parameter_list, **prefix):
  200. """
  201. Send a line formatted as an IRC message.
  202. First argument is the command, all subsequent arguments are parameters
  203. to that command. If a prefix is desired, it may be specified with the
  204. keyword argument 'prefix'.
  205. The L{sendCommand} method is generally preferred over this one.
  206. Notably, this method does not support sending message tags, while the
  207. L{sendCommand} method does.
  208. """
  209. if not command:
  210. raise ValueError("IRC message requires a command.")
  211. if ' ' in command or command[0] == ':':
  212. # Not the ONLY way to screw up, but provides a little
  213. # sanity checking to catch likely dumb mistakes.
  214. raise ValueError("Somebody screwed up, 'cuz this doesn't" \
  215. " look like a command to me: %s" % command)
  216. line = ' '.join([command] + list(parameter_list))
  217. if 'prefix' in prefix:
  218. line = ":%s %s" % (prefix['prefix'], line)
  219. self.sendLine(line)
  220. if len(parameter_list) > 15:
  221. log.msg("Message has %d parameters (RFC allows 15):\n%s" %
  222. (len(parameter_list), line))
  223. def sendCommand(self, command, parameters, prefix=None, tags=None):
  224. """
  225. Send to the remote peer a line formatted as an IRC message.
  226. @param command: The command or numeric to send.
  227. @type command: L{unicode}
  228. @param parameters: The parameters to send with the command.
  229. @type parameters: A L{tuple} or L{list} of L{unicode} parameters
  230. @param prefix: The prefix to send with the command. If not
  231. given, no prefix is sent.
  232. @type prefix: L{unicode}
  233. @param tags: A dict of message tags. If not given, no message
  234. tags are sent. The dict key should be the name of the tag
  235. to send as a string; the value should be the unescaped value
  236. to send with the tag, or either None or "" if no value is to
  237. be sent with the tag.
  238. @type tags: L{dict} of tags (L{unicode}) => values (L{unicode})
  239. @see: U{https://ircv3.net/specs/core/message-tags-3.2.html}
  240. """
  241. if not command:
  242. raise ValueError("IRC message requires a command.")
  243. if " " in command or command[0] == ":":
  244. # Not the ONLY way to screw up, but provides a little
  245. # sanity checking to catch likely dumb mistakes.
  246. raise ValueError('Invalid command: "%s"' % (command,))
  247. if tags is None:
  248. tags = {}
  249. line = " ".join([command] + list(parameters))
  250. if prefix:
  251. line = ":%s %s" % (prefix, line)
  252. if tags:
  253. tagStr = self._stringTags(tags)
  254. line = "@%s %s" % (tagStr, line)
  255. self.sendLine(line)
  256. if len(parameters) > 15:
  257. log.msg("Message has %d parameters (RFC allows 15):\n%s" %
  258. (len(parameters), line))
  259. def _stringTags(self, tags):
  260. """
  261. Converts a tag dictionary to a string.
  262. @param tags: The tag dict passed to sendMsg.
  263. @rtype: L{unicode}
  264. @return: IRCv3-format tag string
  265. """
  266. self._validateTags(tags)
  267. tagStrings = []
  268. for tag, value in tags.items():
  269. if value:
  270. tagStrings.append("%s=%s" % (tag, self._escapeTagValue(value)))
  271. else:
  272. tagStrings.append(tag)
  273. return ";".join(tagStrings)
  274. def _validateTags(self, tags):
  275. """
  276. Checks the tag dict for errors and raises L{ValueError} if an
  277. error is found.
  278. @param tags: The tag dict passed to sendMsg.
  279. """
  280. for tag, value in tags.items():
  281. if not tag:
  282. raise ValueError("A tag name is required.")
  283. for char in tag:
  284. if not char.isalnum() and char not in ("-", "/", "."):
  285. raise ValueError("Tag contains invalid characters.")
  286. def _escapeTagValue(self, value):
  287. """
  288. Escape the given tag value according to U{escaping rules in IRCv3
  289. <https://ircv3.net/specs/core/message-tags-3.2.html>}.
  290. @param value: The string value to escape.
  291. @type value: L{str}
  292. @return: The escaped string for sending as a message value
  293. @rtype: L{str}
  294. """
  295. return (value.replace("\\", "\\\\")
  296. .replace(";", "\\:")
  297. .replace(" ", "\\s")
  298. .replace("\r", "\\r")
  299. .replace("\n", "\\n")
  300. )
  301. def dataReceived(self, data):
  302. """
  303. This hack is to support mIRC, which sends LF only, even though the RFC
  304. says CRLF. (Also, the flexibility of LineReceiver to turn "line mode"
  305. on and off was not required.)
  306. """
  307. if isinstance(data, bytes):
  308. data = data.decode("utf-8")
  309. lines = (self.buffer + data).split(LF)
  310. # Put the (possibly empty) element after the last LF back in the
  311. # buffer
  312. self.buffer = lines.pop()
  313. for line in lines:
  314. if len(line) <= 2:
  315. # This is a blank line, at best.
  316. continue
  317. if line[-1] == CR:
  318. line = line[:-1]
  319. prefix, command, params = parsemsg(line)
  320. # mIRC is a big pile of doo-doo
  321. command = command.upper()
  322. # DEBUG: log.msg( "%s %s %s" % (prefix, command, params))
  323. self.handleCommand(command, prefix, params)
  324. def handleCommand(self, command, prefix, params):
  325. """
  326. Determine the function to call for the given command and call it with
  327. the given arguments.
  328. @param command: The IRC command to determine the function for.
  329. @type command: L{bytes}
  330. @param prefix: The prefix of the IRC message (as returned by
  331. L{parsemsg}).
  332. @type prefix: L{bytes}
  333. @param params: A list of parameters to call the function with.
  334. @type params: L{list}
  335. """
  336. method = getattr(self, "irc_%s" % command, None)
  337. try:
  338. if method is not None:
  339. method(prefix, params)
  340. else:
  341. self.irc_unknown(prefix, command, params)
  342. except:
  343. log.deferr()
  344. def irc_unknown(self, prefix, command, params):
  345. """
  346. Called by L{handleCommand} on a command that doesn't have a defined
  347. handler. Subclasses should override this method.
  348. """
  349. raise NotImplementedError(command, prefix, params)
  350. # Helper methods
  351. def privmsg(self, sender, recip, message):
  352. """
  353. Send a message to a channel or user
  354. @type sender: C{str} or C{unicode}
  355. @param sender: Who is sending this message. Should be of the form
  356. username!ident@hostmask (unless you know better!).
  357. @type recip: C{str} or C{unicode}
  358. @param recip: The recipient of this message. If a channel, it must
  359. start with a channel prefix.
  360. @type message: C{str} or C{unicode}
  361. @param message: The message being sent.
  362. """
  363. self.sendCommand("PRIVMSG", (recip, ":%s" % (lowQuote(message),)), sender)
  364. def notice(self, sender, recip, message):
  365. """
  366. Send a "notice" to a channel or user.
  367. Notices differ from privmsgs in that the RFC claims they are different.
  368. Robots are supposed to send notices and not respond to them. Clients
  369. typically display notices differently from privmsgs.
  370. @type sender: C{str} or C{unicode}
  371. @param sender: Who is sending this message. Should be of the form
  372. username!ident@hostmask (unless you know better!).
  373. @type recip: C{str} or C{unicode}
  374. @param recip: The recipient of this message. If a channel, it must
  375. start with a channel prefix.
  376. @type message: C{str} or C{unicode}
  377. @param message: The message being sent.
  378. """
  379. self.sendCommand("NOTICE", (recip, ":%s" % (message,)), sender)
  380. def action(self, sender, recip, message):
  381. """
  382. Send an action to a channel or user.
  383. @type sender: C{str} or C{unicode}
  384. @param sender: Who is sending this message. Should be of the form
  385. username!ident@hostmask (unless you know better!).
  386. @type recip: C{str} or C{unicode}
  387. @param recip: The recipient of this message. If a channel, it must
  388. start with a channel prefix.
  389. @type message: C{str} or C{unicode}
  390. @param message: The action being sent.
  391. """
  392. self.sendLine(":%s ACTION %s :%s" % (sender, recip, message))
  393. def topic(self, user, channel, topic, author=None):
  394. """
  395. Send the topic to a user.
  396. @type user: C{str} or C{unicode}
  397. @param user: The user receiving the topic. Only their nickname, not
  398. the full hostmask.
  399. @type channel: C{str} or C{unicode}
  400. @param channel: The channel for which this is the topic.
  401. @type topic: C{str} or C{unicode} or L{None}
  402. @param topic: The topic string, unquoted, or None if there is no topic.
  403. @type author: C{str} or C{unicode}
  404. @param author: If the topic is being changed, the full username and
  405. hostmask of the person changing it.
  406. """
  407. if author is None:
  408. if topic is None:
  409. self.sendLine(':%s %s %s %s :%s' % (
  410. self.hostname, RPL_NOTOPIC, user, channel, 'No topic is set.'))
  411. else:
  412. self.sendLine(":%s %s %s %s :%s" % (
  413. self.hostname, RPL_TOPIC, user, channel, lowQuote(topic)))
  414. else:
  415. self.sendLine(":%s TOPIC %s :%s" % (author, channel, lowQuote(topic)))
  416. def topicAuthor(self, user, channel, author, date):
  417. """
  418. Send the author of and time at which a topic was set for the given
  419. channel.
  420. This sends a 333 reply message, which is not part of the IRC RFC.
  421. @type user: C{str} or C{unicode}
  422. @param user: The user receiving the topic. Only their nickname, not
  423. the full hostmask.
  424. @type channel: C{str} or C{unicode}
  425. @param channel: The channel for which this information is relevant.
  426. @type author: C{str} or C{unicode}
  427. @param author: The nickname (without hostmask) of the user who last set
  428. the topic.
  429. @type date: C{int}
  430. @param date: A POSIX timestamp (number of seconds since the epoch) at
  431. which the topic was last set.
  432. """
  433. self.sendLine(':%s %d %s %s %s %d' % (
  434. self.hostname, 333, user, channel, author, date))
  435. def names(self, user, channel, names):
  436. """
  437. Send the names of a channel's participants to a user.
  438. @type user: C{str} or C{unicode}
  439. @param user: The user receiving the name list. Only their nickname,
  440. not the full hostmask.
  441. @type channel: C{str} or C{unicode}
  442. @param channel: The channel for which this is the namelist.
  443. @type names: C{list} of C{str} or C{unicode}
  444. @param names: The names to send.
  445. """
  446. # XXX If unicode is given, these limits are not quite correct
  447. prefixLength = len(channel) + len(user) + 10
  448. namesLength = 512 - prefixLength
  449. L = []
  450. count = 0
  451. for n in names:
  452. if count + len(n) + 1 > namesLength:
  453. self.sendLine(":%s %s %s = %s :%s" % (
  454. self.hostname, RPL_NAMREPLY, user, channel, ' '.join(L)))
  455. L = [n]
  456. count = len(n)
  457. else:
  458. L.append(n)
  459. count += len(n) + 1
  460. if L:
  461. self.sendLine(":%s %s %s = %s :%s" % (
  462. self.hostname, RPL_NAMREPLY, user, channel, ' '.join(L)))
  463. self.sendLine(":%s %s %s %s :End of /NAMES list" % (
  464. self.hostname, RPL_ENDOFNAMES, user, channel))
  465. def who(self, user, channel, memberInfo):
  466. """
  467. Send a list of users participating in a channel.
  468. @type user: C{str} or C{unicode}
  469. @param user: The user receiving this member information. Only their
  470. nickname, not the full hostmask.
  471. @type channel: C{str} or C{unicode}
  472. @param channel: The channel for which this is the member information.
  473. @type memberInfo: C{list} of C{tuples}
  474. @param memberInfo: For each member of the given channel, a 7-tuple
  475. containing their username, their hostmask, the server to which they
  476. are connected, their nickname, the letter "H" or "G" (standing for
  477. "Here" or "Gone"), the hopcount from C{user} to this member, and
  478. this member's real name.
  479. """
  480. for info in memberInfo:
  481. (username, hostmask, server, nickname, flag, hops, realName) = info
  482. assert flag in ("H", "G")
  483. self.sendLine(":%s %s %s %s %s %s %s %s %s :%d %s" % (
  484. self.hostname, RPL_WHOREPLY, user, channel,
  485. username, hostmask, server, nickname, flag, hops, realName))
  486. self.sendLine(":%s %s %s %s :End of /WHO list." % (
  487. self.hostname, RPL_ENDOFWHO, user, channel))
  488. def whois(self, user, nick, username, hostname, realName, server, serverInfo, oper, idle, signOn, channels):
  489. """
  490. Send information about the state of a particular user.
  491. @type user: C{str} or C{unicode}
  492. @param user: The user receiving this information. Only their nickname,
  493. not the full hostmask.
  494. @type nick: C{str} or C{unicode}
  495. @param nick: The nickname of the user this information describes.
  496. @type username: C{str} or C{unicode}
  497. @param username: The user's username (eg, ident response)
  498. @type hostname: C{str}
  499. @param hostname: The user's hostmask
  500. @type realName: C{str} or C{unicode}
  501. @param realName: The user's real name
  502. @type server: C{str} or C{unicode}
  503. @param server: The name of the server to which the user is connected
  504. @type serverInfo: C{str} or C{unicode}
  505. @param serverInfo: A descriptive string about that server
  506. @type oper: C{bool}
  507. @param oper: Indicates whether the user is an IRC operator
  508. @type idle: C{int}
  509. @param idle: The number of seconds since the user last sent a message
  510. @type signOn: C{int}
  511. @param signOn: A POSIX timestamp (number of seconds since the epoch)
  512. indicating the time the user signed on
  513. @type channels: C{list} of C{str} or C{unicode}
  514. @param channels: A list of the channels which the user is participating in
  515. """
  516. self.sendLine(":%s %s %s %s %s %s * :%s" % (
  517. self.hostname, RPL_WHOISUSER, user, nick, username, hostname, realName))
  518. self.sendLine(":%s %s %s %s %s :%s" % (
  519. self.hostname, RPL_WHOISSERVER, user, nick, server, serverInfo))
  520. if oper:
  521. self.sendLine(":%s %s %s %s :is an IRC operator" % (
  522. self.hostname, RPL_WHOISOPERATOR, user, nick))
  523. self.sendLine(":%s %s %s %s %d %d :seconds idle, signon time" % (
  524. self.hostname, RPL_WHOISIDLE, user, nick, idle, signOn))
  525. self.sendLine(":%s %s %s %s :%s" % (
  526. self.hostname, RPL_WHOISCHANNELS, user, nick, ' '.join(channels)))
  527. self.sendLine(":%s %s %s %s :End of WHOIS list." % (
  528. self.hostname, RPL_ENDOFWHOIS, user, nick))
  529. def join(self, who, where):
  530. """
  531. Send a join message.
  532. @type who: C{str} or C{unicode}
  533. @param who: The name of the user joining. Should be of the form
  534. username!ident@hostmask (unless you know better!).
  535. @type where: C{str} or C{unicode}
  536. @param where: The channel the user is joining.
  537. """
  538. self.sendLine(":%s JOIN %s" % (who, where))
  539. def part(self, who, where, reason=None):
  540. """
  541. Send a part message.
  542. @type who: C{str} or C{unicode}
  543. @param who: The name of the user joining. Should be of the form
  544. username!ident@hostmask (unless you know better!).
  545. @type where: C{str} or C{unicode}
  546. @param where: The channel the user is joining.
  547. @type reason: C{str} or C{unicode}
  548. @param reason: A string describing the misery which caused this poor
  549. soul to depart.
  550. """
  551. if reason:
  552. self.sendLine(":%s PART %s :%s" % (who, where, reason))
  553. else:
  554. self.sendLine(":%s PART %s" % (who, where))
  555. def channelMode(self, user, channel, mode, *args):
  556. """
  557. Send information about the mode of a channel.
  558. @type user: C{str} or C{unicode}
  559. @param user: The user receiving the name list. Only their nickname,
  560. not the full hostmask.
  561. @type channel: C{str} or C{unicode}
  562. @param channel: The channel for which this is the namelist.
  563. @type mode: C{str}
  564. @param mode: A string describing this channel's modes.
  565. @param args: Any additional arguments required by the modes.
  566. """
  567. self.sendLine(":%s %s %s %s %s %s" % (
  568. self.hostname, RPL_CHANNELMODEIS, user, channel, mode, ' '.join(args)))
  569. class ServerSupportedFeatures(_CommandDispatcherMixin):
  570. """
  571. Handle ISUPPORT messages.
  572. Feature names match those in the ISUPPORT RFC draft identically.
  573. Information regarding the specifics of ISUPPORT was gleaned from
  574. <http://www.irc.org/tech_docs/draft-brocklesby-irc-isupport-03.txt>.
  575. """
  576. prefix = 'isupport'
  577. def __init__(self):
  578. self._features = {
  579. 'CHANNELLEN': 200,
  580. 'CHANTYPES': tuple('#&'),
  581. 'MODES': 3,
  582. 'NICKLEN': 9,
  583. 'PREFIX': self._parsePrefixParam('(ovh)@+%'),
  584. # The ISUPPORT draft explicitly says that there is no default for
  585. # CHANMODES, but we're defaulting it here to handle the case where
  586. # the IRC server doesn't send us any ISUPPORT information, since
  587. # IRCClient.getChannelModeParams relies on this value.
  588. 'CHANMODES': self._parseChanModesParam(['b', '', 'lk', ''])}
  589. @classmethod
  590. def _splitParamArgs(cls, params, valueProcessor=None):
  591. """
  592. Split ISUPPORT parameter arguments.
  593. Values can optionally be processed by C{valueProcessor}.
  594. For example::
  595. >>> ServerSupportedFeatures._splitParamArgs(['A:1', 'B:2'])
  596. (('A', '1'), ('B', '2'))
  597. @type params: C{iterable} of C{str}
  598. @type valueProcessor: C{callable} taking {str}
  599. @param valueProcessor: Callable to process argument values, or L{None}
  600. to perform no processing
  601. @rtype: C{list} of C{(str, object)}
  602. @return: Sequence of C{(name, processedValue)}
  603. """
  604. if valueProcessor is None:
  605. valueProcessor = lambda x: x
  606. def _parse():
  607. for param in params:
  608. if ':' not in param:
  609. param += ':'
  610. a, b = param.split(':', 1)
  611. yield a, valueProcessor(b)
  612. return list(_parse())
  613. @classmethod
  614. def _unescapeParamValue(cls, value):
  615. """
  616. Unescape an ISUPPORT parameter.
  617. The only form of supported escape is C{\\xHH}, where HH must be a valid
  618. 2-digit hexadecimal number.
  619. @rtype: C{str}
  620. """
  621. def _unescape():
  622. parts = value.split('\\x')
  623. # The first part can never be preceded by the escape.
  624. yield parts.pop(0)
  625. for s in parts:
  626. octet, rest = s[:2], s[2:]
  627. try:
  628. octet = int(octet, 16)
  629. except ValueError:
  630. raise ValueError('Invalid hex octet: %r' % (octet,))
  631. yield chr(octet) + rest
  632. if '\\x' not in value:
  633. return value
  634. return ''.join(_unescape())
  635. @classmethod
  636. def _splitParam(cls, param):
  637. """
  638. Split an ISUPPORT parameter.
  639. @type param: C{str}
  640. @rtype: C{(str, list)}
  641. @return C{(key, arguments)}
  642. """
  643. if '=' not in param:
  644. param += '='
  645. key, value = param.split('=', 1)
  646. return key, [cls._unescapeParamValue(v) for v in value.split(',')]
  647. @classmethod
  648. def _parsePrefixParam(cls, prefix):
  649. """
  650. Parse the ISUPPORT "PREFIX" parameter.
  651. The order in which the parameter arguments appear is significant, the
  652. earlier a mode appears the more privileges it gives.
  653. @rtype: C{dict} mapping C{str} to C{(str, int)}
  654. @return: A dictionary mapping a mode character to a two-tuple of
  655. C({symbol, priority)}, the lower a priority (the lowest being
  656. C{0}) the more privileges it gives
  657. """
  658. if not prefix:
  659. return None
  660. if prefix[0] != '(' and ')' not in prefix:
  661. raise ValueError('Malformed PREFIX parameter')
  662. modes, symbols = prefix.split(')', 1)
  663. symbols = zip(symbols, range(len(symbols)))
  664. modes = modes[1:]
  665. return dict(zip(modes, symbols))
  666. @classmethod
  667. def _parseChanModesParam(self, params):
  668. """
  669. Parse the ISUPPORT "CHANMODES" parameter.
  670. See L{isupport_CHANMODES} for a detailed explanation of this parameter.
  671. """
  672. names = ('addressModes', 'param', 'setParam', 'noParam')
  673. if len(params) > len(names):
  674. raise ValueError(
  675. 'Expecting a maximum of %d channel mode parameters, got %d' % (
  676. len(names), len(params)))
  677. items = map(lambda key, value: (key, value or ''), names, params)
  678. return dict(items)
  679. def getFeature(self, feature, default=None):
  680. """
  681. Get a server supported feature's value.
  682. A feature with the value L{None} is equivalent to the feature being
  683. unsupported.
  684. @type feature: C{str}
  685. @param feature: Feature name
  686. @type default: C{object}
  687. @param default: The value to default to, assuming that C{feature}
  688. is not supported
  689. @return: Feature value
  690. """
  691. return self._features.get(feature, default)
  692. def hasFeature(self, feature):
  693. """
  694. Determine whether a feature is supported or not.
  695. @rtype: C{bool}
  696. """
  697. return self.getFeature(feature) is not None
  698. def parse(self, params):
  699. """
  700. Parse ISUPPORT parameters.
  701. If an unknown parameter is encountered, it is simply added to the
  702. dictionary, keyed by its name, as a tuple of the parameters provided.
  703. @type params: C{iterable} of C{str}
  704. @param params: Iterable of ISUPPORT parameters to parse
  705. """
  706. for param in params:
  707. key, value = self._splitParam(param)
  708. if key.startswith('-'):
  709. self._features.pop(key[1:], None)
  710. else:
  711. self._features[key] = self.dispatch(key, value)
  712. def isupport_unknown(self, command, params):
  713. """
  714. Unknown ISUPPORT parameter.
  715. """
  716. return tuple(params)
  717. def isupport_CHANLIMIT(self, params):
  718. """
  719. The maximum number of each channel type a user may join.
  720. """
  721. return self._splitParamArgs(params, _intOrDefault)
  722. def isupport_CHANMODES(self, params):
  723. """
  724. Available channel modes.
  725. There are 4 categories of channel mode::
  726. addressModes - Modes that add or remove an address to or from a
  727. list, these modes always take a parameter.
  728. param - Modes that change a setting on a channel, these modes
  729. always take a parameter.
  730. setParam - Modes that change a setting on a channel, these modes
  731. only take a parameter when being set.
  732. noParam - Modes that change a setting on a channel, these modes
  733. never take a parameter.
  734. """
  735. try:
  736. return self._parseChanModesParam(params)
  737. except ValueError:
  738. return self.getFeature('CHANMODES')
  739. def isupport_CHANNELLEN(self, params):
  740. """
  741. Maximum length of a channel name a client may create.
  742. """
  743. return _intOrDefault(params[0], self.getFeature('CHANNELLEN'))
  744. def isupport_CHANTYPES(self, params):
  745. """
  746. Valid channel prefixes.
  747. """
  748. return tuple(params[0])
  749. def isupport_EXCEPTS(self, params):
  750. """
  751. Mode character for "ban exceptions".
  752. The presence of this parameter indicates that the server supports
  753. this functionality.
  754. """
  755. return params[0] or 'e'
  756. def isupport_IDCHAN(self, params):
  757. """
  758. Safe channel identifiers.
  759. The presence of this parameter indicates that the server supports
  760. this functionality.
  761. """
  762. return self._splitParamArgs(params)
  763. def isupport_INVEX(self, params):
  764. """
  765. Mode character for "invite exceptions".
  766. The presence of this parameter indicates that the server supports
  767. this functionality.
  768. """
  769. return params[0] or 'I'
  770. def isupport_KICKLEN(self, params):
  771. """
  772. Maximum length of a kick message a client may provide.
  773. """
  774. return _intOrDefault(params[0])
  775. def isupport_MAXLIST(self, params):
  776. """
  777. Maximum number of "list modes" a client may set on a channel at once.
  778. List modes are identified by the "addressModes" key in CHANMODES.
  779. """
  780. return self._splitParamArgs(params, _intOrDefault)
  781. def isupport_MODES(self, params):
  782. """
  783. Maximum number of modes accepting parameters that may be sent, by a
  784. client, in a single MODE command.
  785. """
  786. return _intOrDefault(params[0])
  787. def isupport_NETWORK(self, params):
  788. """
  789. IRC network name.
  790. """
  791. return params[0]
  792. def isupport_NICKLEN(self, params):
  793. """
  794. Maximum length of a nickname the client may use.
  795. """
  796. return _intOrDefault(params[0], self.getFeature('NICKLEN'))
  797. def isupport_PREFIX(self, params):
  798. """
  799. Mapping of channel modes that clients may have to status flags.
  800. """
  801. try:
  802. return self._parsePrefixParam(params[0])
  803. except ValueError:
  804. return self.getFeature('PREFIX')
  805. def isupport_SAFELIST(self, params):
  806. """
  807. Flag indicating that a client may request a LIST without being
  808. disconnected due to the large amount of data generated.
  809. """
  810. return True
  811. def isupport_STATUSMSG(self, params):
  812. """
  813. The server supports sending messages to only to clients on a channel
  814. with a specific status.
  815. """
  816. return params[0]
  817. def isupport_TARGMAX(self, params):
  818. """
  819. Maximum number of targets allowable for commands that accept multiple
  820. targets.
  821. """
  822. return dict(self._splitParamArgs(params, _intOrDefault))
  823. def isupport_TOPICLEN(self, params):
  824. """
  825. Maximum length of a topic that may be set.
  826. """
  827. return _intOrDefault(params[0])
  828. class IRCClient(basic.LineReceiver):
  829. """
  830. Internet Relay Chat client protocol, with sprinkles.
  831. In addition to providing an interface for an IRC client protocol,
  832. this class also contains reasonable implementations of many common
  833. CTCP methods.
  834. TODO
  835. ====
  836. - Limit the length of messages sent (because the IRC server probably
  837. does).
  838. - Add flood protection/rate limiting for my CTCP replies.
  839. - NickServ cooperation. (a mix-in?)
  840. @ivar nickname: Nickname the client will use.
  841. @ivar password: Password used to log on to the server. May be L{None}.
  842. @ivar realname: Supplied to the server during login as the "Real name"
  843. or "ircname". May be L{None}.
  844. @ivar username: Supplied to the server during login as the "User name".
  845. May be L{None}
  846. @ivar userinfo: Sent in reply to a C{USERINFO} CTCP query. If L{None}, no
  847. USERINFO reply will be sent.
  848. "This is used to transmit a string which is settable by
  849. the user (and never should be set by the client)."
  850. @ivar fingerReply: Sent in reply to a C{FINGER} CTCP query. If L{None}, no
  851. FINGER reply will be sent.
  852. @type fingerReply: Callable or String
  853. @ivar versionName: CTCP VERSION reply, client name. If L{None}, no VERSION
  854. reply will be sent.
  855. @type versionName: C{str}, or None.
  856. @ivar versionNum: CTCP VERSION reply, client version.
  857. @type versionNum: C{str}, or None.
  858. @ivar versionEnv: CTCP VERSION reply, environment the client is running in.
  859. @type versionEnv: C{str}, or None.
  860. @ivar sourceURL: CTCP SOURCE reply, a URL where the source code of this
  861. client may be found. If L{None}, no SOURCE reply will be sent.
  862. @ivar lineRate: Minimum delay between lines sent to the server. If
  863. L{None}, no delay will be imposed.
  864. @type lineRate: Number of Seconds.
  865. @ivar motd: Either L{None} or, between receipt of I{RPL_MOTDSTART} and
  866. I{RPL_ENDOFMOTD}, a L{list} of L{str}, each of which is the content
  867. of an I{RPL_MOTD} message.
  868. @ivar erroneousNickFallback: Default nickname assigned when an unregistered
  869. client triggers an C{ERR_ERRONEUSNICKNAME} while trying to register
  870. with an illegal nickname.
  871. @type erroneousNickFallback: C{str}
  872. @ivar _registered: Whether or not the user is registered. It becomes True
  873. once a welcome has been received from the server.
  874. @type _registered: C{bool}
  875. @ivar _attemptedNick: The nickname that will try to get registered. It may
  876. change if it is illegal or already taken. L{nickname} becomes the
  877. L{_attemptedNick} that is successfully registered.
  878. @type _attemptedNick: C{str}
  879. @type supported: L{ServerSupportedFeatures}
  880. @ivar supported: Available ISUPPORT features on the server
  881. @type hostname: C{str}
  882. @ivar hostname: Host name of the IRC server the client is connected to.
  883. Initially the host name is L{None} and later is set to the host name
  884. from which the I{RPL_WELCOME} message is received.
  885. @type _heartbeat: L{task.LoopingCall}
  886. @ivar _heartbeat: Looping call to perform the keepalive by calling
  887. L{IRCClient._sendHeartbeat} every L{heartbeatInterval} seconds, or
  888. L{None} if there is no heartbeat.
  889. @type heartbeatInterval: C{float}
  890. @ivar heartbeatInterval: Interval, in seconds, to send I{PING} messages to
  891. the server as a form of keepalive, defaults to 120 seconds. Use L{None}
  892. to disable the heartbeat.
  893. """
  894. hostname = None
  895. motd = None
  896. nickname = 'irc'
  897. password = None
  898. realname = None
  899. username = None
  900. ### Responses to various CTCP queries.
  901. userinfo = None
  902. # fingerReply is a callable returning a string, or a str()able object.
  903. fingerReply = None
  904. versionName = None
  905. versionNum = None
  906. versionEnv = None
  907. sourceURL = "http://twistedmatrix.com/downloads/"
  908. dcc_destdir = '.'
  909. dcc_sessions = None
  910. # If this is false, no attempt will be made to identify
  911. # ourself to the server.
  912. performLogin = 1
  913. lineRate = None
  914. _queue = None
  915. _queueEmptying = None
  916. delimiter = b'\n' # b'\r\n' will also work (see dataReceived)
  917. __pychecker__ = 'unusednames=params,prefix,channel'
  918. _registered = False
  919. _attemptedNick = ''
  920. erroneousNickFallback = 'defaultnick'
  921. _heartbeat = None
  922. heartbeatInterval = 120
  923. def _reallySendLine(self, line):
  924. quoteLine = lowQuote(line)
  925. if isinstance(quoteLine, unicode):
  926. quoteLine = quoteLine.encode("utf-8")
  927. quoteLine += b'\r'
  928. return basic.LineReceiver.sendLine(self, quoteLine)
  929. def sendLine(self, line):
  930. if self.lineRate is None:
  931. self._reallySendLine(line)
  932. else:
  933. self._queue.append(line)
  934. if not self._queueEmptying:
  935. self._sendLine()
  936. def _sendLine(self):
  937. if self._queue:
  938. self._reallySendLine(self._queue.pop(0))
  939. self._queueEmptying = reactor.callLater(self.lineRate,
  940. self._sendLine)
  941. else:
  942. self._queueEmptying = None
  943. def connectionLost(self, reason):
  944. basic.LineReceiver.connectionLost(self, reason)
  945. self.stopHeartbeat()
  946. def _createHeartbeat(self):
  947. """
  948. Create the heartbeat L{LoopingCall}.
  949. """
  950. return task.LoopingCall(self._sendHeartbeat)
  951. def _sendHeartbeat(self):
  952. """
  953. Send a I{PING} message to the IRC server as a form of keepalive.
  954. """
  955. self.sendLine('PING ' + self.hostname)
  956. def stopHeartbeat(self):
  957. """
  958. Stop sending I{PING} messages to keep the connection to the server
  959. alive.
  960. @since: 11.1
  961. """
  962. if self._heartbeat is not None:
  963. self._heartbeat.stop()
  964. self._heartbeat = None
  965. def startHeartbeat(self):
  966. """
  967. Start sending I{PING} messages every L{IRCClient.heartbeatInterval}
  968. seconds to keep the connection to the server alive during periods of no
  969. activity.
  970. @since: 11.1
  971. """
  972. self.stopHeartbeat()
  973. if self.heartbeatInterval is None:
  974. return
  975. self._heartbeat = self._createHeartbeat()
  976. self._heartbeat.start(self.heartbeatInterval, now=False)
  977. ### Interface level client->user output methods
  978. ###
  979. ### You'll want to override these.
  980. ### Methods relating to the server itself
  981. def created(self, when):
  982. """
  983. Called with creation date information about the server, usually at logon.
  984. @type when: C{str}
  985. @param when: A string describing when the server was created, probably.
  986. """
  987. def yourHost(self, info):
  988. """
  989. Called with daemon information about the server, usually at logon.
  990. @type info: C{str}
  991. @param when: A string describing what software the server is running, probably.
  992. """
  993. def myInfo(self, servername, version, umodes, cmodes):
  994. """
  995. Called with information about the server, usually at logon.
  996. @type servername: C{str}
  997. @param servername: The hostname of this server.
  998. @type version: C{str}
  999. @param version: A description of what software this server runs.
  1000. @type umodes: C{str}
  1001. @param umodes: All the available user modes.
  1002. @type cmodes: C{str}
  1003. @param cmodes: All the available channel modes.
  1004. """
  1005. def luserClient(self, info):
  1006. """
  1007. Called with information about the number of connections, usually at logon.
  1008. @type info: C{str}
  1009. @param info: A description of the number of clients and servers
  1010. connected to the network, probably.
  1011. """
  1012. def bounce(self, info):
  1013. """
  1014. Called with information about where the client should reconnect.
  1015. @type info: C{str}
  1016. @param info: A plaintext description of the address that should be
  1017. connected to.
  1018. """
  1019. def isupport(self, options):
  1020. """
  1021. Called with various information about what the server supports.
  1022. @type options: C{list} of C{str}
  1023. @param options: Descriptions of features or limits of the server, possibly
  1024. in the form "NAME=VALUE".
  1025. """
  1026. def luserChannels(self, channels):
  1027. """
  1028. Called with the number of channels existent on the server.
  1029. @type channels: C{int}
  1030. """
  1031. def luserOp(self, ops):
  1032. """
  1033. Called with the number of ops logged on to the server.
  1034. @type ops: C{int}
  1035. """
  1036. def luserMe(self, info):
  1037. """
  1038. Called with information about the server connected to.
  1039. @type info: C{str}
  1040. @param info: A plaintext string describing the number of users and servers
  1041. connected to this server.
  1042. """
  1043. ### Methods involving me directly
  1044. def privmsg(self, user, channel, message):
  1045. """
  1046. Called when I have a message from a user to me or a channel.
  1047. """
  1048. pass
  1049. def joined(self, channel):
  1050. """
  1051. Called when I finish joining a channel.
  1052. channel has the starting character (C{'#'}, C{'&'}, C{'!'}, or C{'+'})
  1053. intact.
  1054. """
  1055. def left(self, channel):
  1056. """
  1057. Called when I have left a channel.
  1058. channel has the starting character (C{'#'}, C{'&'}, C{'!'}, or C{'+'})
  1059. intact.
  1060. """
  1061. def noticed(self, user, channel, message):
  1062. """
  1063. Called when I have a notice from a user to me or a channel.
  1064. If the client makes any automated replies, it must not do so in
  1065. response to a NOTICE message, per the RFC::
  1066. The difference between NOTICE and PRIVMSG is that
  1067. automatic replies MUST NEVER be sent in response to a
  1068. NOTICE message. [...] The object of this rule is to avoid
  1069. loops between clients automatically sending something in
  1070. response to something it received.
  1071. """
  1072. def modeChanged(self, user, channel, set, modes, args):
  1073. """
  1074. Called when users or channel's modes are changed.
  1075. @type user: C{str}
  1076. @param user: The user and hostmask which instigated this change.
  1077. @type channel: C{str}
  1078. @param channel: The channel where the modes are changed. If args is
  1079. empty the channel for which the modes are changing. If the changes are
  1080. at server level it could be equal to C{user}.
  1081. @type set: C{bool} or C{int}
  1082. @param set: True if the mode(s) is being added, False if it is being
  1083. removed. If some modes are added and others removed at the same time
  1084. this function will be called twice, the first time with all the added
  1085. modes, the second with the removed ones. (To change this behaviour
  1086. override the irc_MODE method)
  1087. @type modes: C{str}
  1088. @param modes: The mode or modes which are being changed.
  1089. @type args: C{tuple}
  1090. @param args: Any additional information required for the mode
  1091. change.
  1092. """
  1093. def pong(self, user, secs):
  1094. """
  1095. Called with the results of a CTCP PING query.
  1096. """
  1097. pass
  1098. def signedOn(self):
  1099. """
  1100. Called after successfully signing on to the server.
  1101. """
  1102. pass
  1103. def kickedFrom(self, channel, kicker, message):
  1104. """
  1105. Called when I am kicked from a channel.
  1106. """
  1107. pass
  1108. def nickChanged(self, nick):
  1109. """
  1110. Called when my nick has been changed.
  1111. """
  1112. self.nickname = nick
  1113. ### Things I observe other people doing in a channel.
  1114. def userJoined(self, user, channel):
  1115. """
  1116. Called when I see another user joining a channel.
  1117. """
  1118. pass
  1119. def userLeft(self, user, channel):
  1120. """
  1121. Called when I see another user leaving a channel.
  1122. """
  1123. pass
  1124. def userQuit(self, user, quitMessage):
  1125. """
  1126. Called when I see another user disconnect from the network.
  1127. """
  1128. pass
  1129. def userKicked(self, kickee, channel, kicker, message):
  1130. """
  1131. Called when I observe someone else being kicked from a channel.
  1132. """
  1133. pass
  1134. def action(self, user, channel, data):
  1135. """
  1136. Called when I see a user perform an ACTION on a channel.
  1137. """
  1138. pass
  1139. def topicUpdated(self, user, channel, newTopic):
  1140. """
  1141. In channel, user changed the topic to newTopic.
  1142. Also called when first joining a channel.
  1143. """
  1144. pass
  1145. def userRenamed(self, oldname, newname):
  1146. """
  1147. A user changed their name from oldname to newname.
  1148. """
  1149. pass
  1150. ### Information from the server.
  1151. def receivedMOTD(self, motd):
  1152. """
  1153. I received a message-of-the-day banner from the server.
  1154. motd is a list of strings, where each string was sent as a separate
  1155. message from the server. To display, you might want to use::
  1156. '\\n'.join(motd)
  1157. to get a nicely formatted string.
  1158. """
  1159. pass
  1160. ### user input commands, client->server
  1161. ### Your client will want to invoke these.
  1162. def join(self, channel, key=None):
  1163. """
  1164. Join a channel.
  1165. @type channel: C{str}
  1166. @param channel: The name of the channel to join. If it has no prefix,
  1167. C{'#'} will be prepended to it.
  1168. @type key: C{str}
  1169. @param key: If specified, the key used to join the channel.
  1170. """
  1171. if channel[0] not in CHANNEL_PREFIXES:
  1172. channel = '#' + channel
  1173. if key:
  1174. self.sendLine("JOIN %s %s" % (channel, key))
  1175. else:
  1176. self.sendLine("JOIN %s" % (channel,))
  1177. def leave(self, channel, reason=None):
  1178. """
  1179. Leave a channel.
  1180. @type channel: C{str}
  1181. @param channel: The name of the channel to leave. If it has no prefix,
  1182. C{'#'} will be prepended to it.
  1183. @type reason: C{str}
  1184. @param reason: If given, the reason for leaving.
  1185. """
  1186. if channel[0] not in CHANNEL_PREFIXES:
  1187. channel = '#' + channel
  1188. if reason:
  1189. self.sendLine("PART %s :%s" % (channel, reason))
  1190. else:
  1191. self.sendLine("PART %s" % (channel,))
  1192. def kick(self, channel, user, reason=None):
  1193. """
  1194. Attempt to kick a user from a channel.
  1195. @type channel: C{str}
  1196. @param channel: The name of the channel to kick the user from. If it has
  1197. no prefix, C{'#'} will be prepended to it.
  1198. @type user: C{str}
  1199. @param user: The nick of the user to kick.
  1200. @type reason: C{str}
  1201. @param reason: If given, the reason for kicking the user.
  1202. """
  1203. if channel[0] not in CHANNEL_PREFIXES:
  1204. channel = '#' + channel
  1205. if reason:
  1206. self.sendLine("KICK %s %s :%s" % (channel, user, reason))
  1207. else:
  1208. self.sendLine("KICK %s %s" % (channel, user))
  1209. part = leave
  1210. def invite(self, user, channel):
  1211. """
  1212. Attempt to invite user to channel
  1213. @type user: C{str}
  1214. @param user: The user to invite
  1215. @type channel: C{str}
  1216. @param channel: The channel to invite the user too
  1217. @since: 11.0
  1218. """
  1219. if channel[0] not in CHANNEL_PREFIXES:
  1220. channel = '#' + channel
  1221. self.sendLine("INVITE %s %s" % (user, channel))
  1222. def topic(self, channel, topic=None):
  1223. """
  1224. Attempt to set the topic of the given channel, or ask what it is.
  1225. If topic is None, then I sent a topic query instead of trying to set the
  1226. topic. The server should respond with a TOPIC message containing the
  1227. current topic of the given channel.
  1228. @type channel: C{str}
  1229. @param channel: The name of the channel to change the topic on. If it
  1230. has no prefix, C{'#'} will be prepended to it.
  1231. @type topic: C{str}
  1232. @param topic: If specified, what to set the topic to.
  1233. """
  1234. # << TOPIC #xtestx :fff
  1235. if channel[0] not in CHANNEL_PREFIXES:
  1236. channel = '#' + channel
  1237. if topic != None:
  1238. self.sendLine("TOPIC %s :%s" % (channel, topic))
  1239. else:
  1240. self.sendLine("TOPIC %s" % (channel,))
  1241. def mode(self, chan, set, modes, limit = None, user = None, mask = None):
  1242. """
  1243. Change the modes on a user or channel.
  1244. The C{limit}, C{user}, and C{mask} parameters are mutually exclusive.
  1245. @type chan: C{str}
  1246. @param chan: The name of the channel to operate on.
  1247. @type set: C{bool}
  1248. @param set: True to give the user or channel permissions and False to
  1249. remove them.
  1250. @type modes: C{str}
  1251. @param modes: The mode flags to set on the user or channel.
  1252. @type limit: C{int}
  1253. @param limit: In conjunction with the C{'l'} mode flag, limits the
  1254. number of users on the channel.
  1255. @type user: C{str}
  1256. @param user: The user to change the mode on.
  1257. @type mask: C{str}
  1258. @param mask: In conjunction with the C{'b'} mode flag, sets a mask of
  1259. users to be banned from the channel.
  1260. """
  1261. if set:
  1262. line = 'MODE %s +%s' % (chan, modes)
  1263. else:
  1264. line = 'MODE %s -%s' % (chan, modes)
  1265. if limit is not None:
  1266. line = '%s %d' % (line, limit)
  1267. elif user is not None:
  1268. line = '%s %s' % (line, user)
  1269. elif mask is not None:
  1270. line = '%s %s' % (line, mask)
  1271. self.sendLine(line)
  1272. def say(self, channel, message, length=None):
  1273. """
  1274. Send a message to a channel
  1275. @type channel: C{str}
  1276. @param channel: The channel to say the message on. If it has no prefix,
  1277. C{'#'} will be prepended to it.
  1278. @type message: C{str}
  1279. @param message: The message to say.
  1280. @type length: C{int}
  1281. @param length: The maximum number of octets to send at a time. This has
  1282. the effect of turning a single call to C{msg()} into multiple
  1283. commands to the server. This is useful when long messages may be
  1284. sent that would otherwise cause the server to kick us off or
  1285. silently truncate the text we are sending. If None is passed, the
  1286. entire message is always send in one command.
  1287. """
  1288. if channel[0] not in CHANNEL_PREFIXES:
  1289. channel = '#' + channel
  1290. self.msg(channel, message, length)
  1291. def _safeMaximumLineLength(self, command):
  1292. """
  1293. Estimate a safe maximum line length for the given command.
  1294. This is done by assuming the maximum values for nickname length,
  1295. realname and hostname combined with the command that needs to be sent
  1296. and some guessing. A theoretical maximum value is used because it is
  1297. possible that our nickname, username or hostname changes (on the server
  1298. side) while the length is still being calculated.
  1299. """
  1300. # :nickname!realname@hostname COMMAND ...
  1301. theoretical = ':%s!%s@%s %s' % (
  1302. 'a' * self.supported.getFeature('NICKLEN'),
  1303. # This value is based on observation.
  1304. 'b' * 10,
  1305. # See <http://tools.ietf.org/html/rfc2812#section-2.3.1>.
  1306. 'c' * 63,
  1307. command)
  1308. # Fingers crossed.
  1309. fudge = 10
  1310. return MAX_COMMAND_LENGTH - len(theoretical) - fudge
  1311. def msg(self, user, message, length=None):
  1312. """
  1313. Send a message to a user or channel.
  1314. The message will be split into multiple commands to the server if:
  1315. - The message contains any newline characters
  1316. - Any span between newline characters is longer than the given
  1317. line-length.
  1318. @param user: Username or channel name to which to direct the
  1319. message.
  1320. @type user: C{str}
  1321. @param message: Text to send.
  1322. @type message: C{str}
  1323. @param length: Maximum number of octets to send in a single
  1324. command, including the IRC protocol framing. If L{None} is given
  1325. then L{IRCClient._safeMaximumLineLength} is used to determine a
  1326. value.
  1327. @type length: C{int}
  1328. """
  1329. fmt = 'PRIVMSG %s :' % (user,)
  1330. if length is None:
  1331. length = self._safeMaximumLineLength(fmt)
  1332. # Account for the line terminator.
  1333. minimumLength = len(fmt) + 2
  1334. if length <= minimumLength:
  1335. raise ValueError("Maximum length must exceed %d for message "
  1336. "to %s" % (minimumLength, user))
  1337. for line in split(message, length - minimumLength):
  1338. self.sendLine(fmt + line)
  1339. def notice(self, user, message):
  1340. """
  1341. Send a notice to a user.
  1342. Notices are like normal message, but should never get automated
  1343. replies.
  1344. @type user: C{str}
  1345. @param user: The user to send a notice to.
  1346. @type message: C{str}
  1347. @param message: The contents of the notice to send.
  1348. """
  1349. self.sendLine("NOTICE %s :%s" % (user, message))
  1350. def away(self, message=''):
  1351. """
  1352. Mark this client as away.
  1353. @type message: C{str}
  1354. @param message: If specified, the away message.
  1355. """
  1356. self.sendLine("AWAY :%s" % message)
  1357. def back(self):
  1358. """
  1359. Clear the away status.
  1360. """
  1361. # An empty away marks us as back
  1362. self.away()
  1363. def whois(self, nickname, server=None):
  1364. """
  1365. Retrieve user information about the given nickname.
  1366. @type nickname: C{str}
  1367. @param nickname: The nickname about which to retrieve information.
  1368. @since: 8.2
  1369. """
  1370. if server is None:
  1371. self.sendLine('WHOIS ' + nickname)
  1372. else:
  1373. self.sendLine('WHOIS %s %s' % (server, nickname))
  1374. def register(self, nickname, hostname='foo', servername='bar'):
  1375. """
  1376. Login to the server.
  1377. @type nickname: C{str}
  1378. @param nickname: The nickname to register.
  1379. @type hostname: C{str}
  1380. @param hostname: If specified, the hostname to logon as.
  1381. @type servername: C{str}
  1382. @param servername: If specified, the servername to logon as.
  1383. """
  1384. if self.password is not None:
  1385. self.sendLine("PASS %s" % self.password)
  1386. self.setNick(nickname)
  1387. if self.username is None:
  1388. self.username = nickname
  1389. self.sendLine("USER %s %s %s :%s" % (self.username, hostname, servername, self.realname))
  1390. def setNick(self, nickname):
  1391. """
  1392. Set this client's nickname.
  1393. @type nickname: C{str}
  1394. @param nickname: The nickname to change to.
  1395. """
  1396. self._attemptedNick = nickname
  1397. self.sendLine("NICK %s" % nickname)
  1398. def quit(self, message = ''):
  1399. """
  1400. Disconnect from the server
  1401. @type message: C{str}
  1402. @param message: If specified, the message to give when quitting the
  1403. server.
  1404. """
  1405. self.sendLine("QUIT :%s" % message)
  1406. ### user input commands, client->client
  1407. def describe(self, channel, action):
  1408. """
  1409. Strike a pose.
  1410. @type channel: C{str}
  1411. @param channel: The name of the channel to have an action on. If it
  1412. has no prefix, it is sent to the user of that name.
  1413. @type action: C{str}
  1414. @param action: The action to preform.
  1415. @since: 9.0
  1416. """
  1417. self.ctcpMakeQuery(channel, [('ACTION', action)])
  1418. _pings = None
  1419. _MAX_PINGRING = 12
  1420. def ping(self, user, text = None):
  1421. """
  1422. Measure round-trip delay to another IRC client.
  1423. """
  1424. if self._pings is None:
  1425. self._pings = {}
  1426. if text is None:
  1427. chars = string.ascii_letters + string.digits + string.punctuation
  1428. key = ''.join([random.choice(chars) for i in range(12)])
  1429. else:
  1430. key = str(text)
  1431. self._pings[(user, key)] = time.time()
  1432. self.ctcpMakeQuery(user, [('PING', key)])
  1433. if len(self._pings) > self._MAX_PINGRING:
  1434. # Remove some of the oldest entries.
  1435. byValue = [(v, k) for (k, v) in self._pings.items()]
  1436. byValue.sort()
  1437. excess = len(self._pings) - self._MAX_PINGRING
  1438. for i in range(excess):
  1439. del self._pings[byValue[i][1]]
  1440. def dccSend(self, user, file):
  1441. """
  1442. This is supposed to send a user a file directly. This generally
  1443. doesn't work on any client, and this method is included only for
  1444. backwards compatibility and completeness.
  1445. @param user: C{str} representing the user
  1446. @param file: an open file (unknown, since this is not implemented)
  1447. """
  1448. raise NotImplementedError(
  1449. "XXX!!! Help! I need to bind a socket, have it listen, and tell me its address. "
  1450. "(and stop accepting once we've made a single connection.)")
  1451. def dccResume(self, user, fileName, port, resumePos):
  1452. """
  1453. Send a DCC RESUME request to another user.
  1454. """
  1455. self.ctcpMakeQuery(user, [
  1456. ('DCC', ['RESUME', fileName, port, resumePos])])
  1457. def dccAcceptResume(self, user, fileName, port, resumePos):
  1458. """
  1459. Send a DCC ACCEPT response to clients who have requested a resume.
  1460. """
  1461. self.ctcpMakeQuery(user, [
  1462. ('DCC', ['ACCEPT', fileName, port, resumePos])])
  1463. ### server->client messages
  1464. ### You might want to fiddle with these,
  1465. ### but it is safe to leave them alone.
  1466. def irc_ERR_NICKNAMEINUSE(self, prefix, params):
  1467. """
  1468. Called when we try to register or change to a nickname that is already
  1469. taken.
  1470. """
  1471. self._attemptedNick = self.alterCollidedNick(self._attemptedNick)
  1472. self.setNick(self._attemptedNick)
  1473. def alterCollidedNick(self, nickname):
  1474. """
  1475. Generate an altered version of a nickname that caused a collision in an
  1476. effort to create an unused related name for subsequent registration.
  1477. @param nickname: The nickname a user is attempting to register.
  1478. @type nickname: C{str}
  1479. @returns: A string that is in some way different from the nickname.
  1480. @rtype: C{str}
  1481. """
  1482. return nickname + '_'
  1483. def irc_ERR_ERRONEUSNICKNAME(self, prefix, params):
  1484. """
  1485. Called when we try to register or change to an illegal nickname.
  1486. The server should send this reply when the nickname contains any
  1487. disallowed characters. The bot will stall, waiting for RPL_WELCOME, if
  1488. we don't handle this during sign-on.
  1489. @note: The method uses the spelling I{erroneus}, as it appears in
  1490. the RFC, section 6.1.
  1491. """
  1492. if not self._registered:
  1493. self.setNick(self.erroneousNickFallback)
  1494. def irc_ERR_PASSWDMISMATCH(self, prefix, params):
  1495. """
  1496. Called when the login was incorrect.
  1497. """
  1498. raise IRCPasswordMismatch("Password Incorrect.")
  1499. def irc_RPL_WELCOME(self, prefix, params):
  1500. """
  1501. Called when we have received the welcome from the server.
  1502. """
  1503. self.hostname = prefix
  1504. self._registered = True
  1505. self.nickname = self._attemptedNick
  1506. self.signedOn()
  1507. self.startHeartbeat()
  1508. def irc_JOIN(self, prefix, params):
  1509. """
  1510. Called when a user joins a channel.
  1511. """
  1512. nick = prefix.split('!')[0]
  1513. channel = params[-1]
  1514. if nick == self.nickname:
  1515. self.joined(channel)
  1516. else:
  1517. self.userJoined(nick, channel)
  1518. def irc_PART(self, prefix, params):
  1519. """
  1520. Called when a user leaves a channel.
  1521. """
  1522. nick = prefix.split('!')[0]
  1523. channel = params[0]
  1524. if nick == self.nickname:
  1525. self.left(channel)
  1526. else:
  1527. self.userLeft(nick, channel)
  1528. def irc_QUIT(self, prefix, params):
  1529. """
  1530. Called when a user has quit.
  1531. """
  1532. nick = prefix.split('!')[0]
  1533. self.userQuit(nick, params[0])
  1534. def irc_MODE(self, user, params):
  1535. """
  1536. Parse a server mode change message.
  1537. """
  1538. channel, modes, args = params[0], params[1], params[2:]
  1539. if modes[0] not in '-+':
  1540. modes = '+' + modes
  1541. if channel == self.nickname:
  1542. # This is a mode change to our individual user, not a channel mode
  1543. # that involves us.
  1544. paramModes = self.getUserModeParams()
  1545. else:
  1546. paramModes = self.getChannelModeParams()
  1547. try:
  1548. added, removed = parseModes(modes, args, paramModes)
  1549. except IRCBadModes:
  1550. log.err(None, 'An error occurred while parsing the following '
  1551. 'MODE message: MODE %s' % (' '.join(params),))
  1552. else:
  1553. if added:
  1554. modes, params = zip(*added)
  1555. self.modeChanged(user, channel, True, ''.join(modes), params)
  1556. if removed:
  1557. modes, params = zip(*removed)
  1558. self.modeChanged(user, channel, False, ''.join(modes), params)
  1559. def irc_PING(self, prefix, params):
  1560. """
  1561. Called when some has pinged us.
  1562. """
  1563. self.sendLine("PONG %s" % params[-1])
  1564. def irc_PRIVMSG(self, prefix, params):
  1565. """
  1566. Called when we get a message.
  1567. """
  1568. user = prefix
  1569. channel = params[0]
  1570. message = params[-1]
  1571. if not message:
  1572. # Don't raise an exception if we get blank message.
  1573. return
  1574. if message[0] == X_DELIM:
  1575. m = ctcpExtract(message)
  1576. if m['extended']:
  1577. self.ctcpQuery(user, channel, m['extended'])
  1578. if not m['normal']:
  1579. return
  1580. message = ' '.join(m['normal'])
  1581. self.privmsg(user, channel, message)
  1582. def irc_NOTICE(self, prefix, params):
  1583. """
  1584. Called when a user gets a notice.
  1585. """
  1586. user = prefix
  1587. channel = params[0]
  1588. message = params[-1]
  1589. if message[0]==X_DELIM:
  1590. m = ctcpExtract(message)
  1591. if m['extended']:
  1592. self.ctcpReply(user, channel, m['extended'])
  1593. if not m['normal']:
  1594. return
  1595. message = ' '.join(m['normal'])
  1596. self.noticed(user, channel, message)
  1597. def irc_NICK(self, prefix, params):
  1598. """
  1599. Called when a user changes their nickname.
  1600. """
  1601. nick = prefix.split('!', 1)[0]
  1602. if nick == self.nickname:
  1603. self.nickChanged(params[0])
  1604. else:
  1605. self.userRenamed(nick, params[0])
  1606. def irc_KICK(self, prefix, params):
  1607. """
  1608. Called when a user is kicked from a channel.
  1609. """
  1610. kicker = prefix.split('!')[0]
  1611. channel = params[0]
  1612. kicked = params[1]
  1613. message = params[-1]
  1614. if kicked.lower() == self.nickname.lower():
  1615. # Yikes!
  1616. self.kickedFrom(channel, kicker, message)
  1617. else:
  1618. self.userKicked(kicked, channel, kicker, message)
  1619. def irc_TOPIC(self, prefix, params):
  1620. """
  1621. Someone in the channel set the topic.
  1622. """
  1623. user = prefix.split('!')[0]
  1624. channel = params[0]
  1625. newtopic = params[1]
  1626. self.topicUpdated(user, channel, newtopic)
  1627. def irc_RPL_TOPIC(self, prefix, params):
  1628. """
  1629. Called when the topic for a channel is initially reported or when it
  1630. subsequently changes.
  1631. """
  1632. user = prefix.split('!')[0]
  1633. channel = params[1]
  1634. newtopic = params[2]
  1635. self.topicUpdated(user, channel, newtopic)
  1636. def irc_RPL_NOTOPIC(self, prefix, params):
  1637. user = prefix.split('!')[0]
  1638. channel = params[1]
  1639. newtopic = ""
  1640. self.topicUpdated(user, channel, newtopic)
  1641. def irc_RPL_MOTDSTART(self, prefix, params):
  1642. if params[-1].startswith("- "):
  1643. params[-1] = params[-1][2:]
  1644. self.motd = [params[-1]]
  1645. def irc_RPL_MOTD(self, prefix, params):
  1646. if params[-1].startswith("- "):
  1647. params[-1] = params[-1][2:]
  1648. if self.motd is None:
  1649. self.motd = []
  1650. self.motd.append(params[-1])
  1651. def irc_RPL_ENDOFMOTD(self, prefix, params):
  1652. """
  1653. I{RPL_ENDOFMOTD} indicates the end of the message of the day
  1654. messages. Deliver the accumulated lines to C{receivedMOTD}.
  1655. """
  1656. motd = self.motd
  1657. self.motd = None
  1658. self.receivedMOTD(motd)
  1659. def irc_RPL_CREATED(self, prefix, params):
  1660. self.created(params[1])
  1661. def irc_RPL_YOURHOST(self, prefix, params):
  1662. self.yourHost(params[1])
  1663. def irc_RPL_MYINFO(self, prefix, params):
  1664. info = params[1].split(None, 3)
  1665. while len(info) < 4:
  1666. info.append(None)
  1667. self.myInfo(*info)
  1668. def irc_RPL_BOUNCE(self, prefix, params):
  1669. self.bounce(params[1])
  1670. def irc_RPL_ISUPPORT(self, prefix, params):
  1671. args = params[1:-1]
  1672. # Several ISUPPORT messages, in no particular order, may be sent
  1673. # to the client at any given point in time (usually only on connect,
  1674. # though.) For this reason, ServerSupportedFeatures.parse is intended
  1675. # to mutate the supported feature list.
  1676. self.supported.parse(args)
  1677. self.isupport(args)
  1678. def irc_RPL_LUSERCLIENT(self, prefix, params):
  1679. self.luserClient(params[1])
  1680. def irc_RPL_LUSEROP(self, prefix, params):
  1681. try:
  1682. self.luserOp(int(params[1]))
  1683. except ValueError:
  1684. pass
  1685. def irc_RPL_LUSERCHANNELS(self, prefix, params):
  1686. try:
  1687. self.luserChannels(int(params[1]))
  1688. except ValueError:
  1689. pass
  1690. def irc_RPL_LUSERME(self, prefix, params):
  1691. self.luserMe(params[1])
  1692. def irc_unknown(self, prefix, command, params):
  1693. pass
  1694. ### Receiving a CTCP query from another party
  1695. ### It is safe to leave these alone.
  1696. def ctcpQuery(self, user, channel, messages):
  1697. """
  1698. Dispatch method for any CTCP queries received.
  1699. Duplicated CTCP queries are ignored and no dispatch is
  1700. made. Unrecognized CTCP queries invoke L{IRCClient.ctcpUnknownQuery}.
  1701. """
  1702. seen = set()
  1703. for tag, data in messages:
  1704. method = getattr(self, 'ctcpQuery_%s' % tag, None)
  1705. if tag not in seen:
  1706. if method is not None:
  1707. method(user, channel, data)
  1708. else:
  1709. self.ctcpUnknownQuery(user, channel, tag, data)
  1710. seen.add(tag)
  1711. def ctcpUnknownQuery(self, user, channel, tag, data):
  1712. """
  1713. Fallback handler for unrecognized CTCP queries.
  1714. No CTCP I{ERRMSG} reply is made to remove a potential denial of service
  1715. avenue.
  1716. """
  1717. log.msg('Unknown CTCP query from %r: %r %r' % (user, tag, data))
  1718. def ctcpQuery_ACTION(self, user, channel, data):
  1719. self.action(user, channel, data)
  1720. def ctcpQuery_PING(self, user, channel, data):
  1721. nick = user.split('!')[0]
  1722. self.ctcpMakeReply(nick, [("PING", data)])
  1723. def ctcpQuery_FINGER(self, user, channel, data):
  1724. if data is not None:
  1725. self.quirkyMessage("Why did %s send '%s' with a FINGER query?"
  1726. % (user, data))
  1727. if not self.fingerReply:
  1728. return
  1729. if callable(self.fingerReply):
  1730. reply = self.fingerReply()
  1731. else:
  1732. reply = str(self.fingerReply)
  1733. nick = user.split('!')[0]
  1734. self.ctcpMakeReply(nick, [('FINGER', reply)])
  1735. def ctcpQuery_VERSION(self, user, channel, data):
  1736. if data is not None:
  1737. self.quirkyMessage("Why did %s send '%s' with a VERSION query?"
  1738. % (user, data))
  1739. if self.versionName:
  1740. nick = user.split('!')[0]
  1741. self.ctcpMakeReply(nick, [('VERSION', '%s:%s:%s' %
  1742. (self.versionName,
  1743. self.versionNum or '',
  1744. self.versionEnv or ''))])
  1745. def ctcpQuery_SOURCE(self, user, channel, data):
  1746. if data is not None:
  1747. self.quirkyMessage("Why did %s send '%s' with a SOURCE query?"
  1748. % (user, data))
  1749. if self.sourceURL:
  1750. nick = user.split('!')[0]
  1751. # The CTCP document (Zeuge, Rollo, Mesander 1994) says that SOURCE
  1752. # replies should be responded to with the location of an anonymous
  1753. # FTP server in host:directory:file format. I'm taking the liberty
  1754. # of bringing it into the 21st century by sending a URL instead.
  1755. self.ctcpMakeReply(nick, [('SOURCE', self.sourceURL),
  1756. ('SOURCE', None)])
  1757. def ctcpQuery_USERINFO(self, user, channel, data):
  1758. if data is not None:
  1759. self.quirkyMessage("Why did %s send '%s' with a USERINFO query?"
  1760. % (user, data))
  1761. if self.userinfo:
  1762. nick = user.split('!')[0]
  1763. self.ctcpMakeReply(nick, [('USERINFO', self.userinfo)])
  1764. def ctcpQuery_CLIENTINFO(self, user, channel, data):
  1765. """
  1766. A master index of what CTCP tags this client knows.
  1767. If no arguments are provided, respond with a list of known tags, sorted
  1768. in alphabetical order.
  1769. If an argument is provided, provide human-readable help on
  1770. the usage of that tag.
  1771. """
  1772. nick = user.split('!')[0]
  1773. if not data:
  1774. # XXX: prefixedMethodNames gets methods from my *class*,
  1775. # but it's entirely possible that this *instance* has more
  1776. # methods.
  1777. names = sorted(reflect.prefixedMethodNames(self.__class__,
  1778. 'ctcpQuery_'))
  1779. self.ctcpMakeReply(nick, [('CLIENTINFO', ' '.join(names))])
  1780. else:
  1781. args = data.split()
  1782. method = getattr(self, 'ctcpQuery_%s' % (args[0],), None)
  1783. if not method:
  1784. self.ctcpMakeReply(nick, [('ERRMSG',
  1785. "CLIENTINFO %s :"
  1786. "Unknown query '%s'"
  1787. % (data, args[0]))])
  1788. return
  1789. doc = getattr(method, '__doc__', '')
  1790. self.ctcpMakeReply(nick, [('CLIENTINFO', doc)])
  1791. def ctcpQuery_ERRMSG(self, user, channel, data):
  1792. # Yeah, this seems strange, but that's what the spec says to do
  1793. # when faced with an ERRMSG query (not a reply).
  1794. nick = user.split('!')[0]
  1795. self.ctcpMakeReply(nick, [('ERRMSG',
  1796. "%s :No error has occurred." % data)])
  1797. def ctcpQuery_TIME(self, user, channel, data):
  1798. if data is not None:
  1799. self.quirkyMessage("Why did %s send '%s' with a TIME query?"
  1800. % (user, data))
  1801. nick = user.split('!')[0]
  1802. self.ctcpMakeReply(nick,
  1803. [('TIME', ':%s' %
  1804. time.asctime(time.localtime(time.time())))])
  1805. def ctcpQuery_DCC(self, user, channel, data):
  1806. """
  1807. Initiate a Direct Client Connection
  1808. @param user: The hostmask of the user/client.
  1809. @type user: L{bytes}
  1810. @param channel: The name of the IRC channel.
  1811. @type channel: L{bytes}
  1812. @param data: The DCC request message.
  1813. @type data: L{bytes}
  1814. """
  1815. if not data: return
  1816. dcctype = data.split(None, 1)[0].upper()
  1817. handler = getattr(self, "dcc_" + dcctype, None)
  1818. if handler:
  1819. if self.dcc_sessions is None:
  1820. self.dcc_sessions = []
  1821. data = data[len(dcctype)+1:]
  1822. handler(user, channel, data)
  1823. else:
  1824. nick = user.split('!')[0]
  1825. self.ctcpMakeReply(nick, [('ERRMSG',
  1826. "DCC %s :Unknown DCC type '%s'"
  1827. % (data, dcctype))])
  1828. self.quirkyMessage("%s offered unknown DCC type %s"
  1829. % (user, dcctype))
  1830. def dcc_SEND(self, user, channel, data):
  1831. # Use shlex.split for those who send files with spaces in the names.
  1832. data = shlex.split(data)
  1833. if len(data) < 3:
  1834. raise IRCBadMessage("malformed DCC SEND request: %r" % (data,))
  1835. (filename, address, port) = data[:3]
  1836. address = dccParseAddress(address)
  1837. try:
  1838. port = int(port)
  1839. except ValueError:
  1840. raise IRCBadMessage("Indecipherable port %r" % (port,))
  1841. size = -1
  1842. if len(data) >= 4:
  1843. try:
  1844. size = int(data[3])
  1845. except ValueError:
  1846. pass
  1847. # XXX Should we bother passing this data?
  1848. self.dccDoSend(user, address, port, filename, size, data)
  1849. def dcc_ACCEPT(self, user, channel, data):
  1850. data = shlex.split(data)
  1851. if len(data) < 3:
  1852. raise IRCBadMessage("malformed DCC SEND ACCEPT request: %r" % (
  1853. data,))
  1854. (filename, port, resumePos) = data[:3]
  1855. try:
  1856. port = int(port)
  1857. resumePos = int(resumePos)
  1858. except ValueError:
  1859. return
  1860. self.dccDoAcceptResume(user, filename, port, resumePos)
  1861. def dcc_RESUME(self, user, channel, data):
  1862. data = shlex.split(data)
  1863. if len(data) < 3:
  1864. raise IRCBadMessage("malformed DCC SEND RESUME request: %r" % (
  1865. data,))
  1866. (filename, port, resumePos) = data[:3]
  1867. try:
  1868. port = int(port)
  1869. resumePos = int(resumePos)
  1870. except ValueError:
  1871. return
  1872. self.dccDoResume(user, filename, port, resumePos)
  1873. def dcc_CHAT(self, user, channel, data):
  1874. data = shlex.split(data)
  1875. if len(data) < 3:
  1876. raise IRCBadMessage("malformed DCC CHAT request: %r" % (data,))
  1877. (filename, address, port) = data[:3]
  1878. address = dccParseAddress(address)
  1879. try:
  1880. port = int(port)
  1881. except ValueError:
  1882. raise IRCBadMessage("Indecipherable port %r" % (port,))
  1883. self.dccDoChat(user, channel, address, port, data)
  1884. ### The dccDo methods are the slightly higher-level siblings of
  1885. ### common dcc_ methods; the arguments have been parsed for them.
  1886. def dccDoSend(self, user, address, port, fileName, size, data):
  1887. """
  1888. Called when I receive a DCC SEND offer from a client.
  1889. By default, I do nothing here.
  1890. @param user: The hostmask of the requesting user.
  1891. @type user: L{bytes}
  1892. @param address: The IP address of the requesting user.
  1893. @type address: L{bytes}
  1894. @param port: An integer representing the port of the requesting user.
  1895. @type port: L{int}
  1896. @param fileName: The name of the file to be transferred.
  1897. @type fileName: L{bytes}
  1898. @param size: The size of the file to be transferred, which may be C{-1}
  1899. if the size of the file was not specified in the DCC SEND request.
  1900. @type size: L{int}
  1901. @param data: A 3-list of [fileName, address, port].
  1902. @type data: L{list}
  1903. """
  1904. def dccDoResume(self, user, file, port, resumePos):
  1905. """
  1906. Called when a client is trying to resume an offered file via DCC send.
  1907. It should be either replied to with a DCC ACCEPT or ignored (default).
  1908. @param user: The hostmask of the user who wants to resume the transfer
  1909. of a file previously offered via DCC send.
  1910. @type user: L{bytes}
  1911. @param file: The name of the file to resume the transfer of.
  1912. @type file: L{bytes}
  1913. @param port: An integer representing the port of the requesting user.
  1914. @type port: L{int}
  1915. @param resumePos: The position in the file from where the transfer
  1916. should resume.
  1917. @type resumePos: L{int}
  1918. """
  1919. pass
  1920. def dccDoAcceptResume(self, user, file, port, resumePos):
  1921. """
  1922. Called when a client has verified and accepted a DCC resume request
  1923. made by us. By default it will do nothing.
  1924. @param user: The hostmask of the user who has accepted the DCC resume
  1925. request.
  1926. @type user: L{bytes}
  1927. @param file: The name of the file to resume the transfer of.
  1928. @type file: L{bytes}
  1929. @param port: An integer representing the port of the accepting user.
  1930. @type port: L{int}
  1931. @param resumePos: The position in the file from where the transfer
  1932. should resume.
  1933. @type resumePos: L{int}
  1934. """
  1935. pass
  1936. def dccDoChat(self, user, channel, address, port, data):
  1937. pass
  1938. #factory = DccChatFactory(self, queryData=(user, channel, data))
  1939. #reactor.connectTCP(address, port, factory)
  1940. #self.dcc_sessions.append(factory)
  1941. #def ctcpQuery_SED(self, user, data):
  1942. # """Simple Encryption Doodoo
  1943. #
  1944. # Feel free to implement this, but no specification is available.
  1945. # """
  1946. # raise NotImplementedError
  1947. def ctcpMakeReply(self, user, messages):
  1948. """
  1949. Send one or more C{extended messages} as a CTCP reply.
  1950. @type messages: a list of extended messages. An extended
  1951. message is a (tag, data) tuple, where 'data' may be L{None}.
  1952. """
  1953. self.notice(user, ctcpStringify(messages))
  1954. ### client CTCP query commands
  1955. def ctcpMakeQuery(self, user, messages):
  1956. """
  1957. Send one or more C{extended messages} as a CTCP query.
  1958. @type messages: a list of extended messages. An extended
  1959. message is a (tag, data) tuple, where 'data' may be L{None}.
  1960. """
  1961. self.msg(user, ctcpStringify(messages))
  1962. ### Receiving a response to a CTCP query (presumably to one we made)
  1963. ### You may want to add methods here, or override UnknownReply.
  1964. def ctcpReply(self, user, channel, messages):
  1965. """
  1966. Dispatch method for any CTCP replies received.
  1967. """
  1968. for m in messages:
  1969. method = getattr(self, "ctcpReply_%s" % m[0], None)
  1970. if method:
  1971. method(user, channel, m[1])
  1972. else:
  1973. self.ctcpUnknownReply(user, channel, m[0], m[1])
  1974. def ctcpReply_PING(self, user, channel, data):
  1975. nick = user.split('!', 1)[0]
  1976. if (not self._pings) or ((nick, data) not in self._pings):
  1977. raise IRCBadMessage(
  1978. "Bogus PING response from %s: %s" % (user, data))
  1979. t0 = self._pings[(nick, data)]
  1980. self.pong(user, time.time() - t0)
  1981. def ctcpUnknownReply(self, user, channel, tag, data):
  1982. """
  1983. Called when a fitting ctcpReply_ method is not found.
  1984. @param user: The hostmask of the user.
  1985. @type user: L{bytes}
  1986. @param channel: The name of the IRC channel.
  1987. @type channel: L{bytes}
  1988. @param tag: The CTCP request tag for which no fitting method is found.
  1989. @type tag: L{bytes}
  1990. @param data: The CTCP message.
  1991. @type data: L{bytes}
  1992. """
  1993. # FIXME:7560:
  1994. # Add code for handling arbitrary queries and not treat them as
  1995. # anomalies.
  1996. log.msg("Unknown CTCP reply from %s: %s %s\n"
  1997. % (user, tag, data))
  1998. ### Error handlers
  1999. ### You may override these with something more appropriate to your UI.
  2000. def badMessage(self, line, excType, excValue, tb):
  2001. """
  2002. When I get a message that's so broken I can't use it.
  2003. @param line: The indecipherable message.
  2004. @type line: L{bytes}
  2005. @param excType: The exception type of the exception raised by the
  2006. message.
  2007. @type excType: L{type}
  2008. @param excValue: The exception parameter of excType or its associated
  2009. value(the second argument to C{raise}).
  2010. @type excValue: L{BaseException}
  2011. @param tb: The Traceback as a traceback object.
  2012. @type tb: L{traceback}
  2013. """
  2014. log.msg(line)
  2015. log.msg(''.join(traceback.format_exception(excType, excValue, tb)))
  2016. def quirkyMessage(self, s):
  2017. """
  2018. This is called when I receive a message which is peculiar, but not
  2019. wholly indecipherable.
  2020. @param s: The peculiar message.
  2021. @type s: L{bytes}
  2022. """
  2023. log.msg(s + '\n')
  2024. ### Protocol methods
  2025. def connectionMade(self):
  2026. self.supported = ServerSupportedFeatures()
  2027. self._queue = []
  2028. if self.performLogin:
  2029. self.register(self.nickname)
  2030. def dataReceived(self, data):
  2031. if isinstance(data, unicode):
  2032. data = data.encode("utf-8")
  2033. data = data.replace(b'\r', b'')
  2034. basic.LineReceiver.dataReceived(self, data)
  2035. def lineReceived(self, line):
  2036. if bytes != str and isinstance(line, bytes):
  2037. # decode bytes from transport to unicode
  2038. line = line.decode("utf-8")
  2039. line = lowDequote(line)
  2040. try:
  2041. prefix, command, params = parsemsg(line)
  2042. if command in numeric_to_symbolic:
  2043. command = numeric_to_symbolic[command]
  2044. self.handleCommand(command, prefix, params)
  2045. except IRCBadMessage:
  2046. self.badMessage(line, *sys.exc_info())
  2047. def getUserModeParams(self):
  2048. """
  2049. Get user modes that require parameters for correct parsing.
  2050. @rtype: C{[str, str]}
  2051. @return C{[add, remove]}
  2052. """
  2053. return ['', '']
  2054. def getChannelModeParams(self):
  2055. """
  2056. Get channel modes that require parameters for correct parsing.
  2057. @rtype: C{[str, str]}
  2058. @return C{[add, remove]}
  2059. """
  2060. # PREFIX modes are treated as "type B" CHANMODES, they always take
  2061. # parameter.
  2062. params = ['', '']
  2063. prefixes = self.supported.getFeature('PREFIX', {})
  2064. params[0] = params[1] = ''.join(prefixes.keys())
  2065. chanmodes = self.supported.getFeature('CHANMODES')
  2066. if chanmodes is not None:
  2067. params[0] += chanmodes.get('addressModes', '')
  2068. params[0] += chanmodes.get('param', '')
  2069. params[1] = params[0]
  2070. params[0] += chanmodes.get('setParam', '')
  2071. return params
  2072. def handleCommand(self, command, prefix, params):
  2073. """
  2074. Determine the function to call for the given command and call it with
  2075. the given arguments.
  2076. @param command: The IRC command to determine the function for.
  2077. @type command: L{bytes}
  2078. @param prefix: The prefix of the IRC message (as returned by
  2079. L{parsemsg}).
  2080. @type prefix: L{bytes}
  2081. @param params: A list of parameters to call the function with.
  2082. @type params: L{list}
  2083. """
  2084. method = getattr(self, "irc_%s" % command, None)
  2085. try:
  2086. if method is not None:
  2087. method(prefix, params)
  2088. else:
  2089. self.irc_unknown(prefix, command, params)
  2090. except:
  2091. log.deferr()
  2092. def __getstate__(self):
  2093. dct = self.__dict__.copy()
  2094. dct['dcc_sessions'] = None
  2095. dct['_pings'] = None
  2096. return dct
  2097. def dccParseAddress(address):
  2098. if '.' in address:
  2099. pass
  2100. else:
  2101. try:
  2102. address = int(address)
  2103. except ValueError:
  2104. raise IRCBadMessage("Indecipherable address %r" % (address,))
  2105. else:
  2106. address = (
  2107. (address >> 24) & 0xFF,
  2108. (address >> 16) & 0xFF,
  2109. (address >> 8) & 0xFF,
  2110. address & 0xFF,
  2111. )
  2112. address = '.'.join(map(str,address))
  2113. return address
  2114. class DccFileReceiveBasic(protocol.Protocol, styles.Ephemeral):
  2115. """
  2116. Bare protocol to receive a Direct Client Connection SEND stream.
  2117. This does enough to keep the other guy talking, but you'll want to extend
  2118. my dataReceived method to *do* something with the data I get.
  2119. @ivar bytesReceived: An integer representing the number of bytes of data
  2120. received.
  2121. @type bytesReceived: L{int}
  2122. """
  2123. bytesReceived = 0
  2124. def __init__(self, resumeOffset=0):
  2125. """
  2126. @param resumeOffset: An integer representing the amount of bytes from
  2127. where the transfer of data should be resumed.
  2128. @type resumeOffset: L{int}
  2129. """
  2130. self.bytesReceived = resumeOffset
  2131. self.resume = (resumeOffset != 0)
  2132. def dataReceived(self, data):
  2133. """
  2134. See: L{protocol.Protocol.dataReceived}
  2135. Warning: This just acknowledges to the remote host that the data has
  2136. been received; it doesn't I{do} anything with the data, so you'll want
  2137. to override this.
  2138. """
  2139. self.bytesReceived = self.bytesReceived + len(data)
  2140. self.transport.write(struct.pack('!i', self.bytesReceived))
  2141. class DccSendProtocol(protocol.Protocol, styles.Ephemeral):
  2142. """
  2143. Protocol for an outgoing Direct Client Connection SEND.
  2144. @ivar blocksize: An integer representing the size of an individual block of
  2145. data.
  2146. @type blocksize: L{int}
  2147. @ivar file: The file to be sent. This can be either a file object or
  2148. simply the name of the file.
  2149. @type file: L{file} or L{bytes}
  2150. @ivar bytesSent: An integer representing the number of bytes sent.
  2151. @type bytesSent: L{int}
  2152. @ivar completed: An integer representing whether the transfer has been
  2153. completed or not.
  2154. @type completed: L{int}
  2155. @ivar connected: An integer representing whether the connection has been
  2156. established or not.
  2157. @type connected: L{int}
  2158. """
  2159. blocksize = 1024
  2160. file = None
  2161. bytesSent = 0
  2162. completed = 0
  2163. connected = 0
  2164. def __init__(self, file):
  2165. if type(file) is str:
  2166. self.file = open(file, 'r')
  2167. def connectionMade(self):
  2168. self.connected = 1
  2169. self.sendBlock()
  2170. def dataReceived(self, data):
  2171. # XXX: Do we need to check to see if len(data) != fmtsize?
  2172. bytesShesGot = struct.unpack("!I", data)
  2173. if bytesShesGot < self.bytesSent:
  2174. # Wait for her.
  2175. # XXX? Add some checks to see if we've stalled out?
  2176. return
  2177. elif bytesShesGot > self.bytesSent:
  2178. # self.transport.log("DCC SEND %s: She says she has %d bytes "
  2179. # "but I've only sent %d. I'm stopping "
  2180. # "this screwy transfer."
  2181. # % (self.file,
  2182. # bytesShesGot, self.bytesSent))
  2183. self.transport.loseConnection()
  2184. return
  2185. self.sendBlock()
  2186. def sendBlock(self):
  2187. block = self.file.read(self.blocksize)
  2188. if block:
  2189. self.transport.write(block)
  2190. self.bytesSent = self.bytesSent + len(block)
  2191. else:
  2192. # Nothing more to send, transfer complete.
  2193. self.transport.loseConnection()
  2194. self.completed = 1
  2195. def connectionLost(self, reason):
  2196. self.connected = 0
  2197. if hasattr(self.file, "close"):
  2198. self.file.close()
  2199. class DccSendFactory(protocol.Factory):
  2200. protocol = DccSendProtocol
  2201. def __init__(self, file):
  2202. self.file = file
  2203. def buildProtocol(self, connection):
  2204. p = self.protocol(self.file)
  2205. p.factory = self
  2206. return p
  2207. def fileSize(file):
  2208. """
  2209. I'll try my damndest to determine the size of this file object.
  2210. @param file: The file object to determine the size of.
  2211. @type file: L{file}
  2212. @rtype: L{int} or L{None}
  2213. @return: The size of the file object as an integer if it can be determined,
  2214. otherwise return L{None}.
  2215. """
  2216. size = None
  2217. if hasattr(file, "fileno"):
  2218. fileno = file.fileno()
  2219. try:
  2220. stat_ = os.fstat(fileno)
  2221. size = stat_[stat.ST_SIZE]
  2222. except:
  2223. pass
  2224. else:
  2225. return size
  2226. if hasattr(file, "name") and path.exists(file.name):
  2227. try:
  2228. size = path.getsize(file.name)
  2229. except:
  2230. pass
  2231. else:
  2232. return size
  2233. if hasattr(file, "seek") and hasattr(file, "tell"):
  2234. try:
  2235. try:
  2236. file.seek(0, 2)
  2237. size = file.tell()
  2238. finally:
  2239. file.seek(0, 0)
  2240. except:
  2241. pass
  2242. else:
  2243. return size
  2244. return size
  2245. class DccChat(basic.LineReceiver, styles.Ephemeral):
  2246. """
  2247. Direct Client Connection protocol type CHAT.
  2248. DCC CHAT is really just your run o' the mill basic.LineReceiver
  2249. protocol. This class only varies from that slightly, accepting
  2250. either LF or CR LF for a line delimeter for incoming messages
  2251. while always using CR LF for outgoing.
  2252. The lineReceived method implemented here uses the DCC connection's
  2253. 'client' attribute (provided upon construction) to deliver incoming
  2254. lines from the DCC chat via IRCClient's normal privmsg interface.
  2255. That's something of a spoof, which you may well want to override.
  2256. """
  2257. queryData = None
  2258. delimiter = CR + NL
  2259. client = None
  2260. remoteParty = None
  2261. buffer = b""
  2262. def __init__(self, client, queryData=None):
  2263. """
  2264. Initialize a new DCC CHAT session.
  2265. queryData is a 3-tuple of
  2266. (fromUser, targetUserOrChannel, data)
  2267. as received by the CTCP query.
  2268. (To be honest, fromUser is the only thing that's currently
  2269. used here. targetUserOrChannel is potentially useful, while
  2270. the 'data' argument is solely for informational purposes.)
  2271. """
  2272. self.client = client
  2273. if queryData:
  2274. self.queryData = queryData
  2275. self.remoteParty = self.queryData[0]
  2276. def dataReceived(self, data):
  2277. self.buffer = self.buffer + data
  2278. lines = self.buffer.split(LF)
  2279. # Put the (possibly empty) element after the last LF back in the
  2280. # buffer
  2281. self.buffer = lines.pop()
  2282. for line in lines:
  2283. if line[-1] == CR:
  2284. line = line[:-1]
  2285. self.lineReceived(line)
  2286. def lineReceived(self, line):
  2287. log.msg("DCC CHAT<%s> %s" % (self.remoteParty, line))
  2288. self.client.privmsg(self.remoteParty,
  2289. self.client.nickname, line)
  2290. class DccChatFactory(protocol.ClientFactory):
  2291. protocol = DccChat
  2292. noisy = 0
  2293. def __init__(self, client, queryData):
  2294. self.client = client
  2295. self.queryData = queryData
  2296. def buildProtocol(self, addr):
  2297. p = self.protocol(client=self.client, queryData=self.queryData)
  2298. p.factory = self
  2299. return p
  2300. def clientConnectionFailed(self, unused_connector, unused_reason):
  2301. self.client.dcc_sessions.remove(self)
  2302. def clientConnectionLost(self, unused_connector, unused_reason):
  2303. self.client.dcc_sessions.remove(self)
  2304. def dccDescribe(data):
  2305. """
  2306. Given the data chunk from a DCC query, return a descriptive string.
  2307. @param data: The data from a DCC query.
  2308. @type data: L{bytes}
  2309. @rtype: L{bytes}
  2310. @return: A descriptive string.
  2311. """
  2312. orig_data = data
  2313. data = data.split()
  2314. if len(data) < 4:
  2315. return orig_data
  2316. (dcctype, arg, address, port) = data[:4]
  2317. if '.' in address:
  2318. pass
  2319. else:
  2320. try:
  2321. address = int(address)
  2322. except ValueError:
  2323. pass
  2324. else:
  2325. address = (
  2326. (address >> 24) & 0xFF,
  2327. (address >> 16) & 0xFF,
  2328. (address >> 8) & 0xFF,
  2329. address & 0xFF,
  2330. )
  2331. address = '.'.join(map(str, address))
  2332. if dcctype == 'SEND':
  2333. filename = arg
  2334. size_txt = ''
  2335. if len(data) >= 5:
  2336. try:
  2337. size = int(data[4])
  2338. size_txt = ' of size %d bytes' % (size,)
  2339. except ValueError:
  2340. pass
  2341. dcc_text = ("SEND for file '%s'%s at host %s, port %s"
  2342. % (filename, size_txt, address, port))
  2343. elif dcctype == 'CHAT':
  2344. dcc_text = ("CHAT for host %s, port %s"
  2345. % (address, port))
  2346. else:
  2347. dcc_text = orig_data
  2348. return dcc_text
  2349. class DccFileReceive(DccFileReceiveBasic):
  2350. """
  2351. Higher-level coverage for getting a file from DCC SEND.
  2352. I allow you to change the file's name and destination directory. I won't
  2353. overwrite an existing file unless I've been told it's okay to do so. If
  2354. passed the resumeOffset keyword argument I will attempt to resume the file
  2355. from that amount of bytes.
  2356. XXX: I need to let the client know when I am finished.
  2357. XXX: I need to decide how to keep a progress indicator updated.
  2358. XXX: Client needs a way to tell me "Do not finish until I say so."
  2359. XXX: I need to make sure the client understands if the file cannot be written.
  2360. @ivar filename: The name of the file to get.
  2361. @type filename: L{bytes}
  2362. @ivar fileSize: The size of the file to get, which has a default value of
  2363. C{-1} if the size of the file was not specified in the DCC SEND
  2364. request.
  2365. @type fileSize: L{int}
  2366. @ivar destDir: The destination directory for the file to be received.
  2367. @type destDir: L{bytes}
  2368. @ivar overwrite: An integer representing whether an existing file should be
  2369. overwritten or not. This initially is an L{int} but can be modified to
  2370. be a L{bool} using the L{set_overwrite} method.
  2371. @type overwrite: L{int} or L{bool}
  2372. @ivar queryData: queryData is a 3-tuple of (user, channel, data).
  2373. @type queryData: L{tuple}
  2374. @ivar fromUser: This is the hostmask of the requesting user and is found at
  2375. index 0 of L{queryData}.
  2376. @type fromUser: L{bytes}
  2377. """
  2378. filename = 'dcc'
  2379. fileSize = -1
  2380. destDir = '.'
  2381. overwrite = 0
  2382. fromUser = None
  2383. queryData = None
  2384. def __init__(self, filename, fileSize=-1, queryData=None,
  2385. destDir='.', resumeOffset=0):
  2386. DccFileReceiveBasic.__init__(self, resumeOffset=resumeOffset)
  2387. self.filename = filename
  2388. self.destDir = destDir
  2389. self.fileSize = fileSize
  2390. self._resumeOffset = resumeOffset
  2391. if queryData:
  2392. self.queryData = queryData
  2393. self.fromUser = self.queryData[0]
  2394. def set_directory(self, directory):
  2395. """
  2396. Set the directory where the downloaded file will be placed.
  2397. May raise OSError if the supplied directory path is not suitable.
  2398. @param directory: The directory where the file to be received will be
  2399. placed.
  2400. @type directory: L{bytes}
  2401. """
  2402. if not path.exists(directory):
  2403. raise OSError(errno.ENOENT, "You see no directory there.",
  2404. directory)
  2405. if not path.isdir(directory):
  2406. raise OSError(errno.ENOTDIR, "You cannot put a file into "
  2407. "something which is not a directory.",
  2408. directory)
  2409. if not os.access(directory, os.X_OK | os.W_OK):
  2410. raise OSError(errno.EACCES,
  2411. "This directory is too hard to write in to.",
  2412. directory)
  2413. self.destDir = directory
  2414. def set_filename(self, filename):
  2415. """
  2416. Change the name of the file being transferred.
  2417. This replaces the file name provided by the sender.
  2418. @param filename: The new name for the file.
  2419. @type filename: L{bytes}
  2420. """
  2421. self.filename = filename
  2422. def set_overwrite(self, boolean):
  2423. """
  2424. May I overwrite existing files?
  2425. @param boolean: A boolean value representing whether existing files
  2426. should be overwritten or not.
  2427. @type boolean: L{bool}
  2428. """
  2429. self.overwrite = boolean
  2430. # Protocol-level methods.
  2431. def connectionMade(self):
  2432. dst = path.abspath(path.join(self.destDir,self.filename))
  2433. exists = path.exists(dst)
  2434. if self.resume and exists:
  2435. # I have been told I want to resume, and a file already
  2436. # exists - Here we go
  2437. self.file = open(dst, 'rb+')
  2438. self.file.seek(self._resumeOffset)
  2439. self.file.truncate()
  2440. log.msg("Attempting to resume %s - starting from %d bytes" %
  2441. (self.file, self.file.tell()))
  2442. elif self.resume and not exists:
  2443. raise OSError(errno.ENOENT,
  2444. "You cannot resume writing to a file "
  2445. "that does not exist!",
  2446. dst)
  2447. elif self.overwrite or not exists:
  2448. self.file = open(dst, 'wb')
  2449. else:
  2450. raise OSError(errno.EEXIST,
  2451. "There's a file in the way. "
  2452. "Perhaps that's why you cannot open it.",
  2453. dst)
  2454. def dataReceived(self, data):
  2455. self.file.write(data)
  2456. DccFileReceiveBasic.dataReceived(self, data)
  2457. # XXX: update a progress indicator here?
  2458. def connectionLost(self, reason):
  2459. """
  2460. When the connection is lost, I close the file.
  2461. @param reason: The reason why the connection was lost.
  2462. @type reason: L{Failure}
  2463. """
  2464. self.connected = 0
  2465. logmsg = ("%s closed." % (self,))
  2466. if self.fileSize > 0:
  2467. logmsg = ("%s %d/%d bytes received"
  2468. % (logmsg, self.bytesReceived, self.fileSize))
  2469. if self.bytesReceived == self.fileSize:
  2470. pass # Hooray!
  2471. elif self.bytesReceived < self.fileSize:
  2472. logmsg = ("%s (Warning: %d bytes short)"
  2473. % (logmsg, self.fileSize - self.bytesReceived))
  2474. else:
  2475. logmsg = ("%s (file larger than expected)"
  2476. % (logmsg,))
  2477. else:
  2478. logmsg = ("%s %d bytes received"
  2479. % (logmsg, self.bytesReceived))
  2480. if hasattr(self, 'file'):
  2481. logmsg = "%s and written to %s.\n" % (logmsg, self.file.name)
  2482. if hasattr(self.file, 'close'): self.file.close()
  2483. # self.transport.log(logmsg)
  2484. def __str__(self):
  2485. if not self.connected:
  2486. return "<Unconnected DccFileReceive object at %x>" % (id(self),)
  2487. from_ = self.transport.getPeer()
  2488. if self.fromUser:
  2489. from_ = "%s (%s)" % (self.fromUser, from_)
  2490. s = ("DCC transfer of '%s' from %s" % (self.filename, from_))
  2491. return s
  2492. def __repr__(self):
  2493. s = ("<%s at %x: GET %s>"
  2494. % (self.__class__, id(self), self.filename))
  2495. return s
  2496. _OFF = '\x0f'
  2497. _BOLD = '\x02'
  2498. _COLOR = '\x03'
  2499. _REVERSE_VIDEO = '\x16'
  2500. _UNDERLINE = '\x1f'
  2501. # Mapping of IRC color names to their color values.
  2502. _IRC_COLORS = dict(
  2503. zip(['white', 'black', 'blue', 'green', 'lightRed', 'red', 'magenta',
  2504. 'orange', 'yellow', 'lightGreen', 'cyan', 'lightCyan', 'lightBlue',
  2505. 'lightMagenta', 'gray', 'lightGray'], range(16)))
  2506. # Mapping of IRC color values to their color names.
  2507. _IRC_COLOR_NAMES = dict((code, name) for name, code in _IRC_COLORS.items())
  2508. class _CharacterAttributes(_textattributes.CharacterAttributesMixin):
  2509. """
  2510. Factory for character attributes, including foreground and background color
  2511. and non-color attributes such as bold, reverse video and underline.
  2512. Character attributes are applied to actual text by using object
  2513. indexing-syntax (C{obj['abc']}) after accessing a factory attribute, for
  2514. example::
  2515. attributes.bold['Some text']
  2516. These can be nested to mix attributes::
  2517. attributes.bold[attributes.underline['Some text']]
  2518. And multiple values can be passed::
  2519. attributes.normal[attributes.bold['Some'], ' text']
  2520. Non-color attributes can be accessed by attribute name, available
  2521. attributes are:
  2522. - bold
  2523. - reverseVideo
  2524. - underline
  2525. Available colors are:
  2526. 0. white
  2527. 1. black
  2528. 2. blue
  2529. 3. green
  2530. 4. light red
  2531. 5. red
  2532. 6. magenta
  2533. 7. orange
  2534. 8. yellow
  2535. 9. light green
  2536. 10. cyan
  2537. 11. light cyan
  2538. 12. light blue
  2539. 13. light magenta
  2540. 14. gray
  2541. 15. light gray
  2542. @ivar fg: Foreground colors accessed by attribute name, see above
  2543. for possible names.
  2544. @ivar bg: Background colors accessed by attribute name, see above
  2545. for possible names.
  2546. @since: 13.1
  2547. """
  2548. fg = _textattributes._ColorAttribute(
  2549. _textattributes._ForegroundColorAttr, _IRC_COLORS)
  2550. bg = _textattributes._ColorAttribute(
  2551. _textattributes._BackgroundColorAttr, _IRC_COLORS)
  2552. attrs = {
  2553. 'bold': _BOLD,
  2554. 'reverseVideo': _REVERSE_VIDEO,
  2555. 'underline': _UNDERLINE}
  2556. attributes = _CharacterAttributes()
  2557. class _FormattingState(_textattributes._FormattingStateMixin):
  2558. """
  2559. Formatting state/attributes of a single character.
  2560. Attributes include:
  2561. - Formatting nullifier
  2562. - Bold
  2563. - Underline
  2564. - Reverse video
  2565. - Foreground color
  2566. - Background color
  2567. @since: 13.1
  2568. """
  2569. compareAttributes = (
  2570. 'off', 'bold', 'underline', 'reverseVideo', 'foreground', 'background')
  2571. def __init__(self, off=False, bold=False, underline=False,
  2572. reverseVideo=False, foreground=None, background=None):
  2573. self.off = off
  2574. self.bold = bold
  2575. self.underline = underline
  2576. self.reverseVideo = reverseVideo
  2577. self.foreground = foreground
  2578. self.background = background
  2579. def toMIRCControlCodes(self):
  2580. """
  2581. Emit a mIRC control sequence that will set up all the attributes this
  2582. formatting state has set.
  2583. @return: A string containing mIRC control sequences that mimic this
  2584. formatting state.
  2585. """
  2586. attrs = []
  2587. if self.bold:
  2588. attrs.append(_BOLD)
  2589. if self.underline:
  2590. attrs.append(_UNDERLINE)
  2591. if self.reverseVideo:
  2592. attrs.append(_REVERSE_VIDEO)
  2593. if self.foreground is not None or self.background is not None:
  2594. c = ''
  2595. if self.foreground is not None:
  2596. c += '%02d' % (self.foreground,)
  2597. if self.background is not None:
  2598. c += ',%02d' % (self.background,)
  2599. attrs.append(_COLOR + c)
  2600. return _OFF + ''.join(map(str, attrs))
  2601. def _foldr(f, z, xs):
  2602. """
  2603. Apply a function of two arguments cumulatively to the items of
  2604. a sequence, from right to left, so as to reduce the sequence to
  2605. a single value.
  2606. @type f: C{callable} taking 2 arguments
  2607. @param z: Initial value.
  2608. @param xs: Sequence to reduce.
  2609. @return: Single value resulting from reducing C{xs}.
  2610. """
  2611. return reduce(lambda x, y: f(y, x), reversed(xs), z)
  2612. class _FormattingParser(_CommandDispatcherMixin):
  2613. """
  2614. A finite-state machine that parses formatted IRC text.
  2615. Currently handled formatting includes: bold, reverse, underline,
  2616. mIRC color codes and the ability to remove all current formatting.
  2617. @see: U{http://www.mirc.co.uk/help/color.txt}
  2618. @type _formatCodes: C{dict} mapping C{str} to C{str}
  2619. @cvar _formatCodes: Mapping of format code values to names.
  2620. @type state: C{str}
  2621. @ivar state: Current state of the finite-state machine.
  2622. @type _buffer: C{str}
  2623. @ivar _buffer: Buffer, containing the text content, of the formatting
  2624. sequence currently being parsed, the buffer is used as the content for
  2625. L{_attrs} before being added to L{_result} and emptied upon calling
  2626. L{emit}.
  2627. @type _attrs: C{set}
  2628. @ivar _attrs: Set of the applicable formatting states (bold, underline,
  2629. etc.) for the current L{_buffer}, these are applied to L{_buffer} when
  2630. calling L{emit}.
  2631. @type foreground: L{_ForegroundColorAttr}
  2632. @ivar foreground: Current foreground color attribute, or L{None}.
  2633. @type background: L{_BackgroundColorAttr}
  2634. @ivar background: Current background color attribute, or L{None}.
  2635. @ivar _result: Current parse result.
  2636. """
  2637. prefix = 'state'
  2638. _formatCodes = {
  2639. _OFF: 'off',
  2640. _BOLD: 'bold',
  2641. _COLOR: 'color',
  2642. _REVERSE_VIDEO: 'reverseVideo',
  2643. _UNDERLINE: 'underline'}
  2644. def __init__(self):
  2645. self.state = 'TEXT'
  2646. self._buffer = ''
  2647. self._attrs = set()
  2648. self._result = None
  2649. self.foreground = None
  2650. self.background = None
  2651. def process(self, ch):
  2652. """
  2653. Handle input.
  2654. @type ch: C{str}
  2655. @param ch: A single character of input to process
  2656. """
  2657. self.dispatch(self.state, ch)
  2658. def complete(self):
  2659. """
  2660. Flush the current buffer and return the final parsed result.
  2661. @return: Structured text and attributes.
  2662. """
  2663. self.emit()
  2664. if self._result is None:
  2665. self._result = attributes.normal
  2666. return self._result
  2667. def emit(self):
  2668. """
  2669. Add the currently parsed input to the result.
  2670. """
  2671. if self._buffer:
  2672. attrs = [getattr(attributes, name) for name in self._attrs]
  2673. attrs.extend(filter(None, [self.foreground, self.background]))
  2674. if not attrs:
  2675. attrs.append(attributes.normal)
  2676. attrs.append(self._buffer)
  2677. attr = _foldr(operator.getitem, attrs.pop(), attrs)
  2678. if self._result is None:
  2679. self._result = attr
  2680. else:
  2681. self._result[attr]
  2682. self._buffer = ''
  2683. def state_TEXT(self, ch):
  2684. """
  2685. Handle the "text" state.
  2686. Along with regular text, single token formatting codes are handled
  2687. in this state too.
  2688. @param ch: The character being processed.
  2689. """
  2690. formatName = self._formatCodes.get(ch)
  2691. if formatName == 'color':
  2692. self.emit()
  2693. self.state = 'COLOR_FOREGROUND'
  2694. else:
  2695. if formatName is None:
  2696. self._buffer += ch
  2697. else:
  2698. self.emit()
  2699. if formatName == 'off':
  2700. self._attrs = set()
  2701. self.foreground = self.background = None
  2702. else:
  2703. self._attrs.symmetric_difference_update([formatName])
  2704. def state_COLOR_FOREGROUND(self, ch):
  2705. """
  2706. Handle the foreground color state.
  2707. Foreground colors can consist of up to two digits and may optionally
  2708. end in a I{,}. Any non-digit or non-comma characters are treated as
  2709. invalid input and result in the state being reset to "text".
  2710. @param ch: The character being processed.
  2711. """
  2712. # Color codes may only be a maximum of two characters.
  2713. if ch.isdigit() and len(self._buffer) < 2:
  2714. self._buffer += ch
  2715. else:
  2716. if self._buffer:
  2717. # Wrap around for color numbers higher than we support, like
  2718. # most other IRC clients.
  2719. col = int(self._buffer) % len(_IRC_COLORS)
  2720. self.foreground = getattr(attributes.fg, _IRC_COLOR_NAMES[col])
  2721. else:
  2722. # If there were no digits, then this has been an empty color
  2723. # code and we can reset the color state.
  2724. self.foreground = self.background = None
  2725. if ch == ',' and self._buffer:
  2726. # If there's a comma and it's not the first thing, move on to
  2727. # the background state.
  2728. self._buffer = ''
  2729. self.state = 'COLOR_BACKGROUND'
  2730. else:
  2731. # Otherwise, this is a bogus color code, fall back to text.
  2732. self._buffer = ''
  2733. self.state = 'TEXT'
  2734. self.emit()
  2735. self.process(ch)
  2736. def state_COLOR_BACKGROUND(self, ch):
  2737. """
  2738. Handle the background color state.
  2739. Background colors can consist of up to two digits and must occur after
  2740. a foreground color and must be preceded by a I{,}. Any non-digit
  2741. character is treated as invalid input and results in the state being
  2742. set to "text".
  2743. @param ch: The character being processed.
  2744. """
  2745. # Color codes may only be a maximum of two characters.
  2746. if ch.isdigit() and len(self._buffer) < 2:
  2747. self._buffer += ch
  2748. else:
  2749. if self._buffer:
  2750. # Wrap around for color numbers higher than we support, like
  2751. # most other IRC clients.
  2752. col = int(self._buffer) % len(_IRC_COLORS)
  2753. self.background = getattr(attributes.bg, _IRC_COLOR_NAMES[col])
  2754. self._buffer = ''
  2755. self.emit()
  2756. self.state = 'TEXT'
  2757. self.process(ch)
  2758. def parseFormattedText(text):
  2759. """
  2760. Parse text containing IRC formatting codes into structured information.
  2761. Color codes are mapped from 0 to 15 and wrap around if greater than 15.
  2762. @type text: C{str}
  2763. @param text: Formatted text to parse.
  2764. @return: Structured text and attributes.
  2765. @since: 13.1
  2766. """
  2767. state = _FormattingParser()
  2768. for ch in text:
  2769. state.process(ch)
  2770. return state.complete()
  2771. def assembleFormattedText(formatted):
  2772. """
  2773. Assemble formatted text from structured information.
  2774. Currently handled formatting includes: bold, reverse, underline,
  2775. mIRC color codes and the ability to remove all current formatting.
  2776. It is worth noting that assembled text will always begin with the control
  2777. code to disable other attributes for the sake of correctness.
  2778. For example::
  2779. from twisted.words.protocols.irc import attributes as A
  2780. assembleFormattedText(
  2781. A.normal[A.bold['Time: '], A.fg.lightRed['Now!']])
  2782. Would produce "Time: " in bold formatting, followed by "Now!" with a
  2783. foreground color of light red and without any additional formatting.
  2784. Available attributes are:
  2785. - bold
  2786. - reverseVideo
  2787. - underline
  2788. Available colors are:
  2789. 0. white
  2790. 1. black
  2791. 2. blue
  2792. 3. green
  2793. 4. light red
  2794. 5. red
  2795. 6. magenta
  2796. 7. orange
  2797. 8. yellow
  2798. 9. light green
  2799. 10. cyan
  2800. 11. light cyan
  2801. 12. light blue
  2802. 13. light magenta
  2803. 14. gray
  2804. 15. light gray
  2805. @see: U{http://www.mirc.co.uk/help/color.txt}
  2806. @param formatted: Structured text and attributes.
  2807. @rtype: C{str}
  2808. @return: String containing mIRC control sequences that mimic those
  2809. specified by I{formatted}.
  2810. @since: 13.1
  2811. """
  2812. return _textattributes.flatten(
  2813. formatted, _FormattingState(), 'toMIRCControlCodes')
  2814. def stripFormatting(text):
  2815. """
  2816. Remove all formatting codes from C{text}, leaving only the text.
  2817. @type text: C{str}
  2818. @param text: Formatted text to parse.
  2819. @rtype: C{str}
  2820. @return: Plain text without any control sequences.
  2821. @since: 13.1
  2822. """
  2823. formatted = parseFormattedText(text)
  2824. return _textattributes.flatten(
  2825. formatted, _textattributes.DefaultFormattingState())
  2826. # CTCP constants and helper functions
  2827. X_DELIM = chr(0o01)
  2828. def ctcpExtract(message):
  2829. """
  2830. Extract CTCP data from a string.
  2831. @return: A C{dict} containing two keys:
  2832. - C{'extended'}: A list of CTCP (tag, data) tuples.
  2833. - C{'normal'}: A list of strings which were not inside a CTCP delimiter.
  2834. """
  2835. extended_messages = []
  2836. normal_messages = []
  2837. retval = {'extended': extended_messages,
  2838. 'normal': normal_messages }
  2839. messages = message.split(X_DELIM)
  2840. odd = 0
  2841. # X1 extended data X2 nomal data X3 extended data X4 normal...
  2842. while messages:
  2843. if odd:
  2844. extended_messages.append(messages.pop(0))
  2845. else:
  2846. normal_messages.append(messages.pop(0))
  2847. odd = not odd
  2848. extended_messages[:] = filter(None, extended_messages)
  2849. normal_messages[:] = filter(None, normal_messages)
  2850. extended_messages[:] = map(ctcpDequote, extended_messages)
  2851. for i in range(len(extended_messages)):
  2852. m = extended_messages[i].split(SPC, 1)
  2853. tag = m[0]
  2854. if len(m) > 1:
  2855. data = m[1]
  2856. else:
  2857. data = None
  2858. extended_messages[i] = (tag, data)
  2859. return retval
  2860. # CTCP escaping
  2861. M_QUOTE= chr(0o20)
  2862. mQuoteTable = {
  2863. NUL: M_QUOTE + '0',
  2864. NL: M_QUOTE + 'n',
  2865. CR: M_QUOTE + 'r',
  2866. M_QUOTE: M_QUOTE + M_QUOTE
  2867. }
  2868. mDequoteTable = {}
  2869. for k, v in mQuoteTable.items():
  2870. mDequoteTable[v[-1]] = k
  2871. del k, v
  2872. mEscape_re = re.compile('%s.' % (re.escape(M_QUOTE),), re.DOTALL)
  2873. def lowQuote(s):
  2874. for c in (M_QUOTE, NUL, NL, CR):
  2875. s = s.replace(c, mQuoteTable[c])
  2876. return s
  2877. def lowDequote(s):
  2878. def sub(matchobj, mDequoteTable=mDequoteTable):
  2879. s = matchobj.group()[1]
  2880. try:
  2881. s = mDequoteTable[s]
  2882. except KeyError:
  2883. s = s
  2884. return s
  2885. return mEscape_re.sub(sub, s)
  2886. X_QUOTE = '\\'
  2887. xQuoteTable = {
  2888. X_DELIM: X_QUOTE + 'a',
  2889. X_QUOTE: X_QUOTE + X_QUOTE
  2890. }
  2891. xDequoteTable = {}
  2892. for k, v in xQuoteTable.items():
  2893. xDequoteTable[v[-1]] = k
  2894. xEscape_re = re.compile('%s.' % (re.escape(X_QUOTE),), re.DOTALL)
  2895. def ctcpQuote(s):
  2896. for c in (X_QUOTE, X_DELIM):
  2897. s = s.replace(c, xQuoteTable[c])
  2898. return s
  2899. def ctcpDequote(s):
  2900. def sub(matchobj, xDequoteTable=xDequoteTable):
  2901. s = matchobj.group()[1]
  2902. try:
  2903. s = xDequoteTable[s]
  2904. except KeyError:
  2905. s = s
  2906. return s
  2907. return xEscape_re.sub(sub, s)
  2908. def ctcpStringify(messages):
  2909. """
  2910. @type messages: a list of extended messages. An extended
  2911. message is a (tag, data) tuple, where 'data' may be L{None}, a
  2912. string, or a list of strings to be joined with whitespace.
  2913. @returns: String
  2914. """
  2915. coded_messages = []
  2916. for (tag, data) in messages:
  2917. if data:
  2918. if not isinstance(data, str):
  2919. try:
  2920. # data as list-of-strings
  2921. data = " ".join(map(str, data))
  2922. except TypeError:
  2923. # No? Then use it's %s representation.
  2924. pass
  2925. m = "%s %s" % (tag, data)
  2926. else:
  2927. m = str(tag)
  2928. m = ctcpQuote(m)
  2929. m = "%s%s%s" % (X_DELIM, m, X_DELIM)
  2930. coded_messages.append(m)
  2931. line = ''.join(coded_messages)
  2932. return line
  2933. # Constants (from RFC 2812)
  2934. RPL_WELCOME = '001'
  2935. RPL_YOURHOST = '002'
  2936. RPL_CREATED = '003'
  2937. RPL_MYINFO = '004'
  2938. RPL_ISUPPORT = '005'
  2939. RPL_BOUNCE = '010'
  2940. RPL_USERHOST = '302'
  2941. RPL_ISON = '303'
  2942. RPL_AWAY = '301'
  2943. RPL_UNAWAY = '305'
  2944. RPL_NOWAWAY = '306'
  2945. RPL_WHOISUSER = '311'
  2946. RPL_WHOISSERVER = '312'
  2947. RPL_WHOISOPERATOR = '313'
  2948. RPL_WHOISIDLE = '317'
  2949. RPL_ENDOFWHOIS = '318'
  2950. RPL_WHOISCHANNELS = '319'
  2951. RPL_WHOWASUSER = '314'
  2952. RPL_ENDOFWHOWAS = '369'
  2953. RPL_LISTSTART = '321'
  2954. RPL_LIST = '322'
  2955. RPL_LISTEND = '323'
  2956. RPL_UNIQOPIS = '325'
  2957. RPL_CHANNELMODEIS = '324'
  2958. RPL_NOTOPIC = '331'
  2959. RPL_TOPIC = '332'
  2960. RPL_INVITING = '341'
  2961. RPL_SUMMONING = '342'
  2962. RPL_INVITELIST = '346'
  2963. RPL_ENDOFINVITELIST = '347'
  2964. RPL_EXCEPTLIST = '348'
  2965. RPL_ENDOFEXCEPTLIST = '349'
  2966. RPL_VERSION = '351'
  2967. RPL_WHOREPLY = '352'
  2968. RPL_ENDOFWHO = '315'
  2969. RPL_NAMREPLY = '353'
  2970. RPL_ENDOFNAMES = '366'
  2971. RPL_LINKS = '364'
  2972. RPL_ENDOFLINKS = '365'
  2973. RPL_BANLIST = '367'
  2974. RPL_ENDOFBANLIST = '368'
  2975. RPL_INFO = '371'
  2976. RPL_ENDOFINFO = '374'
  2977. RPL_MOTDSTART = '375'
  2978. RPL_MOTD = '372'
  2979. RPL_ENDOFMOTD = '376'
  2980. RPL_YOUREOPER = '381'
  2981. RPL_REHASHING = '382'
  2982. RPL_YOURESERVICE = '383'
  2983. RPL_TIME = '391'
  2984. RPL_USERSSTART = '392'
  2985. RPL_USERS = '393'
  2986. RPL_ENDOFUSERS = '394'
  2987. RPL_NOUSERS = '395'
  2988. RPL_TRACELINK = '200'
  2989. RPL_TRACECONNECTING = '201'
  2990. RPL_TRACEHANDSHAKE = '202'
  2991. RPL_TRACEUNKNOWN = '203'
  2992. RPL_TRACEOPERATOR = '204'
  2993. RPL_TRACEUSER = '205'
  2994. RPL_TRACESERVER = '206'
  2995. RPL_TRACESERVICE = '207'
  2996. RPL_TRACENEWTYPE = '208'
  2997. RPL_TRACECLASS = '209'
  2998. RPL_TRACERECONNECT = '210'
  2999. RPL_TRACELOG = '261'
  3000. RPL_TRACEEND = '262'
  3001. RPL_STATSLINKINFO = '211'
  3002. RPL_STATSCOMMANDS = '212'
  3003. RPL_ENDOFSTATS = '219'
  3004. RPL_STATSUPTIME = '242'
  3005. RPL_STATSOLINE = '243'
  3006. RPL_UMODEIS = '221'
  3007. RPL_SERVLIST = '234'
  3008. RPL_SERVLISTEND = '235'
  3009. RPL_LUSERCLIENT = '251'
  3010. RPL_LUSEROP = '252'
  3011. RPL_LUSERUNKNOWN = '253'
  3012. RPL_LUSERCHANNELS = '254'
  3013. RPL_LUSERME = '255'
  3014. RPL_ADMINME = '256'
  3015. RPL_ADMINLOC1 = '257'
  3016. RPL_ADMINLOC2 = '258'
  3017. RPL_ADMINEMAIL = '259'
  3018. RPL_TRYAGAIN = '263'
  3019. ERR_NOSUCHNICK = '401'
  3020. ERR_NOSUCHSERVER = '402'
  3021. ERR_NOSUCHCHANNEL = '403'
  3022. ERR_CANNOTSENDTOCHAN = '404'
  3023. ERR_TOOMANYCHANNELS = '405'
  3024. ERR_WASNOSUCHNICK = '406'
  3025. ERR_TOOMANYTARGETS = '407'
  3026. ERR_NOSUCHSERVICE = '408'
  3027. ERR_NOORIGIN = '409'
  3028. ERR_NORECIPIENT = '411'
  3029. ERR_NOTEXTTOSEND = '412'
  3030. ERR_NOTOPLEVEL = '413'
  3031. ERR_WILDTOPLEVEL = '414'
  3032. ERR_BADMASK = '415'
  3033. # Defined in errata.
  3034. # https://www.rfc-editor.org/errata_search.php?rfc=2812&eid=2822
  3035. ERR_TOOMANYMATCHES = '416'
  3036. ERR_UNKNOWNCOMMAND = '421'
  3037. ERR_NOMOTD = '422'
  3038. ERR_NOADMININFO = '423'
  3039. ERR_FILEERROR = '424'
  3040. ERR_NONICKNAMEGIVEN = '431'
  3041. ERR_ERRONEUSNICKNAME = '432'
  3042. ERR_NICKNAMEINUSE = '433'
  3043. ERR_NICKCOLLISION = '436'
  3044. ERR_UNAVAILRESOURCE = '437'
  3045. ERR_USERNOTINCHANNEL = '441'
  3046. ERR_NOTONCHANNEL = '442'
  3047. ERR_USERONCHANNEL = '443'
  3048. ERR_NOLOGIN = '444'
  3049. ERR_SUMMONDISABLED = '445'
  3050. ERR_USERSDISABLED = '446'
  3051. ERR_NOTREGISTERED = '451'
  3052. ERR_NEEDMOREPARAMS = '461'
  3053. ERR_ALREADYREGISTRED = '462'
  3054. ERR_NOPERMFORHOST = '463'
  3055. ERR_PASSWDMISMATCH = '464'
  3056. ERR_YOUREBANNEDCREEP = '465'
  3057. ERR_YOUWILLBEBANNED = '466'
  3058. ERR_KEYSET = '467'
  3059. ERR_CHANNELISFULL = '471'
  3060. ERR_UNKNOWNMODE = '472'
  3061. ERR_INVITEONLYCHAN = '473'
  3062. ERR_BANNEDFROMCHAN = '474'
  3063. ERR_BADCHANNELKEY = '475'
  3064. ERR_BADCHANMASK = '476'
  3065. ERR_NOCHANMODES = '477'
  3066. ERR_BANLISTFULL = '478'
  3067. ERR_NOPRIVILEGES = '481'
  3068. ERR_CHANOPRIVSNEEDED = '482'
  3069. ERR_CANTKILLSERVER = '483'
  3070. ERR_RESTRICTED = '484'
  3071. ERR_UNIQOPPRIVSNEEDED = '485'
  3072. ERR_NOOPERHOST = '491'
  3073. ERR_NOSERVICEHOST = '492'
  3074. ERR_UMODEUNKNOWNFLAG = '501'
  3075. ERR_USERSDONTMATCH = '502'
  3076. # And hey, as long as the strings are already intern'd...
  3077. symbolic_to_numeric = {
  3078. "RPL_WELCOME": '001',
  3079. "RPL_YOURHOST": '002',
  3080. "RPL_CREATED": '003',
  3081. "RPL_MYINFO": '004',
  3082. "RPL_ISUPPORT": '005',
  3083. "RPL_BOUNCE": '010',
  3084. "RPL_USERHOST": '302',
  3085. "RPL_ISON": '303',
  3086. "RPL_AWAY": '301',
  3087. "RPL_UNAWAY": '305',
  3088. "RPL_NOWAWAY": '306',
  3089. "RPL_WHOISUSER": '311',
  3090. "RPL_WHOISSERVER": '312',
  3091. "RPL_WHOISOPERATOR": '313',
  3092. "RPL_WHOISIDLE": '317',
  3093. "RPL_ENDOFWHOIS": '318',
  3094. "RPL_WHOISCHANNELS": '319',
  3095. "RPL_WHOWASUSER": '314',
  3096. "RPL_ENDOFWHOWAS": '369',
  3097. "RPL_LISTSTART": '321',
  3098. "RPL_LIST": '322',
  3099. "RPL_LISTEND": '323',
  3100. "RPL_UNIQOPIS": '325',
  3101. "RPL_CHANNELMODEIS": '324',
  3102. "RPL_NOTOPIC": '331',
  3103. "RPL_TOPIC": '332',
  3104. "RPL_INVITING": '341',
  3105. "RPL_SUMMONING": '342',
  3106. "RPL_INVITELIST": '346',
  3107. "RPL_ENDOFINVITELIST": '347',
  3108. "RPL_EXCEPTLIST": '348',
  3109. "RPL_ENDOFEXCEPTLIST": '349',
  3110. "RPL_VERSION": '351',
  3111. "RPL_WHOREPLY": '352',
  3112. "RPL_ENDOFWHO": '315',
  3113. "RPL_NAMREPLY": '353',
  3114. "RPL_ENDOFNAMES": '366',
  3115. "RPL_LINKS": '364',
  3116. "RPL_ENDOFLINKS": '365',
  3117. "RPL_BANLIST": '367',
  3118. "RPL_ENDOFBANLIST": '368',
  3119. "RPL_INFO": '371',
  3120. "RPL_ENDOFINFO": '374',
  3121. "RPL_MOTDSTART": '375',
  3122. "RPL_MOTD": '372',
  3123. "RPL_ENDOFMOTD": '376',
  3124. "RPL_YOUREOPER": '381',
  3125. "RPL_REHASHING": '382',
  3126. "RPL_YOURESERVICE": '383',
  3127. "RPL_TIME": '391',
  3128. "RPL_USERSSTART": '392',
  3129. "RPL_USERS": '393',
  3130. "RPL_ENDOFUSERS": '394',
  3131. "RPL_NOUSERS": '395',
  3132. "RPL_TRACELINK": '200',
  3133. "RPL_TRACECONNECTING": '201',
  3134. "RPL_TRACEHANDSHAKE": '202',
  3135. "RPL_TRACEUNKNOWN": '203',
  3136. "RPL_TRACEOPERATOR": '204',
  3137. "RPL_TRACEUSER": '205',
  3138. "RPL_TRACESERVER": '206',
  3139. "RPL_TRACESERVICE": '207',
  3140. "RPL_TRACENEWTYPE": '208',
  3141. "RPL_TRACECLASS": '209',
  3142. "RPL_TRACERECONNECT": '210',
  3143. "RPL_TRACELOG": '261',
  3144. "RPL_TRACEEND": '262',
  3145. "RPL_STATSLINKINFO": '211',
  3146. "RPL_STATSCOMMANDS": '212',
  3147. "RPL_ENDOFSTATS": '219',
  3148. "RPL_STATSUPTIME": '242',
  3149. "RPL_STATSOLINE": '243',
  3150. "RPL_UMODEIS": '221',
  3151. "RPL_SERVLIST": '234',
  3152. "RPL_SERVLISTEND": '235',
  3153. "RPL_LUSERCLIENT": '251',
  3154. "RPL_LUSEROP": '252',
  3155. "RPL_LUSERUNKNOWN": '253',
  3156. "RPL_LUSERCHANNELS": '254',
  3157. "RPL_LUSERME": '255',
  3158. "RPL_ADMINME": '256',
  3159. "RPL_ADMINLOC1": '257',
  3160. "RPL_ADMINLOC2": '258',
  3161. "RPL_ADMINEMAIL": '259',
  3162. "RPL_TRYAGAIN": '263',
  3163. "ERR_NOSUCHNICK": '401',
  3164. "ERR_NOSUCHSERVER": '402',
  3165. "ERR_NOSUCHCHANNEL": '403',
  3166. "ERR_CANNOTSENDTOCHAN": '404',
  3167. "ERR_TOOMANYCHANNELS": '405',
  3168. "ERR_WASNOSUCHNICK": '406',
  3169. "ERR_TOOMANYTARGETS": '407',
  3170. "ERR_NOSUCHSERVICE": '408',
  3171. "ERR_NOORIGIN": '409',
  3172. "ERR_NORECIPIENT": '411',
  3173. "ERR_NOTEXTTOSEND": '412',
  3174. "ERR_NOTOPLEVEL": '413',
  3175. "ERR_WILDTOPLEVEL": '414',
  3176. "ERR_BADMASK": '415',
  3177. "ERR_TOOMANYMATCHES": '416',
  3178. "ERR_UNKNOWNCOMMAND": '421',
  3179. "ERR_NOMOTD": '422',
  3180. "ERR_NOADMININFO": '423',
  3181. "ERR_FILEERROR": '424',
  3182. "ERR_NONICKNAMEGIVEN": '431',
  3183. "ERR_ERRONEUSNICKNAME": '432',
  3184. "ERR_NICKNAMEINUSE": '433',
  3185. "ERR_NICKCOLLISION": '436',
  3186. "ERR_UNAVAILRESOURCE": '437',
  3187. "ERR_USERNOTINCHANNEL": '441',
  3188. "ERR_NOTONCHANNEL": '442',
  3189. "ERR_USERONCHANNEL": '443',
  3190. "ERR_NOLOGIN": '444',
  3191. "ERR_SUMMONDISABLED": '445',
  3192. "ERR_USERSDISABLED": '446',
  3193. "ERR_NOTREGISTERED": '451',
  3194. "ERR_NEEDMOREPARAMS": '461',
  3195. "ERR_ALREADYREGISTRED": '462',
  3196. "ERR_NOPERMFORHOST": '463',
  3197. "ERR_PASSWDMISMATCH": '464',
  3198. "ERR_YOUREBANNEDCREEP": '465',
  3199. "ERR_YOUWILLBEBANNED": '466',
  3200. "ERR_KEYSET": '467',
  3201. "ERR_CHANNELISFULL": '471',
  3202. "ERR_UNKNOWNMODE": '472',
  3203. "ERR_INVITEONLYCHAN": '473',
  3204. "ERR_BANNEDFROMCHAN": '474',
  3205. "ERR_BADCHANNELKEY": '475',
  3206. "ERR_BADCHANMASK": '476',
  3207. "ERR_NOCHANMODES": '477',
  3208. "ERR_BANLISTFULL": '478',
  3209. "ERR_NOPRIVILEGES": '481',
  3210. "ERR_CHANOPRIVSNEEDED": '482',
  3211. "ERR_CANTKILLSERVER": '483',
  3212. "ERR_RESTRICTED": '484',
  3213. "ERR_UNIQOPPRIVSNEEDED": '485',
  3214. "ERR_NOOPERHOST": '491',
  3215. "ERR_NOSERVICEHOST": '492',
  3216. "ERR_UMODEUNKNOWNFLAG": '501',
  3217. "ERR_USERSDONTMATCH": '502',
  3218. }
  3219. numeric_to_symbolic = {}
  3220. for k, v in symbolic_to_numeric.items():
  3221. numeric_to_symbolic[v] = k