dr.botzo/modules/Alias.py

83 lines
3.4 KiB
Python
Raw Normal View History

"""
Alias - have internal shortcuts to commands
Copyright (C) 2010 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/>.
"""
from ConfigParser import NoOptionError, NoSectionError
import re
from extlib import irclib
from Module import Module
class Alias(Module):
"""
Allows for commands to be aliased as !command, circumventing bot addressing stuff.
"""
def do(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
"""
See if there is an alias ("!command") in the text, and if so, translate it into
an internal bot command and run it.
"""
# first see if the aliases are being directly manipulated via add/remove
whats = what.split(' ')
try:
if whats[0] == '!alias' and whats[1] == 'add' and len(whats) >= 4:
if not self.config.has_section(self.__class__.__name__):
self.config.add_section(self.__class__.__name__)
self.config.set(self.__class__.__name__, whats[2], ' '.join(whats[3:]))
replystr = 'added alias ' + whats[2]
return self.reply(connection, replypath, replystr)
if whats[0] == '!alias' and whats[1] == 'remove' and len(whats) >= 3:
if not self.config.has_section(self.__class__.__name__):
self.config.add_section(self.__class__.__name__)
if self.config.remove_option(self.__class__.__name__, whats[2]):
replystr = 'removed alias ' + whats[2]
return self.reply(connection, replypath, replystr)
elif whats[0] == '!alias' and whats[1] == 'list':
try:
alist = self.config.options(self.__class__.__name__)
self.remove_metaoptions(alist)
alist.sort()
liststr = ', '.join(alist)
return self.reply(connection, replypath, liststr)
except NoSectionError: pass
except NoOptionError: pass
except NoSectionError: pass
# now that that's over, try doing alias work
try:
alias_list = self.config.options(self.__class__.__name__)
self.remove_metaoptions(alias_list)
for alias in alias_list:
alias_re = re.compile(alias)
if alias_re.match(what):
command = re.sub(alias, self.config.get(self.__class__.__name__, alias), what)
reply = self.try_recursion(connection, event, nick, userhost, None, command, admin_unlocked)
if not reply == command:
return self.reply(connection, replypath, reply)
except NoOptionError: pass
except NoSectionError: pass
# vi:tabstop=4:expandtab:autoindent
# kate: indent-mode python;indent-width 4;replace-tabs on;