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, loud = True): |
---|
11 | self.receive() |
---|
12 | if loud: |
---|
13 | print('FROM '+ self.nick + '|| ' + msg) |
---|
14 | self.log += msg+'\r\n' |
---|
15 | self.sck.send((msg+'\r\n').encode()) |
---|
16 | |
---|
17 | def sendPrivMsg(self, recip, msg, loud = True): |
---|
18 | self.sendRaw('PRIVMSG '+recip+' :'+msg, loud) |
---|
19 | |
---|
20 | def connect(self): |
---|
21 | try: |
---|
22 | self.sck.connect(('127.0.0.1', 6667)) |
---|
23 | except: |
---|
24 | print("IRC connection failed for " + self.nick) |
---|
25 | sys.exit(1) |
---|
26 | |
---|
27 | print("IRC connection established for " + self.nick) |
---|
28 | |
---|
29 | self.sendRaw('USER ' + (self.nick + " ")*3) |
---|
30 | self.sendRaw('NICK ' + self.nick) |
---|
31 | self.sendRaw('JOIN &bitlbee') |
---|
32 | |
---|
33 | def jabberLogin(self): |
---|
34 | self.sendPrivMsg("&bitlbee", "account add jabber "+self.nick+"@localhost "+self.pw) |
---|
35 | time.sleep(0.3) |
---|
36 | self.sendPrivMsg("&bitlbee", "account on") |
---|
37 | time.sleep(1) |
---|
38 | self.receive() |
---|
39 | if self.log.find('Logged in') == -1: |
---|
40 | print("Jabber login failed for " + self.nick) |
---|
41 | sys.exit(1) |
---|
42 | else: |
---|
43 | print("Jabber login successful for " + self.nick) |
---|
44 | |
---|
45 | def receive(self): |
---|
46 | text = '' |
---|
47 | while True: |
---|
48 | readable, _, _ = select.select([self.sck], [], [], 5) |
---|
49 | if self.sck in readable: |
---|
50 | text += self.sck.recv(2040).decode() |
---|
51 | for line in text.split('\n'): |
---|
52 | if line.find('PING') != -1: |
---|
53 | self.sendRaw('PONG ' + line.split()[1]) |
---|
54 | else: |
---|
55 | break |
---|
56 | self.log += text |
---|
57 | return text |
---|
58 | |
---|
59 | def addJabberBuddy(self, nick): |
---|
60 | self.sendPrivMsg("&bitlbee", "add 0 " + nick+"@localhost") |
---|
61 | |
---|
62 | def blockJabberBuddy(self, nick): |
---|
63 | self.sendPrivMsg("&bitlbee", "block " + nick) |
---|
64 | |
---|
65 | def unblockJabberBuddy(self, nick): |
---|
66 | self.sendPrivMsg("&bitlbee", "allow " + nick) |
---|
67 | |
---|
68 | def renameJabberBuddy(self, oldnick, newnick): |
---|
69 | self.sendPrivMsg("&bitlbee", "rename " + oldnick + " " + newnick) |
---|
70 | |
---|
71 | def testSendMessage(sender, receiver, message, shouldreceive = True): |
---|
72 | sender.sendPrivMsg(b.nick, message) |
---|
73 | received = receiver.receive().find(message) != -1 |
---|
74 | if shouldreceive ^ received: |
---|
75 | print('Test failed: Message from ' + sender.nick + ' to ' + receiver.nick) |
---|
76 | print('Sender Log:' + a.log) |
---|
77 | print('Receiver Log:' + b.log) |
---|
78 | sys.exit(1) |
---|
79 | |
---|
80 | def runTests(): |
---|
81 | clis = [] |
---|
82 | clis += [ircClient('test1', 'asd')] |
---|
83 | clis += [ircClient('test2', 'asd')] |
---|
84 | for cli in clis: |
---|
85 | cli.connect() |
---|
86 | cli.jabberLogin() |
---|
87 | |
---|
88 | clis[0].addJabberBuddy(clis[1].nick) |
---|
89 | |
---|
90 | print("Test: Send message") |
---|
91 | testSendMessage(clis[0],clis[1]),'ohai <3') |
---|
92 | testSendMessage(clis[1],clis[0]),'uwu *pounces*') |
---|
93 | print("Test passed") |
---|
94 | |
---|
95 | print("Test: Block/Unblock") |
---|
96 | clis[0].blockJabberBuddy(clis[1].nick) |
---|
97 | testSendMessage(clis[1],clis[0]),'m-meow?', shouldreceive = False) |
---|
98 | clis[0].unblockJabberBuddy(clis[1].nick) |
---|
99 | testSendMessage(clis[1],clis[0]),'*purrs*') |
---|
100 | print("Test passed") |
---|
101 | |
---|
102 | if __name__ == "__main__": |
---|
103 | runTests() |
---|