comparison sat_frontends/jp/cmd_account.py @ 3040:fee60f17ebac

jp: jp asyncio port: /!\ this commit is huge. Jp is temporarily not working with `dbus` bridge /!\ This patch implements the port of jp to asyncio, so it is now correctly using the bridge asynchronously, and it can be used with bridges like `pb`. This also simplify the code, notably for things which were previously implemented with many callbacks (like pagination with RSM). During the process, some behaviours have been modified/fixed, in jp and backends, check diff for details.
author Goffi <goffi@goffi.org>
date Wed, 25 Sep 2019 08:56:41 +0200
parents ab2696e34d29
children 9d0df638c8b4
comparison
equal deleted inserted replaced
3039:a1bc34f90fa5 3040:fee60f17ebac
37 "create", 37 "create",
38 use_profile=False, 38 use_profile=False,
39 use_verbose=True, 39 use_verbose=True,
40 help=_("create a XMPP account"), 40 help=_("create a XMPP account"),
41 ) 41 )
42 self.need_loop = True
43 42
44 def add_parser_options(self): 43 def add_parser_options(self):
45 self.parser.add_argument( 44 self.parser.add_argument(
46 "jid", help=_("jid to create") 45 "jid", help=_("jid to create")
47 ) 46 )
70 self.parser.add_argument( 69 self.parser.add_argument(
71 "-P", 70 "-P",
72 "--port", 71 "--port",
73 type=int, 72 type=int,
74 default=0, 73 default=0,
75 help=_("server port (IP address or domain, default: use localhost)"), 74 help=_(f"server port (default: {C.XMPP_C2S_PORT})"),
76 ) 75 )
77 76
78 def _setParamCb(self): 77 async def start(self):
79 self.host.bridge.setParam( 78 try:
80 "Password", 79 await self.host.bridge.inBandAccountNew(
81 self.args.password, 80 self.args.jid,
82 "Connection", 81 self.args.password,
83 profile_key=self.args.profile, 82 self.args.email,
84 callback=self.host.quit, 83 self.args.host,
85 errback=self.errback, 84 self.args.port,
86 ) 85 )
87 86 except Exception as e:
88 def _session_started(self, __): 87 self.disp(
89 self.host.bridge.setParam( 88 f"can't create account on {self.args.host or 'localhost'!r} with jid "
90 "JabberID", 89 f"{self.args.jid!r} using In-Band Registration: {e}", error=True)
91 self.args.jid, 90 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
92 "Connection", 91
93 profile_key=self.args.profile, 92 self.disp(_("XMPP account created"), 1)
94 callback=self._setParamCb, 93
95 errback=self.errback, 94 if self.args.profile is None:
96 ) 95 self.host.quit()
97 96
98 def _profileCreateCb(self): 97
98 self.disp(_("creating profile"), 2)
99 try:
100 await self.host.bridge.profileCreate(
101 self.args.profile,
102 self.args.password,
103 "",
104 )
105 except Exception as e:
106 self.disp(
107 _(f"Can't create profile {self.args.profile} to associate with jid "
108 f"{self.args.jid}: {e}"),
109 error=True,
110 )
111 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
112
99 self.disp(_("profile created"), 1) 113 self.disp(_("profile created"), 1)
100 self.host.bridge.profileStartSession( 114 try:
101 self.args.password, 115 await self.host.bridge.profileStartSession(
102 self.args.profile, 116 self.args.password,
103 callback=self._session_started, 117 self.args.profile,
104 errback=self.errback, 118 )
105 ) 119 except Exception as e:
106 120 self.disp(f"can't start profile session: {e}", error=True)
107 def _profileCreateEb(self, failure_): 121 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
122
123 try:
124 await self.host.bridge.setParam(
125 "JabberID",
126 self.args.jid,
127 "Connection",
128 profile_key=self.args.profile,
129 )
130 except Exception as e:
131 self.disp(f"can't set JabberID parameter: {e}", error=True)
132 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
133
134 try:
135 await self.host.bridge.setParam(
136 "Password",
137 self.args.password,
138 "Connection",
139 profile_key=self.args.profile,
140 )
141 except Exception as e:
142 self.disp(f"can't set Password parameter: {e}", error=True)
143 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
144
108 self.disp( 145 self.disp(
109 _( 146 f"profile {self.args.profile} successfully created and associated to the new "
110 "Can't create profile {profile} to associate with jid {jid}: {msg}" 147 f"account", 1)
111 ).format(profile=self.args.profile, jid=self.args.jid, msg=failure_), 148 self.host.quit()
112 error=True, 149
113 ) 150
114 self.host.quit(C.EXIT_BRIDGE_ERRBACK) 151 class AccountModify(base.CommandBase):
115 152 def __init__(self, host):
116 def accountNewCb(self): 153 super(AccountModify, self).__init__(
117 self.disp(_("XMPP account created"), 1) 154 host, "modify", help=_("change password for XMPP account")
118 if self.args.profile is not None: 155 )
119 self.disp(_("creating profile"), 2) 156
120 self.host.bridge.profileCreate( 157 def add_parser_options(self):
158 self.parser.add_argument(
159 "password", help=_("new XMPP password")
160 )
161
162 async def start(self):
163 try:
164 await self.host.bridge.inBandPasswordChange(
165 self.args.password,
121 self.args.profile, 166 self.args.profile,
122 self.args.password, 167 )
123 "", 168 except Exception as e:
124 callback=self._profileCreateCb, 169 self.disp(f"can't change XMPP password: {e}", error=True)
125 errback=self._profileCreateEb, 170 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
126 )
127 else: 171 else:
128 self.host.quit() 172 self.host.quit()
129 173
130 def accountNewEb(self, failure_):
131 self.disp(
132 _("Can't create new account on server {host} with jid {jid}: {msg}").format(
133 host=self.args.host or "localhost", jid=self.args.jid, msg=failure_
134 ),
135 error=True,
136 )
137 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
138
139 def start(self):
140 self.host.bridge.inBandAccountNew(
141 self.args.jid,
142 self.args.password,
143 self.args.email,
144 self.args.host,
145 self.args.port,
146 callback=self.accountNewCb,
147 errback=self.accountNewEb,
148 )
149
150
151 class AccountModify(base.CommandBase):
152 def __init__(self, host):
153 super(AccountModify, self).__init__(
154 host, "modify", help=_("change password for XMPP account")
155 )
156 self.need_loop = True
157
158 def add_parser_options(self):
159 self.parser.add_argument(
160 "password", help=_("new XMPP password")
161 )
162
163 def start(self):
164 self.host.bridge.inBandPasswordChange(
165 self.args.password,
166 self.args.profile,
167 callback=self.host.quit,
168 errback=self.errback,
169 )
170
171 174
172 class AccountDelete(base.CommandBase): 175 class AccountDelete(base.CommandBase):
173 def __init__(self, host): 176 def __init__(self, host):
174 super(AccountDelete, self).__init__( 177 super(AccountDelete, self).__init__(
175 host, "delete", help=_("delete a XMPP account") 178 host, "delete", help=_("delete a XMPP account")
176 ) 179 )
177 self.need_loop = True
178 180
179 def add_parser_options(self): 181 def add_parser_options(self):
180 self.parser.add_argument( 182 self.parser.add_argument(
181 "-f", 183 "-f",
182 "--force", 184 "--force",
183 action="store_true", 185 action="store_true",
184 help=_("delete account without confirmation"), 186 help=_("delete account without confirmation"),
185 ) 187 )
186 188
187 def _got_jid(self, jid_str): 189 async def start(self):
190 try:
191 jid_str = await self.host.bridge.asyncGetParamA(
192 "JabberID",
193 "Connection",
194 profile_key=self.profile,
195 )
196 except Exception as e:
197 self.disp(f"can't get JID of the profile: {e}", error=True)
198 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
199
188 jid_ = jid.JID(jid_str) 200 jid_ = jid.JID(jid_str)
189 if not self.args.force: 201 if not self.args.force:
190 message = ( 202 message = (
191 "You are about to delete the XMPP account with jid {jid_}\n" 203 f"You are about to delete the XMPP account with jid {jid_!r}\n"
192 'This is the XMPP account of profile "{profile}"\n' 204 f"This is the XMPP account of profile {self.profile!r}\n"
193 "Are you sure that you want to delete this account ?".format( 205 f"Are you sure that you want to delete this account?"
194 jid_=jid_, profile=self.profile 206 )
195 ) 207 await self.host.confirmOrQuit(message, _("Account deletion cancelled"))
196 ) 208
197 res = input("{} (y/N)? ".format(message)) 209 try:
198 if res not in ("y", "Y"): 210 await self.host.bridge.inBandUnregister(jid_.domain, self.args.profile)
199 self.disp(_("Account deletion cancelled")) 211 except Exception as e:
200 self.host.quit(2) 212 self.disp(f"can't delete XMPP account with jid {jid_!r}: {e}", error=True)
201 self.host.bridge.inBandUnregister( 213 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
202 jid_.domain, self.args.profile, callback=self.host.quit, errback=self.errback 214
203 ) 215 self.host.quit()
204
205 def start(self):
206 self.host.bridge.asyncGetParamA(
207 "JabberID",
208 "Connection",
209 profile_key=self.profile,
210 callback=self._got_jid,
211 errback=self.errback,
212 )
213 216
214 217
215 class Account(base.CommandBase): 218 class Account(base.CommandBase):
216 subcommands = (AccountCreate, AccountModify, AccountDelete) 219 subcommands = (AccountCreate, AccountModify, AccountDelete)
217 220