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 | class IrcClient: |
---|
18 | def __init__(self, nick, pwd): |
---|
19 | self.nick = nick |
---|
20 | self.pwd = pwd |
---|
21 | self.log = '' |
---|
22 | self.tmplog = '' |
---|
23 | self.sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
---|
24 | |
---|
25 | def send_raw(self, msg, loud = False, log = True): |
---|
26 | self.receive() |
---|
27 | if loud: |
---|
28 | print('FROM '+ self.nick + '|| ' + msg) |
---|
29 | if log: |
---|
30 | self.log += msg+'\r\n' |
---|
31 | self.tmplog += msg+'\r\n' |
---|
32 | self.sck.send((msg+'\r\n').encode()) |
---|
33 | |
---|
34 | def send_priv_msg(self, recip, msg, loud = False): |
---|
35 | self.send_raw('PRIVMSG '+recip+' :'+msg, loud) |
---|
36 | |
---|
37 | def connect(self): |
---|
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: |
---|
48 | return False |
---|
49 | |
---|
50 | self.send_raw('USER ' + (self.nick + " ")*3) |
---|
51 | self.send_raw('NICK ' + self.nick) |
---|
52 | self.send_raw('JOIN &bitlbee') |
---|
53 | return True |
---|
54 | |
---|
55 | def jabber_login(self): |
---|
56 | self.send_priv_msg("&bitlbee", "account add jabber "+self.nick+"@localhost "+self.pwd) |
---|
57 | time.sleep(0.3) |
---|
58 | self.send_priv_msg("&bitlbee", "account on") |
---|
59 | time.sleep(1) |
---|
60 | self.receive() |
---|
61 | return (self.log.find('Logged in') != -1) |
---|
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: |
---|
71 | self.send_raw('PONG ' + line.split()[1]) |
---|
72 | else: |
---|
73 | break |
---|
74 | self.log += text |
---|
75 | self.tmplog += text |
---|
76 | return text |
---|
77 | |
---|
78 | def add_jabber_buddy(self, nick): |
---|
79 | self.send_priv_msg("&bitlbee", "add 0 " + nick+"@localhost") |
---|
80 | |
---|
81 | def block_jabber_buddy(self, nick): |
---|
82 | self.send_priv_msg("&bitlbee", "block " + nick) |
---|
83 | |
---|
84 | def unblock_jabber_buddy(self, nick): |
---|
85 | self.send_priv_msg("&bitlbee", "allow " + nick) |
---|
86 | |
---|
87 | def rename_jabber_buddy(self, oldnick, newnick): |
---|
88 | self.send_priv_msg("&bitlbee", "rename " + oldnick + " " + newnick) |
---|
89 | |
---|
90 | def msg_comes_thru(sender, receiver, message): |
---|
91 | sender.send_priv_msg(receiver.nick, message) |
---|
92 | received = receiver.receive().find(message) != -1 |
---|
93 | return received |
---|
94 | |
---|
95 | def perform_test(test_function): |
---|
96 | clis = [] |
---|
97 | clis += [IrcClient('test1', 'asd')] |
---|
98 | clis += [IrcClient('test2', 'asd')] |
---|
99 | |
---|
100 | fail = not test_function(clis) |
---|
101 | |
---|
102 | for cli in clis: |
---|
103 | cli.receive() |
---|
104 | print(SEPARATOR) |
---|
105 | print("Test Log "+ cli.nick+":") |
---|
106 | print(cli.tmplog) |
---|
107 | print(SEPARATOR) |
---|
108 | |
---|
109 | if fail: |
---|
110 | sys.exit(1) |
---|
111 | |
---|
112 | def connect_test(clis): |
---|
113 | ret = True |
---|
114 | for cli in clis: |
---|
115 | ret = ret & cli.connect() |
---|
116 | return ret |
---|
117 | |
---|
118 | def yes_test(clis): |
---|
119 | ret = False |
---|
120 | clis[0].send_priv_msg("&bitlbee", "yes") |
---|
121 | clis[0].receive() |
---|
122 | for x, fun in enumerate(FUN): |
---|
123 | if (clis[0].log.find(fun) != -1): |
---|
124 | ret = True |
---|
125 | if x: |
---|
126 | print("The RNG gods smile upon us") |
---|
127 | break |
---|
128 | return ret |
---|
129 | |
---|
130 | def jabber_login_test(clis): |
---|
131 | ret = True |
---|
132 | for cli in clis: |
---|
133 | ret = ret & cli.jabber_login() |
---|
134 | return ret |
---|
135 | |
---|
136 | def add_buddy_test(clis): |
---|
137 | clis[0].add_jabber_buddy(clis[1].nick) |
---|
138 | clis[1].send_priv_msg("&bitlbee", "yes") |
---|
139 | |
---|
140 | clis[1].add_jabber_buddy(clis[0].nick) |
---|
141 | clis[0].send_priv_msg("&bitlbee", "yes") |
---|
142 | |
---|
143 | clis[0].send_priv_msg("&bitlbee", "blist") |
---|
144 | junk = clis[0].receive() |
---|
145 | ret = junk.find(clis[1].nick) != -1 |
---|
146 | ret = ret & (junk.find("1 available") != -1) |
---|
147 | ''' |
---|
148 | clis[0].send_priv_msg("&bitlbee", "remove " +clis[1].nick) |
---|
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") |
---|
154 | time.sleep(1) |
---|
155 | clis[0].send_priv_msg("&bitlbee", "blist") |
---|
156 | junk = clis[0].receive() |
---|
157 | ret = ret & (junk.find("1 available") != -1) |
---|
158 | ret = ret & (junk.find(clis[1].nick) != -1) |
---|
159 | ''' |
---|
160 | return ret |
---|
161 | |
---|
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 |
---|
185 | |
---|
186 | def status_test(clis): |
---|
187 | status = "get out of my room mom" |
---|
188 | clis[1].send_priv_msg("&bitlbee", "set status '"+status+"'") |
---|
189 | clis[0].send_priv_msg("&bitlbee", "info "+clis[1].nick) |
---|
190 | ret = (clis[0].receive().find("jabber - Status message: "+status) != -1) |
---|
191 | |
---|
192 | clis[1].send_priv_msg("&bitlbee", "set") |
---|
193 | ret = ret & (clis[1].receive().find(status) != -1) |
---|
194 | |
---|
195 | clis[1].send_priv_msg("&bitlbee", "set -del status") |
---|
196 | clis[0].send_priv_msg("&bitlbee", "info "+clis[1].nick) |
---|
197 | ret = ret & (clis[0].receive().find("jabber - Status message: (none)") != -1) |
---|
198 | return ret |
---|
199 | |
---|
200 | def offline_test(clis): |
---|
201 | clis[0].send_priv_msg("&bitlbee", "account off") |
---|
202 | |
---|
203 | junk = clis[0].receive() |
---|
204 | ret = (junk.find(clis[1].nick) != -1) |
---|
205 | ret = ret & (junk.find("QUIT") != -1) |
---|
206 | |
---|
207 | junk = clis[1].receive() |
---|
208 | ret = ret & (junk.find(clis[0].nick) != -1) |
---|
209 | ret = ret & (junk.find("QUIT") != -1) |
---|
210 | |
---|
211 | clis[0].send_priv_msg(clis[1].nick, "i'm not ur mom") |
---|
212 | ret = ret & (clis[0].receive().find("No such nick/channel") != -1) |
---|
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) |
---|
218 | ret = ret & (junk.find("JOIN") != -1) |
---|
219 | |
---|
220 | junk = clis[1].receive() |
---|
221 | ret = ret & (junk.find(clis[0].nick) != -1) |
---|
222 | ret = ret & (junk.find("JOIN") != -1) |
---|
223 | |
---|
224 | return ret |
---|
225 | |
---|
226 | def default_target_test(clis): |
---|
227 | clis[0].send_priv_msg("&bitlbee", "set default_target last") |
---|
228 | clis[0].send_priv_msg("&bitlbee", "test2: ur mah default now") |
---|
229 | |
---|
230 | ret = (clis[1].receive().find("ur mah default now") != -1) |
---|
231 | |
---|
232 | clis[0].send_priv_msg("&bitlbee", "mrow") |
---|
233 | ret = ret & (clis[1].receive().find("mrow") != -1) |
---|
234 | |
---|
235 | clis[0].send_priv_msg("root", "set default_target root") |
---|
236 | junk = clis[0].receive() |
---|
237 | ret = ret & (junk.find("default_target") != -1) |
---|
238 | ret = ret & (junk.find("root") != -1) |
---|
239 | |
---|
240 | clis[0].send_priv_msg("&bitlbee", "yes") |
---|
241 | ret = ret & (clis[1].receive().find("yes") == -1) |
---|
242 | return ret |
---|
243 | |
---|
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 |
---|