-
Notifications
You must be signed in to change notification settings - Fork 5
/
IRCbot.py
215 lines (158 loc) · 8.34 KB
/
IRCbot.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import warnings
warnings.simplefilter("ignore", RuntimeWarning)
import socket
import time
import traceback
import Queue
import datetime
import logging
from IRC_readwrite_threads import IRC_reader, IRC_writer, ThreadShuttingDown
from commandHandler import commandHandling
from configReader import Configuration
class IRC_Main():
def __init__(self, configObj):
config = configObj.config
self.host = config.get("Connection Info", "server")
self.port = config.getint("Connection Info", "port")
self.name = config.get("Connection Info", "nickname")
self.passw = config.get("Connection Info", "password")
self.channels = configObj.getChannels()
self.myident = config.get("Connection Info", "ident")
self.realname = config.get("Connection Info", "realname")
self.forceIPv6 = config.getboolean("Networking", "force ipv6")
self.bindIP = config.get("Networking", "bind address")
self.adminlist = configObj.getAdmins()
self.prefix = config.get("Administration", "command prefix")
self.loglevel = config.get("Administration", "logging level")
self.nsAuth = False
self.shutdown = False
def start(self):
if self.forceIPv6 == True:
if socket.has_ipv6:
try:
info = socket.getaddrinfo(self.host, self.port, socket.AF_INET6, socket.SOCK_STREAM, socket.SOL_TCP)
# We grab the information from the first entry in the info list,
# which is hopefully the one we require.
# NOTE: Potential problem: Does it still work if the URL has no IPv6 address?
family, socktype, proto, canonname, sockaddr = info[0]
except socket.gaierror as error:
print "getaddrinfo failed! Maybe IPv6 isn't supported on this platform? Check your config!"
raise error
self.serverConn = socket.socket(socket.AF_INET6)
self.serverConn.settimeout(300)
if self.bindIP != "":
self.serverConn.bind((self.bindIP, 0))
self.serverConn.connect(sockaddr)
else:
raise RuntimeError("IPv6 isn't supported on this platform. Please check the config file.")
else:
self.serverConn = socket.create_connection((self.host, self.port), 300,
source_address = (self.bindIP, 0))
self.readThread = IRC_reader(self.serverConn)
self.writeThread = IRC_writer(self.serverConn)
self.readThread.start()
self.writeThread.start()
self.writeThread.sendMsg('PASS ' + self.passw)
self.writeThread.sendMsg('NICK ' + self.name)
self.writeThread.sendMsg('USER {0} * * {1}'.format(self.myident, self.realname))
self.comHandle = commandHandling(self.channels, self.prefix, self.name, self.myident, self.adminlist, self.loglevel)
peerinfo = self.serverConn.getpeername()
clientinfo = self.serverConn.getsockname()
self.__root_logger__ = logging.getLogger("IRCMainLoop")
self.__root_logger__.info("Connected to %s (IP address: %s, port: %s)",self.host, peerinfo[0], peerinfo[1])
self.__root_logger__.debug("Local IP: %s, local port used by this connection: %s", clientinfo[0], clientinfo[1])
self.__root_logger__.info("BOT IS NOW ONLINE: Starting to listen for server responses.")
while self.shutdown == False:
try:
msg = self.readThread.readMsg()
msgParts = msg.split(" ", 2)
#print msgParts
if msgParts[0][0] == ":":
prefix = msgParts[0][1:]
else:
prefix = None
if prefix == None:
command = msgParts[0]
try:
commandParameters = msgParts[1]
except IndexError:
commandParameters = ""
else:
command = msgParts[1]
try:
commandParameters = msgParts[2]
except IndexError:
commandParameters = ""
self.comHandle.handle(self.writeThread.sendMsg, prefix, command, commandParameters, self.nsAuth)
except Queue.Empty:
pass
# Bugfix for when only the writeThread, i.e. the one that sends data to server, dies
# we raise an exception so the main loop exits and the readThread is shut down too
if self.writeThread.ready == False:
self.__root_logger__.critical("Write Thread was shut down, raising exception.")
raise ThreadShuttingDown("writeThread", time.time())
self.comHandle.timeEventChecker()
time.sleep(0.0012)
self.__root_logger__.info("Main loop has been stopped")
self.readThread.ready = False
self.writeThread.ready = False
self.__root_logger__.info("Read and Write thread signaled to stop.")
def customNickAuth(self, result):
if isinstance(result, str):
self.nsAuth = result
else:
raise TypeError
def write_starting_date():
startFile = open("lastStart.txt", "w")
startFile.write("Started at: "+str(datetime.datetime.today()))
startFile.close()
write_starting_date()
configObj = Configuration()
configObj.loadConfig()
configObj.check_options()
bot = IRC_Main(configObj)
try:
bot.start()
except Exception as error:
if getattr(bot, "__root_logger__", None) != None:
bot.__root_logger__.exception("The bot has encountered an exception and had to shut down.")
log = True
else:
print "Tried to log an error, but logger wasn't initialized."
log = False
print "OH NO I DIED: "+str(error)
traceb = str(traceback.format_exc())
print traceb
excFile = open("exception.txt", "w")
excFile.write("Oh no! The bot died! \n"+str(traceb)+"\nTime of death: "+str(datetime.datetime.today())+"\n")
excFile.write("-----------------------------------------------------\n")
# Check if the attribute 'comhandle' is contained in 'bot'. If so, proceed with playback of packet log.
# This is for the cases where the program crashes while comHandle is being initialized, resulting
# in comHandle missing afterwards.
if getattr(bot, "comHandle", None) != None:
for i in range(bot.comHandle.PacketsReceivedBeforeDeath.qsize()):
msg = bot.comHandle.PacketsReceivedBeforeDeath.get(block = False)
excFile.write(msg.encode("utf-8", "replace"))
excFile.write("\n")
bot.comHandle.threading.sigquitAll()
if log: bot.__root_logger__.debug("All threads were signaled to shut down.")
excFile.write("-----------------------------------------------------\n")
excFile.write("ReadThread Exception: \n")
if getattr(bot, "readThread", None) != None:
excFile.write(str(bot.readThread.error)+" \n")
bot.readThread.ready = False
else:
excFile.write("ReadThread not initialized\n")
if log: bot.__root_logger__.info(u"Exception encountered by ReadThread (if any): %s\n", str(bot.readThread.error))
excFile.write("-----------------------------------------------------\n")
excFile.write("WriteThread Exception: \n")
if getattr(bot, "writeThread", None) != None:
excFile.write(str(bot.writeThread.error)+" \n")
#bot.writeThread.waitUntilEmpty()
bot.writeThread.signal = True
else:
excFile.write("WriteThread not initialized\n")
if log: bot.__root_logger__.info(u"Exception encountered by WriteThread (if any): %s\n", str(bot.writeThread.error))
excFile.close()
if log: bot.__root_logger__.info("End of Session\n\n\n\n")
logging.shutdown()