From 48a20d7e8ef1d8b60af26b832c7065c516b19287 Mon Sep 17 00:00:00 2001 From: Bruno Bord Date: Thu, 21 Nov 2013 22:26:14 +0100 Subject: [PATCH 1/5] irc.json file as a configuration file. fallbacks provided --- .gitignore | 3 +++ irc-sample.json | 5 +++++ sprbt.py | 30 ++++++++++++++---------------- 3 files changed, 22 insertions(+), 16 deletions(-) create mode 100644 .gitignore create mode 100644 irc-sample.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6ad08c6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.pyc +irc.json + diff --git a/irc-sample.json b/irc-sample.json new file mode 100644 index 0000000..800ab19 --- /dev/null +++ b/irc-sample.json @@ -0,0 +1,5 @@ +[{ + "host": "irc.freenode.org", + "port": 6667, + "channel": "#brunobordtest" +}] diff --git a/sprbt.py b/sprbt.py index bf9a3c6..4c93a1d 100755 --- a/sprbt.py +++ b/sprbt.py @@ -7,16 +7,18 @@ from datetime import timedelta import select import functions +import json + class IRCConnector( threading.Thread): - def __init__ (self, host, port): - self.host = host - self.port = port - self.channel = "#cah" - self.identity = "superbot" - self.realname = "superbot" - self.hostname = "supermatt.net" - self.botname = "humanitybot" + def __init__ (self, config): + self.host = config.get('host') + self.port = config.get('port') + self.channel = config.get('channel', "#cah") + self.identity = config.get("identity", "superbot") + self.realname = config.get("realname", "superbot") + self.hostname = config.get("hostname", "supermatt.net") + self.botname = config.get('botname', "humanitybot") self.allmessages = [] self.lastmessage = datetime.now() self.pulsetime = 500 @@ -71,7 +73,7 @@ def run (self): self.output(pong) self.s.send(pong) - if re.search(":End of /MOTD command.", line): + if re.search(":End of ", line): joinchannel = "JOIN %s\n" %self.channel self.output(joinchannel) self.s.send("PRIVMSG nickserv :identify hum4n1ty\n") @@ -140,12 +142,8 @@ def run (self): #print self.allmessages -irc_connections = [{ - "host": "irc.darkmyst.org", - "port": 6667, - "channels": ["#cah"] - }] +irc_connections = json.load(open('irc.json')) -for irc in irc_connections: - IRCThread = IRCConnector(irc['host'], irc['port']) +for config in irc_connections: + IRCThread = IRCConnector(config) IRCThread.start() From ce15f4df7b6db32b39d41f933be5b12897e1d655 Mon Sep 17 00:00:00 2001 From: Bruno Bord Date: Thu, 21 Nov 2013 22:28:53 +0100 Subject: [PATCH 2/5] help on readme file --- README.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 255c84b..7197c87 100644 --- a/README.md +++ b/README.md @@ -5,4 +5,18 @@ A Cards Against Humanity IRC bot This is currently a very poorly written bot, however most of the functionality is complete. -Future versions will focus on making it easier for users to run their own version, simply by editing a json file containing any options that are required. +Future versions will focus on making it easier for users to run their own +version, simply by editing a json file containing any options that are required. + +Configuration file +------------------ + +Simply copy the content of the ``irc-sample.json`` file provided and save it as ``irc.json``. Adapt it to your needs. You can provide the following keys: + +* host +* port +* channel +* identity +* realname +* hostname +* botname From 6c08f3019d82b8a80647f56dfb16c8bc679cf97a Mon Sep 17 00:00:00 2001 From: Bruno Bord Date: Thu, 21 Nov 2013 22:30:44 +0100 Subject: [PATCH 3/5] added the password config property --- README.md | 1 + sprbt.py | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7197c87..61c5ecb 100644 --- a/README.md +++ b/README.md @@ -20,3 +20,4 @@ Simply copy the content of the ``irc-sample.json`` file provided and save it as * realname * hostname * botname +* password diff --git a/sprbt.py b/sprbt.py index 4c93a1d..3f9e26e 100755 --- a/sprbt.py +++ b/sprbt.py @@ -19,6 +19,7 @@ def __init__ (self, config): self.realname = config.get("realname", "superbot") self.hostname = config.get("hostname", "supermatt.net") self.botname = config.get('botname', "humanitybot") + self.password = config.get('password', 'hum4n1ty') self.allmessages = [] self.lastmessage = datetime.now() self.pulsetime = 500 @@ -76,7 +77,7 @@ def run (self): if re.search(":End of ", line): joinchannel = "JOIN %s\n" %self.channel self.output(joinchannel) - self.s.send("PRIVMSG nickserv :identify hum4n1ty\n") + self.s.send("PRIVMSG nickserv :identify %s\n" % self.password) self.s.send(joinchannel) self.inchannel = True From e7b920f6d0fdca3e2754cb9291fc1a9b00c686cf Mon Sep 17 00:00:00 2001 From: Bruno Bord Date: Thu, 21 Nov 2013 22:31:48 +0100 Subject: [PATCH 4/5] back to the initial search --- sprbt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sprbt.py b/sprbt.py index 3f9e26e..b4ddb8f 100755 --- a/sprbt.py +++ b/sprbt.py @@ -74,7 +74,7 @@ def run (self): self.output(pong) self.s.send(pong) - if re.search(":End of ", line): + if re.search(":End of /MOTD command.", line): joinchannel = "JOIN %s\n" %self.channel self.output(joinchannel) self.s.send("PRIVMSG nickserv :identify %s\n" % self.password) From aecf6e709c0d15c17c6ab56533bb9fe4a4bc7344 Mon Sep 17 00:00:00 2001 From: Bruno Bord Date: Thu, 21 Nov 2013 22:32:40 +0100 Subject: [PATCH 5/5] better main loop handling --- sprbt.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/sprbt.py b/sprbt.py index b4ddb8f..b88dc28 100755 --- a/sprbt.py +++ b/sprbt.py @@ -143,8 +143,9 @@ def run (self): #print self.allmessages -irc_connections = json.load(open('irc.json')) +if __name__ == '__main__': + irc_connections = json.load(open('irc.json')) -for config in irc_connections: - IRCThread = IRCConnector(config) - IRCThread.start() + for config in irc_connections: + IRCThread = IRCConnector(config) + IRCThread.start()