meeting_bot_secret.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #TODO:
  2. # Expand home path
  3. import logging
  4. import threading
  5. import time
  6. import json
  7. import asyncio
  8. def load_config():
  9. with open('/home/{user}/.config/darkfi/meeting_bot_config.json', 'r') as config:
  10. data = json.load(config)
  11. logging.info(f"Config loaded: {data}")
  12. return data
  13. def thread_run(host, port, nickname, channel):
  14. asyncio.run(channel_listen(host, port, nickname, channel))
  15. async def channel_listen(host, port, nickname, channel):
  16. logging.info(f"Starting listening to channel: {channel['name']}")
  17. logging.info(f"Start a connection {host}:{port}")
  18. reader, writer = await asyncio.open_connection(host, port)
  19. logging.info("Send CAP msg")
  20. cap_msg = f"CAP REQ : no-history \r\n"
  21. writer.write(cap_msg.encode('utf8'))
  22. logging.info("Send NICK msg")
  23. nick_msg = f"NICK {nickname} \r\n"
  24. writer.write(nick_msg.encode('utf8'))
  25. logging.info("Send CAP END msg")
  26. cap_end_msg = f"CAP END \r\n"
  27. writer.write(cap_end_msg.encode('utf8'))
  28. logging.info(f"Send JOIN msg: {channel['name']}")
  29. join_msg = f"JOIN {channel['name']} \r\n"
  30. writer.write(join_msg.encode('utf8'))
  31. topics = []
  32. logging.info("Start...")
  33. while True:
  34. msg = await reader.read(1024)
  35. msg = msg.decode('utf8').strip()
  36. if not msg:
  37. continue
  38. command = msg.split(" ")[1]
  39. if command == "PRIVMSG":
  40. msg_title = msg.split(" ")[3][1:]
  41. if not msg_title:
  42. continue
  43. reply = None
  44. if msg_title == "!start":
  45. reply = f"PRIVMSG {channel['name']} :meeting started \r\n"
  46. msg_title = "!list"
  47. if msg_title == "!end":
  48. reply = f"PRIVMSG {channel['name']} :meeting end \r\n"
  49. topics = []
  50. if msg_title == "!topic":
  51. topic = msg.split(" ", 4)
  52. if len(topic) != 5:
  53. continue
  54. topic = topic[4]
  55. topics.append(topic)
  56. reply = f"PRIVMSG {channel['name']} :add topic: {topic} \r\n"
  57. if msg_title == "!list":
  58. rep = f"PRIVMSG {channel['name']} :topics: \r\n"
  59. writer.write(rep.encode('utf8'))
  60. for i, topic in enumerate(topics, 1):
  61. rep = f"PRIVMSG {channel['name']} :{i}-{topic} \r\n"
  62. writer.write(rep.encode('utf8'))
  63. await writer.drain()
  64. if msg_title == "!next":
  65. if len(topics) == 0:
  66. reply = f"PRIVMSG {channel['name']} :no topics \r\n"
  67. else:
  68. tp = topics.pop(0)
  69. reply = f"PRIVMSG {channel['name']} :current topic: {tp} \r\n"
  70. if reply != None:
  71. writer.write(reply.encode('utf8'))
  72. await writer.drain()
  73. if command == "QUIT":
  74. break
  75. writer.close()
  76. if __name__ == "__main__":
  77. format = "%(asctime)s: %(message)s"
  78. logging.basicConfig(format=format, level=logging.INFO,
  79. datefmt="%H:%M:%S")
  80. data = load_config()
  81. threads = list()
  82. for index in range(len(data['channels'])):
  83. logging.info(f"Main : create and start thread {index}.")
  84. x = threading.Thread(target=thread_run, args=(data['host'], data['port'], data['nickname'], data['channels'][index],))
  85. threads.append(x)
  86. x.start()
  87. for index, thread in enumerate(threads):
  88. logging.info(f"Main : before joining thread {index}.")
  89. thread.join()
  90. logging.info(f"Main : thread {index} done")