-
Notifications
You must be signed in to change notification settings - Fork 3
/
bot.py
executable file
·159 lines (127 loc) · 3.82 KB
/
bot.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
#!/usr/bin/python
###
# Simple IRC Log bot
###
import signal
import socket
import ssl
import sys
import time
import getopt
import os
import errno
import LogParsers.BasicParser
server = "irc.freenode.net"
port = 6697
channel = ""
botnick = ""
logdir = "logs"
password = ""
parser = LogParsers.BasicParser.BasicParser()
irc_C = None
irc = None
def logline(line, logdir):
global parser
filename = time.strftime("%Y-%m-%d") + ".md"
try:
newline = parser.parse(line)
with open(logdir + "/" + filename, "a") as logfile:
print "[LOGGING] " + newline
logfile.write(newline + "\n")
logfile.close()
except Exception as err:
print "[EXCEPTION] " + str(err)
def sig_int_handler(signal, frame):
print("SIGINT: Shutting down nicely...")
irc.send("QUIT (Botdeath)\r\n")
sys.exit()
def send(command):
global irc
print "[SENT] " + command
irc.send(command)
def usage():
print("IRC Log bot by Marcus Povey <marcus@marcus-povey.co.uk>");
print;
print("Usage:");
print("\t./bot -s server -p port -c channel -n nick -d logdirectory -a nickservpass");
def runbot(server, port, channel, botnick, logdir, password):
global irc_C, irc
irc_C = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #defines the socket
irc = ssl.wrap_socket(irc_C)
print "Establishing connection to %s on %s:%d" % (channel, server, port)
print "Logging to %s" % (logdir)
# Connect
irc.connect((server, port))
irc.setblocking(False)
send("USER " + botnick + " " + botnick + " bla :" + botnick + "\r\n")
send("NICK " + botnick + "\r\n")
if len(password) > 0:
send("PRIVMSG NickServ :IDENTIFY "+ botnick + " " + password + "\r\n") #auth
send("JOIN " + channel + "\r\n")
while True:
time.sleep(1)
try:
for text in irc.makefile('r'):
print "[RECEIVED] " + text.strip()
# Prevent Timeout
if text.find('PING') != -1:
send('PONG ' + text.split() [1] + '\r\n')
# Someone on IRC said something on the channel, so log it, and mayby parse it for meaning
if text.find('PRIVMSG '+ channel) != -1:
logline(text, logdir)
except Exception:
continue
def main():
global server
global port
global channel
global botnick
global logdir
global password
signal.signal(signal.SIGINT, sig_int_handler)
try:
opts, args = getopt.getopt(sys.argv[1:], "s:p:c:n:d:a:h", ["help"])
except getopt.GetoptError, err:
print str(err)
usage()
sys.exit()
for o, a in opts:
if o == "-s":
server = a
elif o == "-p":
port = int(a)
elif o == "-c":
channel = a
elif o == "-n":
botnick = a
elif o == "-d":
logdir = a
elif o == "-a":
password = a
elif o in ("-h", "--help"):
usage()
sys.exit()
else:
usage()
sys.exit()
# Check channel is defined
if len(channel) == 0:
usage()
sys.exit()
# Check nick is defined
if len(botnick) == 0:
usage()
sys.exit()
# Normalise log path and create dirs
logdir = os.path.abspath(logdir) + "/" + channel
try:
os.makedirs(logdir)
except OSError as exc: # Python >2.5
if exc.errno == errno.EEXIST and os.path.isdir(logdir):
pass
else: raise
# Can't put # on command line, so add it
channel = "#" + channel
runbot(server, port, channel, botnick, logdir, password)
if __name__ == "__main__":
main()