#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2010 British Broadcasting Corporation and Kamaelia Contributors(1)
#
# (1) Kamaelia Contributors are listed in the AUTHORS file and at
#     http://www.kamaelia.org/AUTHORS - please extend this file,
#     not this notice.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# -------------------------------------------------------------------------
#

from Kamaelia.File.Writing import SimpleFileWriter
import os
import Kamaelia.Support.Protocol.IRC
import time

"""
===================================
Supporting Functions for IRC Logger
===================================
BasicLogger and Logger depend heavily on this library. These functions are in a
separate module from the Logger components so we can reload these functions
without stopping Logger

There are no components in this module. 
"""

logging = True
lastlog = time.time()

def cannedResponse():
    return [
   "Hi, I'm a bot. I've been put here to answer faq's and log the channel.",
   "I've not really been configured yet, so I won't do much here yet :-)",
           ]

def cannedYesTheyreAround():
    return [
   "Hi, I'm a bot. I've been put here to answer faq's and log the channel.",
   "I've not really been configured yet, so I won't do much here yet :-)",
           ]

def respondToQueries(self, msg):
    """Takes a BasicLogger as its first argument. If this function recognizes
    "msg" as a command, then it sends back the appropriate response to IRC
    """
    replyLines = ""
    tag = 'PRIVMSG'

    if msg[0] == 'PRIVMSG' and msg[3].split(':')[0] == self.name:
        words = msg[3].split()
        if words[1] == 'logfile':
            replyLines = [self.logname]
        elif words[1] == 'infofile':
            replyLines = [self.infoname]
        elif words[1] == 'help':
            replyLines = ["Name: %s   Channel: %s" % (self.name, self.channel),
                          "I do a simple job -- recording all channel traffic.",
                          "Lines prefixed by [off] won't get recorded",
                          "I respond to the following: 'logfile', 'infofile', 'help', 'date', 'time', 'dance', 'poke', 'slap', 'ecky', 'boo', and 'reload {modulename}'."
                          ]
        elif words[1] == 'date':
            replyLines = [self.currentDateString()]
        elif words[1] == 'time':
            replyLines = [self.currentTimeString()]
        elif words[1] == 'dance':
            tag = 'ME'
            replyLines = ['does the macarena']
        elif words[1] == 'poke':
            replyLines = ['Not the eye! Not the eye!']
        elif words[1] == 'slap':
            replyLines = ['Ouch!']
        elif words[1] == 'ecky':
            replyLines = ['Ptang!']
        elif words[1] == 'boo':
            replyLines = ['Nice try, but that didn\'t scare me']
        elif words[1] == 'learn':
            replyLines = ['OK, trying, but not ready to do that yet - I will though' + str(len(words)) ]
        
    if msg[0] == 'PRIVMSG':
       words = [ x.lower() for x in msg[3].split() ]

       if  ("any" in words) \
           and (("mentors" in words) or ("mentor" in words)):
           replyLines = cannedResponse()
       elif  ("when" in words) \
           and (("feedback" in words) or ("expect" in words)):
           replyLines = cannedResponse()

       elif  ("when" in words) \
           and (("feedback" in words) or ("expect" in words)):
           replyLines = cannedResponse()

       elif   ("i" in words) \
           and (("have" in words) or ("had" in words)) \
           and (("question" in words) or ("query" in words) or ("doubt" in words)):
           replyLines = cannedResponse()

       elif  ("who" in words) \
           and ("can" in words) \
           and ("i" in words) \
           and ("ask" in words) \
           and (("soc" in words) or ("gcos" in words) or ("gsoc" in words)):
           replyLines = cannedResponse()

       elif  (("about" in words) or ("around" in words)) \
           and (("is" in words) or ("are" in words)) \
           and (("mentors-" in words) or ("ms-" in words) or ("mhrd" in words) or ("lawouach" in words)):
           replyLines = cannedYesTheyreAround()

       elif  ("anyone" in words) \
           and ("seen" in words) \
           and (("mentors-" in words) or ("ms-" in words) or ("mhrd" in words) or ("lawouach" in words)):
           replyLines = cannedYesTheyreAround()

       elif  ("hi" in words) \
           and (("everybody" in words) or ("evreybody" in words)):
           replyLines = [ "hi" ]

       elif ("kamaeliabot" in words):
          if ("bonjour" in words):
               replyLines = ["Bonjour"]
          elif ("parrot" in words):
               replyLines = [ repr(words) ]

       if replyLines == "":
          if ("anyone" in words) and ("know" in words):