1 | import socket |
---|
2 | import sys |
---|
3 | import time |
---|
4 | import select |
---|
5 | |
---|
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 | |
---|
15 | SEPARATOR = "="*60 |
---|
16 | |
---|
17 | ALWAYSSHOWLOG = False |
---|
18 | |
---|
19 | class IrcClient: |
---|
20 | def __init__(self, nick, pwd): |
---|
21 | self.nick = nick |
---|
22 | self.pwd = pwd |
---|
23 | self.log = '' |
---|
24 | self.tmplog = '' |
---|
25 | self.sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
---|
26 | |
---|
27 | def send_raw(self, msg, loud = False, log = True): |
---|
28 | self.receive() |
---|
29 | if loud: |
---|
30 | print('FROM '+ self.nick + '|| ' + msg) |
---|
31 | if log: |
---|
32 | self.log += msg+'\r\n' |
---|
33 | self.tmplog += msg+'\r\n' |
---|
34 | self.sck.send((msg+'\r\n').encode()) |
---|
35 | |
---|
36 | def send_priv_msg(self, recip, msg, loud = False): |
---|
37 | self.send_raw('PRIVMSG '+recip+' :'+msg, loud) |
---|
38 | |
---|
39 | def connect(self): |
---|
40 | connected = False |
---|
41 | for x in range(5): |
---|
42 | try: |
---|
43 | self.sck.connect(('127.0.0.1', 6667)) |
---|
44 | connected = True |
---|
45 | break |
---|
46 | except: |
---|
47 | time.sleep(1) |
---|
48 | |
---|
49 | if not connected: |
---|
50 | return False |
---|
51 | |
---|
52 | self.send_raw('USER ' + (self.nick + " ")*3) |
---|
53 | self.send_raw('NICK ' + self.nick) |
---|
54 | self.send_raw('JOIN &bitlbee') |
---|
55 | return True |
---|
56 | |
---|
57 | def jabber_login(self): |
---|
58 | self.send_priv_msg("&bitlbee", "account add jabber "+self.nick+"@localhost "+self.pwd) |
---|
59 | time.sleep(0.3) |
---|
60 | self.send_priv_msg("&bitlbee", "account on") |
---|
61 | time.sleep(1) |
---|
62 | self.receive() |
---|
63 | return (self.log.find('Logged in') != -1) |
---|
64 | |
---|
65 | def receive(self): |
---|
66 | text = '' |
---|
67 | while True: |
---|
68 | readable, _, _ = select.select([self.sck], [], [], 5) |
---|
69 | if self.sck in readable: |
---|
70 | text += self.sck.recv(2040).decode() |
---|
71 | for line in text.split('\n'): |
---|
72 | if line.find('PING') != -1: |
---|
73 | self.send_raw('PONG ' + line.split()[1]) |
---|
74 | else: |
---|
75 | break |
---|
76 | self.log += text |
---|
77 | self.tmplog += text |
---|
78 | return text |
---|
79 | |
---|
80 | def add_jabber_buddy(self, nick): |
---|
81 | self.send_priv_msg("&bitlbee", "add 0 " + nick+"@localhost") |
---|
82 | |
---|
83 | def block_jabber_buddy(self, nick): |
---|
84 | self.send_priv_msg("&bitlbee", "block " + nick) |
---|
85 | |
---|
86 | def unblock_jabber_buddy(self, nick): |
---|
87 | self.send_priv_msg("&bitlbee", "allow " + nick) |
---|
88 | |
---|
89 | def rename_jabber_buddy(self, oldnick, newnick): |
---|
90 | self.send_priv_msg("&bitlbee", "rename " + oldnick + " " + newnick) |
---|
91 | |
---|
92 | def msg_comes_thru(sender, receiver, message): |
---|
93 | sender.send_priv_msg(receiver.nick, message) |
---|
94 | received = receiver.receive().find(message) != -1 |
---|
95 | return received |
---|
96 | |
---|
97 | def perform_test(test_function): |
---|
98 | clis = [] |
---|
99 | clis += [IrcClient('test1', 'asd')] |
---|
100 | clis += [IrcClient('test2', 'asd')] |
---|
101 | |
---|
102 | fail = not test_function(clis) |
---|
103 | |
---|
104 | if ALWAYSSHOWLOG or fail: |
---|
105 | for cli in clis: |
---|
106 | cli.receive() |
---|
107 | print(SEPARATOR) |
---|
108 | print("Test Log "+ cli.nick+":") |
---|
109 | print(cli.tmplog) |
---|
110 | print(SEPARATOR) |
---|
111 | |
---|
112 | if fail: |
---|
113 | sys.exit(1) |
---|
114 | |
---|
115 | def connect_test(clis): |
---|
116 | ret = True |
---|
117 | for cli in clis: |
---|
118 | ret = ret & cli.connect() |
---|
119 | return ret |
---|
120 | |
---|
121 | def yes_test(clis): |
---|
122 | ret = False |
---|
123 | clis[0].send_priv_msg("&bitlbee", "yes") |
---|
124 | clis[0].receive() |
---|
125 | for x, fun in enumerate(FUN): |
---|
126 | if (clis[0].log.find(fun) != -1): |
---|
127 | ret = True |
---|
128 | if x: |
---|
129 | print("The RNG gods smile upon us") |
---|
130 | break |
---|
131 | return ret |
---|
132 | |
---|
133 | def jabber_login_test(clis): |
---|
134 | ret = True |
---|
135 | for cli in clis: |
---|
136 | ret = ret & cli.jabber_login() |
---|
137 | return ret |
---|
138 | |
---|
139 | def jabber_delete_account_test(clis): |
---|
140 | ret = True |
---|
141 | clis[1].receive() |
---|
142 | clis[1].send_priv_msg("&bitlbee", "account list") |
---|
143 | time.sleep(0.5) |
---|
144 | ret = ret & (clis[1].receive().find(clis[1].nick+'@localhost') != -1) |
---|
145 | |
---|
146 | clis[1].send_priv_msg("&bitlbee", "account on") |
---|
147 | clis[1].send_priv_msg("&bitlbee", "account "+clis[1].nick+" del") |
---|
148 | clis[1].receive() |
---|
149 | clis[1].send_priv_msg("&bitlbee", "account list") |
---|
150 | time.sleep(0.5) |
---|
151 | ret = ret & (clis[1].receive().find(clis[1].nick+'@localhost') != -1) |
---|
152 | |
---|
153 | clis[1].send_priv_msg("&bitlbee", "account off") |
---|
154 | clis[1].send_priv_msg("&bitlbee", "account "+clis[1].nick+" del") |
---|
155 | clis[1].receive() |
---|
156 | clis[1].send_priv_msg("&bitlbee", "account list") |
---|
157 | time.sleep(0.5) |
---|
158 | ret = ret & (clis[1].receive().find(clis[1].nick+'@localhost') == -1) |
---|
159 | return ret |
---|
160 | |
---|
161 | def register_test(clis): |
---|
162 | clis[1].send_priv_msg("&bitlbee", "register "+clis[1].pwd*2) |
---|
163 | time.sleep(0.5) |
---|
164 | return (clis[1].receive().find('Account successfully created') != -1) |
---|
165 | |
---|
166 | def unregister_test(clis): |
---|
167 | clis[1].send_priv_msg("&bitlbee", "drop "+clis[1].pwd*2) |
---|
168 | time.sleep(0.5) |
---|
169 | ret = (clis[1].receive().find('removed') != -1) |
---|
170 | clis[1].send_priv_msg("&bitlbee", "drop "+clis[1].pwd*2) |
---|
171 | time.sleep(0.5) |
---|
172 | ret = ret & (clis[1].receive().find('That account does not exist') != -1) |
---|
173 | return ret |
---|
174 | |
---|
175 | def identify_test(clis): |
---|
176 | ret = True |
---|
177 | clis[1].send_priv_msg("&bitlbee", "identify "+clis[1].pwd) |
---|
178 | time.sleep(0.5) |
---|
179 | ret = ret & (clis[1].receive().find('Incorrect password') != -1) |
---|
180 | |
---|
181 | clis[1].send_priv_msg("&bitlbee", "identify "+clis[1].pwd*2) |
---|
182 | time.sleep(0.5) |
---|
183 | ret = ret & (clis[1].receive().find('Password accepted') != -1) |
---|
184 | return ret |
---|
185 | |
---|
186 | def identify_nonexist_test(clis): |
---|
187 | clis[1].send_priv_msg("&bitlbee", "register "+clis[1].pwd) |
---|
188 | time.sleep(0.5) |
---|
189 | return (clis[1].receive().find('The nick is (probably) not registered') != -1) |
---|
190 | |
---|
191 | def add_buddy_test(clis): |
---|
192 | clis[0].add_jabber_buddy(clis[1].nick) |
---|
193 | clis[1].send_priv_msg("&bitlbee", "yes") |
---|
194 | |
---|
195 | clis[1].add_jabber_buddy(clis[0].nick) |
---|
196 | clis[0].send_priv_msg("&bitlbee", "yes") |
---|
197 | |
---|
198 | clis[0].send_priv_msg("&bitlbee", "blist") |
---|
199 | junk = clis[0].receive() |
---|
200 | ret = junk.find(clis[1].nick) != -1 |
---|
201 | ret = ret & (junk.find("1 available") != -1) |
---|
202 | return ret |
---|
203 | |
---|
204 | def remove_buddy_test(clis): |
---|
205 | ret = add_buddy_test(clis) |
---|
206 | |
---|
207 | clis[0].send_priv_msg("&bitlbee", "remove " +clis[1].nick) |
---|
208 | clis[0].send_priv_msg("&bitlbee", "blist all") |
---|
209 | time.sleep(0.5) |
---|
210 | ret = ret & (clis[0].receive().find(clis[1].nick) == -1) |
---|
211 | |
---|
212 | clis[0].add_jabber_buddy(clis[1].nick) |
---|
213 | clis[1].send_priv_msg("&bitlbee", "yes") |
---|
214 | time.sleep(1) |
---|
215 | clis[0].send_priv_msg("&bitlbee", "blist all") |
---|
216 | time.sleep(0.5) |
---|
217 | junk = clis[0].receive() |
---|
218 | #ret = ret & (junk.find("1 available") != -1) |
---|
219 | ret = ret & (junk.find(clis[1].nick) != -1) |
---|
220 | return ret |
---|
221 | |
---|
222 | def message_test(clis): |
---|
223 | ret = msg_comes_thru(clis[0], clis[1], 'ohai <3') |
---|
224 | ret = ret & msg_comes_thru(clis[1], clis[0], 'uwu *pounces*') |
---|
225 | return ret |
---|
226 | |
---|
227 | def block_test(clis): |
---|
228 | clis[0].block_jabber_buddy(clis[1].nick) |
---|
229 | ret = not msg_comes_thru(clis[1], clis[0], 'm-meow?') |
---|
230 | clis[0].unblock_jabber_buddy(clis[1].nick) |
---|
231 | ret = ret & msg_comes_thru(clis[1], clis[0], '*purrs*') |
---|
232 | return ret |
---|
233 | |
---|
234 | def rename_test(clis): |
---|
235 | newname = "xXx_pup_LINKENPARK4EVA" |
---|
236 | message = "rawr meanmz i luv<3 u in dinosaur" |
---|
237 | |
---|
238 | clis[0].rename_jabber_buddy(clis[1].nick, newname) |
---|
239 | clis[0].send_priv_msg(newname, message) |
---|
240 | ret = clis[1].receive().find(message) != -1 |
---|
241 | |
---|
242 | clis[0].rename_jabber_buddy("-del", newname) |
---|
243 | ret = ret & msg_comes_thru(clis[0], clis[1], "rawr") |
---|
244 | return ret |
---|
245 | |
---|
246 | def status_test(clis): |
---|
247 | status = "get out of my room mom" |
---|
248 | clis[1].send_priv_msg("&bitlbee", "set status '"+status+"'") |
---|
249 | clis[0].send_priv_msg("&bitlbee", "info "+clis[1].nick) |
---|
250 | ret = (clis[0].receive().find("jabber - Status message: "+status) != -1) |
---|
251 | |
---|
252 | clis[1].send_priv_msg("&bitlbee", "set") |
---|
253 | ret = ret & (clis[1].receive().find(status) != -1) |
---|
254 | |
---|
255 | clis[1].send_priv_msg("&bitlbee", "set -del status") |
---|
256 | clis[0].send_priv_msg("&bitlbee", "info "+clis[1].nick) |
---|
257 | ret = ret & (clis[0].receive().find("jabber - Status message: (none)") != -1) |
---|
258 | return ret |
---|
259 | |
---|
260 | def offline_test(clis): |
---|
261 | clis[0].send_priv_msg("&bitlbee", "account off") |
---|
262 | |
---|
263 | junk = clis[0].receive() |
---|
264 | ret = (junk.find(clis[1].nick) != -1) |
---|
265 | ret = ret & (junk.find("QUIT") != -1) |
---|
266 | |
---|
267 | junk = clis[1].receive() |
---|
268 | ret = ret & (junk.find(clis[0].nick) != -1) |
---|
269 | ret = ret & (junk.find("QUIT") != -1) |
---|
270 | |
---|
271 | clis[0].send_priv_msg(clis[1].nick, "i'm not ur mom") |
---|
272 | ret = ret & (clis[0].receive().find("No such nick/channel") != -1) |
---|
273 | |
---|
274 | clis[0].send_priv_msg("&bitlbee", "account on") |
---|
275 | |
---|
276 | junk = clis[0].receive() |
---|
277 | ret = ret & (junk.find(clis[1].nick) != -1) |
---|
278 | ret = ret & (junk.find("JOIN") != -1) |
---|
279 | |
---|
280 | junk = clis[1].receive() |
---|
281 | ret = ret & (junk.find(clis[0].nick) != -1) |
---|
282 | ret = ret & (junk.find("JOIN") != -1) |
---|
283 | |
---|
284 | return ret |
---|
285 | |
---|
286 | def default_target_test(clis): |
---|
287 | clis[0].send_priv_msg("&bitlbee", "set default_target last") |
---|
288 | clis[0].send_priv_msg("&bitlbee", "test2: ur mah default now") |
---|
289 | |
---|
290 | ret = (clis[1].receive().find("ur mah default now") != -1) |
---|
291 | |
---|
292 | clis[0].send_priv_msg("&bitlbee", "mrow") |
---|
293 | ret = ret & (clis[1].receive().find("mrow") != -1) |
---|
294 | |
---|
295 | clis[0].send_priv_msg("root", "set default_target root") |
---|
296 | junk = clis[0].receive() |
---|
297 | ret = ret & (junk.find("default_target") != -1) |
---|
298 | ret = ret & (junk.find("root") != -1) |
---|
299 | |
---|
300 | clis[0].send_priv_msg("&bitlbee", "yes") |
---|
301 | ret = ret & (clis[1].receive().find("yes") == -1) |
---|
302 | return ret |
---|
303 | |
---|
304 | def help_test(clis): |
---|
305 | clis[0].send_priv_msg("&bitlbee", "help") |
---|
306 | ret = (clis[0].receive().find("identify_methods") != -1) |
---|
307 | clis[0].send_priv_msg("&bitlbee", "help commands") |
---|
308 | ret = ret & (clis[0].receive().find("qlist") != -1) |
---|
309 | return ret |
---|