def connect(self): """Open a connection to dAmn!""" if self.sock and self.sock.sock: return self.sock = Connection(self.CONST.SERVER, self.CONST.CLIENT, self.CONST.PORT) self.sock.connect() self.flag.connecting = self.sock.connected() return self.flag.connecting
class dAmnSock: """ dAmnSock class. This is a base class for managing a connection to a dAmn server. """ class platform: """Information about the dAmn Viper platform.""" name = 'dAmn Viper' version = 2 state = 'RC' build = 52 stamp = '20022011-002000' series = 'Swift' author = 'photofroggy' class user: """User login data.""" username = None password = None cookie = None token = None class flag: """Status flags are stored here""" connecting = False shaking = False loggingin = False connected = False class CONST: """dAmnSock style 'constants'.""" SERVER = 'chat.deviantart.com' CLIENT = 'dAmnClient 0.3' PORT = 3900 class session: status = False cookie = None token = None extras = {'remember_me':'1'} agent = 'dAmnViper (python 3.x) dAmnSock/1.1' sock = None def nullflags(self): self.flag.connecting = False self.flag.shaking = False self.flag.loggingin = False self.flag.connected = False def logger(self, ns, msg, showns=True, mute=False, pkt=None): if mute: return stamp = time.strftime('%H:%M:%S|') sys.stdout.write('>> {0}{1} {2}\n'.format(stamp, (ns + '|' if showns else ''), msg)) sys.stdout.flush() def get_token(self): self.on_get_token() if not self.user.password: return self.session = Login(self.user.username, self.user.password, self.extras, self.agent) if self.session.status[0] == 1: self.user.cookie = self.session.cookie self.user.token = self.session.token self.user.password = None self.on_token() def on_get_token(self): """Overwrite this method to do shit when get_token is called.""" pass def on_token(self): """Overwrite this method to do stuff after get_token is finished.""" pass def connect(self): """Open a connection to dAmn!""" if self.sock and self.sock.sock: return self.sock = Connection(self.CONST.SERVER, self.CONST.CLIENT, self.CONST.PORT) self.sock.connect() self.flag.connecting = self.sock.connected() return self.flag.connecting def handshake(self, vals=None): # Should be able to define values sent in the handshake packet. if not self.flag.connecting: return False self.flag.shaking = True # Raise the flag, hoe. shake = self.CONST.CLIENT shake+= '\nagent='+self.agent if vals == None: shake+= '\n' else: shake+= "\n{0}\n".format("\n".join(['='.join([key, vals[key]]) for key in vals])) if len(shake+'\0') == self.send(shake): return True return False def login(self): # Send our login packet. Set the loggingin flag to true. self.flag.loggingin = True return bool(self.send('login {0}\npk={1}\n'.format(self.user.username, self.user.token))) def send(self, data): # Send data to dAmn! if self.sock == None or not self.sock.connected(): return 0 return self.sock.send(data) def get_packet(self): # Return a single packet from the queue. if not self.sock.connected(): self.nullflags() return None return self.sock.get_packet() def close(self): # This is how we close our connection! if self.sock and self.sock.connected(): self.sock.close() # PROTOCOL! # The methods below pretty much define the protocol for outgoing packets. def raw(self, data): # Send a raw dAmn packet. return self.send(data) def pong(self): # dAmn likes to play ping pong... return self.send('pong\n') def join(self, ns): # Send a join packet to dAmn. return self.send('join {0}\n'.format(ns)) def part(self, ns): # Send a part packet to dAmn return self.send('part {0}\n'.format(ns)) def say(self, ns, message): # Send a message to a dAmn channel namespace. return self.send('send {0}\n\nmsg main\n\n{1}'.format(ns, str(message))) def action(self, ns, message): # Send an action to a dAmn channel namespace. return self.send('send {0}\n\naction main\n\n{1}'.format(ns, str(message))) def me(self, ns, message): # This is just another way to do an action. return self.action(ns, message) def npmsg(self, ns, message): # Send a non-parsed message to a dAmn channel namespace. return self.send('send {0}\n\nnpmsg main\n\n{1}'.format(ns, str(message))) def promote(self, ns, user, pc=None): # Promote a user in a dAmn channel. return self.send('send {0}\n\npromote {1}\n{2}'.format(ns, user, '' if pc == None else '\n'+str(pc))) def demote(self, ns, user, pc=None): # Demote a user in a dAmn channel. return self.send('send {0}\n\ndemote {1}\n{2}'.format(ns, user, '' if pc == None else '\n'+str(pc))) def kick(self, ns, user, r=None): # Kick a user out of a dAmn channel. return self.send('kick {0}\nu={1}\n{2}'.format(ns, user, '' if r == None else '\n'+str(r))) def ban(self, ns, user): # Ban a user from a dAmn channel. return self.send('send {0}\n\nban {1}\n'.format(ns, user)) def unban(self, ns, user): # Unban someone from a dAmn channel. return self.send('send {0}\n\nunban {1}\n'.format(ns, user)) def get(self, ns, p): # Get a property for a dAmn channel. return self.send('get {0}\np={1}\n'.format(ns, p)) def set(self, ns, p, val): # Set a property for a dAmn channel. return self.send('set {0}\np={1}\n\n{2}'.format(ns, p, val)) def admin(self, ns, command): # Send an admin command to a dAmn channel. No need for multiple methods here. return self.send('send {0}\n\nadmin\n\n{1}'.format(ns, command)) def disconnect(self): # Send a disconnect packet to the dAmn server. return self.send('disconnect\n') def kill(self, ns, r=None): # Send a kill packet to the dAmn server. return self.send('kill {0}\n{1}'.format(ns, '' if r == None else '\n'+str(r))) # END PROTOCOL METHODS def parser(self): # Just a shortcut for getting the Packet class. return Packet