[946c6da] | 1 | import socket |
---|
| 2 | import sys |
---|
| 3 | import time |
---|
| 4 | import select |
---|
[90417ce] | 5 | |
---|
[946c6da] | 6 | MESSAGETEST = True |
---|
| 7 | BLOCKTEST = False |
---|
| 8 | OFFLINETEST = False |
---|
| 9 | RENAMETEST = True |
---|
| 10 | |
---|
| 11 | FAILED = False |
---|
| 12 | |
---|
| 13 | class IrcClient: |
---|
| 14 | def __init__(self, nick, pwd): |
---|
[90417ce] | 15 | self.nick = nick |
---|
[946c6da] | 16 | self.pwd = pwd |
---|
[90417ce] | 17 | self.log = '' |
---|
| 18 | self.sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
---|
| 19 | |
---|
[946c6da] | 20 | def send_raw(self, msg, loud = True): |
---|
[d24e6f7] | 21 | self.receive() |
---|
[ef2cbca] | 22 | if loud: |
---|
| 23 | print('FROM '+ self.nick + '|| ' + msg) |
---|
[90417ce] | 24 | self.log += msg+'\r\n' |
---|
| 25 | self.sck.send((msg+'\r\n').encode()) |
---|
| 26 | |
---|
[946c6da] | 27 | def send_priv_msg(self, recip, msg, loud = True): |
---|
| 28 | self.send_raw('PRIVMSG '+recip+' :'+msg, loud) |
---|
[90417ce] | 29 | |
---|
| 30 | def connect(self): |
---|
[314c3f8] | 31 | try: |
---|
| 32 | self.sck.connect(('127.0.0.1', 6667)) |
---|
| 33 | except: |
---|
[ef2cbca] | 34 | print("IRC connection failed for " + self.nick) |
---|
[314c3f8] | 35 | sys.exit(1) |
---|
[ef2cbca] | 36 | |
---|
| 37 | print("IRC connection established for " + self.nick) |
---|
[314c3f8] | 38 | |
---|
[946c6da] | 39 | self.send_raw('USER ' + (self.nick + " ")*3) |
---|
| 40 | self.send_raw('NICK ' + self.nick) |
---|
| 41 | self.send_raw('JOIN &bitlbee') |
---|
[90417ce] | 42 | |
---|
[946c6da] | 43 | def jabber_login(self): |
---|
| 44 | self.send_priv_msg("&bitlbee", "account add jabber "+self.nick+"@localhost "+self.pwd) |
---|
[90417ce] | 45 | time.sleep(0.3) |
---|
[946c6da] | 46 | self.send_priv_msg("&bitlbee", "account on") |
---|
[314c3f8] | 47 | time.sleep(1) |
---|
| 48 | self.receive() |
---|
| 49 | if self.log.find('Logged in') == -1: |
---|
[ef2cbca] | 50 | print("Jabber login failed for " + self.nick) |
---|
[314c3f8] | 51 | sys.exit(1) |
---|
[ef2cbca] | 52 | else: |
---|
| 53 | print("Jabber login successful for " + self.nick) |
---|
[90417ce] | 54 | |
---|
| 55 | def receive(self): |
---|
| 56 | text = '' |
---|
| 57 | while True: |
---|
| 58 | readable, _, _ = select.select([self.sck], [], [], 5) |
---|
| 59 | if self.sck in readable: |
---|
| 60 | text += self.sck.recv(2040).decode() |
---|
| 61 | for line in text.split('\n'): |
---|
| 62 | if line.find('PING') != -1: |
---|
[946c6da] | 63 | self.send_raw('PONG ' + line.split()[1]) |
---|
[90417ce] | 64 | else: |
---|
| 65 | break |
---|
| 66 | self.log += text |
---|
| 67 | return text |
---|
| 68 | |
---|
[946c6da] | 69 | def add_jabber_buddy(self, nick): |
---|
| 70 | self.send_priv_msg("&bitlbee", "add 0 " + nick+"@localhost") |
---|
[ef2cbca] | 71 | |
---|
[946c6da] | 72 | def block_jabber_buddy(self, nick): |
---|
| 73 | self.send_priv_msg("&bitlbee", "block " + nick) |
---|
[43a257d] | 74 | |
---|
[946c6da] | 75 | def unblock_jabber_buddy(self, nick): |
---|
| 76 | self.send_priv_msg("&bitlbee", "allow " + nick) |
---|
[ef2cbca] | 77 | |
---|
[946c6da] | 78 | def rename_jabber_buddy(self, oldnick, newnick): |
---|
| 79 | self.send_priv_msg("&bitlbee", "rename " + oldnick + " " + newnick) |
---|
[ef2cbca] | 80 | |
---|
[946c6da] | 81 | def test_send_message(sender, receiver, message): |
---|
| 82 | sender.send_priv_msg(receiver.nick, message) |
---|
[12886d4] | 83 | received = receiver.receive().find(message) != -1 |
---|
[946c6da] | 84 | return received; |
---|
[43a257d] | 85 | |
---|
[946c6da] | 86 | def run_tests(): |
---|
| 87 | global FAILED |
---|
[90417ce] | 88 | clis = [] |
---|
[946c6da] | 89 | clis += [IrcClient('test1', 'asd')] |
---|
| 90 | clis += [IrcClient('test2', 'asd')] |
---|
[90417ce] | 91 | for cli in clis: |
---|
| 92 | cli.connect() |
---|
[946c6da] | 93 | cli.jabber_login() |
---|
| 94 | |
---|
| 95 | clis[0].add_jabber_buddy(clis[1].nick) |
---|
| 96 | |
---|
| 97 | if MESSAGETEST: |
---|
| 98 | print("Test: Send message") |
---|
| 99 | ret = test_send_message(clis[0], clis[1], 'ohai <3') |
---|
| 100 | ret = ret & test_send_message(clis[1], clis[0], 'uwu *pounces*') |
---|
| 101 | if ret: |
---|
| 102 | print("Test passed") |
---|
| 103 | else: |
---|
| 104 | print("Test failed") |
---|
| 105 | FAILED = True; |
---|
| 106 | |
---|
| 107 | |
---|
| 108 | if BLOCKTEST: |
---|
| 109 | print("Test: Block/Unblock") |
---|
| 110 | clis[0].block_jabber_buddy(clis[1].nick) |
---|
| 111 | ret = not test_send_message(clis[1], clis[0], 'm-meow?') |
---|
| 112 | clis[0].unblock_jabber_buddy(clis[1].nick) |
---|
| 113 | ret = ret & test_send_message(clis[1], clis[0], '*purrs*') |
---|
| 114 | if ret: |
---|
| 115 | print("Test passed") |
---|
| 116 | else: |
---|
| 117 | print("Test failed") |
---|
| 118 | FAILED = True; |
---|
[d24e6f7] | 119 | |
---|
[946c6da] | 120 | if RENAMETEST: |
---|
| 121 | print("Test: Rename buddy") |
---|
[e98404d] | 122 | newname = "xXx_pup_LINKENPARK4EVA" |
---|
| 123 | message = "rawr meanmz i luv<3 u in dinosaur" |
---|
[43a257d] | 124 | |
---|
[946c6da] | 125 | clis[0].rename_jabber_buddy(clis[1].nick, newname) |
---|
| 126 | clis[0].send_priv_msg(newname, message) |
---|
| 127 | ret = clis[1].receive().find(message) != -1 |
---|
[ef2cbca] | 128 | |
---|
[946c6da] | 129 | clis[0].rename_jabber_buddy(newname, clis[1].nick) |
---|
| 130 | ret = ret & test_send_message(clis[0], clis[1], "rawr") |
---|
| 131 | if ret: |
---|
| 132 | print("Test passed") |
---|
| 133 | else: |
---|
| 134 | print("Test failed") |
---|
| 135 | FAILED = True; |
---|
[e98404d] | 136 | |
---|
| 137 | if FAILED: |
---|
| 138 | print("\ntest1 Log:\n"+clis[0].log) |
---|
| 139 | print("\ntest2 Log:\n"+clis[1].log) |
---|
[ef2cbca] | 140 | |
---|
[90417ce] | 141 | if __name__ == "__main__": |
---|
[946c6da] | 142 | run_tests() |
---|
| 143 | if FAILED: |
---|
| 144 | sys.exit(1) |
---|