diff options
| author | 2013-04-19 14:15:32 -0400 | |
|---|---|---|
| committer | 2013-04-19 14:15:32 -0400 | |
| commit | 29b0cac9281439ff7c8d63559a5aaadaebc45441 (patch) | |
| tree | 4938617417d544dc66b0b9fc2e13c616e8b84cde | |
| parent | 23d50e6e408d22921105d68507cf5ae8c392096a (diff) | |
| download | pyrc-29b0cac9281439ff7c8d63559a5aaadaebc45441.tar.gz pyrc-29b0cac9281439ff7c8d63559a5aaadaebc45441.tar.bz2 pyrc-29b0cac9281439ff7c8d63559a5aaadaebc45441.zip | |
Refactor and rename base class.
| -rw-r--r-- | PyRC.py | 48 |
1 files changed, 28 insertions, 20 deletions
@@ -30,13 +30,15 @@ A module to connect to and interact with an Internet Relay Chat server. """ +import errno import select import socket import string import sys + class Client: - def __init__(self, (host, port, hostpass)): + def __init__(self): try: self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.s.setblocking(0) @@ -46,20 +48,25 @@ class Client: print 'Could not open socket.' sys.exit(1) - self.host = host - self.port = port - self.hostpass = hostpass + self.host = self.port = self.hostpass = '' self.send_buf = '' self.recv_buf = '' - def connect(self, (nick, ident, realname)): - """connect((nick, ident, realname)) + def connect(self, host, port=6667, hostpass="", nick="PyRC", + ident="PyRC", realname="PyRC Bot"): + """connect(host, port, hostpass, nick, ident, realname) + +Connects to the host on the specified port, optionally with a host +password, then attempts to use the specified nick, ident, and realname.""" + + self.host = host + self.port = port + self.hostpass = hostpass - Connects to the specified host.""" try: self.s.connect((self.host, self.port)) except socket.error, msg: - if (msg[0] == 115): + if (msg[0] == errno.EINPROGRESS): # errno 115 pass else: self.s.close() @@ -78,7 +85,8 @@ class Client: self.s.send('USER %s %s %s :%s\r\n' % (nick, socket.gethostname(), self.host, realname)) connected = True except socket.error, msg: - if msg[0] == 11 or msg[0] == 115: + if msg[0] == errno.EAGAIN or msg[0] == errno.EINPROGRESS: + # Sending would cause the socket to block. Keep trying. pass else: self.s.close() @@ -99,10 +107,10 @@ class Client: self.s.send('QUIT :%s\r\n' % reason) connected = False else: - self.s.send('QUIT :PyRC. http://www.pyrc.org\r\n') + self.s.send('QUIT :PyRC. https://git.rmky.org\r\n') connected = False except socket.error, msg: - if msg[0] == 11: + if msg[0] == errno.EAGAIN or msg[0] == errno.EINPROGRESS: pass else: self.s.close() @@ -118,7 +126,7 @@ class Client: try: self.s.send('JOIN %s %s\r\n' % (channel, key)) except socket.error, msg: - if msg[0] == 11: + if msg[0] == errno.EAGAIN or msg[0] == errno.EINPROGRESS: pass else: self.s.close() @@ -134,7 +142,7 @@ class Client: try: self.s.send('PART %s\r\n' % channel) except socket.error, msg: - if msg[0] == 11: + if msg[0] == errno.EAGAIN or msg[0] == errno.EINPROGRESS: pass else: self.s.close() @@ -150,7 +158,7 @@ class Client: try: self.s.send('PRIVMSG %s :%s\r\n' % (dest, message)) except socket.error, msg: - if msg[0] == 11: + if msg[0] == errno.EAGAIN or msg[0] == errno.EINPROGRESS: pass else: self.s.close() @@ -166,7 +174,7 @@ class Client: try: self.s.send('NOTICE %s :%s\r\n' % (dest, message)) except socket.error, msg: - if msg[0] == 11: + if msg[0] == errno.EAGAIN or msg[0] == errno.EINPROGRESS: pass else: self.s.close() @@ -182,7 +190,7 @@ class Client: try: self.s.send('INVITE %s %s\r\n' % (dest, channel)) except socket.error, msg: - if msg[0] == 11: + if msg[0] == errno.EAGAIN or msg[0] == errno.EINPROGRESS: pass else: self.s.close() @@ -191,14 +199,14 @@ class Client: print "Could not send to %s %s" % (self.host, self.port) sys.exit(1) - def nick(self, nickname): + def change_nick(self, nickname): """nick(nickname) Changes nickname to the specified nickname.""" try: self.s.send('NICK %s\r\n' % nickname) except socket.error, msg: - if msg[0] == 11: + if msg[0] == errno.EAGAIN or msg[0] == errno.EINPROGRESS: pass else: self.s.close() @@ -214,7 +222,7 @@ class Client: try: self.s.send('PONG %s\r\n' % dest) except socket.error, msg: - if msg[0] == 11: + if msg[0] == errno.EAGAIN or msg[0] == errno.EINPROGRESS: pass else: self.s.close() @@ -233,7 +241,7 @@ class Client: while 1: tempbuff += self.s.recv(512) except socket.error, msg: - if msg[0] == 11: + if msg[0] == errno.EAGAIN or msg[0] == errno.EINPROGRESS: self.recv_buf += tempbuff else: self.s.close() |