forked from khufu9/riemann
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
132 lines (111 loc) · 4.29 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import re
import string
import time
import utils.youtube as youtube
import utils.get_url_title as get_url_title
import irc
from irc.protocol import IRCProtocolParser
from irc.connection import IRCConnection
class Bot:
def __init__(self, config):
self.config = config
self.parser = IRCProtocolParser(self)
host = config["server"]["host"]
port = config["server"]["port"]
self.connection = IRCConnection(self.parser, host, port)
def nick_string(self, nick = None):
if nick is None:
nick = self.config["nick"][self.config["nickIndex"]]
return "NICK "+nick
def next_nick(self):
index = (self.config["nickIndex"] + 1) % len(self.config["nick"])
self.config["nickIndex"] = index
return index == 0 # Wrapped
def user_string(self):
username = self.config["user"]["username"]
hostname = self.config["user"]["hostname"]
servername = self.config["user"]["servername"]
realname = self.config["user"]["realname"]
return " ".join(["USER", username, hostname, servername, realname])
def join_string(self):
channels = ",".join(self.config["channels"])
return "JOIN " + channels
def connect(self):
self.config["nickIndex"] = 0
self.connection.connect()
def event_cb(self, event):
print "event:", event
if event["event"] == "register":
self.connection.send(self.nick_string() + "\r\n")
self.connection.send(self.user_string() + "\r\n")
elif event["event"] == "ping":
msg = event["data"]
print "PING EVENT DETECTED"
self.connection.send(string.replace(msg, "PING", "PONG")+"\r\n")
elif event["event"] == "message":
print event["data"]
if event["code"] == irc.RPL_ENDOFMOTD or event["code"] == irc.ERR_NOMOTD:
self.connection.send(self.join_string() + "\r\n")
elif event["code"] == irc.ERR_NICKNAMEINUSE:
if self.next_nick():
self.config["timeout"] = 300
self.connection.quit()
return
self.connection.send(self.nick_string() + "\r\n")
elif event["event"] == "PRIVMSG":
print event
words = event["content"].split()
for word in words:
if "youtube" in word or "youtu.be" in word:
test = youtube.getTitle(word)
if test is not None:
if event["destination"] in self.config["nick"]:
dst = event["source"]
else:
dst = event["destination"]
self.connection.send("PRIVMSG " + dst +" : > " + test + "\r\n")
# The following should be put last after specific www services
elif "http://" in word or "https://" in word:
test = get_url_title.get_title(word)
if test is not None:
if event["destination"] in self.config["nick"]:
dst = event["source"]
else:
dst = event["destination"]
self.connection.send("PRIVMSG " + dst +" : > " + test + "\r\n")
elif event["event"] == "disconnect":
pass
if __name__ == "__main__":
config = {
"server": {
"host":"irc.quakenet.org",
"port":6667
},
"nick": [
"riemann",
"riemannbot",
"riemannbot_"
],
"nickIndex": 0,
"user": {
"username": "riemannbot",
"hostname": "hyperspace",
"servername": "indrome.com",
"realname": ":Mr. Bot" # Colon allows for space
},
"channels":[
"#blashyrk"
],
"timeout": 0
}
bot = Bot(config)
# Will exit after first disconnect
while 1:
bot.connect()
if bot.config["timeout"] > 0:
print "timeout:", bot.config["timeout"], "seconds"
time.sleep(bot.config["timeout"])
bot.config["timeout"] = 0
else:
print "timeout: 5 seconds"
time.sleep(5)