1 | import socket, sys, time, select |
---|
2 | |
---|
3 | class ircClient: |
---|
4 | def __init__(self, nick, pw): |
---|
5 | self.nick = nick |
---|
6 | self.pw = pw |
---|
7 | self.log = '' |
---|
8 | self.sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
---|
9 | |
---|
10 | def sendRaw(self, msg): |
---|
11 | self.receive() |
---|
12 | self.log += msg+'\r\n' |
---|
13 | self.sck.send((msg+'\r\n').encode()) |
---|
14 | |
---|
15 | def sendPrivMsg(self, recip, msg): |
---|
16 | self.sendRaw('PRIVMSG '+recip+' :'+msg) |
---|
17 | |
---|
18 | def connect(self): |
---|
19 | try: |
---|
20 | self.sck.connect(('127.0.0.1', 6667)) |
---|
21 | except: |
---|
22 | print("Connection failed") |
---|
23 | sys.exit(1) |
---|
24 | |
---|
25 | self.sendRaw('USER ' + (self.nick + " ")*3) |
---|
26 | self.sendRaw('NICK ' + self.nick) |
---|
27 | self.sendRaw('JOIN &bitlbee') |
---|
28 | |
---|
29 | def jabberLogin(self): |
---|
30 | self.sendPrivMsg("&bitlbee", "account add jabber "+self.nick+"@localhost "+self.pw) |
---|
31 | time.sleep(0.3) |
---|
32 | self.sendPrivMsg("&bitlbee", "account on") |
---|
33 | time.sleep(1) |
---|
34 | self.receive() |
---|
35 | if self.log.find('Logged in') == -1: |
---|
36 | print("Jabber Login failed") |
---|
37 | sys.exit(1) |
---|
38 | |
---|
39 | def receive(self): |
---|
40 | text = '' |
---|
41 | while True: |
---|
42 | readable, _, _ = select.select([self.sck], [], [], 5) |
---|
43 | if self.sck in readable: |
---|
44 | text += self.sck.recv(2040).decode() |
---|
45 | for line in text.split('\n'): |
---|
46 | if line.find('PING') != -1: |
---|
47 | self.sendRaw('PONG ' + line.split()[1]) |
---|
48 | else: |
---|
49 | break |
---|
50 | self.log += text |
---|
51 | return text |
---|
52 | |
---|
53 | def runTests(): |
---|
54 | clis = [] |
---|
55 | clis += [ircClient('test1', 'asd')] |
---|
56 | clis += [ircClient('test2', 'asd')] |
---|
57 | for cli in clis: |
---|
58 | cli.connect() |
---|
59 | cli.jabberLogin() |
---|
60 | a, b = clis[0], clis[1] |
---|
61 | |
---|
62 | a.sendPrivMsg(b.nick, 'ohai qtie') |
---|
63 | a.receive() |
---|
64 | b.receive() |
---|
65 | if b.log.find('ohai qtie') == -1: |
---|
66 | print('Message not coming through') |
---|
67 | print('Sender Log:' + a.log) |
---|
68 | print('Receiver Log:' + b.log) |
---|
69 | sys.exit(1) |
---|
70 | |
---|
71 | if __name__ == "__main__": |
---|
72 | runTests() |
---|