Small IRC python module
Example, echo messages on channel :
from irc import IRC
import logging
def handler_privmsg(irc, msg):
if msg['args'][0] == '#mychannel':
irc.privmsg('#mychannel', msg['content'])
if __name__ == '__main__':
irc = IRC(user='t0x0shBot',
nick='t0x0shBot',
host='irc.root-me.org',
port=6667,
channels=['#mychannel'],
log_level=logging.INFO)
irc.set_handler('PRIVMSG', handler_privmsg)
irc.loop()
class IRC
-
__init__(self, user, nick, host, port, channels, log_level)
: Constructor-
user : IRC user (type str)
-
nick : IRC nick (type str)
-
host : server host (type str)
-
port : server port (type int)
-
channels : list of channels to join (type str list)
-
log_level : level of logging (constants logging.DEBUG, logging.WARNING, etc...)
-
send(self, msg)
: Send an IRC msg- msg : message to send without "\r\n" (type str)
-
privmsg(self, to, msg)
: Send a PRIVMSG to channel or nick-
to : channel or nick to send (type str)
-
msg : message to send (type str)
-
-
set_handler(self, cmd, handler)
: Add event handler when handling IRC command-
cmd : the command triggering the handler (type str)
-
handler : the function handler (type function(IRC, dict))
The second parameter is the parsed IRC message, viewed as a dictionnary. It has the following fields :
-
full : the raw message (type str)
-
host : the host from where the message come from (type str)
-
cmd : the IRC command (PRIVMSG, MODE, JOIN, etc) (type str)
-
args : arguments of the command (type str)
-
content : content of the message (type str)
-
-
set_timer(self, handler, seconds)
: Add function to be executed every X seconds-
handler : the function to be called (type function(IRC))
-
seconds : number of seconds between each call (type int)
-
-
loop(self)
: Infinite loop, handling commands and timers.
-
-
author : Tosh
-
license : GPL