forked from Progether/IRC-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.py
executable file
·106 lines (86 loc) · 3.87 KB
/
core.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
import socket, re
import chatLog
from settings import read_config
class IRCBot:
def __init__(self, tempCacheSize=4096):
conf = read_config()
self.network = conf['network']
self.port = int(conf['port'])
self.channel = conf['channel']
self.quitCmd = conf['quit']
self.nickname = conf['nick']
self.password = conf['password']
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.tempCacheSize = tempCacheSize
self.cLog = chatLog.ChatLog()
self.addonList = list()
self.regexIsCommand = re.compile(r"(?P<command>!!..+)")
self.regexCommandSplitCommand = re.compile(r"!!(?P<command>\w+)\s(?P<arguments>.*).*")
self.regexIsChat = re.compile(r":(?P<user>\w+)!(?P<isp>.+)\sPRIVMSG\s(?P<channel>[#\w-]+)\s:(?P<message>.+)")
def run(self):
self.socket.connect((self.network, self.port))
self.log(self.socket.recv(self.tempCacheSize))
self.socket.send('NICK %s \r\n' % self.nickname)
self.socket.send('USER %s some stuff :Python IRC\r\n' % self.nickname)#change this
self.socket.send('JOIN %s \r\n' % self.channel)
# need to register bot nick bofre you can use this. If you'd rather skip register comment out next lines.
self.socket.send('PRIVMSG NickServ :IDENTIFY %s %s\r\n' % (self.nickname, self.password))
self.mainLoop()
def mainLoop(self):
while True:
receivedData = self.socket.recv(self.tempCacheSize)
#self.log(recievedData)
messageInfo = dict()
isChat = self.regexIsChat.match(receivedData)
if isChat:
messageInfo['user'] = isChat.group('user')
messageInfo['isp'] = isChat.group('isp')
messageInfo['channel'] = isChat.group('channel')
messageInfo['message'] = isChat.group('message')
isCommand = self.regexIsCommand.match(messageInfo['message'])
if isCommand:
split = self.regexCommandSplitCommand.match(messageInfo['message'])
commandName = split.group('command')
commandArguments = split.group('arguments')
for addon in self.addonList:
if addon.commandList.has_key(commandName):
addon.commandList[commandName](addon, commandArguments, messageInfo)
for addon in self.addonList:
for behaviour in addon.behaviourList:
behaviour(addon, receivedData)
#temporary quit method, should be changed so only admins can use
if receivedData.find(self.quitCmd) != -1:
self.log("Quitting")
self.socket.send('QUIT\r\n')
break
#make sure we don't time out of server
if receivedData.find('PING') != -1:
self.socket.send('PONG %s \r\n' % receivedData.split()[1])
else:
# moved log here to filter out ping/pong chatter
self.log(receivedData)
def log(self, stringToLog):
#change eventually to log in a file but for now print is fine
print stringToLog
self.cLog.addLog(stringToLog)
def registerAddon(self, **options):
def decorator(f):
self.addonList.append(f())
return f
return decorator
class AddonBase:
commandList = dict()
behaviourList = list()
@classmethod
def registerBehaviour(self, **options):
def decorator(f):
self.behaviourList.append(f)
return f
return decorator
@classmethod
def registerCommand(self, commandName, **options):
def decorator(f):
self.commandList[commandName] = f
return f
return decorator
ircBot = IRCBot()