#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
 Copyright (C) 2004 jan gerber <j@reboot.fm>

 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 2 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 Library General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
'''

# re_codec decoder irc bot
# baesd on
# Example program using ircbot.py. by Joel Rosdahl <joel@rosdahl.net>
#
# tail -f --follow=name  /var/log/syslog | decoderbot.py
#
"""A simple decoder status bot
The known commands are:
    stats -- Prints some decoder information
"""

import string
from ircbot import SingleServerIRCBot
from irclib import nm_to_n, nm_to_h, irc_lower, ip_numstr_to_quad, ip_quad_to_numstr
import re
import os
import time
import thread
import read_syslog
import sys

class DecoderBot(SingleServerIRCBot):
    def __init__(self, channel, nickname, server, port=6667):        
        self.last_fallback_log_line=None
        SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)
        self.channel = channel       
        self.start()
        
    def on_connect(self,connection, event):
        print self.connection.info()
        
    def on_join(self,connection, event):
        thread.start_new(status_thread, (self,))

    def on_nicknameinuse(self, c, e):
        c.nick(c.get_nickname() + "_")

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

    def on_privmsg(self, c, e):
        self.do_command(e, e.arguments()[0])
    
    def check_status(self):
        if read_syslog.fallback_change:
            if read_syslog.fallback_change=="fallback":
                self.connection.privmsg(self.channel,"fallback on FM")
            if read_syslog.fallback_change=='in':
                self.connection.privmsg(self.channel,"back on AIR")
            read_syslog.fallback_change=None

    def last_fallback(self):
        return "%s - %s" % (read_syslog.fallback_start, read_syslog.fallback_end)

    def last_pppreconnect(self):
        return read_syslog.pppd_connect

    def do_command(self, e, cmd):
        nick = nm_to_n(e.source())
        c = self.connection

        if cmd != "":
            c.notice(nick, "--- decoder statistics ---")
            c.notice(nick, "Last Fallback:      %s" % self.last_fallback())
            c.notice(nick, "Last PPP Reconnect: %s" % self.last_pppreconnect())
            f = os.popen('uptime', 'r')
            uptime = f.read()
            f.close()
            c.notice(nick, "Uptime: %s" % uptime)

def main():
    #bot = DecoderBot('#reboot.fm', 'decoder', 'irc.reboot.fm', 6667)
    bot = DecoderBot('#reboot.fm', 'decoder', 'irc.reboot.fm', 5555)

def status_thread(self):
        while 1:
            self.check_status()
            time.sleep(1)

if __name__ == "__main__":
    thread.start_new(read_syslog.update_syslog_status, (sys.stdin,))
    main()
