#!/usr/bin/python -W ignore::DeprecationWarning
#
# archipel-rolesnode
#
# Copyright (C) 2010 Antoine Mercadal <antoine.mercadal@inframonde.eu>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import os
import sys
from optparse import OptionParser
import xmpp
import archipelcore.pubsub


def perform(jid, password, pubsubserver, action):
    if not pubsubserver : pubsubserver = "pubsub." + jid.getDomain()

    xmppclient = xmpp.Client(jid.getDomain(), debug=[])
    if not xmppclient.connect():
        print "\033[31mERROR: cannot connect to the server. exiting\033[0m"
        sys.exit(0)

    if xmppclient.auth(jid.getNode(), password, "configurator") == None:
        print "\033[31mERROR: bad authentication. exiting\033[0m"
        sys.exit(0)

    #creating/getting the roles pubsub node
    rolesNodeName = "/archipel/roles"

    pubSubNodeRoles = archipelcore.pubsub.TNPubSubNode(xmppclient, pubsubserver, rolesNodeName)

    if action:
        if not pubSubNodeRoles.recover(wait=True):
            pubSubNodeRoles.create(wait=True)
        pubSubNodeRoles.configure({
                archipelcore.pubsub.XMPP_PUBSUB_VAR_ACCESS_MODEL: archipelcore.pubsub.XMPP_PUBSUB_VAR_ACCESS_MODEL_OPEN,
                archipelcore.pubsub.XMPP_PUBSUB_VAR_PUBLISH_MODEL: archipelcore.pubsub.XMPP_PUBSUB_VAR_ACCESS_MODEL_OPEN,
                archipelcore.pubsub.XMPP_PUBSUB_VAR_DELIVER_NOTIFICATION: 1,
                archipelcore.pubsub.XMPP_PUBSUB_VAR_MAX_ITEMS: 100,
                archipelcore.pubsub.XMPP_PUBSUB_VAR_PERSIST_ITEMS: 1,
                archipelcore.pubsub.XMPP_PUBSUB_VAR_NOTIFY_RECTRACT: 0,
                archipelcore.pubsub.XMPP_PUBSUB_VAR_DELIVER_PAYLOADS: 1,
                archipelcore.pubsub.XMPP_PUBSUB_VAR_SEND_LAST_PUBLISHED_ITEM: archipelcore.pubsub.XMPP_PUBSUB_VAR_SEND_LAST_PUBLISHED_ITEM_NEVER
        }, wait=True)
        print "\033[32mSUCCESS: pubsub node /archipel/roles created!\033[0m"
    else:
        if pubSubNodeRoles.recover(wait=True):
            pubSubNodeRoles.delete(wait=True)
        print "\033[32mSUCCESS: pubsub node /archipel/roles deleted!\033[0m"

if __name__ == "__main__":
    parser = OptionParser()
    parser.add_option("-j", "--jid",
                        dest="jid",
                        help="set the JID to use",
                        metavar="JID")
    parser.add_option("-p", "--password",
                        dest="password",
                        help="set the password associated to the JID",
                        metavar="PASSWORD")
    parser.add_option("-P", "--pubsubserver",
                        dest="pubsubserver",
                        help="set the pubsubserver to use. if not given it will be pubsub.[jid.getDomain()]",
                        metavar="PUBSUBSERVER",
                        default=None)
    parser.add_option("-c", "--create",
                        action="store_true",
                        dest="action",
                        help="create the node (default action)",
                        metavar="ACTION",
                        default=True)
    parser.add_option("-d", "--delete",
                        action="store_false",
                        dest="action",
                        help="delete the node",
                        metavar="ACTION")
    options, args = parser.parse_args()

    if not options.jid or not options.password:
        parser.error("you must enter a JID and a PASSWORD. see --help for help")

    perform(xmpp.JID(options.jid), options.password, options.pubsubserver, options.action)