Преглед изворни кода

ircd bot for muliple private channels

serinko пре 3 година
родитељ
комит
07c3d2810b

+ 13 - 6
bin/ircd/script/README.md

@@ -1,11 +1,18 @@
 # IRC Private Channel Bot
 
-The meeting bot used in IRC Channels can be slightly hacked to run in private channels. This is a demo version - a quick fix. Download this folder and follow the setup:
+meeting_bot_secret.py is an upgraded version of the meeting_bot.py for IRC. This version allows for multiple channels, including private ones.
+
+**Note:**
+
+* {user} needs to be exchanged with your *username*.
+* Every channel runs a bot on a different thread - be careful how many bots you deploy!
+
 
 **Setup**
 
-1. Open meeting_bot_config.json
-2. Change "channel" and "secret" values to ones of the room you want to deploy the bot to
-3. Save the file
-4. Navigate terminal to the folder where is demo_priv_meeting_bot.py
-5. `$ python demo_priv_meeting_bot.py`
+* Donwload  *meeting_bot_secret.py* and *meeting_bot_secret_config.py* 
+* Copy meeting_bot_secret_config.py to /home/{user}/.config/darkfi
+* Open the config and set up all the channels and the values of name and secret (if not private, secret must be {null})
+* Change {user} to your *username* in *meeting_bot_secret.py* in the path of *load_config()* function
+* Navigate terminal to the folder where is *meeting_bot_secret.py*
+* Run the bot: `$ python meeting_bot_secret.py`

+ 0 - 102
bin/ircd/script/demo_priv_meeting_bot.py

@@ -1,102 +0,0 @@
-# TODO
-# 1) Enable for multiple chats
-
-import json
-import asyncio
-
-
-async def start():
-
-    with open('meeting_bot_config.json', 'r') as config:
-        data = json.load(config)
-
-    
-        
-        host = data['host']
-        port = data['port']
-        channel = data['channel']
-        nickname = data['nickname']
-        secret = data['secret']
-    
-    print(f"Start a connection {host}:{port}")
-    reader, writer = await asyncio.open_connection(host, port)
-
-    print("Send CAP msg")
-    cap_msg = f"CAP REQ : no-history \r\n"
-    writer.write(cap_msg.encode('utf8'))
-
-    print("Send NICK msg")
-    nick_msg = f"NICK {nickname} \r\n"
-    writer.write(nick_msg.encode('utf8'))
-
-    print("Send CAP END msg")
-    cap_end_msg = f"CAP END \r\n"
-    writer.write(cap_end_msg.encode('utf8'))
-
-    print(f"Send JOIN msg: {channel}")
-    join_msg = f"JOIN {channel} \r\n"
-    writer.write(join_msg.encode('utf8'))
-
-    topics = []
-
-    print("Start...")
-    while True:
-        msg = await reader.read(1024)
-        msg = msg.decode('utf8').strip()
-
-        if not msg:
-            continue
-
-        command = msg.split(" ")[1]
-
-        if command == "PRIVMSG":
-
-            msg_title = msg.split(" ")[3][1:]
-
-            if not msg_title:
-                continue
-
-            reply = None
-
-            if msg_title == "!start":
-                reply = f"PRIVMSG {channel} :meeting started \r\n"
-                msg_title = "!list"
-
-            if msg_title == "!end":
-                reply = f"PRIVMSG {channel} :meeting end \r\n"
-                topics = []
-
-            if msg_title == "!topic":
-                topic = msg.split(" ", 4)
-                if len(topic) != 5:
-                    continue
-                topic = topic[4]
-                topics.append(topic)
-                reply = f"PRIVMSG {channel} :add topic: {topic} \r\n"
-
-            if msg_title == "!list":
-                rep = f"PRIVMSG {channel} :topics: \r\n"
-                writer.write(rep.encode('utf8'))
-
-                for i, topic in enumerate(topics, 1):
-                    rep = f"PRIVMSG {channel} :{i}-{topic} \r\n"
-                    writer.write(rep.encode('utf8'))
-                await writer.drain()
-
-            if msg_title == "!next":
-                if len(topics) == 0:
-                    reply = f"PRIVMSG {channel} :no topics \r\n"
-                else:
-                    tp = topics.pop(0)
-                    reply = f"PRIVMSG {channel} :current topic: {tp} \r\n"
-
-            if reply != None:
-                writer.write(reply.encode('utf8'))
-                await writer.drain()
-
-        if command == "QUIT":
-            break
-
-    writer.close()
-
-asyncio.run(start())

+ 0 - 7
bin/ircd/script/meeting_bot_config.json

@@ -1,7 +0,0 @@
-{
-    "host":"127.0.0.1",
-    "port":6667,
-    "channel":"#enter_channel_name",
-    "nickname":"meeting_bot",
-    "secret":"#enter_secret"
-}

+ 121 - 0
bin/ircd/script/meeting_bot_secret.py

@@ -0,0 +1,121 @@
+#TODO:
+# Expand home path
+
+import logging
+import threading
+import time
+import json
+import asyncio
+
+def load_config():
+    with open('/home/{user}/.config/darkfi/meeting_bot_config.json', 'r') as config:
+        data = json.load(config)    
+    logging.info(f"Config loaded: {data}")
+    return data
+
+def thread_run(host, port, nickname, channel):
+    asyncio.run(channel_listen(host, port, nickname, channel))
+
+async def channel_listen(host, port, nickname, channel):
+    logging.info(f"Starting listening to channel: {channel['name']}")
+
+    logging.info(f"Start a connection {host}:{port}")
+    reader, writer = await asyncio.open_connection(host, port)
+
+    logging.info("Send CAP msg")
+    cap_msg = f"CAP REQ : no-history \r\n"
+    writer.write(cap_msg.encode('utf8'))
+
+    logging.info("Send NICK msg")
+    nick_msg = f"NICK {nickname} \r\n"
+    writer.write(nick_msg.encode('utf8'))
+
+    logging.info("Send CAP END msg")
+    cap_end_msg = f"CAP END \r\n"
+    writer.write(cap_end_msg.encode('utf8'))
+
+    logging.info(f"Send JOIN msg: {channel['name']}")
+    join_msg = f"JOIN {channel['name']} \r\n"
+    writer.write(join_msg.encode('utf8'))
+
+    topics = []
+
+    logging.info("Start...")
+    while True:
+        msg = await reader.read(1024)
+        msg = msg.decode('utf8').strip()
+
+        if not msg:
+            continue
+
+        command = msg.split(" ")[1]
+
+        if command == "PRIVMSG":
+
+            msg_title = msg.split(" ")[3][1:]
+
+            if not msg_title:
+                continue
+
+            reply = None
+
+            if msg_title == "!start":
+                reply = f"PRIVMSG {channel['name']} :meeting started \r\n"
+                msg_title = "!list"
+
+            if msg_title == "!end":
+                reply = f"PRIVMSG {channel['name']} :meeting end \r\n"
+                topics = []
+
+            if msg_title == "!topic":
+                topic = msg.split(" ", 4)
+                if len(topic) != 5:
+                    continue
+                topic = topic[4]
+                topics.append(topic)
+                reply = f"PRIVMSG {channel['name']} :add topic: {topic} \r\n"
+
+            if msg_title == "!list":
+                rep = f"PRIVMSG {channel['name']} :topics: \r\n"
+                writer.write(rep.encode('utf8'))
+
+                for i, topic in enumerate(topics, 1):
+                    rep = f"PRIVMSG {channel['name']} :{i}-{topic} \r\n"
+                    writer.write(rep.encode('utf8'))
+                await writer.drain()
+
+            if msg_title == "!next":
+                if len(topics) == 0:
+                    reply = f"PRIVMSG {channel['name']} :no topics \r\n"
+                else:
+                    tp = topics.pop(0)
+                    reply = f"PRIVMSG {channel['name']} :current topic: {tp} \r\n"
+
+            if reply != None:
+                writer.write(reply.encode('utf8'))
+                await writer.drain()
+
+        if command == "QUIT":
+            break
+
+    writer.close()
+
+if __name__ == "__main__":
+    format = "%(asctime)s: %(message)s"
+    logging.basicConfig(format=format, level=logging.INFO,
+                        datefmt="%H:%M:%S")
+
+    data = load_config()
+
+    threads = list()
+    for index in range(len(data['channels'])):
+        logging.info(f"Main    : create and start thread {index}.")
+        x = threading.Thread(target=thread_run, args=(data['host'], data['port'], data['nickname'], data['channels'][index],))
+        threads.append(x)
+        x.start()
+
+    for index, thread in enumerate(threads):
+        logging.info(f"Main    : before joining thread {index}.")
+        thread.join()
+        logging.info(f"Main    : thread {index} done")
+

+ 20 - 0
bin/ircd/script/meeting_bot_secret_config.json

@@ -0,0 +1,20 @@
+{
+	"host":"127.0.0.1",
+	"port":6667,
+	"nickname":"meeting_bot",
+	"channels":
+	[
+		{
+			"name":"#dev",
+			"secret":null
+		},
+		{
+			"name":"#enter_channel_name",
+			"secret":"#enter_secret"
+		},
+		{
+			"name":"#enter_channel_name_2",
+			"secret":"#enter_secret_2"
+        	}
+	]
+}