forked from cybits/cybot
/
irc.py
199 lines (178 loc) · 5.5 KB
/
irc.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
import socket
import sys
import ssl
import time
import random
import itertools
import json
import threading
from config import Config
from commands import get_command
if len(sys.argv) < 2:
print("Usage: irc.py [config.json]")
exit(1)
#load config from json, check if everything is alright
server = Config.server
port = Config.port
channel_list = Config.channels
botnick = Config.bot_nick
password = Config.password
commandprefix = Config.prefix
def sendmsg(recipient, msg):
"""Sends a message."""
with lock:
if msg and isinstance(msg, tuple):
for i in msg:
ircsock.send("PRIVMSG %s :%s\n" % (recipient, i))
elif msg:
ircsock.send(bytes("PRIVMSG %s :%s\n" % (recipient, msg), 'UTF-8'))
def joinchan(chan):
"""Joins a channel."""
with lock:
print("trying")
ircsock.send(bytes("JOIN %s\n" % chan, 'UTF-8'))
def auth(pass_word):
"""Works only on Nickserv servers"""
sendmsg("Nickserv", "identify %s" % pass_word)
def parsemsg(s):
"""Breaks a message from an IRC server into its prefix, command, and
arguments.
"""
# TODO: Refactor the fuck out of this
prefix = ""
trailing = []
retargs = []
raw = s
command = ""
if not s:
pass
if s[0] == ":":
prefix, s = s[1:].split(" ", 1)
if s.find(" :") != -1:
s, trailing = s.split(" :", 1)
args = s.split()
args.append(trailing)
if trailing[0] == commandprefix:
commands = args[2][1:].strip().split() if len(args) >= 3 else ""
if commands:
command = commands[0]
retargs = commands[1:]
else:
pass
else:
args = s.split()
event = args[0]
channel = args[1]
# If there is nothing in command at this point
# We append whatever is in event as a command.
# Easier to handle events like ping
command = event if not command else command
ret = {"prefix": prefix,
"command": command,
"raw": raw,
"event": event,
"args": retargs,
"channel": channel,
# Because circular imports
"sendmsg": sendmsg}
return ret
_partial_data = None
def process_data(data):
"""Process the data received from the socket. Ensures that there is no
partial command at the end of the data chunk (that can happen if the data
does not fit in the socket buffer). If that happens the partial command will
be reconstructed next time this function is called.
data: raw data from the socket.
"""
global _partial_data
try:
data = data.decode(encoding='UTF-8')
except Exception as e:
return
if not data:
return []
lines = data.splitlines()
# There is at least one newline => this data chunk contains the end of at
# least one command. If previous command was stored then it is complete now.
if "\n" in data and _partial_data:
lines[0] = _partial_data + lines[0]
_partial_data = None
# Store partial data.
if not data.endswith("\n"):
if _partial_data is None:
_partial_data = ""
_partial_data += lines.pop()
return lines
def isplit(iterable,splitters):
return [list(g) for k,g in itertools.groupby(iterable,lambda x:x in splitters) if not k]
def pipe_commands(args, channel):
pipelist = args["args"].copy()
pipelist.insert(0,args["command"])
l = isplit(pipelist,"|")
out = None
for i in l:
cmd = i[0].strip(".")
a = i[1:]
if out:
a.append(out)
args["command"] = cmd
args["args"] = a
print(a)
c = get_command(args["command"])
print(type(c), c)
if not cmd == "rate" or not cmd == "r8":
out = "".join(c(args))
sendmsg(channel, out)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
lock = threading.Lock()
time.sleep(.5)
s.connect((server, port))
time.sleep(.5)
ircsock = ssl.wrap_socket(s)
time.sleep(.5)
ircsock.send(bytes("USER %s %s %s :some stuff\n" % (botnick, botnick, botnick), 'UTF-8'))
time.sleep(.5)
ircsock.send(bytes("NICK %s\n" % botnick, 'UTF-8'))
if password:
time.sleep(.5)
auth(password)
time.sleep(.5)
for channel in channel_list:
joinchan(channel)
time.sleep(.5)
while True:
data = ircsock.recv(1024)
try:
valid_data = process_data(data)
except Exception as e:
continue
if not valid_data:
continue
print(data)
for ircmsg in process_data(data):
if "PING :" in ircmsg:
with lock:
ircsock.send(bytes("PONG :ping\n", 'UTF-8'))
elif "/QUOTE PONG" in ircmsg:
confirm = "PONG " + ircmsg.split()[-1:][0] + "\r\n"
with lock:
ircsock.send(bytes(confirm, 'UTF-8'))
for channel in channel_list:
joinchan(channel)
time.sleep(.5)
elif any(channel in ircmsg for channel in channel_list):
try:
args = parsemsg(str(ircmsg))
args['config'] = Config.config
channel = args['channel']
if "|" in args["args"]:
pipe_commands(args, channel)
else:
cmd = get_command(args["command"])
try:
sendmsg(channel, cmd(args))
except Exception as e:
print(e)
sendmsg(channel, (str(e)))
except Exception as e:
pass