This repository was archived by the owner on Jan 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathirc.py
62 lines (55 loc) · 2.04 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
raw_server_messages = False
raw_client_messages = False
# ----------------------------------------------------------------------------
#TODO Improve/unify repl reporting/debugging code
import socket
import ure
from gc import collect
sock=None
channel=""
nick=""
def join():
global sock, nick, channel # FIXME?
sock.send(b"NICK %s\r\n" % nick)
sock.send(b"USER %s 8 * :%s\r\n" % (nick,nick))
#for ch in c:
sock.send(b"JOIN %s\r\n" % channel)
print("joining channel " + str(channel))
def send_msg_to_channel(chan, msg, use_notice):
global sock # FIXME?
tosend = b"%s %s :%s\r\n" % ("NOTICE" if use_notice else "PRIVMSG",chan,msg)
sock.send(tosend)
if raw_client_messages: print("Client sent: [%s]" % tosend)
def do_server():
global sock,nick
collect()
line = sock.readline()
if line != None:
if len(line) < 3: print("Unexpected short line length (%d characters)" % len(line))
if not (line[-2]==13 and line[-1]==10): # Assume all IRC messages should end in \r\n
print("Invalid message from server; ignoring message"); return
if raw_server_messages and len(line)>1: print("Server sent: [%s]" % line)
if line.find(b"PING :") == 0:
_ = sock.send(b"PONG :%s" % line[6:])
if raw_client_messages: print("Client sent: [%s]" % (b"PONG :%s" % line[6:]))
elif line.find(b"PRIVMSG " + str(channel)) != -1:
print("msg received")
return 1
elif line.find(b":Nickname is already in use") != -1:
print("Nickname '%s' is already in use" % nick)
nick = nick + "_"
join()
elif line.find(b"Welcome to HackINT") != -1:
print("Connected, joining channel")
join()
else:
pass
def connect(server,port,channel_,nick_):#,suffixes_,actions_,responses_):
global sock,channel,nick
channel=channel_
nick=nick_
sock = socket.socket()
addr = socket.getaddrinfo(server, port)
sock.connect(addr[0][-1])
sock.setblocking(False)
join()