Source

xpoty2xmpp / xpoty2xmpp / TransportGateway.py

#
# Copyright (C) 2012, Dmitry Nezhevenko
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
from xpoty2xmpp.xpoty.XpotyRoster import buddy_to_jid

def TransportGatewayGetHandler(stanza, transport):
    reply = stanza.make_result_response()
    query_node = reply.add_new_content("jabber:iq:gateway", "query")
    query_node.newChild(None, "desc", "Please enter IM name of person you would like to contact.")
    query_node.newChild(None, "prompt", "ID")
    transport.transport_send(reply)


def TransportGatewaySetHandler(stanza, transport):
    prompt = stanza.xpath_eval("r:query/r:prompt", {"r":"jabber:iq:gateway"})
    if prompt is None or prompt[0].getContent().strip() == "":
        err = stanza.make_error_response("bad-request")
        transport.transport_send(err)
        return
    prompt = prompt[0].getContent().strip()
    buddy_jid = buddy_to_jid(prompt, str(transport.jid))

    reply = stanza.make_result_response()
    query_node = reply.add_new_content("jabber:iq:gateway", "query")
    query_node.newChild(None, "jid", buddy_jid)
    transport.transport_send(reply)