comparison libervia/cli/cmd_account.py @ 4075:47401850dec6

refactoring: rename `libervia.frontends.jp` to `libervia.cli`
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 14:54:26 +0200
parents libervia/frontends/jp/cmd_account.py@26b7ed2817da
children 0d7bb4df2343
comparison
equal deleted inserted replaced
4074:26b7ed2817da 4075:47401850dec6
1 #!/usr/bin/env python3
2
3
4 # Libervia CLI
5 # Copyright (C) 2009-2021 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 libervia.cli.constants import Const as C
23 from libervia.frontends.bridge.bridge_frontend import BridgeException
24 from libervia.backend.core.log import getLogger
25 from libervia.backend.core.i18n import _
26 from libervia.cli import base
27 from libervia.frontends.tools import jid
28
29
30 log = getLogger(__name__)
31
32 __commands__ = ["Account"]
33
34
35 class AccountCreate(base.CommandBase):
36 def __init__(self, host):
37 super(AccountCreate, self).__init__(
38 host,
39 "create",
40 use_profile=False,
41 use_verbose=True,
42 help=_("create a XMPP account"),
43 )
44
45 def add_parser_options(self):
46 self.parser.add_argument(
47 "jid", help=_("jid to create")
48 )
49 self.parser.add_argument(
50 "password", help=_("password of the account")
51 )
52 self.parser.add_argument(
53 "-p",
54 "--profile",
55 help=_(
56 "create a profile to use this account (default: don't create profile)"
57 ),
58 )
59 self.parser.add_argument(
60 "-e",
61 "--email",
62 default="",
63 help=_("email (usage depends of XMPP server)"),
64 )
65 self.parser.add_argument(
66 "-H",
67 "--host",
68 default="",
69 help=_("server host (IP address or domain, default: use localhost)"),
70 )
71 self.parser.add_argument(
72 "-P",
73 "--port",
74 type=int,
75 default=0,
76 help=_("server port (default: {port})").format(
77 port=C.XMPP_C2S_PORT
78 ),
79 )
80
81 async def start(self):
82 try:
83 await self.host.bridge.in_band_account_new(
84 self.args.jid,
85 self.args.password,
86 self.args.email,
87 self.args.host,
88 self.args.port,
89 )
90
91 except BridgeException as e:
92 if e.condition == 'conflict':
93 self.disp(
94 f"The account {self.args.jid} already exists",
95 error=True
96 )
97 self.host.quit(C.EXIT_CONFLICT)
98 else:
99 self.disp(
100 f"can't create account on {self.args.host or 'localhost'!r} with jid "
101 f"{self.args.jid!r} using In-Band Registration: {e}", error=True)
102 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
103 except Exception as e:
104 self.disp(f"Internal error: {e}", error=True)
105 self.host.quit(C.EXIT_INTERNAL_ERROR)
106
107 self.disp(_("XMPP account created"), 1)
108
109 if self.args.profile is None:
110 self.host.quit()
111
112
113 self.disp(_("creating profile"), 2)
114 try:
115 await self.host.bridge.profile_create(
116 self.args.profile,
117 self.args.password,
118 "",
119 )
120 except BridgeException as e:
121 if e.condition == 'conflict':
122 self.disp(
123 f"The profile {self.args.profile} already exists",
124 error=True
125 )
126 self.host.quit(C.EXIT_CONFLICT)
127 else:
128 self.disp(
129 _("Can't create profile {profile} to associate with jid "
130 "{jid}: {e}").format(
131 profile=self.args.profile,
132 jid=self.args.jid,
133 e=e
134 ),
135 error=True,
136 )
137 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
138 except Exception as e:
139 self.disp(f"Internal error: {e}", error=True)
140 self.host.quit(C.EXIT_INTERNAL_ERROR)
141
142 self.disp(_("profile created"), 1)
143 try:
144 await self.host.bridge.profile_start_session(
145 self.args.password,
146 self.args.profile,
147 )
148 except Exception as e:
149 self.disp(f"can't start profile session: {e}", error=True)
150 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
151
152 try:
153 await self.host.bridge.param_set(
154 "JabberID",
155 self.args.jid,
156 "Connection",
157 profile_key=self.args.profile,
158 )
159 except Exception as e:
160 self.disp(f"can't set JabberID parameter: {e}", error=True)
161 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
162
163 try:
164 await self.host.bridge.param_set(
165 "Password",
166 self.args.password,
167 "Connection",
168 profile_key=self.args.profile,
169 )
170 except Exception as e:
171 self.disp(f"can't set Password parameter: {e}", error=True)
172 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
173
174 self.disp(
175 f"profile {self.args.profile} successfully created and associated to the new "
176 f"account", 1)
177 self.host.quit()
178
179
180 class AccountModify(base.CommandBase):
181 def __init__(self, host):
182 super(AccountModify, self).__init__(
183 host, "modify", help=_("change password for XMPP account")
184 )
185
186 def add_parser_options(self):
187 self.parser.add_argument(
188 "password", help=_("new XMPP password")
189 )
190
191 async def start(self):
192 try:
193 await self.host.bridge.in_band_password_change(
194 self.args.password,
195 self.args.profile,
196 )
197 except Exception as e:
198 self.disp(f"can't change XMPP password: {e}", error=True)
199 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
200 else:
201 self.host.quit()
202
203
204 class AccountDelete(base.CommandBase):
205 def __init__(self, host):
206 super(AccountDelete, self).__init__(
207 host, "delete", help=_("delete a XMPP account")
208 )
209
210 def add_parser_options(self):
211 self.parser.add_argument(
212 "-f",
213 "--force",
214 action="store_true",
215 help=_("delete account without confirmation"),
216 )
217
218 async def start(self):
219 try:
220 jid_str = await self.host.bridge.param_get_a_async(
221 "JabberID",
222 "Connection",
223 profile_key=self.profile,
224 )
225 except Exception as e:
226 self.disp(f"can't get JID of the profile: {e}", error=True)
227 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
228
229 jid_ = jid.JID(jid_str)
230 if not self.args.force:
231 message = (
232 f"You are about to delete the XMPP account with jid {jid_!r}\n"
233 f"This is the XMPP account of profile {self.profile!r}\n"
234 f"Are you sure that you want to delete this account?"
235 )
236 await self.host.confirm_or_quit(message, _("Account deletion cancelled"))
237
238 try:
239 await self.host.bridge.in_band_unregister(jid_.domain, self.args.profile)
240 except Exception as e:
241 self.disp(f"can't delete XMPP account with jid {jid_!r}: {e}", error=True)
242 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
243
244 self.host.quit()
245
246
247 class Account(base.CommandBase):
248 subcommands = (AccountCreate, AccountModify, AccountDelete)
249
250 def __init__(self, host):
251 super(Account, self).__init__(
252 host, "account", use_profile=False, help=("XMPP account management")
253 )