#!/usr/bin/env python
# Goshubot IRC Bot
# written by Daniel Oaks <daniel@danieloaks.net>
# licensed under the BSD 2-clause license

from time import strftime, localtime
from gbot.modules import Module
from gbot.libs.girclib import NickMask


class ctcp_reply(Module):
    """Provides basic CTCP replies."""

    def __init__(self, bot):
        Module.__init__(self, bot)
        self.events = {
            'in': {
                'ctcp': [(-30, self.ctcp_reply)],
            },
        }

    def ctcp_reply(self, event):

        if event.arguments[0] == 'VERSION':
            message = 'VERSION Goshu:3:https://github.com/DanielOaks/goshu'

            # tell them owner nick(s) if one is online
            runner_level, online_runners = self.bot.accounts.online_bot_runners(event.server)
            if online_runners:
                runner_msg = "Hi, I'm an IRC bot! These are my online contact{}: {}".format('s' if len(online_runners) > 1 else '', ' '.join(online_runners))
                self.bot.irc.servers[event.server].msg(NickMask(event.source).nick, runner_msg)

            self.bot.irc.servers[event.server].ctcp_reply(NickMask(event.source).nick, message)

        elif event.arguments[0] == 'USERINFO':
            userinfostring = None
            # userinfostring = "Please don't kline me, I'll play nice!"
            if userinfostring:
                self.bot.irc.servers[event.server].ctcp_reply(NickMask(event.source).nick, 'USERINFO :{}'.format(userinfostring))

        elif event.arguments[0] == 'CLIENTINFO':
            self.bot.irc.servers[event.server].ctcp_reply(NickMask(event.source).nick, 'CLIENTINFO :Understood CTCP Pairs: CLIENTINFO, ERRMSG, PING, TIME, USERINFO, VERSION')

        elif event.arguments[0] == 'ERRMSG':
            # self.bot.irc.servers[event.server].ctcp_reply(nm_to_n(event.source, 'ERRMSG '+event.arguments()[1]+':ERRMSG echo, no error has occured') #could be bad, errmsg-storm, anyone?
            pass

        elif event.arguments[0] == 'TIME':
            self.bot.irc.servers[event.server].ctcp_reply(NickMask(event.source).nick, 'TIME {}'.format(strftime("%a %b %d, %H:%M:%S %Y", localtime())))

        elif event.arguments[0] == 'PING':
            if len(event.arguments) > 1:
                self.bot.irc.servers[event.server].ctcp_reply(NickMask(event.source).nick, 'PING ' + event.arguments[1])

@@ -821,16 +821,84 @@
 unners:%0A
+                trailing_s = 's' if len(online_runners) %3E 1 else ''%0A
         
@@ -943,29 +943,16 @@
 ot! 
-These are my o
+O
 nline 
-c
+C
 onta
@@ -956,16 +956,17 @@
 ntact%7B%7D:
+ 
  %7B%7D%22.for
@@ -973,46 +973,18 @@
 mat(
-'s' if len(online_runners) %3E 1 else ''
+trailing_s
 , ' 
@@ -1040,61 +1040,17 @@
 irc.
-servers%5Bevent.server%5D.msg(NickMask(event.source).nick
+msg(event
 , ru
@@ -1057,16 +1057,27 @@
 nner_msg
+, 'private'
 )%0A%0A     

Fix dumb ctcp bug