ircsupport.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. IRC support for Instance Messenger.
  5. """
  6. from zope.interface import implementer
  7. from twisted.internet import defer, protocol, reactor
  8. from twisted.internet.defer import succeed
  9. from twisted.words.im import basesupport, interfaces, locals
  10. from twisted.words.im.locals import ONLINE
  11. from twisted.words.protocols import irc
  12. class IRCPerson(basesupport.AbstractPerson):
  13. def imperson_whois(self):
  14. if self.account.client is None:
  15. raise locals.OfflineError
  16. self.account.client.sendLine("WHOIS %s" % self.name)
  17. ### interface impl
  18. def isOnline(self):
  19. return ONLINE
  20. def getStatus(self):
  21. return ONLINE
  22. def setStatus(self, status):
  23. self.status = status
  24. self.chat.getContactsList().setContactStatus(self)
  25. def sendMessage(self, text, meta=None):
  26. if self.account.client is None:
  27. raise locals.OfflineError
  28. for line in text.split("\n"):
  29. if meta and meta.get("style", None) == "emote":
  30. self.account.client.ctcpMakeQuery(self.name, [("ACTION", line)])
  31. else:
  32. self.account.client.msg(self.name, line)
  33. return succeed(text)
  34. @implementer(interfaces.IGroup)
  35. class IRCGroup(basesupport.AbstractGroup):
  36. def imgroup_testAction(self):
  37. pass
  38. def imtarget_kick(self, target):
  39. if self.account.client is None:
  40. raise locals.OfflineError
  41. reason = "for great justice!"
  42. self.account.client.sendLine(f"KICK #{self.name} {target.name} :{reason}")
  43. ### Interface Implementation
  44. def setTopic(self, topic):
  45. if self.account.client is None:
  46. raise locals.OfflineError
  47. self.account.client.topic(self.name, topic)
  48. def sendGroupMessage(self, text, meta={}):
  49. if self.account.client is None:
  50. raise locals.OfflineError
  51. if meta and meta.get("style", None) == "emote":
  52. self.account.client.ctcpMakeQuery(self.name, [("ACTION", text)])
  53. return succeed(text)
  54. # standard shmandard, clients don't support plain escaped newlines!
  55. for line in text.split("\n"):
  56. self.account.client.say(self.name, line)
  57. return succeed(text)
  58. def leave(self):
  59. if self.account.client is None:
  60. raise locals.OfflineError
  61. self.account.client.leave(self.name)
  62. self.account.client.getGroupConversation(self.name, 1)
  63. class IRCProto(basesupport.AbstractClientMixin, irc.IRCClient):
  64. def __init__(self, account, chatui, logonDeferred=None):
  65. basesupport.AbstractClientMixin.__init__(self, account, chatui, logonDeferred)
  66. self._namreplies = {}
  67. self._ingroups = {}
  68. self._groups = {}
  69. self._topics = {}
  70. def getGroupConversation(self, name, hide=0):
  71. name = name.lower()
  72. return self.chat.getGroupConversation(
  73. self.chat.getGroup(name, self), stayHidden=hide
  74. )
  75. def getPerson(self, name):
  76. return self.chat.getPerson(name, self)
  77. def connectionMade(self):
  78. # XXX: Why do I duplicate code in IRCClient.register?
  79. try:
  80. self.performLogin = True
  81. self.nickname = self.account.username
  82. self.password = self.account.password
  83. self.realname = "Twisted-IM user"
  84. irc.IRCClient.connectionMade(self)
  85. for channel in self.account.channels:
  86. self.joinGroup(channel)
  87. self.account._isOnline = 1
  88. if self._logonDeferred is not None:
  89. self._logonDeferred.callback(self)
  90. self.chat.getContactsList()
  91. except BaseException:
  92. import traceback
  93. traceback.print_exc()
  94. def setNick(self, nick):
  95. self.name = nick
  96. self.accountName = "%s (IRC)" % nick
  97. irc.IRCClient.setNick(self, nick)
  98. def kickedFrom(self, channel, kicker, message):
  99. """
  100. Called when I am kicked from a channel.
  101. """
  102. return self.chat.getGroupConversation(self.chat.getGroup(channel[1:], self), 1)
  103. def userKicked(self, kickee, channel, kicker, message):
  104. pass
  105. def noticed(self, username, channel, message):
  106. self.privmsg(username, channel, message, {"dontAutoRespond": 1})
  107. def privmsg(self, username, channel, message, metadata=None):
  108. if metadata is None:
  109. metadata = {}
  110. username = username.split("!", 1)[0]
  111. if username == self.name:
  112. return
  113. if channel[0] == "#":
  114. group = channel[1:]
  115. self.getGroupConversation(group).showGroupMessage(
  116. username, message, metadata
  117. )
  118. return
  119. self.chat.getConversation(self.getPerson(username)).showMessage(
  120. message, metadata
  121. )
  122. def action(self, username, channel, emote):
  123. username = username.split("!", 1)[0]
  124. if username == self.name:
  125. return
  126. meta = {"style": "emote"}
  127. if channel[0] == "#":
  128. group = channel[1:]
  129. self.getGroupConversation(group).showGroupMessage(username, emote, meta)
  130. return
  131. self.chat.getConversation(self.getPerson(username)).showMessage(emote, meta)
  132. def irc_RPL_NAMREPLY(self, prefix, params):
  133. """
  134. RPL_NAMREPLY
  135. >> NAMES #bnl
  136. << :Arlington.VA.US.Undernet.Org 353 z3p = #bnl :pSwede Dan-- SkOyg AG
  137. """
  138. group = params[2][1:].lower()
  139. users = params[3].split()
  140. for ui in range(len(users)):
  141. while users[ui][0] in ["@", "+"]: # channel modes
  142. users[ui] = users[ui][1:]
  143. if group not in self._namreplies:
  144. self._namreplies[group] = []
  145. self._namreplies[group].extend(users)
  146. for nickname in users:
  147. try:
  148. self._ingroups[nickname].append(group)
  149. except BaseException:
  150. self._ingroups[nickname] = [group]
  151. def irc_RPL_ENDOFNAMES(self, prefix, params):
  152. group = params[1][1:]
  153. self.getGroupConversation(group).setGroupMembers(
  154. self._namreplies[group.lower()]
  155. )
  156. del self._namreplies[group.lower()]
  157. def irc_RPL_TOPIC(self, prefix, params):
  158. self._topics[params[1][1:]] = params[2]
  159. def irc_333(self, prefix, params):
  160. group = params[1][1:]
  161. self.getGroupConversation(group).setTopic(self._topics[group], params[2])
  162. del self._topics[group]
  163. def irc_TOPIC(self, prefix, params):
  164. nickname = prefix.split("!")[0]
  165. group = params[0][1:]
  166. topic = params[1]
  167. self.getGroupConversation(group).setTopic(topic, nickname)
  168. def irc_JOIN(self, prefix, params):
  169. nickname = prefix.split("!")[0]
  170. group = params[0][1:].lower()
  171. if nickname != self.nickname:
  172. try:
  173. self._ingroups[nickname].append(group)
  174. except BaseException:
  175. self._ingroups[nickname] = [group]
  176. self.getGroupConversation(group).memberJoined(nickname)
  177. def irc_PART(self, prefix, params):
  178. nickname = prefix.split("!")[0]
  179. group = params[0][1:].lower()
  180. if nickname != self.nickname:
  181. if group in self._ingroups[nickname]:
  182. self._ingroups[nickname].remove(group)
  183. self.getGroupConversation(group).memberLeft(nickname)
  184. def irc_QUIT(self, prefix, params):
  185. nickname = prefix.split("!")[0]
  186. if nickname in self._ingroups:
  187. for group in self._ingroups[nickname]:
  188. self.getGroupConversation(group).memberLeft(nickname)
  189. self._ingroups[nickname] = []
  190. def irc_NICK(self, prefix, params):
  191. fromNick = prefix.split("!")[0]
  192. toNick = params[0]
  193. if fromNick not in self._ingroups:
  194. return
  195. for group in self._ingroups[fromNick]:
  196. self.getGroupConversation(group).memberChangedNick(fromNick, toNick)
  197. self._ingroups[toNick] = self._ingroups[fromNick]
  198. del self._ingroups[fromNick]
  199. def irc_unknown(self, prefix, command, params):
  200. pass
  201. # GTKIM calls
  202. def joinGroup(self, name):
  203. self.join(name)
  204. self.getGroupConversation(name)
  205. @implementer(interfaces.IAccount)
  206. class IRCAccount(basesupport.AbstractAccount):
  207. gatewayType = "IRC"
  208. _groupFactory = IRCGroup
  209. _personFactory = IRCPerson
  210. def __init__(
  211. self, accountName, autoLogin, username, password, host, port, channels=""
  212. ):
  213. basesupport.AbstractAccount.__init__(
  214. self, accountName, autoLogin, username, password, host, port
  215. )
  216. self.channels = [channel.strip() for channel in channels.split(",")]
  217. if self.channels == [""]:
  218. self.channels = []
  219. def _startLogOn(self, chatui):
  220. logonDeferred = defer.Deferred()
  221. cc = protocol.ClientCreator(reactor, IRCProto, self, chatui, logonDeferred)
  222. d = cc.connectTCP(self.host, self.port)
  223. d.addErrback(logonDeferred.errback)
  224. return logonDeferred
  225. def logOff(self):
  226. # IAccount.logOff
  227. pass