[946c6da] | 1 | import socket |
---|
| 2 | import sys |
---|
| 3 | import time |
---|
| 4 | import select |
---|
[90417ce] | 5 | |
---|
[c386390] | 6 | FUN = [ |
---|
| 7 | "Did I ask you something?", |
---|
| 8 | "Oh yeah, that's right.", |
---|
| 9 | "Alright, alright. Now go back to work.", |
---|
| 10 | "Buuuuuuuuuuuuuuuurp... Excuse me!", |
---|
| 11 | "Yes?", |
---|
| 12 | "No?", |
---|
| 13 | ] |
---|
| 14 | |
---|
[c248e37] | 15 | SEPARATOR = "="*60 |
---|
[946c6da] | 16 | |
---|
| 17 | class IrcClient: |
---|
| 18 | def __init__(self, nick, pwd): |
---|
[90417ce] | 19 | self.nick = nick |
---|
[946c6da] | 20 | self.pwd = pwd |
---|
[90417ce] | 21 | self.log = '' |
---|
[23fd63d] | 22 | self.tmplog = '' |
---|
[90417ce] | 23 | self.sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
---|
| 24 | |
---|
[0167270] | 25 | def send_raw(self, msg, loud = False, log = True): |
---|
[d24e6f7] | 26 | self.receive() |
---|
[ef2cbca] | 27 | if loud: |
---|
| 28 | print('FROM '+ self.nick + '|| ' + msg) |
---|
[0167270] | 29 | if log: |
---|
| 30 | self.log += msg+'\r\n' |
---|
| 31 | self.tmplog += msg+'\r\n' |
---|
[90417ce] | 32 | self.sck.send((msg+'\r\n').encode()) |
---|
| 33 | |
---|
[d28ca33] | 34 | def send_priv_msg(self, recip, msg, loud = False): |
---|
[946c6da] | 35 | self.send_raw('PRIVMSG '+recip+' :'+msg, loud) |
---|
[90417ce] | 36 | |
---|
| 37 | def connect(self): |
---|
[a18cf1b] | 38 | connected = False |
---|
| 39 | for x in range(5): |
---|
| 40 | try: |
---|
| 41 | self.sck.connect(('127.0.0.1', 6667)) |
---|
| 42 | connected = True |
---|
| 43 | break |
---|
| 44 | except: |
---|
| 45 | time.sleep(1) |
---|
| 46 | |
---|
| 47 | if not connected: |
---|
[c386390] | 48 | return False |
---|
[314c3f8] | 49 | |
---|
[946c6da] | 50 | self.send_raw('USER ' + (self.nick + " ")*3) |
---|
| 51 | self.send_raw('NICK ' + self.nick) |
---|
| 52 | self.send_raw('JOIN &bitlbee') |
---|
[c386390] | 53 | return True |
---|
[90417ce] | 54 | |
---|
[946c6da] | 55 | def jabber_login(self): |
---|
| 56 | self.send_priv_msg("&bitlbee", "account add jabber "+self.nick+"@localhost "+self.pwd) |
---|
[90417ce] | 57 | time.sleep(0.3) |
---|
[946c6da] | 58 | self.send_priv_msg("&bitlbee", "account on") |
---|
[314c3f8] | 59 | time.sleep(1) |
---|
| 60 | self.receive() |
---|
[b9b29f3] | 61 | return (self.log.find('Logged in') != -1) |
---|
[90417ce] | 62 | |
---|
| 63 | def receive(self): |
---|
| 64 | text = '' |
---|
| 65 | while True: |
---|
| 66 | readable, _, _ = select.select([self.sck], [], [], 5) |
---|
| 67 | if self.sck in readable: |
---|
| 68 | text += self.sck.recv(2040).decode() |
---|
| 69 | for line in text.split('\n'): |
---|
| 70 | if line.find('PING') != -1: |
---|
[946c6da] | 71 | self.send_raw('PONG ' + line.split()[1]) |
---|
[90417ce] | 72 | else: |
---|
| 73 | break |
---|
| 74 | self.log += text |
---|
[23fd63d] | 75 | self.tmplog += text |
---|
[90417ce] | 76 | return text |
---|
| 77 | |
---|
[946c6da] | 78 | def add_jabber_buddy(self, nick): |
---|
| 79 | self.send_priv_msg("&bitlbee", "add 0 " + nick+"@localhost") |
---|
[ef2cbca] | 80 | |
---|
[946c6da] | 81 | def block_jabber_buddy(self, nick): |
---|
| 82 | self.send_priv_msg("&bitlbee", "block " + nick) |
---|
[43a257d] | 83 | |
---|
[946c6da] | 84 | def unblock_jabber_buddy(self, nick): |
---|
| 85 | self.send_priv_msg("&bitlbee", "allow " + nick) |
---|
[ef2cbca] | 86 | |
---|
[946c6da] | 87 | def rename_jabber_buddy(self, oldnick, newnick): |
---|
| 88 | self.send_priv_msg("&bitlbee", "rename " + oldnick + " " + newnick) |
---|
[ef2cbca] | 89 | |
---|
[a4623f4] | 90 | def msg_comes_thru(sender, receiver, message): |
---|
[946c6da] | 91 | sender.send_priv_msg(receiver.nick, message) |
---|
[12886d4] | 92 | received = receiver.receive().find(message) != -1 |
---|
[a4623f4] | 93 | return received |
---|
| 94 | |
---|
[e7434db] | 95 | def perform_test(test_function): |
---|
[2806b5e] | 96 | clis = [] |
---|
[25b84f06] | 97 | clis += [IrcClient('test1', 'asd')] |
---|
| 98 | clis += [IrcClient('test2', 'asd')] |
---|
[a4623f4] | 99 | |
---|
[f0e9ee1] | 100 | fail = not test_function(clis) |
---|
| 101 | |
---|
[f1ba258] | 102 | for cli in clis: |
---|
| 103 | cli.receive() |
---|
[2806b5e] | 104 | print(SEPARATOR) |
---|
| 105 | print("Test Log "+ cli.nick+":") |
---|
| 106 | print(cli.tmplog) |
---|
| 107 | print(SEPARATOR) |
---|
| 108 | |
---|
| 109 | if fail: |
---|
| 110 | sys.exit(1) |
---|
[f0e9ee1] | 111 | |
---|
[c386390] | 112 | def connect_test(clis): |
---|
| 113 | ret = True |
---|
| 114 | for cli in clis: |
---|
| 115 | ret = ret & cli.connect() |
---|
| 116 | return ret |
---|
[a4623f4] | 117 | |
---|
| 118 | def yes_test(clis): |
---|
| 119 | ret = False |
---|
[a3d01fb] | 120 | clis[0].send_priv_msg("&bitlbee", "yes") |
---|
[da75c3d] | 121 | clis[0].receive() |
---|
| 122 | for x, fun in enumerate(FUN): |
---|
| 123 | if (clis[0].log.find(fun) != -1): |
---|
[a4623f4] | 124 | ret = True |
---|
[da75c3d] | 125 | if x: |
---|
| 126 | print("The RNG gods smile upon us") |
---|
[a4623f4] | 127 | break |
---|
| 128 | return ret |
---|
| 129 | |
---|
[c386390] | 130 | def jabber_login_test(clis): |
---|
| 131 | ret = True |
---|
| 132 | for cli in clis: |
---|
| 133 | ret = ret & cli.jabber_login() |
---|
| 134 | return ret |
---|
| 135 | |
---|
[51f144a] | 136 | def add_buddy_test(clis): |
---|
| 137 | clis[0].add_jabber_buddy(clis[1].nick) |
---|
[2c7f522] | 138 | clis[1].send_priv_msg("&bitlbee", "yes") |
---|
[51f144a] | 139 | |
---|
[2c7f522] | 140 | clis[1].add_jabber_buddy(clis[0].nick) |
---|
[51f144a] | 141 | clis[0].send_priv_msg("&bitlbee", "yes") |
---|
| 142 | |
---|
| 143 | clis[0].send_priv_msg("&bitlbee", "blist") |
---|
[8d7cc55] | 144 | junk = clis[0].receive() |
---|
| 145 | ret = junk.find(clis[1].nick) != -1 |
---|
| 146 | ret = ret & (junk.find("1 available") != -1) |
---|
[4f22234c] | 147 | ''' |
---|
[4c87970] | 148 | clis[0].send_priv_msg("&bitlbee", "remove " +clis[1].nick) |
---|
[51f144a] | 149 | clis[0].send_priv_msg("&bitlbee", "blist") |
---|
| 150 | ret = ret & (clis[0].receive().find(clis[1].nick) == -1) |
---|
| 151 | |
---|
| 152 | clis[0].add_jabber_buddy(clis[1].nick) |
---|
| 153 | clis[1].send_priv_msg("&bitlbee", "yes") |
---|
[663f018] | 154 | time.sleep(1) |
---|
[51f144a] | 155 | clis[0].send_priv_msg("&bitlbee", "blist") |
---|
[8d7cc55] | 156 | junk = clis[0].receive() |
---|
[663f018] | 157 | ret = ret & (junk.find("1 available") != -1) |
---|
[8d7cc55] | 158 | ret = ret & (junk.find(clis[1].nick) != -1) |
---|
[4f22234c] | 159 | ''' |
---|
[da215ef] | 160 | return ret |
---|
[51f144a] | 161 | |
---|
[a4623f4] | 162 | def message_test(clis): |
---|
| 163 | ret = msg_comes_thru(clis[0], clis[1], 'ohai <3') |
---|
| 164 | ret = ret & msg_comes_thru(clis[1], clis[0], 'uwu *pounces*') |
---|
| 165 | return ret |
---|
| 166 | |
---|
| 167 | def block_test(clis): |
---|
| 168 | clis[0].block_jabber_buddy(clis[1].nick) |
---|
| 169 | ret = not msg_comes_thru(clis[1], clis[0], 'm-meow?') |
---|
| 170 | clis[0].unblock_jabber_buddy(clis[1].nick) |
---|
| 171 | ret = ret & msg_comes_thru(clis[1], clis[0], '*purrs*') |
---|
| 172 | return ret |
---|
| 173 | |
---|
| 174 | def rename_test(clis): |
---|
| 175 | newname = "xXx_pup_LINKENPARK4EVA" |
---|
| 176 | message = "rawr meanmz i luv<3 u in dinosaur" |
---|
| 177 | |
---|
| 178 | clis[0].rename_jabber_buddy(clis[1].nick, newname) |
---|
| 179 | clis[0].send_priv_msg(newname, message) |
---|
| 180 | ret = clis[1].receive().find(message) != -1 |
---|
| 181 | |
---|
| 182 | clis[0].rename_jabber_buddy("-del", newname) |
---|
| 183 | ret = ret & msg_comes_thru(clis[0], clis[1], "rawr") |
---|
| 184 | return ret |
---|
[43a257d] | 185 | |
---|
[0e2c8b4] | 186 | def status_test(clis): |
---|
[76f0fd1] | 187 | status = "get out of my room mom" |
---|
| 188 | clis[1].send_priv_msg("&bitlbee", "set status '"+status+"'") |
---|
[0e2c8b4] | 189 | clis[0].send_priv_msg("&bitlbee", "info "+clis[1].nick) |
---|
[35c72b7] | 190 | ret = (clis[0].receive().find("jabber - Status message: "+status) != -1) |
---|
[0e2c8b4] | 191 | |
---|
[3d4cb51] | 192 | clis[1].send_priv_msg("&bitlbee", "set") |
---|
| 193 | ret = ret & (clis[1].receive().find(status) != -1) |
---|
| 194 | |
---|
[0e2c8b4] | 195 | clis[1].send_priv_msg("&bitlbee", "set -del status") |
---|
| 196 | clis[0].send_priv_msg("&bitlbee", "info "+clis[1].nick) |
---|
[2e9b39a] | 197 | ret = ret & (clis[0].receive().find("jabber - Status message: (none)") != -1) |
---|
[0e2c8b4] | 198 | return ret |
---|
| 199 | |
---|
| 200 | def offline_test(clis): |
---|
[35c72b7] | 201 | clis[0].send_priv_msg("&bitlbee", "account off") |
---|
| 202 | |
---|
| 203 | junk = clis[0].receive() |
---|
| 204 | ret = (junk.find(clis[1].nick) != -1) |
---|
[76f0fd1] | 205 | ret = ret & (junk.find("QUIT") != -1) |
---|
[35c72b7] | 206 | |
---|
| 207 | junk = clis[1].receive() |
---|
| 208 | ret = ret & (junk.find(clis[0].nick) != -1) |
---|
[76f0fd1] | 209 | ret = ret & (junk.find("QUIT") != -1) |
---|
[35c72b7] | 210 | |
---|
[2e9b39a] | 211 | clis[0].send_priv_msg(clis[1].nick, "i'm not ur mom") |
---|
[76f0fd1] | 212 | ret = ret & (clis[0].receive().find("No such nick/channel") != -1) |
---|
[35c72b7] | 213 | |
---|
| 214 | clis[0].send_priv_msg("&bitlbee", "account on") |
---|
| 215 | |
---|
| 216 | junk = clis[0].receive() |
---|
| 217 | ret = ret & (junk.find(clis[1].nick) != -1) |
---|
[76f0fd1] | 218 | ret = ret & (junk.find("JOIN") != -1) |
---|
[35c72b7] | 219 | |
---|
| 220 | junk = clis[1].receive() |
---|
| 221 | ret = ret & (junk.find(clis[0].nick) != -1) |
---|
[76f0fd1] | 222 | ret = ret & (junk.find("JOIN") != -1) |
---|
[35c72b7] | 223 | |
---|
| 224 | return ret |
---|
[0e2c8b4] | 225 | |
---|
[7361a6a] | 226 | def default_target_test(clis): |
---|
| 227 | clis[0].send_priv_msg("&bitlbee", "set default_target last") |
---|
[5552588] | 228 | clis[0].send_priv_msg("&bitlbee", "test2: ur mah default now") |
---|
| 229 | |
---|
| 230 | ret = (clis[1].receive().find("ur mah default now") != -1) |
---|
[7361a6a] | 231 | |
---|
[5552588] | 232 | clis[0].send_priv_msg("&bitlbee", "mrow") |
---|
| 233 | ret = ret & (clis[1].receive().find("mrow") != -1) |
---|
[7361a6a] | 234 | |
---|
| 235 | clis[0].send_priv_msg("root", "set default_target root") |
---|
[68ba9f4] | 236 | junk = clis[0].receive() |
---|
| 237 | ret = ret & (junk.find("default_target") != -1) |
---|
| 238 | ret = ret & (junk.find("root") != -1) |
---|
[7361a6a] | 239 | |
---|
| 240 | clis[0].send_priv_msg("&bitlbee", "yes") |
---|
| 241 | ret = ret & (clis[1].receive().find("yes") == -1) |
---|
| 242 | return ret |
---|
| 243 | |
---|
[3d4cb51] | 244 | def help_test(clis): |
---|
| 245 | clis[0].send_priv_msg("&bitlbee", "help") |
---|
| 246 | ret = (clis[0].receive().find("identify_methods") != -1) |
---|
| 247 | clis[0].send_priv_msg("&bitlbee", "help commands") |
---|
| 248 | ret = ret & (clis[0].receive().find("qlist") != -1) |
---|
| 249 | return ret |
---|