-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjustirc.py
More file actions
executable file
·126 lines (104 loc) · 4.26 KB
/
Copy pathjustirc.py
File metadata and controls
executable file
·126 lines (104 loc) · 4.26 KB
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
import socket
import time
def parse_irc_packet(packet):
irc_packet = IRCPacket()
irc_packet.parse(packet)
return irc_packet
class IRCPacket:
def __init__(self):
self.prefix = ""
self.command = ""
self.arguments = []
def parse(self, packet):
if packet.startswith(":"):
self.prefix = packet[1:].split(" ")[0]
packet = packet.split(" ", 1)[1]
if " " in packet:
if " :" in packet:
last_argument = packet.split(" :")[1]
packet = packet.split(" :")[0]
for splitted in packet.split(" "):
if not self.command:
self.command = splitted
else:
self.arguments.append(splitted)
self.arguments.append(last_argument)
else:
for splitted in packet.split(" "):
if not self.command:
self.command = splitted
else:
self.arguments.append(splitted)
else:
self.command = packet
class IRCConnection:
def __init__(self):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.nick = ""
self.on_connect = []
self.on_public_message = []
self.on_private_message = []
self.on_ping = []
self.on_welcome = []
self.on_packet_received = []
self.on_join = []
self.on_leave = []
def run_once(self):
packet = parse_irc_packet(next(self.lines)) #Get next line from generator
for event_handler in list(self.on_packet_received):
event_handler(self, packet)
if packet.command == "PRIVMSG":
if packet.arguments[0].startswith("#"):
for event_handler in list(self.on_public_message):
event_handler(self, packet.arguments[0], packet.prefix.split("!")[0], packet.arguments[1])
else:
for event_handler in list(self.on_private_message):
event_handler(self, packet.prefix.split("!")[0], packet.arguments[1])
elif packet.command == "PING":
self.send_line("PONG :{}".format(packet.arguments[0]))
for event_handler in list(self.on_ping):
event_handler(self)
elif packet.command == "433" or packet.command == "437":
#Command 433 is "Nick in use"
#Add underscore to the nick
self.set_nick("{}_".format(self.nick))
elif packet.command == "001":
for event_handler in list(self.on_welcome):
event_handler(self)
elif packet.command == "JOIN":
for event_handler in list(self.on_join):
event_handler(self, packet.arguments[0], packet.prefix.split("!")[0])
elif packet.command == "PART":
for event_handler in list(self.on_leave):
event_handler(self, packet.arguments[0], packet.prefix.split("!")[0])
def run_loop(self):
while True:
self.run_once()
def read_lines(self):
buff = ""
while True:
buff += self.socket.recv(1024).decode("utf-8", "replace")
while "\n" in buff:
line, buff = buff.split("\n", 1)
line = line.replace("\r", "")
yield line
def connect(self, server, port=6667):
self.socket.connect((server, port))
self.lines = self.read_lines()
for event_handler in list(self.on_connect):
event_handler(self)
def send_line(self, line):
self.socket.send("{}\r\n".format(line).encode("utf-8"))
def send_message(self, to, message):
self.send_line("PRIVMSG {} :{}".format(to, message))
def send_notice(self, to, message):
self.send_line("NOTICE {} :{}".format(to, message))
def send_action_message(self, to, action):
self.send_message(to, "\x01ACTION {}\x01".format(action))
def join_channel(self, channel_name):
self.send_line("JOIN {}".format(channel_name))
def set_nick(self, nick):
self.nick = nick
self.send_line("NICK {}".format(nick))
def send_user_packet(self, username):
self.send_line("USER {} 0 * :{}".format(username, username))