redlandbot
index
/home/crschmidt/rdfpython/trunk/redlandbot.py

ircAsync -- An asynchronous IRC client interface.
 
Originally from
http://dev.w3.org/cvsweb/2000/scribe-bot/ircAsync.py?rev=1.8&content-type=text/x-cvsweb-markup
 
This is intended as a component in a semantic web agent
with several interfaces, one of them being IRC.
It's implemented on top of asyncore so that the same
agent can export an HTTP interface in asynchronous,
non-blocking style.
 
Share and Enjoy. Open Source license:
Copyright (c) 2001 W3C (MIT, INRIA, Keio)
http://www.w3.org/Consortium/Legal/copyright-software-19980720
 
$Id: ircAsync.py,v 1.8 2001/08/24 05:50:52 connolly Exp $
 
##################
 
Originally based off w3c code, modified
by crschmidt for redlandbot purposes, completely refactored
by Nico Chauvat <chauvat@nerim.net> to be easier to read and
more maintainable. Many thanks to Nico!

 
Modules
       
RDF
asynchat
asyncore
cPickle
re
socket
urllib2

 
Classes
       
Bot
asynchat.async_chat(asyncore.dispatcher)
IRCClient

 
class Bot
    RDQL-speaking IRC Bot
 
  Methods defined here:
__init__(self, channels)
Load channels from file, load commands from file, start connecting.
addns(self, m, origin, args, text)
Add a namespace to the common_namespaces set
bye(self, origin, args, text)
good bye
catchall(self, m, origin, args, text)
# A catchall, designed to catch anything starting with ^
command_list(self, m, origin, args, text)
List available commands, or, if asked for information on a specific command, return the RDQL query behind it.
connect(self, host, port)
forget_query(self, m, origin, args, text)
Forget a stored query.
help(self, m, origin, args, text)
Help function for in-channel help with commands.
info(self, m, origin, args, text)
General information function.
join_channel(self, m, origin, args, text)
What to do when told to ^join a channel.
listns(self, m, origin, args, text)
List namespaces available to the bot from the common_namespaces function, sorted alphabetically
load_command_dict(self)
Load command dictionary from the file commandlist.crs.
main_loop(self)
new_query(self, m, origin, args, text)
Add a new query to the command database. Useful for
common queries like mailbox, aim name, etc.
ns(self, m, origin, args, text)
Return the stored URL for a namespace. Useful for creating queries or documents.
on_invite(self, m, origin, args, text)
What to do when invited to a cahnnel.
part_channel(self, m, origin, args, text)
Response to a ^part command.
prenamespaced_query(self, m, origin, args, text)
Uses common namespaces to create a query so that you don't have to worry about bindings.
put(self, m, origin, args, text)
Put/Add is the way to add statements to the Model/Graph. It 
uses URL lib to fetch the URL, and then dumps it.
queries(self, commandname, commandparams, m, origin, args, text)
Run stored queries.
query(self, m, origin, args, text)
Run queries against current RDF store. Queries are in RDQL.
reload_commands(self, m, origin, args, text)
Reloads the stored command dictionary. Only useful if you have two different sources modifing the dictionary for some reason.
reply_to(self, origin, args)
Built in IRC reply to to determine whether we reply to user or channel.
run_command(self, m, origin, args, text)
Runs commands stored via newquery.
save_command_dict(self)
Saves the current command dictionary to a file.
spam(self, m, origin, args, text)
spam is not good
tell(self, origin, args, msg)
Built in tell function so we don't have to always use reply_to in later functions.

 
class IRCClient(asynchat.async_chat)
    Implement part of the IRC protocol on top of async_chat
 
 
Method resolution order:
IRCClient
asynchat.async_chat
asyncore.dispatcher

Methods defined here:
__init__(self)
Set up nickname, channels, other information
bind(self, callback, command, textPat=None, doc=None)
callback is the routine to bind; it's called ala
callback(matchObj or None, origin, args, text)
 
command is one of the commands above, e.g. PRIVMSG
textpat is None or a regex object or string to compile to one
 
doc should be a list of strings; each will go on its own line
collect_incoming_data(self, bytes)
append bytes to buffer
found_terminator(self)
called by asyncore when terminator found in stream
handle_connect(self)
callback on connect
makeConn(self, host, port)
connect to host, port
notice(self, dest, text)
send a NOTICE to dest, a channel or user
process_msg(self, args, text, origin)
process newly received message
startChannels(self, channels)
connect to channels
tell(self, dest, text)
send a PRIVMSG to dest, a channel or user
todo(self, args, *text)
queue command

Methods inherited from asynchat.async_chat:
close_when_done(self)
automatically close this channel once the outgoing queue is empty
discard_buffers(self)
get_terminator(self)
handle_close(self)
handle_read(self)
handle_write(self)
initiate_send(self)
push(self, data)
push_with_producer(self, producer)
readable(self)
predicate for inclusion in the readable for select()
refill_buffer(self)
# refill the outgoing buffer by calling the more() method
# of the first producer in the queue
set_terminator(self, term)
Set the input delimiter.  Can be a fixed string of any length, an integer, or None
writable(self)
predicate for inclusion in the writable for select()

Data and other attributes inherited from asynchat.async_chat:
ac_in_buffer_size = 4096
ac_out_buffer_size = 4096

Methods inherited from asyncore.dispatcher:
__getattr__(self, attr)
# cheap inheritance, used to pass all other attribute
# references to the underlying socket object.
__repr__(self)
accept(self)
add_channel(self, map=None)
close(self)
connect(self, address)
create_socket(self, family, type)
del_channel(self, map=None)
handle_accept(self)
handle_error(self)
handle_expt(self)
handle_expt_event(self)
handle_read_event(self)
handle_write_event(self)
listen(self, num)
log(self, message)
log_info(self, message, type='info')
recv(self, buffer_size)
send(self, data)
set_reuse_addr(self)
set_socket(self, sock, map=None)

Data and other attributes inherited from asyncore.dispatcher:
accepting = 0
addr = None
closing = 0
connected = 0
debug = 0

 
Functions
       
common_namespaces()
debug(*args)
Debugging function. Useful for printing data to stdout. Dump any number of variables or strings in and have them printed out in the bot's STDOUT output
get_storage()
Fetches Redland Storage. If you wish to use something other than MySQL, modify this function.
reply_to(myNick, origin, args)
return nick or target, for use in determinig whether message is private or to channel
run(host, port, chan)
shorten_uri(Node)
Shortens URLs using common namespaces for cleaner IRC output
split_origin(origin)
return (nick, user, host)

 
Data
        CR = '\r'
CRLF = '\r\n'
INVITE = 'INVITE'
JOIN = 'JOIN'
LF = '\n'
NICK = 'NICK'
NOTICE = 'NOTICE'
PART = 'PART'
PING = 'PING'
PONG = 'PONG'
PRIVMSG = 'PRIVMSG'
Port = 6667
QUIT = 'QUIT'
RPL_WELCOME = '001'
SPC = ' '
USER = 'USER'