http://canonical.org/~kragen/blosxom.cgi is (the current and probably
quite ephemeral URL of) a blog I feed from an IRC channel; here's the
IRC bot that feeds the blog from the IRC channel.  Trivial bit of
code, really (though it could do with some tweaking), but I expect
that the convenience of blogging stuff through GAIM will have a major
effect on what I blog.

#!/usr/bin/python

# blogbot: simple blogging IRC bot.  Produces blog posts via blosxom
# whenever someone utters a URL on a channel where blogbot is
# listening.  Written using Twisted Python, which really rocks.
# This turns GAIM, irssi, Jabber, and suchlike into instant blogging
# and blog-reading tools.
# TODO:
# - some level of authentication
# - better titles when none are suggested by the user --- e.g. page title
# - support for blosxom categorization
# - all-lowercase filenames
# - provide a link to the IRC channel so people can watch the IRC version
#   of the blog in real time.

# config parameters:
# blogbotchannel, near the top
# "localhost", 6667, near the bottom: the IRC server to talk to
# '/home/kragen/hackblogdata', in the middle: the Blosxom data dir

import sys
sys.path.insert(0, '/home/kragen/lib/python')
from twisted.protocols import irc
from twisted.internet import reactor, protocol
import time, re, cgi, os

blogbotchannel = '#blogbot'

class blogbot(irc.IRCClient):
    def __init__(self, **args):
        self.last_timestamp = time.time()
        self.nickname = 'blogbot'
        self.realname = 'kragen+blogbot'
        self.versionName = 'blogbot'
        self.versionNum = '0'
    def signedOn(self):
        self.join(blogbotchannel)
    def noticed(self, user, channel, message): pass
    def privmsg(self, user, channel, message):
        # user looks like [EMAIL PROTECTED]
        user = user[:user.index('!')]
        message = cgi.escape(message)
        htmlre = 'http://[^<>() ]*[^<>() ,.:;]'
        if not re.search(htmlre, message): return
        title = cgi.escape('from %s' % user, quote=1)
        titlemo = re.match('^(.*): ', message)
        if titlemo:
            title = titlemo.group(1)
            message = message[titlemo.end(0):] + ' -%s' % user
        msghtml = re.sub(htmlre,
                         r'<a href="\g<0>">\g<0></a>',
                         message)
        self.say(blogbotchannel,
                 "I'll blog title=%s, msghtml=%s" % (title, msghtml))
        blog(title, msghtml)

def entryfilename(basefilename, serial):
    dirname = '/home/kragen/hackblogdata'
    return os.path.join(dirname, basefilename + serial + '.txt')

def blog(title, msghtml):
    basefilename = re.sub('[^a-zA-Z0-9]', '', title)
    serial = ''
    while os.path.exists(entryfilename(basefilename, serial)):
        if serial == '': serial = '0'
        serial = str(int(serial) + 1)
    wr = open(entryfilename(basefilename, serial), 'w')
    wr.write("%s\n%s\n" % (title, msghtml))
    wr.close()

class blogbotfactory(protocol.ClientFactory):
    protocol = blogbot
    def clientConnectionLost(self, connector, reason): connector.connect()
    def clientConnectionFailed(self, connector, reason): reactor.stop()

def main():
    irccf = blogbotfactory()
    reactor.connectTCP("localhost", 6667, irccf)
    reactor.run()

if __name__ == "__main__": main()


-- 
<[EMAIL PROTECTED]>       Kragen Sitaker     <http://www.pobox.com/~kragen/>
Edsger Wybe Dijkstra died in August of 2002.  The world has lost a great
man.  See http://advogato.org/person/raph/diary.html?start=252 and
http://www.kode-fu.com/geek/2002_08_04_archive.shtml for details.

Reply via email to