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 addJabberBuddy(self, nick): |
---|
54 | self.sendPrivMsg("&bitlbee", "add 0 " + nick+"@localhost") |
---|
55 | |
---|
56 | def acceptJabberRequest(self): |
---|
57 | recent = self.receive() |
---|
58 | if recent.find("not in your buddy list") == -1: |
---|
59 | print("No request found") |
---|
60 | return |
---|
61 | self.sendPrivMsg("&bitlbee", "yes") |
---|
62 | |
---|
63 | def runTests(): |
---|
64 | clis = [] |
---|
65 | clis += [ircClient('test1', 'asd')] |
---|
66 | clis += [ircClient('test2', 'asd')] |
---|
67 | for cli in clis: |
---|
68 | cli.connect() |
---|
69 | cli.jabberLogin() |
---|
70 | a, b = clis[0], clis[1] |
---|
71 | |
---|
72 | a.addJabberBuddy(b.nick) |
---|
73 | b.acceptJabberRequest() |
---|
74 | a.sendPrivMsg(b.nick, 'ohai qtie') |
---|
75 | |
---|
76 | a.receive() |
---|
77 | b.receive() |
---|
78 | if b.log.find('ohai qtie') == -1: |
---|
79 | print('Message not coming through') |
---|
80 | print('Sender Log:' + a.log) |
---|
81 | print('Receiver Log:' + b.log) |
---|
82 | sys.exit(1) |
---|
83 | print("Message test successful") |
---|
84 | |
---|
85 | if __name__ == "__main__": |
---|
86 | runTests() |
---|