Created
February 7, 2012 20:01
-
-
Save Ivoz/1761559 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
class Msg(object): | |
""" Represents an IRC message to be sent or decoded """ | |
def __init__(self, prefix='', cmd='', params=[]): | |
self.prefix = prefix | |
self.cmd = cmd | |
self.params = [params] if (type(params) != list) else params | |
@classmethod | |
def from_msg(cls, msg): | |
new = Msg() | |
#if '004' in msg: | |
#import ipdb; ipdb.set_trace() ### XXX BREAKPOINT | |
new.decode(msg) | |
return new | |
def decode(self, msg): | |
print msg | |
if msg.startswith(DELIM): | |
self.prefix, msg = msg[1:].split(SPACE, 1) | |
msg = msg.lstrip(SPACE) | |
self.cmd, msg = msg.split(SPACE, 1) | |
if self.cmd in replycodes: | |
self.cmd = replycodes[self.cmd] | |
while (len(msg) > 0): | |
msg = msg.lstrip(SPACE) | |
if msg.startswith(DELIM): | |
self.params.append(msg[1:]) | |
break | |
p, msg = msg.split(SPACE, 1) | |
self.params.append(p) | |
def encode(self): | |
msg = '' | |
if (len(self.prefix) > 0): | |
msg += DELIM + self.prefix + SPACE | |
msg += self.cmd | |
for p in self.params: | |
msg += SPACE | |
if SPACE in p: | |
msg += DELIM + p | |
break | |
else: | |
msg += p | |
return msg | |
def __repr__(self): | |
return self.encode() | |
def __str__(self): | |
return self.encode() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment