-
Notifications
You must be signed in to change notification settings - Fork 0
/
IRCManager.py
63 lines (53 loc) · 1.67 KB
/
IRCManager.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
'''
Created on Aug 30, 2015
@author: derp
'''
import socket
from Queue import Queue
from threading import Thread
def findUsername(data):
splitStr = data.split("!")
if len(splitStr) > 0:
result = splitStr[0].replace(' ', '').replace(':', '').lower()
return result
else:
return None
def findText(data):
firstIndex = data[1:].replace('str:', '').find(':')
lastIndex = data[1:].replace('str:', '').rfind(':')
if firstIndex > 0 and lastIndex > 0:
result = data[lastIndex+1:].replace('str:', '').replace(':', '').replace('\r\n', '')
return result
else:
return ''
def findDirectedUser(text):
directedUser = ''
if text is not None:
findResult = text.find('@')
if findResult >= 0:
for i in range(1, len(text)):
if text[i] != ' ':
directedUser = directedUser + text[i]
else:
break
#if still default, set it to None
if directedUser == '':
directedUser = None
return directedUser
def do_nothing():
return
class IRCManager(object):
irc = None
readingIRCThread = None
ircDataQueue = Queue()
def __init__(self, ircRef):
self.irc = ircRef
self.readingIRCThread = Thread(target=self.ircReader)
self.readingIRCThread.start()
#add all lines from irc into a queue that the main thread can read
#helps prevent loss of irc commands due to event handling that too long
def ircReader(self):
while True:
data = self.irc.recv(1024)
if len(data.replace(' ', '')) > 0:
self.ircDataQueue.put(data)