ircbot.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import sys
  2. import threading
  3. import irc.client
  4. import re
  5. from net.packet_out import whisper
  6. import config
  7. replace_map = {
  8. # colors (we could use mIRC colors but we're just stripping)
  9. "##0": "", "##1": "", "##2": "", "##3": "", "##4": "",
  10. "##5": "", "##6": "", "##7": "", "##8": "", "##9": "",
  11. # afk color
  12. "##a": "",
  13. # bold
  14. "##B": "__", "##b": "__",
  15. # emotes (we're using discord names)
  16. r"%%0": ":grinning:",
  17. r"%%1": ":slightly_smiling_face:",
  18. r"%%2": ":wink:",
  19. r"%%3": ":slightly_frowning_face:",
  20. r"%%4": ":open_mouth:",
  21. r"%%5": ":neutral_face:",
  22. r"%%6": ":worried:",
  23. r"%%7": ":sunglasses:",
  24. r"%%8": ":grin:",
  25. r"%%9": ":rage:",
  26. r"%%:": ":yum:",
  27. r"%%;": ":blush:",
  28. r"%%<": ":sob:",
  29. r"%%=": ":smiling_imp:",
  30. r"%%>": ":no_mouth:",
  31. r"%%?": ":ninja:",
  32. r"%%@": ":nerd:",
  33. r"%%A": ":star:",
  34. r"%%B": ":question:",
  35. r"%%C": ":exclamation:",
  36. r"%%D": ":bulb:",
  37. r"%%E": ":arrow_right:",
  38. r"%%F": ":heart:",
  39. r"%%G": ":smile:",
  40. r"%%H": ":upside_down:",
  41. r"%%I": ":wink:",
  42. r"%%J": ":fearful:",
  43. r"%%K": ":scream:",
  44. r"%%L": ":imp:",
  45. r"%%M": ":flushed:",
  46. r"%%N": ":smiley:",
  47. r"%%O": ":woozy_face:",
  48. r"%%P": ":rage:",
  49. r"%%Q": ":yum:",
  50. r"%%R": ":smirk:",
  51. r"%%S": ":cry:",
  52. r"%%T": ":smiling_imp:",
  53. r"%%U": ":face_with_raised_eyebrow:",
  54. r"%%V": ":ninja:",
  55. r"%%W": ":angry:",
  56. r"%%X": ":star2:",
  57. r"%%Y": ":grey_question:",
  58. r"%%Z": ":grey_exclamation:",
  59. r"%%[": ":speech_left:",
  60. r"%%\\": ":rolling_eyes:",
  61. r"%%]": ":heart_eyes:",
  62. r"%%^": ":sick:",
  63. r"%%_": ":japanese_ogre:",
  64. r"%%`": ":pouting_cat:",
  65. r"%%a": ":laughing:",
  66. r"%%b": ":relaxed:",
  67. r"%%c": ":dizzy_face:",
  68. r"%%d": ":facepalm:",
  69. r"%%e": ":face_with_symbols_over_mouth:",
  70. r"%%f": ":tired_face:",
  71. r"%%g": ":innocent:",
  72. r"%%h": ":angry:",
  73. r"%%i": ":cold_sweat:",
  74. r"%%j": ":speech_baloon:",
  75. r"%%k": ":swearing:",
  76. r"%%l": ":smiley_cat:",
  77. r"%%m": ":sleeping:",
  78. r"%%n": ":unamused:",
  79. r"%%o": ":alien:",
  80. r"%%p": ":smiling_imp:",
  81. r"%%q": ":jack_o_lantern:",
  82. r"%%r": ":no_mouth:",
  83. r"%%s": ":hearts:",
  84. r"%%t": ":money_mouth:",
  85. }
  86. class IRCBot:
  87. def __init__(self):
  88. self._client_thread = threading.Thread(target=self.__client_threadfunc, args=())
  89. self.conn = None
  90. self._active = False
  91. self._ready = False
  92. self._reactor = None
  93. self.broadcastFunc = None
  94. def __client_threadfunc(self):
  95. print '__client_threadfunc started'
  96. self._reactor = irc.client.Reactor()
  97. try:
  98. self.conn = self._reactor.server().connect(config.irc_server, config.irc_port, config.irc_nick, password=config.irc_password)
  99. except irc.client.ServerConnectionError:
  100. print(sys.exc_info()[1])
  101. raise SystemExit(1)
  102. self.conn.add_global_handler("welcome", self.__on_connect)
  103. self.conn.add_global_handler("join", self.__on_join)
  104. self.conn.add_global_handler("pubmsg", self.__on_pubmsg)
  105. self.conn.add_global_handler("disconnect", self.__on_disconnect)
  106. while self._active:
  107. self._reactor.process_once(timeout=1)
  108. def __on_connect(self, conn, event):
  109. print "Connected to IRC on %s:%i" % (config.irc_server, config.irc_port)
  110. if irc.client.is_channel(config.irc_channel):
  111. self.conn.join(config.irc_channel)
  112. def __on_join(self, conn, event):
  113. print "Joined channel %s" % config.irc_channel
  114. self._ready = True
  115. def __on_pubmsg(self, conn, event):
  116. self.broadcastFunc(event.source.nick, event.arguments[0])
  117. def __on_disconnect(self, conn, event):
  118. self.stop()
  119. def isAFK(self, msg):
  120. lower = msg.lower()
  121. if lower[1:3] == "afk" or lower[0:2] == "afk" or lower[0:2] == "##a":
  122. return True # don't relay AFK messages
  123. return False
  124. # strip manaplus formatting
  125. def manaplusToIRC(self, msg):
  126. for manaplus, literal in replace_map.items():
  127. msg = msg.replace(manaplus, literal)
  128. # now that we're done, return it
  129. return msg
  130. def send(self, nick, msg):
  131. if not self._ready:
  132. return
  133. msg = self.manaplusToIRC(msg)
  134. if not msg:
  135. return # if the message is empty, discard it
  136. if msg[:1] == "!":
  137. self.conn.privmsg(config.irc_channel, "Command sent from TMW by %s:" % nick)
  138. self.conn.privmsg(config.irc_channel, msg)
  139. else:
  140. self.conn.privmsg(config.irc_channel, "<%s> %s" % (nick, msg))
  141. def start(self):
  142. self._active = True
  143. self._client_thread.start()
  144. def stop(self):
  145. self._ready = False
  146. if self._active:
  147. self._active = False
  148. self._client_thread.join()
  149. if __name__=='__main__':
  150. print "You should not run this file. Use main.py"