meetbot.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #!/usr/bin/env python3
  2. import asyncio
  3. import logging
  4. import pickle
  5. from time import time
  6. from meetbot_cfg import config
  7. # Initialized channels from the configuration
  8. CHANS = {}
  9. # Pickle DB
  10. PICKLE_DB = "meetbot.pickle"
  11. async def main(debug=False):
  12. global CHANS
  13. loglevel = logging.DEBUG if debug else logging.INFO
  14. logfmt = "%(asctime)s [%(levelname)s]\t%(message)s"
  15. logging.basicConfig(format=logfmt,
  16. level=loglevel,
  17. datefmt="%Y-%m-%d %H:%M:%S")
  18. try:
  19. with open(PICKLE_DB, "rb") as pickle_fd:
  20. CHANS = pickle.load(pickle_fd)
  21. logging.info("Loaded pickle database")
  22. except:
  23. logging.info("Did not find existing pickle database")
  24. host = config["host"]
  25. port = config["port"]
  26. nick = config["nick"]
  27. try:
  28. logging.info("Connecting to %s/%d", host, port)
  29. reader, writer = await asyncio.open_connection(host, port)
  30. logging.debug("--> %s/%d: CAP LS 302", host, port)
  31. msg = "CAP LS 302\r\n"
  32. writer.write(msg.encode("utf-8"))
  33. await writer.drain()
  34. logging.debug("--> %s/%d: NICK %s", host, port, nick)
  35. msg = f"NICK {nick}\r\n"
  36. writer.write(msg.encode("utf-8"))
  37. await writer.drain()
  38. logging.debug("--> %s/%d: USER %s * 0 %s", host, port, nick, nick)
  39. msg = f"USER {nick} * 0 :{nick}\r\n"
  40. writer.write(msg.encode("utf-8"))
  41. await writer.drain()
  42. msg = await reader.readline()
  43. msg = msg.decode("utf-8")
  44. logging.debug("<-- %s/%d: %s", host, port, msg)
  45. logging.debug("--> %s/%d: CAP REQ :no-history", host, port)
  46. msg = "CAP REQ :no-history\r\n"
  47. writer.write(msg.encode("utf-8"))
  48. await writer.drain()
  49. msg = await reader.readline()
  50. msg = msg.decode("utf-8")
  51. logging.debug("<-- %s/%d: %s", host, port, msg)
  52. logging.debug("--> %s/%d: CAP END", host, port)
  53. msg = "CAP END\r\n"
  54. writer.write(msg.encode("utf-8"))
  55. await writer.drain()
  56. msg = await reader.readline()
  57. msg = msg.decode("utf-8")
  58. logging.debug("<-- %s/%d: %s", host, port, msg)
  59. for chan in config["channels"]:
  60. chan = chan["name"]
  61. logging.debug("--> %s/%d: JOIN %s", host, port, chan)
  62. msg = f"JOIN {chan}\r\n"
  63. writer.write(msg.encode("utf-8"))
  64. await writer.drain()
  65. channels = [chan["name"] for chan in config["channels"]]
  66. logging.info("%s/%d: Listening to channels: %s", host, port, channels)
  67. elapsed = 0
  68. while True:
  69. msg = await reader.readline()
  70. msg = msg.decode("utf-8")
  71. if not msg:
  72. continue
  73. split_msg = msg.split(" ")
  74. command = split_msg[1]
  75. chan = split_msg[2]
  76. nick_c = split_msg[0][1:].rsplit("!", 1)[0]
  77. logging.debug("<-- %s/%d: %s", host, port, msg.rstrip())
  78. if command == "PRIVMSG":
  79. msg_title = msg.split(" ")[3][1:].rstrip()
  80. if not msg_title:
  81. continue
  82. if msg_title == "!start":
  83. logging.info("%s: Got !start", chan)
  84. if not CHANS.get(chan):
  85. CHANS[chan] = {}
  86. CHANS[chan]["topics"] = {}
  87. topics = CHANS[chan]["topics"]
  88. reply = f"PRIVMSG {chan} :Meeting started\r\n"
  89. writer.write(reply.encode("utf-8"))
  90. await writer.drain()
  91. if len(topics) == 0:
  92. reply = f"PRIVMSG {chan} :No topics\r\n"
  93. writer.write(reply.encode("utf-8"))
  94. await writer.drain()
  95. continue
  96. reply = f"PRIVMSG {chan} :Topics:\r\n"
  97. writer.write(reply.encode("utf-8"))
  98. await writer.drain()
  99. for i, topic in enumerate(topics):
  100. reply = f"PRIVMSG {chan} :{i+1}. {topic}\e\n"
  101. writer.write(reply.encode("utf-8"))
  102. await writer.drain()
  103. cur_topic = topics.pop(0)
  104. reply = f"PRIVMSG {chan} :Current topic: {cur_topic}\r\n"
  105. CHANS[chan]["topics"] = topics
  106. writer.write(reply.encode("utf-8"))
  107. await writer.drain()
  108. elapsed = time()
  109. continue
  110. if msg_title == "!end":
  111. logging.info("%s: Got !end", chan)
  112. reply = f"PRIVMSG {chan} :Elapsed time: {round((time() - elapsed)/60, 1)} min\r\n"
  113. elapsed = 0
  114. writer.write(reply.encode("utf-8"))
  115. await writer.drain()
  116. reply = f"PRIVMSG {chan} :Meeting ended\r\n"
  117. writer.write(reply.encode("utf-8"))
  118. await writer.drain()
  119. continue
  120. if msg_title == "!topic":
  121. logging.info("%s: Got !topic", chan)
  122. topic = msg.split(" ", 4)
  123. if len(topic) != 5:
  124. continue
  125. topic = topic[4].rstrip() + f" (by {nick_c})"
  126. if topic == "":
  127. continue
  128. if not CHANS.get(chan):
  129. CHANS[chan] = {}
  130. CHANS[chan]["topics"] = {}
  131. topics = CHANS[chan]["topics"]
  132. if topic not in topics:
  133. topics.append(topic)
  134. CHANS[chan]["topics"] = topics
  135. reply = f"PRIVMSG {chan} :Added topic: {topic}\r\n"
  136. else:
  137. reply = f"PRIVMSG {chan} :Topic already in list\r\n"
  138. writer.write(reply.encode("utf-8"))
  139. await writer.drain()
  140. continue
  141. if msg_title == "!deltopic":
  142. logging.info("%s: Got !deltopic", chan)
  143. if not CHANS.get(chan):
  144. CHANS[chan] = {}
  145. CHANS[chan]["topics"] = {}
  146. topics = CHANS[chan]["topics"]
  147. if len(topics) == 0:
  148. reply = f"PRIVMSG {chan} :No topics\r\n"
  149. writer.write(reply.encode("utf-8"))
  150. await writer.drain()
  151. continue
  152. try:
  153. topic = msg.split(" ", 4)
  154. topic = int(topic[4].rstrip())
  155. del topics[topic-1]
  156. CHANS[chan]["topics"] = topics
  157. reply = f"PRIVMSG {chan} :Removed topic {topic}\r\n"
  158. writer.write(reply.encode("utf-8"))
  159. await writer.drain()
  160. except:
  161. reply = f"PRIVMSG {chan} :Topic not found\r\n"
  162. writer.write(reply.encode("utf-8"))
  163. await writer.drain()
  164. continue
  165. if msg_title == "!list":
  166. logging.info("%s: Got !list", chan)
  167. if not CHANS.get(chan):
  168. CHANS[chan] = {}
  169. CHANS[chan]["topics"] = []
  170. topics = CHANS[chan]["topics"]
  171. if len(topics) == 0:
  172. reply = f"PRIVMSG {chan} :No topics\r\n"
  173. else:
  174. reply = f"PRIVMSG {chan} :Topics:\r\n"
  175. writer.write(reply.encode("utf-8"))
  176. await writer.drain()
  177. for i, topic in enumerate(topics):
  178. reply = f"PRIVMSG {chan} :{i+1}. {topic}\r\n"
  179. writer.write(reply.encode("utf-8"))
  180. await writer.drain()
  181. continue
  182. if msg_title == "!next":
  183. logging.info("%s: Got !next", chan)
  184. if not CHANS.get(chan):
  185. CHANS[chan] = {}
  186. CHANS[chan]["topics"] = []
  187. topics = CHANS[chan]["topics"]
  188. reply = f"PRIVMSG {chan} :Elapsed time: {round((time() - elapsed)/60, 1)}, min\r\n"
  189. writer.write(reply.encode("utf-8"))
  190. await writer.drain()
  191. elapsed = time()
  192. if len(topics) == 0:
  193. reply = f"PRIVMSG {chan} :No further topics\r\n"
  194. else:
  195. cur_topic = topics.pop(0)
  196. CHANS[chan]["topics"] = topics
  197. reply = f"PRIVMSG {chan} :Current topic: {cur_topic}\r\n"
  198. writer.write(reply.encode("utf-8"))
  199. await writer.drain()
  200. continue
  201. except KeyboardInterrupt:
  202. return
  203. except ConnectionRefusedError:
  204. logging.error("%s/%d: Connection refused", host, port)
  205. return
  206. if __name__ == "__main__":
  207. from sys import argv
  208. debug = "-v" in argv
  209. try:
  210. asyncio.run(main(debug=debug))
  211. except KeyboardInterrupt:
  212. print("\rCaught ^C, saving pickle and exiting")
  213. with open(PICKLE_DB, "wb") as fdesc:
  214. pickle.dump(CHANS, fdesc, protocol=pickle.HIGHEST_PROTOCOL)