move DrBotIRC into a separate file, since we will probably be hacking more on it soon

This commit is contained in:
Brian S. Stephan 2011-01-07 09:54:51 -06:00
parent 35419f9db9
commit 4d6228b93a
2 changed files with 68 additions and 41 deletions

66
DrBotIRC.py Normal file
View File

@ -0,0 +1,66 @@
"""
DrBotIRC - customizations of irclib, for dr.botzo
Copyright (C) 2011 Brian S. Stephan
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import socket
from extlib import irclib
class DrBotServerConnection(irclib.ServerConnection):
"""Subclass irclib's ServerConnection, in order to expand privmsg."""
def privmsg(self, target, text):
"""Send a PRIVMSG command."""
splitter = "..."
# split messages that are too long. Max length is 512.
# TODO: this does not properly handle when the hostname has been
# masked by the ircd
space = 512 - len('\r\n') - len('PRIVMSG ') - len(' :') - len(target) - len(self.nickname) - len('!') - len(self.username) - len('@') - len(socket.getfqdn())
splitspace = space - (len(splitter) + 1)
if len(text) > space:
times = 1
while len(text) > splitspace:
splitpos = text.rfind(' ', 0, splitspace)
splittext = text[0:splitpos] + ' ' + splitter
text = splitter + ' ' + text[splitpos+1:]
self.send_raw("PRIVMSG %s :%s" % (target, splittext))
times = times + 1
if times >= 4:
return
# done splitting
self.send_raw("PRIVMSG %s :%s" % (target, text))
else:
self.send_raw("PRIVMSG %s :%s" % (target, text))
class DrBotIRC(irclib.IRC):
"""Subclass irclib's IRC, in order to create a DrBotServerConnection."""
def server(self):
c = DrBotServerConnection(self)
self.connections.append(c)
return c
# vi:tabstop=4:expandtab:autoindent
# kate: indent-mode python;indent-width 4;replace-tabs on;

View File

@ -19,56 +19,17 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
from ConfigParser import ConfigParser, NoSectionError, NoOptionError from ConfigParser import ConfigParser, NoSectionError, NoOptionError
import os import os
import re import re
import socket
import sys import sys
import inspect import inspect
import sqlite3 import sqlite3
import DrBotIRC
from extlib import irclib from extlib import irclib
modlist = [] modlist = []
moduleList = [ "Countdown", "Dice", "IrcAdmin", "GoogleTranslate", "Seen", "FactFile" ] moduleList = [ "Countdown", "Dice", "IrcAdmin", "GoogleTranslate", "Seen", "FactFile" ]
modObjs = [] modObjs = []
class DrBotServerConnection(irclib.ServerConnection):
"""Subclass irclib's ServerConnection, in order to expand privmsg."""
def privmsg(self, target, text):
"""Send a PRIVMSG command."""
splitter = "..."
# split messages that are too long. Max length is 512.
# TODO: this does not properly handle when the hostname has been
# masked by the ircd
space = 512 - len('\r\n') - len('PRIVMSG ') - len(' :') - len(target) - len(self.nickname) - len('!') - len(self.username) - len('@') - len(socket.getfqdn())
splitspace = space - (len(splitter) + 1)
if len(text) > space:
times = 1
while len(text) > splitspace:
splitpos = text.rfind(' ', 0, splitspace)
splittext = text[0:splitpos] + ' ' + splitter
text = splitter + ' ' + text[splitpos+1:]
self.send_raw("PRIVMSG %s :%s" % (target, splittext))
times = times + 1
if times >= 4:
return
# done splitting
self.send_raw("PRIVMSG %s :%s" % (target, text))
else:
self.send_raw("PRIVMSG %s :%s" % (target, text))
class DrBotIRC(irclib.IRC):
"""Subclass irclib's IRC, in order to create a DrBotServerConnection."""
def server(self):
c = DrBotServerConnection(self)
self.connections.append(c)
return c
config_file = 'dr.botzo.cfg' config_file = 'dr.botzo.cfg'
# check argv # check argv
if len(sys.argv) == 2: if len(sys.argv) == 2:
@ -125,7 +86,7 @@ except NoSectionError: pass # the database doesn't need to exist
# start up the IRC bot # start up the IRC bot
# create IRC and server objects and connect # create IRC and server objects and connect
irc = DrBotIRC() irc = DrBotIRC.DrBotIRC()
server = irc.server().connect(botserver, botport, botnick, botpass, botuser, botircname) server = irc.server().connect(botserver, botport, botnick, botpass, botuser, botircname)
# load features # load features