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].send_priv_msg("&bitlbee", "account list") |
---|
142 | time.sleep(0.5) |
---|
143 | ret = ret & (clis[0].receive().find(clis[1].nick+'@localhost') != -1) |
---|
144 | |
---|
145 | clis[1].send_priv_msg("&bitlbee", "account on") |
---|
146 | clis[1].send_priv_msg("&bitlbee", "account "+clis[1].nick+" del") |
---|
147 | clis[0].receive() |
---|
148 | clis[1].send_priv_msg("&bitlbee", "account list") |
---|
149 | time.sleep(0.5) |
---|
150 | ret = ret & (clis[0].receive().find(clis[1].nick+'@localhost') != -1) |
---|
151 | |
---|
152 | clis[1].send_priv_msg("&bitlbee", "account off") |
---|
153 | clis[1].send_priv_msg("&bitlbee", "account "+clis[1].nick+" del") |
---|
154 | clis[0].receive() |
---|
155 | clis[1].send_priv_msg("&bitlbee", "account list") |
---|
156 | time.sleep(0.5) |
---|
157 | ret = ret & (clis[0].receive().find(clis[1].nick+'@localhost') == -1) |
---|
158 | return ret |
---|
159 | |
---|
160 | def add_buddy_test(clis): |
---|
161 | clis[0].add_jabber_buddy(clis[1].nick) |
---|
162 | clis[1].send_priv_msg("&bitlbee", "yes") |
---|
163 | |
---|
164 | clis[1].add_jabber_buddy(clis[0].nick) |
---|
165 | clis[0].send_priv_msg("&bitlbee", "yes") |
---|
166 | |
---|
167 | clis[0].send_priv_msg("&bitlbee", "blist") |
---|
168 | junk = clis[0].receive() |
---|
169 | ret = junk.find(clis[1].nick) != -1 |
---|
170 | ret = ret & (junk.find("1 available") != -1) |
---|
171 | return ret |
---|
172 | |
---|
173 | def remove_buddy_test(clis): |
---|
174 | ret = add_buddy_test(clis) |
---|
175 | |
---|
176 | clis[0].send_priv_msg("&bitlbee", "remove " +clis[1].nick) |
---|
177 | clis[0].send_priv_msg("&bitlbee", "blist all") |
---|
178 | time.sleep(0.5) |
---|
179 | ret = ret & (clis[0].receive().find(clis[1].nick) == -1) |
---|
180 | |
---|
181 | clis[0].add_jabber_buddy(clis[1].nick) |
---|
182 | clis[1].send_priv_msg("&bitlbee", "yes") |
---|
183 | time.sleep(1) |
---|
184 | clis[0].send_priv_msg("&bitlbee", "blist all") |
---|
185 | time.sleep(0.5) |
---|
186 | junk = clis[0].receive() |
---|
187 | #ret = ret & (junk.find("1 available") != -1) |
---|
188 | ret = ret & (junk.find(clis[1].nick) != -1) |
---|
189 | return ret |
---|
190 | |
---|
191 | def message_test(clis): |
---|
192 | ret = msg_comes_thru(clis[0], clis[1], 'ohai <3') |
---|
193 | ret = ret & msg_comes_thru(clis[1], clis[0], 'uwu *pounces*') |
---|
194 | return ret |
---|
195 | |
---|
196 | def block_test(clis): |
---|
197 | clis[0].block_jabber_buddy(clis[1].nick) |
---|
198 | ret = not msg_comes_thru(clis[1], clis[0], 'm-meow?') |
---|
199 | clis[0].unblock_jabber_buddy(clis[1].nick) |
---|
200 | ret = ret & msg_comes_thru(clis[1], clis[0], '*purrs*') |
---|
201 | return ret |
---|
202 | |
---|
203 | def rename_test(clis): |
---|
204 | newname = "xXx_pup_LINKENPARK4EVA" |
---|
205 | message = "rawr meanmz i luv<3 u in dinosaur" |
---|
206 | |
---|
207 | clis[0].rename_jabber_buddy(clis[1].nick, newname) |
---|
208 | clis[0].send_priv_msg(newname, message) |
---|
209 | ret = clis[1].receive().find(message) != -1 |
---|
210 | |
---|
211 | clis[0].rename_jabber_buddy("-del", newname) |
---|
212 | ret = ret & msg_comes_thru(clis[0], clis[1], "rawr") |
---|
213 | return ret |
---|
214 | |
---|
215 | def status_test(clis): |
---|
216 | status = "get out of my room mom" |
---|
217 | clis[1].send_priv_msg("&bitlbee", "set status '"+status+"'") |
---|
218 | clis[0].send_priv_msg("&bitlbee", "info "+clis[1].nick) |
---|
219 | ret = (clis[0].receive().find("jabber - Status message: "+status) != -1) |
---|
220 | |
---|
221 | clis[1].send_priv_msg("&bitlbee", "set") |
---|
222 | ret = ret & (clis[1].receive().find(status) != -1) |
---|
223 | |
---|
224 | clis[1].send_priv_msg("&bitlbee", "set -del status") |
---|
225 | clis[0].send_priv_msg("&bitlbee", "info "+clis[1].nick) |
---|
226 | ret = ret & (clis[0].receive().find("jabber - Status message: (none)") != -1) |
---|
227 | return ret |
---|
228 | |
---|
229 | def offline_test(clis): |
---|
230 | clis[0].send_priv_msg("&bitlbee", "account off") |
---|
231 | |
---|
232 | junk = clis[0].receive() |
---|
233 | ret = (junk.find(clis[1].nick) != -1) |
---|
234 | ret = ret & (junk.find("QUIT") != -1) |
---|
235 | |
---|
236 | junk = clis[1].receive() |
---|
237 | ret = ret & (junk.find(clis[0].nick) != -1) |
---|
238 | ret = ret & (junk.find("QUIT") != -1) |
---|
239 | |
---|
240 | clis[0].send_priv_msg(clis[1].nick, "i'm not ur mom") |
---|
241 | ret = ret & (clis[0].receive().find("No such nick/channel") != -1) |
---|
242 | |
---|
243 | clis[0].send_priv_msg("&bitlbee", "account on") |
---|
244 | |
---|
245 | junk = clis[0].receive() |
---|
246 | ret = ret & (junk.find(clis[1].nick) != -1) |
---|
247 | ret = ret & (junk.find("JOIN") != -1) |
---|
248 | |
---|
249 | junk = clis[1].receive() |
---|
250 | ret = ret & (junk.find(clis[0].nick) != -1) |
---|
251 | ret = ret & (junk.find("JOIN") != -1) |
---|
252 | |
---|
253 | return ret |
---|
254 | |
---|
255 | def default_target_test(clis): |
---|
256 | clis[0].send_priv_msg("&bitlbee", "set default_target last") |
---|
257 | clis[0].send_priv_msg("&bitlbee", "test2: ur mah default now") |
---|
258 | |
---|
259 | ret = (clis[1].receive().find("ur mah default now") != -1) |
---|
260 | |
---|
261 | clis[0].send_priv_msg("&bitlbee", "mrow") |
---|
262 | ret = ret & (clis[1].receive().find("mrow") != -1) |
---|
263 | |
---|
264 | clis[0].send_priv_msg("root", "set default_target root") |
---|
265 | junk = clis[0].receive() |
---|
266 | ret = ret & (junk.find("default_target") != -1) |
---|
267 | ret = ret & (junk.find("root") != -1) |
---|
268 | |
---|
269 | clis[0].send_priv_msg("&bitlbee", "yes") |
---|
270 | ret = ret & (clis[1].receive().find("yes") == -1) |
---|
271 | return ret |
---|
272 | |
---|
273 | def help_test(clis): |
---|
274 | clis[0].send_priv_msg("&bitlbee", "help") |
---|
275 | ret = (clis[0].receive().find("identify_methods") != -1) |
---|
276 | clis[0].send_priv_msg("&bitlbee", "help commands") |
---|
277 | ret = ret & (clis[0].receive().find("qlist") != -1) |
---|
278 | return ret |
---|