forked from mutexlox/NO-FIFTH-GLYPH
/
mainLoop.py
109 lines (79 loc) · 3.67 KB
/
mainLoop.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
import time
import sys
import config
import ircConnection
import messageParser
def repl(chans):
con = ircConnection.IRCConnection(config.server, config.port)
con.setNick(config.nick)
con.setUser(config.userName,
config.hostName,
config.server,
config.realName)
setBot = False # have we identified as a bot yet?
devoiced = {} # keep track of who's devoiced and when they can be revoiced
while True: # main REPL
fromServer = con.receive()
# +v users whose punishments have expired
cpy = devoiced.copy()
for user in cpy: # cpy[u] is a tuple with (chan, time)
if cpy[user][1] <= time.time():
con.sendMessage("MODE " + cpy[user][0] + " +v " + user)
del devoiced[user]
if fromServer is not None:
pingResponse = messageParser.pingHandler(fromServer)
if pingResponse != "":
con.sendMessage("PONG " + pingResponse)
continue
print fromServer
mNick = messageParser.getNick(fromServer)
mChan = messageParser.getChannel(fromServer)
message = messageParser.getMessage(fromServer)
isPM = messageParser.isPM(fromServer)
if "End of /MOTD command." in fromServer and not setBot:
setBot = True
con.setBot(config.nick)
if config.password != "":
con.authenticate(config.password)
for chan in chans:
con.join(chan)
if (('e' in message or 'E' in message) and
messageParser.getMessageType(fromServer) == "PRIVMSG" and
mNick != config.nick): # don't devoice ourselves
badWord = ""
for word in message.split():
if 'e' in word or 'E' in word:
badWord = word
break
con.reply("'%s' is a horrid word!" % badWord,
mNick, mChan, isPM)
if not isPM:
con.sendMessage("MODE " + mChan + " -v " + mNick)
devoiced[mNick] = (mChan, time.time() + 15)
# make sure that new people get to talk,
# and that rejoining doesn't get around a -v
if (messageParser.getMessageType(fromServer) == "JOIN" and
mNick not in devoiced):
con.sendMessage("MODE " + messageParser.chanFromJoin(fromServer)
+ " +v " + mNick)
if mNick in config.admins: #admin-only commands
#to avoid accidental commands, ensure command has our prefix
if messageParser.hasPrefix(fromServer):
if messageParser.isQuit(fromServer):
quitMessage = messageParser.partOrQuitMessage(fromServer)
con.quit(quitMessage)
break
if messageParser.isPart(fromServer):
partMessage = messageParser.partOrQuitMessage(fromServer)
if messageParser.partDefault(fromServer):
toPart = mChan # part current chan if none specified
else:
toPart = messageParser.getPartChannel(fromServer)
con.part(partMessage, toPart)
#auto-join on any invite (by an admin)
if messageParser.getMessageType(fromServer) == "INVITE":
con.join(message)
time.sleep(0.5)
con.close()
if __name__ == '__main__':
repl(sys.argv[1:])