Skip to content

Instantly share code, notes, and snippets.

@KyeRussell
Created July 1, 2011 01:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KyeRussell/1057683 to your computer and use it in GitHub Desktop.
Save KyeRussell/1057683 to your computer and use it in GitHub Desktop.
Python IRC Environment
class IRCChannel (object):
""" Simulate an IRC channel environment """
def __init__ (self, chan_name, topic):
self.chan_name = chan_name # Channel name
self.topic = topic # Channel topic
self.users = [] # User list
# Prints joining messages
self.system_msg("You have joined " + self.chan_name)
self.system_msg("Topic for " + self.chan_name + " is: " + self.topic)
def system_msg (self, msg):
print (">>> " + msg)
def say (self, user, msg):
"""Simulate a user saying something in IRC"""
print("<" + user.get_nick() + '> ' + msg + "\n")
def usercount (self):
return len(self.users)
def new_client (self, ircuser_obj):
"""
Add a new client to the channel
Takes an IRCUser object, and called by IRCUser on init.
"""
self.users.append(ircuser_obj)
self.system_msg(ircuser_obj.get_nick() + " (" + ircuser_obj.get_hostname('userhost') + ")" + " has joined " + self.chan_name)
class IRCUser (object):
def __init__(self, name, user, host, chan):
self.nick = name
self.user = user
self.host = host
self.chan = chan
chan.new_client(self)
def get_hostname (self, host_type):
"""
Return the current hostname of the user.
Arguments:
host_type - define the type of hostname that'll be returned.
full - ident!user@host
userhost - user@host
"""
if (host_type == 'full'):
return self.nick + "!" + self.user + "@" + self.host
elif (host_type == 'userhost'):
return self.user + "@" + self.host
def say (self, msg):
self.chan.say(self, msg)
def get_nick (self):
return self.nick
def set_nick (self, newvar):
self.nick = newvar
def __str__(self):
return self.nick
chats = IRCChannel('#chats', 'Premium Chats!')
psycho = IRCUser('Psycho', 'fat', 'fathost.fatisp.america.com', chats)
psycho.say('im totally fat.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment