# -*- coding: utf-8 -*-
__author__ = 'Matthias Uschok <dev@uschok.de>'

import irc.bot
import irc.strings
from jsonserver import JsonServer, callbacks
import status

class StratumsphereStatusBot(irc.bot.SingleServerIRCBot):
    def __init__(self, channel):
        irc.bot.SingleServerIRCBot.__init__(self, [('chat.freenode.net', 6667)], 'fyrebot', 'fyrebot2.0')
        self.channel = channel
        self.guardian = dict()
        self.guardian['present'] = False
        self.guardian['name'] = 'StratumGuardian'

    def on_nicknameinuse(self, c, e):
        c.nick(c.get_nickname() + 'I')

    def on_welcome(self, c, e):
        c.join(self.channel)

    def on_join(self, c, e):
        if irc.strings.lower(self.guardian['name']) in irc.strings.lower(e.source.nick):
            print 'Guardian joined'
            self.guardian['present'] = True
        elif irc.strings.lower(e.source.nick) == irc.strings.lower(c.get_nickname()):
            print 'Joined ' + e.target

    def on_quit(self, c, e):
        if irc.strings.lower(self.guardian['name']) in irc.strings.lower(e.source.nick):
            print 'Guardian quit'
            self.guardian['present'] = False

    def on_currenttopic(self, c, e):
        self.topic = e.arguments[1].split(' || ')
        status.parse_and_update(e.arguments[1])

    def on_topic(self, c, e):
        self.topic = e.arguments[0].split(' || ')
        status.parse_and_update(e.arguments[0])

    def on_namreply(self, c, e):
        names = e.arguments[2].split(' ')
        for name in names:
            if irc.strings.lower(self.guardian['name']) in irc.strings.lower(name):
                self.guardian['present'] = True
        if self.guardian['present']:
            print 'Guardian is present'
        else:
            print 'Guardian is absent'

    def on_pubmsg(self, c, e):
        if e.arguments[0].startswith('sudo ') and not self.guardian['present']:
            self.do_sudo_command(e, e.arguments[0].replace('sudo ', ''))
        # else:
        #     parts = e.arguments[0].split(':',1)
        #     if len(parts) > 1 and irc.strings.lower(parts[0].strip()) == irc.strings.lower(self.connection.get_nickname()):
        #         self.do_command(e, parts[1].strip())


    def on_privmsg(self, c, e):
        c.privmsg(self.channel, 'I\'m being harassed by ' + e.source.nick)

    def do_sudo_command(self, e, command):
        if command == 'open':
            status.update(True, e.source.nick)
            self.send_status()
        elif command == 'close':
            status.update(False, '')
            self.send_status()

    # def do_command(self, e, command):
    #     pass

    def send_status(self):
        if self.guardian['present']:
            self.connection.privmsg(self.channel, status.compose_openclose_msg())
        else:
            new_topic = []

            if len(self.topic) > 0:
                new_topic.append(self.topic[0])
            new_topic.append(status.compose_topic())

            for part in self.topic[2:]:
                new_topic.append(part)

            new_topic_str = new_topic.pop(0)
            for part in new_topic:
                new_topic_str += ' || ' + part

            self.connection.topic(self.channel, new_topic_str)

def main():
    try:
        bot = StratumsphereStatusBot('#stratum0')
        callbacks['send_status'] = bot.send_status
        jsonserver = JsonServer(('134.169.175.96', 8766))

        status.init()
        jsonserver.start()
        bot.start()
    except KeyboardInterrupt:
        bot.connection.quit("I'm meeeltiiiiiiing!")
        jsonserver.stop()
        jsonserver.join()

if __name__ == "__main__":
    main()
@@ -390,130 +390,8 @@
 nnel
-%0A        self.guardian = dict()%0A        self.guardian%5B'present'%5D = False%0A        self.guardian%5B'name'%5D = 'StratumGuardian'
 %0A%0A  
@@ -590,1100 +590,366 @@
 wer(
-self.guardian%5B'name'%5D) in irc.strings.lower(e.source.nick):%0A            print 'Guardian joined'%0A            self.guardian%5B'present'%5D = True%0A        elif irc.strings.lower(e.source.nick) == irc.strings.lower(c.get_nickname()):%0A            print 'Joined ' + e.target%0A%0A    def on_quit(self, c, e):%0A        if irc.strings.lower(self.guardian%5B'name'%5D) in irc.strings.lower(e.source.nick):%0A            print 'Guardian quit'%0A            self.guardian%5B'present'%5D = False%0A%0A    def on_currenttopic(self, c, e):%0A        self.topic = e.arguments%5B1%5D.split(' %7C%7C ')%0A        status.parse_and_update(e.arguments%5B1%5D)%0A%0A    def on_topic(self, c, e):%0A        self.topic = e.arguments%5B0%5D.split(' %7C%7C ')%0A        status.parse_and_update(e.arguments%5B0%5D)%0A%0A    def on_namreply(self, c, e):%0A        names = e.arguments%5B2%5D.split(' ')%0A        for name in names:%0A            if irc.strings.lower(self.guardian%5B'name'%5D) in irc.strings.lower(name):%0A                self.guardian%5B'present'%5D = True%0A        if self.guardian%5B'present'%5D:%0A            print 'Guardian is present'%0A        else:%0A            print 'Guardian is absent'
+e.source.nick) == irc.strings.lower(c.get_nickname()):%0A            print 'Joined ' + e.target%0A%0A    def on_currenttopic(self, c, e):%0A        self.topic = e.arguments%5B1%5D.split(' %7C%7C ')%0A        status.parse_and_update(e.arguments%5B1%5D)%0A%0A    def on_topic(self, c, e):%0A        self.topic = e.arguments%5B0%5D.split(' %7C%7C ')%0A        status.parse_and_update(e.arguments%5B0%5D)
 %0A%0A  
@@ -1032,32 +1032,35 @@
 nd not self.
+is_
 guardian
 %5B'present'%5D:
@@ -1043,35 +1043,34 @@
 .is_guardian
-%5B'
+_
 present
-'%5D
+()
 :%0A          
@@ -1489,16 +1489,233 @@
 .nick)%0A%0A
+    def is_guardian_present(self):%0A        for ch in self.channels.values():%0A                if ch.has_user('StratumGuardian') or ch.has_user('StratumGuardian%60'):%0A                    return True%0A        return False%0A%0A
     def 
@@ -2058,24 +2058,27 @@
 if self.
+is_
 guardian
 %5B'presen
@@ -2073,19 +2073,18 @@
 dian
-%5B'
+_
 present
-'%5D
+()
 :%0A  

use channel method 'has_user' instead of join/part msgs