Advertisement
Guest User

Untitled

a guest
Jul 18th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 43.60 KB | None | 0 0
  1. ###
  2. # Copyright (c) 2002-2005 Jeremiah Fincher
  3. # All rights reserved.
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions are met:
  7. #
  8. # * Redistributions of source code must retain the above copyright notice,
  9. # this list of conditions, and the following disclaimer.
  10. # * Redistributions in binary form must reproduce the above copyright notice,
  11. # this list of conditions, and the following disclaimer in the
  12. # documentation and/or other materials provided with the distribution.
  13. # * Neither the name of the author of this software nor the name of
  14. # contributors to this software may be used to endorse or promote products
  15. # derived from this software without specific prior written consent.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. # POSSIBILITY OF SUCH DAMAGE.
  28. ###
  29.  
  30. import re
  31. import copy
  32. import time
  33. import random
  34.  
  35. import supybot.log as log
  36. import supybot.conf as conf
  37. import supybot.utils as utils
  38. import supybot.world as world
  39. import supybot.ircdb as ircdb
  40. import supybot.ircmsgs as ircmsgs
  41. import supybot.ircutils as ircutils
  42.  
  43. from utils.str import rsplit
  44. from utils.iter import imap, chain, cycle
  45. from utils.structures import queue, smallqueue, RingBuffer
  46.  
  47. ###
  48. # The base class for a callback to be registered with an Irc object. Shows
  49. # the required interface for callbacks -- name(),
  50. # inFilter(irc, msg), outFilter(irc, msg), and __call__(irc, msg) [used so
  51. # functions can be used as callbacks conceivable, and so if refactoring ever
  52. # changes the nature of the callbacks from classes to functions, syntactical
  53. # changes elsewhere won't be required.
  54. ###
  55.  
  56. class IrcCommandDispatcher(object):
  57. """Base class for classes that must dispatch on a command."""
  58. def dispatchCommand(self, command):
  59. """Given a string 'command', dispatches to doCommand."""
  60. return getattr(self, 'do' + command.capitalize(), None)
  61.  
  62.  
  63. class IrcCallback(IrcCommandDispatcher):
  64. """Base class for standard callbacks.
  65.  
  66. Callbacks derived from this class should have methods of the form
  67. "doCommand" -- doPrivmsg, doNick, do433, etc. These will be called
  68. on matching messages.
  69. """
  70. callAfter = ()
  71. callBefore = ()
  72. __metaclass__ = log.MetaFirewall
  73. __firewalled__ = {'die': None,
  74. 'reset': None,
  75. '__call__': None,
  76. 'inFilter': lambda self, irc, msg: msg,
  77. 'outFilter': lambda self, irc, msg: msg,
  78. 'name': lambda self: self.__class__.__name__,
  79. 'callPrecedence': lambda self, irc: ([], []),
  80. }
  81.  
  82. def __init__(self, *args, **kwargs):
  83. #object doesn't take any args, so the buck stops here.
  84. #super(IrcCallback, self).__init__(*args, **kwargs)
  85. pass
  86.  
  87. def __repr__(self):
  88. return '<%s %s %s>' % \
  89. (self.__class__.__name__, self.name(), object.__repr__(self))
  90.  
  91. def name(self):
  92. """Returns the name of the callback."""
  93. return self.__class__.__name__
  94.  
  95. def callPrecedence(self, irc):
  96. """Returns a pair of (callbacks to call before me,
  97. callbacks to call after me)"""
  98. after = []
  99. before = []
  100. for name in self.callBefore:
  101. cb = irc.getCallback(name)
  102. if cb is not None:
  103. after.append(cb)
  104. for name in self.callAfter:
  105. cb = irc.getCallback(name)
  106. if cb is not None:
  107. before.append(cb)
  108. assert self not in after, '%s was in its own after.' % self.name()
  109. assert self not in before, '%s was in its own before.' % self.name()
  110. return (before, after)
  111.  
  112. def inFilter(self, irc, msg):
  113. """Used for filtering/modifying messages as they're entering.
  114.  
  115. ircmsgs.IrcMsg objects are immutable, so this method is expected to
  116. return another ircmsgs.IrcMsg object. Obviously the same IrcMsg
  117. can be returned.
  118. """
  119. return msg
  120.  
  121. def outFilter(self, irc, msg):
  122. """Used for filtering/modifying messages as they're leaving.
  123.  
  124. As with inFilter, an IrcMsg is returned.
  125. """
  126. return msg
  127.  
  128. def __call__(self, irc, msg):
  129. """Used for handling each message."""
  130. method = self.dispatchCommand(msg.command)
  131. if method is not None:
  132. method(irc, msg)
  133.  
  134. def reset(self):
  135. """Resets the callback. Called when reconnecting to the server."""
  136. pass
  137.  
  138. def die(self):
  139. """Makes the callback die. Called when the parent Irc object dies."""
  140. pass
  141.  
  142. ###
  143. # Basic queue for IRC messages. It doesn't presently (but should at some
  144. # later point) reorder messages based on priority or penalty calculations.
  145. ###
  146. _high = frozenset(['MODE', 'KICK', 'PONG', 'NICK', 'PASS', 'CAPAB'])
  147. _low = frozenset(['PRIVMSG', 'PING', 'WHO', 'NOTICE', 'JOIN'])
  148. class IrcMsgQueue(object):
  149. """Class for a queue of IrcMsgs. Eventually, it should be smart.
  150.  
  151. Probably smarter than it is now, though it's gotten quite a bit smarter
  152. than it originally was. A method to "score" methods, and a heapq to
  153. maintain a priority queue of the messages would be the ideal way to do
  154. intelligent queuing.
  155.  
  156. As it stands, however, we simple keep track of 'high priority' messages,
  157. 'low priority' messages, and normal messages, and just make sure to return
  158. the 'high priority' ones before the normal ones before the 'low priority'
  159. ones.
  160. """
  161. __slots__ = ('msgs', 'highpriority', 'normal', 'lowpriority', 'lastJoin')
  162. def __init__(self, iterable=()):
  163. self.reset()
  164. for msg in iterable:
  165. self.enqueue(msg)
  166.  
  167. def reset(self):
  168. """Clears the queue."""
  169. self.lastJoin = 0
  170. self.highpriority = smallqueue()
  171. self.normal = smallqueue()
  172. self.lowpriority = smallqueue()
  173.  
  174. def enqueue(self, msg):
  175. """Enqueues a given message."""
  176. if msg in self and \
  177. conf.supybot.protocols.irc.queuing.duplicates():
  178. s = str(msg).strip()
  179. log.info('Not adding message %q to queue, already added.', s)
  180. return False
  181. else:
  182. if msg.command in _high:
  183. self.highpriority.enqueue(msg)
  184. elif msg.command in _low:
  185. self.lowpriority.enqueue(msg)
  186. else:
  187. self.normal.enqueue(msg)
  188. return True
  189.  
  190. def peek(self):
  191. """Dequeues a given message."""
  192. msg = None
  193. if self.highpriority:
  194. msg = self.highpriority.peek()
  195. elif self.normal:
  196. msg = self.normal.peek()
  197. elif self.lowpriority:
  198. msg = self.lowpriority.peek()
  199. if msg.command == 'JOIN':
  200. limit = conf.supybot.protocols.irc.queuing.rateLimit.join()
  201. now = time.time()
  202. if self.lastJoin + limit <= now:
  203. self.lastJoin = now
  204. else:
  205. msg = None
  206. return msg
  207.  
  208. def dequeue(self):
  209. """Dequeues a given message."""
  210. msg = None
  211. if self.highpriority:
  212. msg = self.highpriority.dequeue()
  213. elif self.normal:
  214. msg = self.normal.dequeue()
  215. elif self.lowpriority:
  216. msg = self.lowpriority.dequeue()
  217. if msg.command == 'JOIN':
  218. limit = conf.supybot.protocols.irc.queuing.rateLimit.join()
  219. now = time.time()
  220. if self.lastJoin + limit <= now:
  221. self.lastJoin = now
  222. else:
  223. self.lowpriority.enqueue(msg)
  224. msg = None
  225. return msg
  226.  
  227. def __contains__(self, msg):
  228. return msg in self.normal or \
  229. msg in self.lowpriority or \
  230. msg in self.highpriority
  231.  
  232. def __nonzero__(self):
  233. return bool(self.highpriority or self.normal or self.lowpriority)
  234.  
  235. def __len__(self):
  236. return len(self.highpriority)+len(self.lowpriority)+len(self.normal)
  237.  
  238. def __repr__(self):
  239. name = self.__class__.__name__
  240. return '%s(%r)' % (name, list(chain(self.highpriority,
  241. self.normal,
  242. self.lowpriority)))
  243. __str__ = __repr__
  244.  
  245.  
  246. ###
  247. # Maintains the state of IRC connection -- the most recent messages, the
  248. # status of various modes (especially ops/halfops/voices) in channels, etc.
  249. ###
  250. class ChannelState(utils.python.Object):
  251. __slots__ = ('users', 'ops', 'halfops', 'bans',
  252. 'quiets', 'exempts','invites','quietsOwner','bansOwner','exemptsOwner','exemptsOwner',
  253. 'voices', 'topic', 'modes', 'synchro','created')
  254. def __init__(self):
  255. self.topic = ''
  256. self.created = 0
  257. self.ops = ircutils.IrcSet()
  258. self.bans = ircutils.IrcSet()
  259. self.quiets = ircutils.IrcSet()
  260. self.exempts = ircutils.IrcSet()
  261. self.invites = ircutils.IrcSet()
  262. self.users = ircutils.IrcSet()
  263. self.voices = ircutils.IrcSet()
  264. self.halfops = ircutils.IrcSet()
  265. self.modes = ircutils.IrcDict()
  266. self.quietsOwner = {}
  267. self.bansOwner = {}
  268. self.exemptsOwner = {}
  269. self.invitesOwner = {}
  270. self.synchro = False
  271.  
  272. def isOp(self, nick):
  273. return nick in self.ops
  274. def isVoice(self, nick):
  275. return nick in self.voices
  276. def isHalfop(self, nick):
  277. return nick in self.halfops
  278.  
  279. def addUser(self, user):
  280. "Adds a given user to the ChannelState. Power prefixes are handled."
  281. nick = user.lstrip('@%+&~')
  282. if not nick:
  283. return
  284. # & is used to denote protected users in UnrealIRCd
  285. # ~ is used to denote channel owner in UnrealIRCd
  286. while user and user[0] in '@%+&~':
  287. (marker, user) = (user[0], user[1:])
  288. assert user, 'Looks like my caller is passing chars, not nicks.'
  289. if marker in '@&~':
  290. self.ops.add(nick)
  291. elif marker == '%':
  292. self.halfops.add(nick)
  293. elif marker == '+':
  294. self.voices.add(nick)
  295. self.users.add(nick)
  296.  
  297. def replaceUser(self, oldNick, newNick):
  298. """Changes the user oldNick to newNick; used for NICK changes."""
  299. # Note that this doesn't have to have the sigil (@%+) that users
  300. # have to have for addUser; it just changes the name of the user
  301. # without changing any of his categories.
  302. for s in (self.users, self.ops, self.halfops, self.voices):
  303. if oldNick in s:
  304. s.remove(oldNick)
  305. s.add(newNick)
  306.  
  307. def removeUser(self, user):
  308. """Removes a given user from the channel."""
  309. self.users.discard(user)
  310. self.ops.discard(user)
  311. self.halfops.discard(user)
  312. self.voices.discard(user)
  313.  
  314. def setMode(self, mode, value=None):
  315. assert mode not in 'ovhbeqI'
  316. self.modes[mode] = value
  317.  
  318. def unsetMode(self, mode):
  319. assert mode not in 'ovhbeqI'
  320. if mode in self.modes:
  321. del self.modes[mode]
  322.  
  323. def doMode(self, msg):
  324. def getSet(c):
  325. if c == 'o':
  326. Set = self.ops
  327. elif c == 'v':
  328. Set = self.voices
  329. elif c == 'h':
  330. Set = self.halfops
  331. elif c == 'b':
  332. Set = self.bans
  333. elif c == 'I':
  334. Set = self.invites
  335. elif c == 'e':
  336. Set = self.exempts
  337. elif c == 'q':
  338. Set = self.quiets
  339. else: # We don't care yet, so we'll just return an empty set.
  340. Set = set()
  341. return Set
  342. for (mode, value) in ircutils.separateModes(msg.args[1:]):
  343. (action, modeChar) = mode
  344. if modeChar in 'ovhbeqI': # supported modes
  345. Set = getSet(modeChar)
  346. if action == '-':
  347. Set.discard(value)
  348. if modeChar == 'b':
  349. if value in self.bansOwner:
  350. del self.bansOwner[value]
  351. elif modeChar == 'I':
  352. if value in self.invitesOwner:
  353. del self.invitesOwner[value]
  354. elif modeChar == 'e':
  355. if value in self.exemptsOwner:
  356. del self.exemptsOwner[value]
  357. elif modeChar == 'q':
  358. if value in self.quietsOwner:
  359. del self.quietsOwner[value]
  360. elif action == '+':
  361. Set.add(value)
  362. if modeChar == 'b':
  363. self.bansOwner[value] = msg.nick
  364. elif modeChar == 'I':
  365. self.invitesOwner[value] = msg.nick
  366. elif modeChar == 'e':
  367. self.exemptsOwner[value] = msg.nick
  368. elif modeChar == 'q':
  369. self.quietsOwner[value] = msg.nick
  370. else:
  371. if action == '+':
  372. self.setMode(modeChar, value)
  373. else:
  374. assert action == '-'
  375. self.unsetMode(modeChar)
  376.  
  377. def __getstate__(self):
  378. return [getattr(self, name) for name in self.__slots__]
  379.  
  380. def __setstate__(self, t):
  381. for (name, value) in zip(self.__slots__, t):
  382. setattr(self, name, value)
  383.  
  384. def __eq__(self, other):
  385. ret = True
  386. for name in self.__slots__:
  387. ret = ret and getattr(self, name) == getattr(other, name)
  388. return ret
  389.  
  390.  
  391. class IrcState(IrcCommandDispatcher):
  392. """Maintains state of the Irc connection. Should also become smarter.
  393. """
  394. __metaclass__ = log.MetaFirewall
  395. __firewalled__ = {'addMsg': None}
  396. def __init__(self, history=None, supported=None,
  397. nicksToHostmasks=None, channels=None):
  398. if history is None:
  399. history = RingBuffer(conf.supybot.protocols.irc.maxHistoryLength())
  400. if supported is None:
  401. supported = utils.InsensitivePreservingDict()
  402. if nicksToHostmasks is None:
  403. nicksToHostmasks = ircutils.IrcDict()
  404. if channels is None:
  405. channels = ircutils.IrcDict()
  406. self.supported = supported
  407. self.history = history
  408. self.channels = channels
  409. self.nicksToHostmasks = nicksToHostmasks
  410.  
  411. def reset(self):
  412. """Resets the state to normal, unconnected state."""
  413. self.history.reset()
  414. self.channels.clear()
  415. self.supported.clear()
  416. self.nicksToHostmasks.clear()
  417. self.history.resize(conf.supybot.protocols.irc.maxHistoryLength())
  418.  
  419. def __reduce__(self):
  420. return (self.__class__, (self.history, self.supported,
  421. self.nicksToHostmasks, self.channels))
  422.  
  423. def __eq__(self, other):
  424. return self.history == other.history and \
  425. self.channels == other.channels and \
  426. self.supported == other.supported and \
  427. self.nicksToHostmasks == other.nicksToHostmasks
  428.  
  429. def __ne__(self, other):
  430. return not self == other
  431.  
  432. def copy(self):
  433. ret = self.__class__()
  434. ret.history = copy.deepcopy(self.history)
  435. ret.nicksToHostmasks = copy.deepcopy(self.nicksToHostmasks)
  436. ret.channels = copy.deepcopy(self.channels)
  437. return ret
  438.  
  439. def addMsg(self, irc, msg):
  440. """Updates the state based on the irc object and the message."""
  441. self.history.append(msg)
  442. if ircutils.isUserHostmask(msg.prefix) and not msg.command == 'NICK':
  443. self.nicksToHostmasks[msg.nick] = msg.prefix
  444. method = self.dispatchCommand(msg.command)
  445. if method is not None:
  446. method(irc, msg)
  447.  
  448. def getTopic(self, channel):
  449. """Returns the topic for a given channel."""
  450. return self.channels[channel].topic
  451.  
  452. def nickToHostmask(self, nick):
  453. """Returns the hostmask for a given nick."""
  454. return self.nicksToHostmasks[nick]
  455.  
  456. _005converters = utils.InsensitivePreservingDict({
  457. 'modes': int,
  458. 'keylen': int,
  459. 'nicklen': int,
  460. 'userlen': int,
  461. 'hostlen': int,
  462. 'kicklen': int,
  463. 'awaylen': int,
  464. 'silence': int,
  465. 'topiclen': int,
  466. 'channellen': int,
  467. 'maxtargets': int,
  468. 'maxnicklen': int,
  469. 'maxchannels': int,
  470. 'watch': int, # DynastyNet, EnterTheGame
  471. })
  472. def _prefixParser(s):
  473. if ')' in s:
  474. (left, right) = s.split(')')
  475. assert left[0] == '(', 'Odd PREFIX in 005: %s' % s
  476. left = left[1:]
  477. assert len(left) == len(right), 'Odd PREFIX in 005: %s' % s
  478. return dict(zip(left, right))
  479. else:
  480. return dict(zip('ovh', s))
  481. _005converters['prefix'] = _prefixParser
  482. del _prefixParser
  483. def _maxlistParser(s):
  484. modes = ''
  485. limits = []
  486. pairs = s.split(',')
  487. for pair in pairs:
  488. (mode, limit) = pair.split(':', 1)
  489. modes += mode
  490. limits += (int(limit),) * len(mode)
  491. return dict(zip(modes, limits))
  492. _005converters['maxlist'] = _maxlistParser
  493. del _maxlistParser
  494. def _maxbansParser(s):
  495. # IRCd using a MAXLIST style string (IRCNet)
  496. if ':' in s:
  497. modes = ''
  498. limits = []
  499. pairs = s.split(',')
  500. for pair in pairs:
  501. (mode, limit) = pair.split(':', 1)
  502. modes += mode
  503. limits += (int(limit),) * len(mode)
  504. d = dict(zip(modes, limits))
  505. assert 'b' in d
  506. return d['b']
  507. else:
  508. return int(s)
  509. _005converters['maxbans'] = _maxbansParser
  510. del _maxbansParser
  511. def do005(self, irc, msg):
  512. for arg in msg.args[1:-1]: # 0 is nick, -1 is "are supported"
  513. if '=' in arg:
  514. (name, value) = arg.split('=', 1)
  515. converter = self._005converters.get(name, lambda x: x)
  516. try:
  517. self.supported[name] = converter(value)
  518. except Exception, e:
  519. log.exception('Uncaught exception in 005 converter:')
  520. log.error('Name: %s, Converter: %s', name, converter)
  521. else:
  522. self.supported[arg] = None
  523.  
  524. def do352(self, irc, msg):
  525. # WHO reply.
  526. (nick, user, host) = (msg.args[5], msg.args[2], msg.args[3])
  527. hostmask = '%s!%s@%s' % (nick, user, host)
  528. self.nicksToHostmasks[nick] = hostmask
  529.  
  530. def do353(self, irc, msg):
  531. # NAMES reply.
  532. (_, type, channel, names) = msg.args
  533. if channel not in self.channels:
  534. self.channels[channel] = ChannelState()
  535. c = self.channels[channel]
  536. for name in names.split():
  537. c.addUser(name)
  538. if type == '@':
  539. c.modes['s'] = None
  540.  
  541. def doJoin(self, irc, msg):
  542. for channel in msg.args[0].split(','):
  543. if channel in self.channels:
  544. self.channels[channel].addUser(msg.nick)
  545. elif msg.nick: # It must be us.
  546. chan = ChannelState()
  547. chan.addUser(msg.nick)
  548. self.channels[channel] = chan
  549. # I don't know why this assert was here.
  550. #assert msg.nick == irc.nick, msg
  551.  
  552. def doMode(self, irc, msg):
  553. channel = msg.args[0]
  554. if ircutils.isChannel(channel): # There can be user modes, as well.
  555. try:
  556. chan = self.channels[channel]
  557. except KeyError:
  558. chan = ChannelState()
  559. self.channels[channel] = chan
  560. chan.doMode(msg)
  561. for (mode, value) in ircutils.separateModes(msg.args[1:]):
  562. (action, modeChar) = mode
  563. if modeChar == 'o' and action == '+' and value == irc.nick:
  564. if not chan.synchro:
  565. irc.queueMsg(ircmsgs.IrcMsg('MODE %s +e' % channel))
  566. irc.queueMsg(ircmsgs.IrcMsg('MODE %s +I' % channel))
  567.  
  568. def do324(self, irc, msg):
  569. channel = msg.args[1]
  570. chan = self.channels[channel]
  571. for (mode, value) in ircutils.separateModes(msg.args[2:]):
  572. modeChar = mode[1]
  573. if mode[0] == '+' and mode[1] not in 'ovh':
  574. chan.setMode(modeChar, value)
  575. elif mode[0] == '-' and mode[1] not in 'ovh':
  576. chan.unsetMode(modeChar)
  577.  
  578. def do329(self, irc, msg):
  579. # This is the last part of an empty mode.
  580. channel = msg.args[1]
  581. chan = self.channels[channel]
  582. chan.created = int(msg.args[2])
  583.  
  584. def doPart(self, irc, msg):
  585. for channel in msg.args[0].split(','):
  586. try:
  587. chan = self.channels[channel]
  588. except KeyError:
  589. continue
  590. if ircutils.strEqual(msg.nick, irc.nick):
  591. del self.channels[channel]
  592. else:
  593. chan.removeUser(msg.nick)
  594.  
  595. def doKick(self, irc, msg):
  596. (channel, users) = msg.args[:2]
  597. chan = self.channels[channel]
  598. for user in users.split(','):
  599. if ircutils.strEqual(user, irc.nick):
  600. del self.channels[channel]
  601. return
  602. else:
  603. chan.removeUser(user)
  604.  
  605. def doQuit(self, irc, msg):
  606. for channel in self.channels.itervalues():
  607. channel.removeUser(msg.nick)
  608. if msg.nick in self.nicksToHostmasks:
  609. # If we're quitting, it may not be.
  610. del self.nicksToHostmasks[msg.nick]
  611.  
  612. def doTopic(self, irc, msg):
  613. if len(msg.args) == 1:
  614. return # Empty TOPIC for information. Does not affect state.
  615. try:
  616. chan = self.channels[msg.args[0]]
  617. chan.topic = msg.args[1]
  618. except KeyError:
  619. pass # We don't have to be in a channel to send a TOPIC.
  620.  
  621. def do332(self, irc, msg):
  622. chan = self.channels[msg.args[1]]
  623. chan.topic = msg.args[2]
  624.  
  625. def do315(self,irc,msg):
  626. channel = msg.args[1]
  627. if ircutils.isChannel(channel):
  628. if channel in self.channels:
  629. chan = self.channels[channel]
  630. chan.synchro = True
  631.  
  632. def doNick(self, irc, msg):
  633. newNick = msg.args[0]
  634. oldNick = msg.nick
  635. try:
  636. if msg.user and msg.host:
  637. # Nick messages being handed out from the bot itself won't
  638. # have the necessary prefix to make a hostmask.
  639. newHostmask = ircutils.joinHostmask(newNick,msg.user,msg.host)
  640. self.nicksToHostmasks[newNick] = newHostmask
  641. del self.nicksToHostmasks[oldNick]
  642. except KeyError:
  643. pass
  644. for channel in self.channels.itervalues():
  645. channel.replaceUser(oldNick, newNick)
  646.  
  647.  
  648.  
  649. ###
  650. # The basic class for handling a connection to an IRC server. Accepts
  651. # callbacks of the IrcCallback interface. Public attributes include 'driver',
  652. # 'queue', and 'state', in addition to the standard nick/user/ident attributes.
  653. ###
  654. _callbacks = []
  655. class Irc(IrcCommandDispatcher):
  656. """The base class for an IRC connection.
  657.  
  658. Handles PING commands already.
  659. """
  660. __metaclass__ = log.MetaFirewall
  661. __firewalled__ = {'die': None,
  662. 'feedMsg': None,
  663. 'takeMsg': None,}
  664. _nickSetters = set(['001', '002', '003', '004', '250', '251', '252',
  665. '254', '255', '265', '266', '372', '375', '376',
  666. '333', '353', '332', '366', '005'])
  667. # We specifically want these callbacks to be common between all Ircs,
  668. # that's why we don't do the normal None default with a check.
  669. def __init__(self, network, callbacks=_callbacks):
  670. self.zombie = False
  671. world.ircs.append(self)
  672. self.network = network
  673. self.callbacks = callbacks
  674. self.state = IrcState()
  675. self.queue = IrcMsgQueue()
  676. self.fastqueue = smallqueue()
  677. self.driver = None # The driver should set this later.
  678. self._setNonResettingVariables()
  679. self._queueConnectMessages()
  680. self.startedSync = ircutils.IrcDict()
  681. self.startedList = ircutils.IrcDict()
  682. self.outbuffer = 0
  683. self.version = None
  684.  
  685. def isChannel(self, s):
  686. """Helper function to check whether a given string is a channel on
  687. the network this Irc object is connected to."""
  688. kw = {}
  689. if 'chantypes' in self.state.supported:
  690. kw['chantypes'] = self.state.supported['chantypes']
  691. if 'channellen' in self.state.supported:
  692. kw['channellen'] = self.state.supported['channellen']
  693. return ircutils.isChannel(s, **kw)
  694.  
  695. def isNick(self, s):
  696. kw = {}
  697. if 'nicklen' in self.state.supported:
  698. kw['nicklen'] = self.state.supported['nicklen']
  699. return ircutils.isNick(s, **kw)
  700.  
  701. # This *isn't* threadsafe!
  702. def addCallback(self, callback):
  703. """Adds a callback to the callbacks list."""
  704. assert not self.getCallback(callback.name())
  705. self.callbacks.append(callback)
  706. # This is the new list we're building, which will be tsorted.
  707. cbs = []
  708. # The vertices are self.callbacks itself. Now we make the edges.
  709. edges = set()
  710. for cb in self.callbacks:
  711. (before, after) = cb.callPrecedence(self)
  712. assert cb not in after, 'cb was in its own after.'
  713. assert cb not in before, 'cb was in its own before.'
  714. for otherCb in before:
  715. edges.add((otherCb, cb))
  716. for otherCb in after:
  717. edges.add((cb, otherCb))
  718. def getFirsts():
  719. firsts = set(self.callbacks) - set(cbs)
  720. for (before, after) in edges:
  721. firsts.discard(after)
  722. return firsts
  723. firsts = getFirsts()
  724. while firsts:
  725. # Then we add these to our list of cbs, and remove all edges that
  726. # originate with these cbs.
  727. for cb in firsts:
  728. cbs.append(cb)
  729. edgesToRemove = []
  730. for edge in edges:
  731. if edge[0] is cb:
  732. edgesToRemove.append(edge)
  733. for edge in edgesToRemove:
  734. edges.remove(edge)
  735. firsts = getFirsts()
  736. assert len(cbs) == len(self.callbacks), \
  737. 'cbs: %s, self.callbacks: %s' % (cbs, self.callbacks)
  738. self.callbacks[:] = cbs
  739.  
  740. def getCallback(self, name):
  741. """Gets a given callback by name."""
  742. name = name.lower()
  743. for callback in self.callbacks:
  744. if callback.name().lower() == name:
  745. return callback
  746. else:
  747. return None
  748.  
  749. def removeCallback(self, name):
  750. """Removes a callback from the callback list."""
  751. name = name.lower()
  752. def nameMatches(cb):
  753. return cb.name().lower() == name
  754. (bad, good) = utils.iter.partition(nameMatches, self.callbacks)
  755. self.callbacks[:] = good
  756. return bad
  757.  
  758. def queueMsg(self, msg):
  759. """Queues a message to be sent to the server."""
  760. if not self.zombie:
  761. return self.queue.enqueue(msg)
  762. else:
  763. log.warning('Refusing to queue %r; %s is a zombie.', msg, self)
  764. return False
  765.  
  766. def sendMsg(self, msg):
  767. """Queues a message to be sent to the server *immediately*"""
  768. if not self.zombie:
  769. self.fastqueue.enqueue(msg)
  770. else:
  771. log.warning('Refusing to send %r; %s is a zombie.', msg, self)
  772.  
  773. def takeMsg(self):
  774. """Called by the IrcDriver; takes a message to be sent."""
  775. if not self.callbacks:
  776. log.critical('No callbacks in %s.', self)
  777. now = time.time()
  778. msg = None
  779. fast = False
  780. if self.fastqueue:
  781. msg = self.fastqueue.peek()
  782. fast = True
  783. elif self.queue:
  784. msg = self.queue.peek()
  785. if msg:
  786. n = self.outbuffer + len(str(msg))
  787. if n < 1483:
  788. if fast:
  789. msg = self.fastqueue.dequeue()
  790. else:
  791. self.lastTake = now
  792. msg = self.queue.dequeue()
  793. else:
  794. if self.afterConnect and not self.outstandingPing:
  795. self.lastping = now
  796. now = str(int(now))
  797. self.outstandingPing = True
  798. msg = ircmsgs.ping(now)
  799. else:
  800. msg = None
  801. else:
  802. if self.afterConnect and \
  803. conf.supybot.protocols.irc.ping() and \
  804. now > self.lastping + conf.supybot.protocols.irc.ping.interval():
  805. if self.outstandingPing:
  806. s = 'Ping sent at %s not replied to.' % \
  807. log.timestamp(self.lastping)
  808. log.warning(s)
  809. self.feedMsg(ircmsgs.error(s))
  810. self.driver.reconnect()
  811. elif not self.zombie:
  812. self.lastping = now
  813. now = str(int(now))
  814. self.outstandingPing = True
  815. self.queueMsg(ircmsgs.ping(now))
  816. if msg:
  817. self.outbuffer += len(str(msg))
  818. #log.info('>> outbuffer: %s msg: %s' % (self.outbuffer,str(msg)))
  819. for callback in reversed(self.callbacks):
  820. msg = callback.outFilter(self, msg)
  821. if msg is None:
  822. log.debug('%s.outFilter returned None.', callback.name())
  823. return self.takeMsg()
  824. world.debugFlush()
  825. if len(str(msg)) > 512:
  826. # Yes, this violates the contract, but at this point it doesn't
  827. # matter. That's why we gotta go munging in private attributes
  828. #
  829. # I'm changing this to a log.debug to fix a possible loop in
  830. # the LogToIrc plugin. Since users can't do anything about
  831. # this issue, there's no fundamental reason to make it a
  832. # warning.
  833. log.debug('Truncating %r, message is too long.', msg)
  834. msg._str = msg._str[:500] + '\r\n'
  835. msg._len = len(str(msg))
  836. # I don't think we should do this. Why should it matter? If it's
  837. # something important, then the server will send it back to us,
  838. # and if it's just a privmsg/notice/etc., we don't care.
  839. # On second thought, we need this for testing.
  840. if world.testing:
  841. self.state.addMsg(self, msg)
  842. log.info('[>>] %s', str(msg).rstrip('\r\n'))
  843. return msg
  844. elif self.zombie:
  845. # We kill the driver here so it doesn't continue to try to
  846. # take messages from us.
  847. self.driver.die()
  848. self._reallyDie()
  849. else:
  850. return None
  851.  
  852. _numericErrorCommandRe = re.compile(r'^[45][0-9][0-9]$')
  853. def feedMsg(self, msg):
  854. """Called by the IrcDriver; feeds a message received."""
  855. msg.tag('receivedBy', self)
  856. msg.tag('receivedOn', self.network)
  857. if msg.args and self.isChannel(msg.args[0]):
  858. channel = msg.args[0]
  859. else:
  860. channel = None
  861. preInFilter = str(msg).rstrip('\r\n')
  862. log.info('[<<] %s', preInFilter)
  863. # Yeah, so this is odd. Some networks (oftc) seem to give us certain
  864. # messages with our nick instead of our prefix. We'll fix that here.
  865. if msg.prefix == self.nick:
  866. log.debug('Got one of those odd nick-instead-of-prefix msgs.')
  867. msg = ircmsgs.IrcMsg(prefix=self.prefix, msg=msg)
  868.  
  869. # This catches cases where we know our own nick (from sending it to the
  870. # server) but we don't yet know our prefix.
  871. if msg.nick == self.nick and self.prefix != msg.prefix:
  872. self.prefix = msg.prefix
  873.  
  874. # This keeps our nick and server attributes updated.
  875. if msg.command in self._nickSetters:
  876. if msg.args[0] != self.nick:
  877. self.nick = msg.args[0]
  878. log.debug('Updating nick attribute to %s.', self.nick)
  879. if msg.prefix != self.server:
  880. self.server = msg.prefix
  881. log.debug('Updating server attribute to %s.', self.server)
  882.  
  883. # Dispatch to specific handlers for commands.
  884. method = self.dispatchCommand(msg.command)
  885. if method is not None:
  886. method(msg)
  887. elif self._numericErrorCommandRe.search(msg.command):
  888. log.error('Unhandled error message from server: %r' % msg)
  889.  
  890. # Now update the IrcState object.
  891. try:
  892. self.state.addMsg(self, msg)
  893. except:
  894. log.exception('Exception in update of IrcState object:')
  895.  
  896. # Now call the callbacks.
  897. world.debugFlush()
  898. for callback in self.callbacks:
  899. try:
  900. m = callback.inFilter(self, msg)
  901. if not m:
  902. log.debug('%s.inFilter returned None', callback.name())
  903. return
  904. msg = m
  905. except:
  906. log.exception('Uncaught exception in inFilter:')
  907. world.debugFlush()
  908. postInFilter = str(msg).rstrip('\r\n')
  909. if postInFilter != preInFilter:
  910. log.debug('Incoming message (post-inFilter): %s', postInFilter)
  911. for callback in self.callbacks:
  912. try:
  913. if callback is not None:
  914. callback(self, msg)
  915. except:
  916. log.exception('Uncaught exception in callback:')
  917. world.debugFlush()
  918.  
  919. def die(self):
  920. """Makes the Irc object *promise* to die -- but it won't die (of its
  921. own volition) until all its queues are clear. Isn't that cool?"""
  922. self.zombie = True
  923. if not self.afterConnect:
  924. self._reallyDie()
  925.  
  926. # This is useless because it's in world.ircs, so it won't be deleted until
  927. # the program exits. Just figured you might want to know.
  928. #def __del__(self):
  929. # self._reallyDie()
  930.  
  931. def reset(self):
  932. """Resets the Irc object. Called when the driver reconnects."""
  933. self._setNonResettingVariables()
  934. self.state.reset()
  935. self.queue.reset()
  936. self.fastqueue.reset()
  937. self.startedSync.clear()
  938. self.startedList.clear()
  939. for callback in self.callbacks:
  940. callback.reset()
  941. self._queueConnectMessages()
  942. self.outbuffer = 0
  943.  
  944. def _setNonResettingVariables(self):
  945. # Configuration stuff.
  946. self.nick = conf.supybot.nick()
  947. self.user = conf.supybot.user()
  948. self.ident = conf.supybot.ident()
  949. self.alternateNicks = conf.supybot.nick.alternates()[:]
  950. self.password = conf.supybot.networks.get(self.network).password()
  951. self.prefix = '%s!%s@%s' % (self.nick, self.ident, 'unset.domain')
  952. # The rest.
  953. self.lastTake = 0
  954. self.inbuffer = 0
  955. self.server = 'unset'
  956. self.afterConnect = False
  957. self.lastping = time.time()
  958. self.outstandingPing = False
  959.  
  960. def _queueConnectMessages(self):
  961. if self.zombie:
  962. self.driver.die()
  963. self._reallyDie()
  964. else:
  965. if self.password:
  966. log.info('Sending PASS command, not logging the password.')
  967. self.queueMsg(ircmsgs.password(self.password))
  968. log.debug('Queuing NICK command, nick is %s.', self.nick)
  969. self.queueMsg(ircmsgs.nick(self.nick))
  970. log.debug('Queuing USER command, ident is %s, user is %s.',
  971. self.ident, self.user)
  972. self.queueMsg(ircmsgs.user(self.ident, self.user))
  973. self.queueMsg(ircmsgs.IrcMsg('CAP REQ identify-msg'))
  974.  
  975. def _getNextNick(self):
  976. if self.alternateNicks:
  977. nick = self.alternateNicks.pop(0)
  978. if '%s' in nick:
  979. nick %= conf.supybot.nick()
  980. return nick
  981. else:
  982. nick = conf.supybot.nick()
  983. ret = nick
  984. L = list(nick)
  985. while len(L) <= 3:
  986. L.append('`')
  987. while ircutils.strEqual(ret, nick):
  988. L[random.randrange(len(L))] = utils.iter.choice('0123456789')
  989. ret = ''.join(L)
  990. return ret
  991.  
  992. def do002(self, msg):
  993. """Logs the ircd version."""
  994. (beginning, version) = rsplit(msg.args[-1], maxsplit=1)
  995. self.version = version
  996. log.info('Server %s has version %s', self.server, version)
  997.  
  998. def doPing(self, msg):
  999. """Handles PING messages."""
  1000. self.sendMsg(ircmsgs.pong(msg.args[0]))
  1001.  
  1002. def doPong(self, msg):
  1003. """Handles PONG messages."""
  1004. self.outstandingPing = False
  1005. self.outbuffer = 0
  1006.  
  1007. def do376(self, msg):
  1008. log.info('Got end of MOTD from %s', self.server)
  1009. self.afterConnect = True
  1010. # Let's reset nicks in case we had to use a weird one.
  1011. self.alternateNicks = conf.supybot.nick.alternates()[:]
  1012. umodes = conf.supybot.protocols.irc.umodes()
  1013. if umodes:
  1014. if umodes[0] not in '+-':
  1015. umodes = '+' + umodes
  1016. log.info('Sending user modes to %s: %s', self.network, umodes)
  1017. self.sendMsg(ircmsgs.mode(self.nick, umodes))
  1018. do377 = do422 = do376
  1019.  
  1020. def do43x(self, msg, problem):
  1021. if not self.afterConnect:
  1022. newNick = self._getNextNick()
  1023. assert newNick != self.nick
  1024. log.info('Got 433: %s %s. Trying %s.',self.nick, problem, newNick)
  1025. self.sendMsg(ircmsgs.nick(newNick))
  1026. def do433(self, msg):
  1027. self.do43x(msg, 'is in use')
  1028. def do432(self, msg):
  1029. self.do43x(msg, 'is not a valid nickname')
  1030.  
  1031. def doJoin(self, msg):
  1032. if msg.nick == self.nick:
  1033. channel = msg.args[0]
  1034. self.startedSync[channel] = time.time()
  1035. self.startedList[channel] = '+b'
  1036. self.queueMsg(ircmsgs.IrcMsg('MODE %s +b' % channel))
  1037.  
  1038. def do367(self, msg):
  1039. channel = msg.args[1];
  1040. if channel in self.startedList:
  1041. c = self.startedList[channel]
  1042. m = ircmsgs.IrcMsg(':%s MODE %s %s %s' % (msg.args[3],channel,c,msg.args[2]))
  1043. self.feedMsg(m)
  1044.  
  1045. def do368(self,msg):
  1046. channel = msg.args[1];
  1047. if channel in self.startedList:
  1048. m = self.startedList[channel]
  1049. if m == '+b':
  1050. self.startedList[channel] = '+q'
  1051. self.queueMsg(ircmsgs.IrcMsg('MODE %s +q' % channel))
  1052. elif m == '+q':
  1053. del self.startedList[channel]
  1054. self.queueMsg(ircmsgs.mode(channel)) # Ends with 329.
  1055. self.queueMsg(ircmsgs.who(channel)) # Ends with 315.
  1056. def do728(self,msg):
  1057. channel = msg.args[1];
  1058. if channel in self.startedList:
  1059. c = self.startedList[channel]
  1060. m = ircmsgs.IrcMsg(':%s MODE %s %s %s' % (msg.args[3],channel,c,msg.args[2]))
  1061. self.feedMsg(m)
  1062.  
  1063. def do729(self,msg):
  1064. channel = msg.args[1];
  1065. if channel in self.startedList:
  1066. del self.startedList[channel]
  1067. self.queueMsg(ircmsgs.mode(channel)) # Ends with 329.
  1068. self.queueMsg(ircmsgs.who(channel)) # Ends with 315.
  1069.  
  1070. def do348(self, msg):
  1071. m = ircmsgs.IrcMsg(':%s MODE %s %s %s' % (msg.args[3],msg.args[1],'+e',msg.args[2]))
  1072. self.feedMsg(m)
  1073.  
  1074. def do346(self, msg):
  1075. m = ircmsgs.IrcMsg(':%s MODE %s %s %s' % (msg.args[3],msg.args[1],'+I',msg.args[2]))
  1076. self.feedMsg(m)
  1077.  
  1078. def do315(self, msg):
  1079. channel = msg.args[1]
  1080. if channel in self.startedSync:
  1081. now = time.time()
  1082. started = self.startedSync.pop(channel)
  1083. elapsed = now - started
  1084. log.info('Join to %s on %s synced in %.2f seconds.',
  1085. channel, self.network, elapsed)
  1086.  
  1087. def doError(self, msg):
  1088. """Handles ERROR messages."""
  1089. log.warning('Error message from %s: %s', self.network, msg.args[0])
  1090. if not self.zombie:
  1091. if msg.args[0].startswith('Closing Link'):
  1092. self.driver.reconnect()
  1093. elif 'too fast' in msg.args[0]: # Connecting too fast.
  1094. self.driver.reconnect(wait=True)
  1095.  
  1096. def doNick(self, msg):
  1097. """Handles NICK messages."""
  1098. if msg.nick == self.nick:
  1099. newNick = msg.args[0]
  1100. self.nick = newNick
  1101. (nick, user, domain) = ircutils.splitHostmask(msg.prefix)
  1102. self.prefix = ircutils.joinHostmask(self.nick, user, domain)
  1103. elif conf.supybot.followIdentificationThroughNickChanges():
  1104. # We use elif here because this means it's someone else's nick
  1105. # change, not our own.
  1106. try:
  1107. id = ircdb.users.getUserId(msg.prefix)
  1108. u = ircdb.users.getUser(id)
  1109. except KeyError:
  1110. return
  1111. if u.auth:
  1112. (_, user, host) = ircutils.splitHostmask(msg.prefix)
  1113. newhostmask = ircutils.joinHostmask(msg.args[0], user, host)
  1114. for (i, (when, authmask)) in enumerate(u.auth[:]):
  1115. if ircutils.strEqual(msg.prefix, authmask):
  1116. log.info('Following identification for %s: %s -> %s',
  1117. u.name, authmask, newhostmask)
  1118. u.auth[i] = (u.auth[i][0], newhostmask)
  1119. ircdb.users.setUser(u)
  1120.  
  1121. def _reallyDie(self):
  1122. """Makes the Irc object die. Dead."""
  1123. log.info('Irc object for %s dying.', self.network)
  1124. # XXX This hasattr should be removed, I'm just putting it here because
  1125. # we're so close to a release. After 0.80.0 we should remove this
  1126. # and fix whatever AttributeErrors arise in the drivers themselves.
  1127. if self.driver is not None and hasattr(self.driver, 'die'):
  1128. self.driver.die()
  1129. if self in world.ircs:
  1130. world.ircs.remove(self)
  1131. # Only kill the callbacks if we're the last Irc.
  1132. if not world.ircs:
  1133. for cb in self.callbacks:
  1134. cb.die()
  1135. # If we shared our list of callbacks, this ensures that
  1136. # cb.die() is only called once for each callback. It's
  1137. # not really necessary since we already check to make sure
  1138. # we're the only Irc object, but a little robustitude never
  1139. # hurt anybody.
  1140. log.debug('Last Irc, clearing callbacks.')
  1141. self.callbacks[:] = []
  1142. else:
  1143. log.warning('Irc object killed twice: %s', utils.stackTrace())
  1144.  
  1145. def __hash__(self):
  1146. return id(self)
  1147.  
  1148. def __eq__(self, other):
  1149. # We check isinstance here, so that if some proxy object (like those
  1150. # defined in callbacks.py) has overridden __eq__, it takes precedence.
  1151. if isinstance(other, self.__class__):
  1152. return id(self) == id(other)
  1153. else:
  1154. return other == self
  1155.  
  1156. def __ne__(self, other):
  1157. return not (self == other)
  1158.  
  1159. def __str__(self):
  1160. return 'Irc object for %s' % self.network
  1161.  
  1162. def __repr__(self):
  1163. return '<irclib.Irc object for %s>' % self.network
  1164.  
  1165.  
  1166. # vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement