comparison frontends/src/jp/cmd_account.py @ 2180:be54e1c3394c

jp (account): command to handle XMPP account creation/password change/deletion using in-band registration
author Goffi <goffi@goffi.org>
date Thu, 09 Mar 2017 23:11:42 +0100
parents
children 8b37a62336c3
comparison
equal deleted inserted replaced
2179:e42c6c131b52 2180:be54e1c3394c
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 # jp: a SAT command line tool
5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 """This module permits to manage XMPP accounts using in-band registration (XEP-0077)"""
21
22 from sat_frontends.jp.constants import Const as C
23 from sat.core.log import getLogger
24 log = getLogger(__name__)
25 from sat.core.i18n import _
26 from sat_frontends.jp import base
27 from sat_frontends.tools import jid
28
29 __commands__ = ["Account"]
30
31
32 class AccountCreate(base.CommandBase):
33
34 def __init__(self, host):
35 super(AccountCreate, self).__init__(host, 'create', use_profile=False, use_verbose=True, help=_(u'create a XMPP account'))
36 self.need_loop = True
37
38 def add_parser_options(self):
39 self.parser.add_argument('jid', type=base.unicode_decoder, help=_(u'jid to create'))
40 self.parser.add_argument('password', type=base.unicode_decoder, help=_(u'password of the account'))
41 self.parser.add_argument('-p', '--profile', type=base.unicode_decoder, help=_(u"create a profile to use this account (default: don't create profile)"))
42 self.parser.add_argument('-e', '--email', type=base.unicode_decoder, default="", help=_(u"email (usage depends of XMPP server)"))
43 self.parser.add_argument('-H', '--host', type=base.unicode_decoder, default="", help=_(u"server host (IP address or domain, default: use localhost)"))
44 self.parser.add_argument('-P', '--port', type=int, default=0, help=_(u"server port (IP address or domain, default: use localhost)"))
45
46 def _setParamCb(self):
47 self.host.bridge.setParam("Password", self.args.password, "Connection", profile_key=self.args.profile, callback=self.host.quit, errback=self.errback)
48
49 def _session_started(self, dummy):
50 self.host.bridge.setParam("JabberID", self.args.jid, "Connection", profile_key=self.args.profile, callback=self._setParamCb, errback=self.errback)
51
52 def _profileCreateCb(self):
53 self.disp(_(u"profile created"), 1)
54 self.host.bridge.profileStartSession(self.args.password, self.args.profile, callback=self._session_started, errback=self.errback)
55
56 def _profileCreateEb(self, failure_):
57 self.disp(_(u"Can't create profile {profile} to associate with jid {jid}: {msg}").format(
58 profile = self.args.profile,
59 jid = self.args.jid,
60 msg = failure_), error=True)
61 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
62
63 def accountNewCb(self):
64 self.disp(_(u"XMPP account created"), 1)
65 if self.args.profile is not None:
66 self.disp(_(u"creating profile"), 2)
67 self.host.bridge.profileCreate(self.args.profile, self.args.password, "", callback=self._profileCreateCb, errback=self._profileCreateEb)
68 else:
69 self.host.quit()
70
71 def accountNewEb(self, failure_):
72 self.disp(_(u"Can't create new account on server {host} with jid {jid}: {msg}").format(
73 host = self.args.host or u"localhost",
74 jid = self.args.jid,
75 msg = failure_), error=True)
76 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
77
78 def start(self):
79 self.host.bridge.inBandAccountNew(self.args.jid, self.args.password, self.args.email, self.args.host, self.args.port,
80 callback=self.accountNewCb, errback=self.accountNewEb)
81
82
83
84 class AccountModify(base.CommandBase):
85
86 def __init__(self, host):
87 super(AccountModify, self).__init__(host, 'modify', help=_(u'change password for XMPP account'))
88 self.need_loop = True
89
90 def add_parser_options(self):
91 self.parser.add_argument('password', type=base.unicode_decoder, help=_(u'new XMPP password'))
92
93 def start(self):
94 self.host.bridge.inBandPasswordChange(self.args.password, self.args.profile,
95 callback=self.host.quit, errback=self.errback)
96
97
98 class AccountDelete(base.CommandBase):
99
100 def __init__(self, host):
101 super(AccountDelete, self).__init__(host, 'delete', help=_(u'delete a XMPP account'))
102 self.need_loop = True
103
104 def add_parser_options(self):
105 self.parser.add_argument('-f', '--force', action='store_true', help=_(u'delete account without confirmation'))
106
107 def _got_jid(self, jid_str):
108 jid_ = jid.JID(jid_str)
109 if not self.args.force:
110 message = (u"You are about to delete the XMPP account with jid {jid_}\n"
111 u"This is the XMPP account of profile \"{profile}\"\n"
112 u"Are you sure that you want to delete this account ?".format(
113 jid_ = jid_,
114 profile=self.profile
115 ))
116 res = raw_input("{} (y/N)? ".format(message))
117 if res not in ("y", "Y"):
118 self.disp(_(u"Account deletion cancelled"))
119 self.host.quit(2)
120 self.host.bridge.inBandUnregister(jid_.domain, self.args.profile,
121 callback=self.host.quit, errback=self.errback)
122
123 def start(self):
124 self.host.bridge.asyncGetParamA("JabberID", "Connection", profile_key=self.profile,
125 callback=self._got_jid, errback=self.errback)
126
127
128 class Account(base.CommandBase):
129 subcommands = (AccountCreate, AccountModify, AccountDelete)
130
131 def __init__(self, host):
132 super(Account, self).__init__(host, 'account', use_profile=False, help=(u'XMPP account management'))