Created
March 28, 2014 00:46
-
-
Save Tehnix/9822583 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/user/bin/env python3.4 | |
| import socket | |
| HOST = 'irc.codetalk.io' | |
| PORT = 6667 | |
| NICK = 'pybot-test' | |
| CHAN = '#lobby-test' | |
| def connect(): | |
| sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
| sock.connect((socket.gethostbyname(HOST), PORT)) | |
| return sock | |
| def sendall(sock, data): | |
| try: | |
| data = bytes(data + '\r\n', 'UTF-8') | |
| sock.sendall(data) | |
| except TypeError: # General handling of failed encoding | |
| print("\n[WARNING] : Failed to encode unicode!\n") | |
| def recv(sock, lenght=4096): | |
| data = sock.recv(4096) | |
| try: | |
| data = str(data, encoding='UTF-8') | |
| return data | |
| except UnicodeDecodeError: # General handling of failed decoding | |
| print("\n[WARNING] : Failed to decode bytes!\n") | |
| except TypeError: | |
| print("\n[WARNING] : Failed to decode bytes!\n") | |
| return '' | |
| def ident(sock): | |
| sendall(sock, 'NICK ' + NICK + '\r\n') | |
| sendall(sock, 'USER ' + NICK + ' 0 * :' + NICK + '\r\n') | |
| def parse(sock, msg): | |
| if msg.find('266') != -1: | |
| sendall(sock, 'JOIN ' + CHAN) | |
| elif msg.find('451') != -1: | |
| ident(sock) | |
| elif msg.startswith('PING'): | |
| sendall(sock, 'PONG :' + msg.split(':')[1]) | |
| def listen(sock): | |
| msg = '' | |
| while True: | |
| chunk = recv(sock).split('\r\n') | |
| for msg in chunk: | |
| print(msg) | |
| parse(sock, msg) | |
| sock = connect() | |
| ident(sock) | |
| listen(sock) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment