summaryrefslogtreecommitdiff
path: root/examples/confirm_answer.py
diff options
context:
space:
mode:
authormathieui <mathieui@mathieui.net>2016-08-19 23:45:24 +0200
committermathieui <mathieui@mathieui.net>2016-08-19 23:48:37 +0200
commit39ee833c29039f635ef65c9d31e551c6c58055d0 (patch)
tree7bd15edfa67f489b5e049917a674ab8a49f24df5 /examples/confirm_answer.py
parent9019e2bc71c7709a5c557cbb17cb59870ddef73e (diff)
downloadslixmpp-39ee833c29039f635ef65c9d31e551c6c58055d0.tar.gz
slixmpp-39ee833c29039f635ef65c9d31e551c6c58055d0.tar.bz2
slixmpp-39ee833c29039f635ef65c9d31e551c6c58055d0.tar.xz
slixmpp-39ee833c29039f635ef65c9d31e551c6c58055d0.zip
Improve XEP-0070 and examples
Diffstat (limited to 'examples/confirm_answer.py')
-rwxr-xr-xexamples/confirm_answer.py39
1 files changed, 27 insertions, 12 deletions
diff --git a/examples/confirm_answer.py b/examples/confirm_answer.py
index 2b2015a2..9cfe30f1 100755
--- a/examples/confirm_answer.py
+++ b/examples/confirm_answer.py
@@ -29,18 +29,33 @@ class AnswerConfirm(slixmpp.ClientXMPP):
def __init__(self, jid, password, trusted):
slixmpp.ClientXMPP.__init__(self, jid, password)
- self.trusted = trusted
- self.api.register(self.confirm, 'xep_0070', 'get_confirm')
-
- def confirm(self, jid, id, url, method):
- log.info('Received confirm request %s from %s to access %s using '
- 'method %s' % (id, jid, url, method))
- if jid not in self.trusted:
- log.info('Denied')
- return False
- log.info('Confirmed')
- return True
-
+ self.add_event_handler("http_confirm", self.confirm)
+ self.add_event_handler("session_start", self.start)
+
+ def start(self, *args):
+ self.make_presence().send()
+
+ def prompt(self, stanza):
+ confirm = stanza['confirm']
+ print('Received confirm request %s from %s to access %s using '
+ 'method %s' % (
+ confirm['id'], stanza['from'], confirm['url'],
+ confirm['method'])
+ )
+ result = input("Do you accept (y/N)? ")
+ return 'y' == result.lower()
+
+ def confirm(self, stanza):
+ if self.prompt(stanza):
+ reply = stanza.reply()
+ else:
+ reply = stanza.reply()
+ reply.enable('error')
+ reply['error']['type'] = 'auth'
+ reply['error']['code'] = '401'
+ reply['error']['condition'] = 'not-authorized'
+ reply.append(stanza['confirm'])
+ reply.send()
if __name__ == '__main__':
# Setup the command line arguments.