[90417ce] | 1 | import socket, sys, time, select |
---|
| 2 | |
---|
| 3 | class ircClient: |
---|
| 4 | def __init__(self, nick, pw): |
---|
| 5 | self.nick = nick |
---|
| 6 | self.pw = pw |
---|
| 7 | self.log = '' |
---|
| 8 | self.sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
---|
| 9 | |
---|
| 10 | def sendRaw(self, msg): |
---|
| 11 | self.log += msg+'\r\n' |
---|
| 12 | self.sck.send((msg+'\r\n').encode()) |
---|
| 13 | |
---|
| 14 | def sendPrivMsg(self, recip, msg): |
---|
| 15 | self.sendRaw('PRIVMSG '+recip+' :'+msg) |
---|
| 16 | |
---|
| 17 | def connect(self): |
---|
| 18 | self.sck.connect(('127.0.0.1', 6667)) |
---|
| 19 | self.sendRaw('USER ' + (self.nick + " ")*3) |
---|
| 20 | self.sendRaw('NICK ' + self.nick) |
---|
| 21 | self.sendRaw('JOIN &bitlbee') |
---|
| 22 | |
---|
| 23 | def bitlbeeLogin(self): |
---|
| 24 | self.sendPrivMsg("&bitlbee", "account add jabber "+self.nick+"@localhost "+self.pw) |
---|
| 25 | time.sleep(0.3) |
---|
| 26 | self.sendPrivMsg("&bitlbee", "account on") |
---|
| 27 | |
---|
| 28 | def receive(self): |
---|
| 29 | text = '' |
---|
| 30 | while True: |
---|
| 31 | readable, _, _ = select.select([self.sck], [], [], 5) |
---|
| 32 | if self.sck in readable: |
---|
| 33 | text += self.sck.recv(2040).decode() |
---|
| 34 | for line in text.split('\n'): |
---|
| 35 | if line.find('PING') != -1: |
---|
| 36 | self.sendRaw('PONG ' + line.split()[1]) |
---|
| 37 | else: |
---|
| 38 | break |
---|
| 39 | self.log += text |
---|
| 40 | return text |
---|
| 41 | |
---|
| 42 | def runTests(): |
---|
| 43 | clis = [] |
---|
| 44 | clis += [ircClient('test1', 'asd')] |
---|
| 45 | clis += [ircClient('test2', 'asd')] |
---|
| 46 | for cli in clis: |
---|
| 47 | cli.connect() |
---|
| 48 | cli.bitlbeeLogin() |
---|
| 49 | a, b = clis[0], clis[1] |
---|
| 50 | a.sendPrivMsg(b.nick, 'ohai qtie') |
---|
| 51 | a.receive() |
---|
| 52 | b.receive() |
---|
| 53 | if not b.log.find('ohai qtie') == -1: |
---|
| 54 | print('yay!') |
---|
| 55 | else: |
---|
| 56 | print('nay...') |
---|
| 57 | sys.exit(1) |
---|
| 58 | |
---|
| 59 | if __name__ == "__main__": |
---|
| 60 | runTests() |
---|