#!/usr/bin/env python2
# -*- coding: utf-8 -*-

#
# watbot par aurelien.aptel@gmail.com
#

### install des deps (root)
# pip install sleekxmpp

### usage
# ./democracybot.py  -n democracy-bot -r 'lafamille@conference.babare.dynamic-dns.net' -j 'babar-bot@babare.dynamic-dns.net' -p password

import sys
import logging
import getpass
from optparse import OptionParser
import re
import sleekxmpp
import math
import threading
from datetime import datetime

from ReferendumActivity import ReferendumActivity
from DiceActivity import DiceActivity
from AnonyTalkActivity import AnonyTalkActivity

if sys.version_info < (3, 0):
    from sleekxmpp.util.misc_ops import setdefaultencoding
    setdefaultencoding('utf8')
else:
    raw_input = input


class MUCBot(sleekxmpp.ClientXMPP):
    def __init__(self, jid, password, room, nick):
        sleekxmpp.ClientXMPP.__init__(self, jid, password)
        self.last_msg = ''
        self.room = room
        self.nick = nick
        self.add_event_handler("session_start", self.start)
        self.add_event_handler("groupchat_message", self.muc_message)
        self.add_event_handler("message", self.private_message)
		
        self.activities = [ReferendumActivity(self), DiceActivity(self), AnonyTalkActivity(self)]

    def start(self, event):
        self.get_roster()
        self.send_presence()
        self.plugin['xep_0045'].joinMUC(self.room,
                                        self.nick,
                                        # If a room password is needed, use:
                                        # password=the_room_password,
                                        wait=True)
	
    def sendMucMessage(self, msgStr):
        self.send_message(mto=self.room, mbody=msgStr, mtype='groupchat') 
    
    def getRealJidForMucnick(self, mucnick):
        return self.plugin['xep_0045'].getJidProperty(self.room, mucnick, 'jid')
           
    def private_message(self, msg):
        if msg['mucnick'] != self.nick and msg['type'] == 'chat': 
            for activity in self.activities:
                activity.private_message(msg)
        
    def muc_message(self, msg):
        if msg['mucnick'] != self.nick: 
            for activity in self.activities:
                activity.muc_message(msg)

if __name__ == '__main__':
    # Setup the command line arguments.
    optp = OptionParser()

    # Output verbosity options.
    optp.add_option('-q', '--quiet', help='set logging to ERROR',
                    action='store_const', dest='loglevel',
                    const=logging.ERROR, default=logging.INFO)
    optp.add_option('-d', '--debug', help='set logging to DEBUG',
                    action='store_const', dest='loglevel',
                    const=logging.DEBUG, default=logging.INFO)
    optp.add_option('-v', '--verbose', help='set logging to COMM',
                    action='store_const', dest='loglevel',
                    const=5, default=logging.INFO)

    # JID and password options.
    optp.add_option("-j", "--jid", dest="jid",
                    help="JID to use")
    optp.add_option("-p", "--password", dest="password",
                    help="password to use")
    optp.add_option("-r", "--room", dest="room",
                    help="MUC room to join")
    optp.add_option("-n", "--nick", dest="nick",
                    help="MUC nickname")

    opts, args = optp.parse_args()

    # Setup logging.
    logging.basicConfig(level=opts.loglevel,
                        format='%(levelname)-8s %(message)s')

    if opts.jid is None:
        opts.jid = raw_input("Username: ")
    if opts.password is None:
        opts.password = getpass.getpass("Password: ")
    if opts.room is None:
        opts.room = raw_input("MUC room: ")
    if opts.nick is None:
        opts.nick = raw_input("MUC nickname: ")

    xmpp = MUCBot(opts.jid, opts.password, opts.room, opts.nick)
    xmpp.register_plugin('xep_0030') # Service Discovery
    xmpp.register_plugin('xep_0045') # Multi-User Chat
    xmpp.register_plugin('xep_0199') # XMPP Ping

    if xmpp.connect():
        xmpp.process(block=True)
        print("Done")
    else:
        print("Unable to connect.")
