"""
Module - dr.botzo modular functionality base class
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 NoSectionError, NoOptionError

import inspect
import re
import sys
import sqlite3
from threading import Timer

from extlib import irclib

class Module(object):
    """Declare a base class used for creating classes that have real functionality."""

    def priority(self):
        return 50

    def timer_interval(self):
        """Run a timer-based event every N seconds (0 to disable)."""

        return 0

    def __init__(self, irc, config, server):
        """Construct a feature module. Inheritors can do special things here, but
        should be sure to call Module.__init__.
        """

        self.irc = irc
        self.config = config
        self.server = server

        self.is_shutdown = False

        # open database connection
        dbfile = self.config.get('dr.botzo', 'database')
        self.conn = sqlite3.connect(dbfile)
        self.conn.row_factory = sqlite3.Row

        # setup regexp function in sqlite
        def regexp(expr, item):
            reg = re.compile(expr, re.IGNORECASE)
            return reg.search(item) is not None
        self.conn.create_function('REGEXP', 2, regexp)

        # set up database for this module
        self.db_init()

        # register the timer, if the implementing module has one
        if self.timer_interval() > 0:
            Timer(self.timer_interval(), self.timer_bootstrap, ()).start()

        # print what was loaded, for debugging
        print("loaded " + self.__class__.__name__)

    def register_handlers(self):
        """Hook handler functions into the IRC library. This is called when the
        module is loaded. Classes with special stuff to do could implement this
        and set up the appropriate handlers, e.g.:

        self.server.add_global_handler('welcome', self.on_connect)

        By default, a module attaches to pubmsg/privmsg, which sets up some common
        variables and then calls do(). You are free to implement do() (see below),
        or override this and do whatever you want.
        """

        self.server.add_global_handler('pubmsg', self.on_pub_or_privmsg, self.priority())
        self.server.add_global_handler('privmsg', self.on_pub_or_privmsg, self.priority())

    def unregister_handlers(self):
        """Unhook handler functions from the IRC library. Inverse of the above.
        This is called by reload, to remove the soon-to-be old object from the server
        global handlers (or whatever has been added via register_handlers). Classes
        inheriting from Module could reimplement this, e.g.:

        self.server.remove_global_handler('welcome', self.on_connect)
        """

        self.server.remove_global_handler('pubmsg', self.on_pub_or_privmsg)
        self.server.remove_global_handler('privmsg', self.on_pub_or_privmsg)

    def on_pub_or_privmsg(self, connection, event):
        """Do a default thing on a pubmsg or privmsg.

        Sets up a couple variables and then calls do(), which by default we
        expect implementers to implement.
        """

        nick = irclib.nm_to_n(event.source())
        userhost = irclib.nm_to_uh(event.source())
        what = event.arguments()[0]
        admin_unlocked = False

        try:
            if userhost == self.config.get('dr.botzo', 'admin_userhost'):
                admin_unlocked = True
        except NoOptionError: pass

        return self.do(connection, event, nick, userhost, what, admin_unlocked)

    def reply(self, connection, event, replystr, stop_responding=False):
        """Reply over IRC to replypath or return a string with the reply.

        The primary utility for this is to properly handle recursion. The recursion
        code in DrBotIRC will set up a couple hints that this method picks up on and
        will appropriately send an IRC event or return a string.

        Unless you know what you are doing, the modules you write should return
        this method rather than send a privmsg reply, as failing to call this
        method will certainly have recursion do odd things with your module.
        """

        replypath = event.target()

        # check for privmsg
        if replypath == connection.get_nickname():
            replypath = irclib.nm_to_n(event.source())

        if replystr is not None:
            if replypath is None:
                return replystr
            else:
                replies = replystr.split('\n')
                for reply in replies:
                    connection.privmsg(replypath, reply)
                if stop_responding:
                    return "NO MORE"

    def sendmsg(self, connection, target, msg):
        """Send a privmsg over IRC to target."""

        if msg is not None:
            if target is not None:
                msgs = msg.split('\n')
                for line in msgs:
                    connection.privmsg(target, line)

    def save(self):
        """Save whatever the module may need to save. Sync files, etc.

        Implement this if you need it.
        """

    def shutdown(self):
        """Do pre-deletion type cleanup.

        Implement this to close databases, write to disk, etc. Note that DrBotIRC
        will call save() before this, so implement appropriately.
        """

        self.is_shutdown = True

    def remove_metaoptions(self, list):
        """Remove metaoptions from provided list, which was probably from a config file."""

        list.remove('debug')

    def retransmit_event(self, event):
        """
        Pretend that some event the bot has generated is rather an incoming IRC
        event. Why one would do this is unclear, but I wrote it and then realized
        I didn't need it.
        """

        self.server._handle_event(event)

    def get_db(self):
        """
        Get a database connection to sqlite3. Once grabbed, it should be closed
        when work is done. Modules that need a database connection should
        test for and create (or, eventually, alter) required table structure
        in their __init__ IF that structure does not already exist. Well-behaved
        modules should use a prefix in their table names (eg "karma_log" rather
        than "log")

        See also db_module_registered, below.
        """

        return self.conn

    def db_module_registered(self, modulename):
        """
        ask the database for a version number for a module. Return that version
        number if the module has registered, or None if not
        """

        conn = self.get_db()
        version = None
        try:
            cur = conn.cursor()
            cur.execute("SELECT version FROM drbotzo_modules WHERE module = :name",
                {'name': modulename})
            version = cur.fetchone()
            if (version != None):
                version = version[0]
        except sqlite3.Error as e:
            print("sqlite error:" + str(e))

        return version

    def db_init(self):
        """
        Set up the database tables and so on, if subclass is planning on using it.
        """

    def do(self, connection, event, nick, userhost, what, admin_unlocked):
        """Do the primary thing this module was intended to do, in most cases.

        Implement this method in your subclass to have a fairly-automatic hook into
        IRC functionality. This is called by DrBotIRC during pubmsg and privmsg
        events.
        """

        print("looks like someone forgot to implement do!")

    def timer_bootstrap(self):
        """Run a thread that does something periodically.

        This only has real functionality if timer_interval() is
        defined to return non-zero.
        """

        # don't actually do stuff if, since the last timer registration,
        # the interval was set to 0
        if self.timer_interval() > 0 and self.is_shutdown == False:
            self.timer_do()

        if self.timer_interval() > 0 and self.is_shutdown == False:
            Timer(self.timer_interval(), self.timer_bootstrap, ()).start()

    def timer_do(self):
        """If we're invoking the timer, do something (that you should implement)."""

        print("looks like someone forgot to implement timer_do!")

    def help_description(self):
        """Return a quick list of commands or other summary, should be
        less than two lines. If you want the module hidden from "!help",
        return None here"""
        return "No description available"

    def help_summary(self):
        """Return a command summary or longer description of this module.
        If this returns None, the summary will be
        "no help available for module foo"
        """
        return None

    def help_detail(self, command):
        """Return detailed help for the given command. Return None if there
        is no suitable help available"""
        return None

    def _unencode_xml(self, text):
        """Convert &lt;, &gt;, &amp; to their real entities."""

        text = text.replace('&lt;', '<')
        text = text.replace('&gt;', '>')
        text = text.replace('&amp;', '&')
        return text

# vi:tabstop=4:expandtab:autoindent
# kate: indent-mode python;indent-width 4;replace-tabs on;