comparison sat_frontends/jp/cmd_encryption.py @ 3028:ab2696e34d29

Python 3 port: /!\ this is a huge commit /!\ starting from this commit, SàT is needs Python 3.6+ /!\ SàT maybe be instable or some feature may not work anymore, this will improve with time This patch port backend, bridge and frontends to Python 3. Roughly this has been done this way: - 2to3 tools has been applied (with python 3.7) - all references to python2 have been replaced with python3 (notably shebangs) - fixed files not handled by 2to3 (notably the shell script) - several manual fixes - fixed issues reported by Python 3 that where not handled in Python 2 - replaced "async" with "async_" when needed (it's a reserved word from Python 3.7) - replaced zope's "implements" with @implementer decorator - temporary hack to handle data pickled in database, as str or bytes may be returned, to be checked later - fixed hash comparison for password - removed some code which is not needed anymore with Python 3 - deactivated some code which needs to be checked (notably certificate validation) - tested with jp, fixed reported issues until some basic commands worked - ported Primitivus (after porting dependencies like urwid satext) - more manual fixes
author Goffi <goffi@goffi.org>
date Tue, 13 Aug 2019 19:08:41 +0200
parents 003b8b4b56a7
children fee60f17ebac
comparison
equal deleted inserted replaced
3027:ff5bcb12ae60 3028:ab2696e34d29
46 self.output(plugins) 46 self.output(plugins)
47 self.host.quit() 47 self.host.quit()
48 48
49 def default_output(self, plugins): 49 def default_output(self, plugins):
50 if not plugins: 50 if not plugins:
51 self.disp(_(u"No encryption plugin registered!")) 51 self.disp(_("No encryption plugin registered!"))
52 self.host.quit(C.EXIT_NOT_FOUND) 52 self.host.quit(C.EXIT_NOT_FOUND)
53 else: 53 else:
54 self.disp(_(u"Following encryption algorithms are available: {algos}").format( 54 self.disp(_("Following encryption algorithms are available: {algos}").format(
55 algos=', '.join([p['name'] for p in plugins]))) 55 algos=', '.join([p['name'] for p in plugins])))
56 self.host.quit() 56 self.host.quit()
57 57
58 def start(self): 58 def start(self):
59 self.host.bridge.encryptionPluginsGet( 59 self.host.bridge.encryptionPluginsGet(
60 callback=self.encryptionPluginsGetCb, 60 callback=self.encryptionPluginsGetCb,
61 errback=partial( 61 errback=partial(
62 self.errback, 62 self.errback,
63 msg=_(u"can't retrieve plugins: {}"), 63 msg=_("can't retrieve plugins: {}"),
64 exit_code=C.EXIT_BRIDGE_ERRBACK, 64 exit_code=C.EXIT_BRIDGE_ERRBACK,
65 ), 65 ),
66 ) 66 )
67 67
68 68
70 70
71 def __init__(self, host): 71 def __init__(self, host):
72 super(EncryptionGet, self).__init__( 72 super(EncryptionGet, self).__init__(
73 host, "get", 73 host, "get",
74 use_output=C.OUTPUT_DICT, 74 use_output=C.OUTPUT_DICT,
75 help=_(u"get encryption session data")) 75 help=_("get encryption session data"))
76 self.need_loop = True 76 self.need_loop = True
77 77
78 def add_parser_options(self): 78 def add_parser_options(self):
79 self.parser.add_argument( 79 self.parser.add_argument(
80 "jid", type=base.unicode_decoder, 80 "jid",
81 help=_(u"jid of the entity to check") 81 help=_("jid of the entity to check")
82 ) 82 )
83 83
84 def messageEncryptionGetCb(self, serialised): 84 def messageEncryptionGetCb(self, serialised):
85 session_data = data_format.deserialise(serialised) 85 session_data = data_format.deserialise(serialised)
86 if session_data is None: 86 if session_data is None:
87 self.disp( 87 self.disp(
88 u"No encryption session found, the messages are sent in plain text.") 88 "No encryption session found, the messages are sent in plain text.")
89 self.host.quit(C.EXIT_NOT_FOUND) 89 self.host.quit(C.EXIT_NOT_FOUND)
90 self.output(session_data) 90 self.output(session_data)
91 self.host.quit() 91 self.host.quit()
92 92
93 def start(self): 93 def start(self):
96 self.host.bridge.messageEncryptionGet( 96 self.host.bridge.messageEncryptionGet(
97 jid, self.profile, 97 jid, self.profile,
98 callback=self.messageEncryptionGetCb, 98 callback=self.messageEncryptionGetCb,
99 errback=partial( 99 errback=partial(
100 self.errback, 100 self.errback,
101 msg=_(u"can't get session: {}"), 101 msg=_("can't get session: {}"),
102 exit_code=C.EXIT_BRIDGE_ERRBACK, 102 exit_code=C.EXIT_BRIDGE_ERRBACK,
103 ), 103 ),
104 ) 104 )
105 105
106 106
107 class EncryptionStart(base.CommandBase): 107 class EncryptionStart(base.CommandBase):
108 108
109 def __init__(self, host): 109 def __init__(self, host):
110 super(EncryptionStart, self).__init__( 110 super(EncryptionStart, self).__init__(
111 host, "start", 111 host, "start",
112 help=_(u"start encrypted session with an entity")) 112 help=_("start encrypted session with an entity"))
113 self.need_loop = True 113 self.need_loop = True
114 114
115 def add_parser_options(self): 115 def add_parser_options(self):
116 self.parser.add_argument( 116 self.parser.add_argument(
117 "--encrypt-noreplace", 117 "--encrypt-noreplace",
118 action="store_true", 118 action="store_true",
119 help=_(u"don't replace encryption algorithm if an other one is already used")) 119 help=_("don't replace encryption algorithm if an other one is already used"))
120 algorithm = self.parser.add_mutually_exclusive_group() 120 algorithm = self.parser.add_mutually_exclusive_group()
121 algorithm.add_argument( 121 algorithm.add_argument(
122 "-n", "--name", help=_(u"algorithm name (DEFAULT: choose automatically)")) 122 "-n", "--name", help=_("algorithm name (DEFAULT: choose automatically)"))
123 algorithm.add_argument( 123 algorithm.add_argument(
124 "-N", "--namespace", 124 "-N", "--namespace",
125 help=_(u"algorithm namespace (DEFAULT: choose automatically)")) 125 help=_("algorithm namespace (DEFAULT: choose automatically)"))
126 self.parser.add_argument( 126 self.parser.add_argument(
127 "jid", type=base.unicode_decoder, 127 "jid",
128 help=_(u"jid of the entity to stop encrypted session with") 128 help=_("jid of the entity to stop encrypted session with")
129 ) 129 )
130 130
131 def encryptionNamespaceGetCb(self, namespace): 131 def encryptionNamespaceGetCb(self, namespace):
132 jids = self.host.check_jids([self.args.jid]) 132 jids = self.host.check_jids([self.args.jid])
133 jid = jids[0] 133 jid = jids[0]
134 self.host.bridge.messageEncryptionStart( 134 self.host.bridge.messageEncryptionStart(
135 jid, namespace, not self.args.encrypt_noreplace, 135 jid, namespace, not self.args.encrypt_noreplace,
136 self.profile, 136 self.profile,
137 callback=self.host.quit, 137 callback=self.host.quit,
138 errback=partial(self.errback, 138 errback=partial(self.errback,
139 msg=_(u"Can't start encryption session: {}"), 139 msg=_("Can't start encryption session: {}"),
140 exit_code=C.EXIT_BRIDGE_ERRBACK, 140 exit_code=C.EXIT_BRIDGE_ERRBACK,
141 )) 141 ))
142 142
143 def start(self): 143 def start(self):
144 if self.args.name is not None: 144 if self.args.name is not None:
145 self.host.bridge.encryptionNamespaceGet(self.args.name, 145 self.host.bridge.encryptionNamespaceGet(self.args.name,
146 callback=self.encryptionNamespaceGetCb, 146 callback=self.encryptionNamespaceGetCb,
147 errback=partial(self.errback, 147 errback=partial(self.errback,
148 msg=_(u"Can't get encryption namespace: {}"), 148 msg=_("Can't get encryption namespace: {}"),
149 exit_code=C.EXIT_BRIDGE_ERRBACK, 149 exit_code=C.EXIT_BRIDGE_ERRBACK,
150 )) 150 ))
151 elif self.args.namespace is not None: 151 elif self.args.namespace is not None:
152 self.encryptionNamespaceGetCb(self.args.namespace) 152 self.encryptionNamespaceGetCb(self.args.namespace)
153 else: 153 else:
154 self.encryptionNamespaceGetCb(u"") 154 self.encryptionNamespaceGetCb("")
155 155
156 156
157 class EncryptionStop(base.CommandBase): 157 class EncryptionStop(base.CommandBase):
158 158
159 def __init__(self, host): 159 def __init__(self, host):
160 super(EncryptionStop, self).__init__( 160 super(EncryptionStop, self).__init__(
161 host, "stop", 161 host, "stop",
162 help=_(u"stop encrypted session with an entity")) 162 help=_("stop encrypted session with an entity"))
163 self.need_loop = True 163 self.need_loop = True
164 164
165 def add_parser_options(self): 165 def add_parser_options(self):
166 self.parser.add_argument( 166 self.parser.add_argument(
167 "jid", type=base.unicode_decoder, 167 "jid",
168 help=_(u"jid of the entity to stop encrypted session with") 168 help=_("jid of the entity to stop encrypted session with")
169 ) 169 )
170 170
171 def start(self): 171 def start(self):
172 jids = self.host.check_jids([self.args.jid]) 172 jids = self.host.check_jids([self.args.jid])
173 jid = jids[0] 173 jid = jids[0]
174 self.host.bridge.messageEncryptionStop( 174 self.host.bridge.messageEncryptionStop(
175 jid, self.profile, 175 jid, self.profile,
176 callback=self.host.quit, 176 callback=self.host.quit,
177 errback=partial( 177 errback=partial(
178 self.errback, 178 self.errback,
179 msg=_(u"can't end encrypted session: {}"), 179 msg=_("can't end encrypted session: {}"),
180 exit_code=C.EXIT_BRIDGE_ERRBACK, 180 exit_code=C.EXIT_BRIDGE_ERRBACK,
181 ), 181 ),
182 ) 182 )
183 183
184 184
185 class TrustUI(base.CommandBase): 185 class TrustUI(base.CommandBase):
186 186
187 def __init__(self, host): 187 def __init__(self, host):
188 super(TrustUI, self).__init__( 188 super(TrustUI, self).__init__(
189 host, "ui", 189 host, "ui",
190 help=_(u"get UI to manage trust")) 190 help=_("get UI to manage trust"))
191 self.need_loop = True 191 self.need_loop = True
192 192
193 def add_parser_options(self): 193 def add_parser_options(self):
194 self.parser.add_argument( 194 self.parser.add_argument(
195 "jid", type=base.unicode_decoder, 195 "jid",
196 help=_(u"jid of the entity to stop encrypted session with") 196 help=_("jid of the entity to stop encrypted session with")
197 ) 197 )
198 algorithm = self.parser.add_mutually_exclusive_group() 198 algorithm = self.parser.add_mutually_exclusive_group()
199 algorithm.add_argument( 199 algorithm.add_argument(
200 "-n", "--name", help=_(u"algorithm name (DEFAULT: current algorithm)")) 200 "-n", "--name", help=_("algorithm name (DEFAULT: current algorithm)"))
201 algorithm.add_argument( 201 algorithm.add_argument(
202 "-N", "--namespace", 202 "-N", "--namespace",
203 help=_(u"algorithm namespace (DEFAULT: current algorithm)")) 203 help=_("algorithm namespace (DEFAULT: current algorithm)"))
204 204
205 def encryptionTrustUIGetCb(self, xmlui_raw): 205 def encryptionTrustUIGetCb(self, xmlui_raw):
206 xmlui = xmlui_manager.create(self.host, xmlui_raw) 206 xmlui = xmlui_manager.create(self.host, xmlui_raw)
207 xmlui.show() 207 xmlui.show()
208 xmlui.submitForm() 208 xmlui.submitForm()
213 self.host.bridge.encryptionTrustUIGet( 213 self.host.bridge.encryptionTrustUIGet(
214 jid, namespace, self.profile, 214 jid, namespace, self.profile,
215 callback=self.encryptionTrustUIGetCb, 215 callback=self.encryptionTrustUIGetCb,
216 errback=partial( 216 errback=partial(
217 self.errback, 217 self.errback,
218 msg=_(u"can't end encrypted session: {}"), 218 msg=_("can't end encrypted session: {}"),
219 exit_code=C.EXIT_BRIDGE_ERRBACK, 219 exit_code=C.EXIT_BRIDGE_ERRBACK,
220 ), 220 ),
221 ) 221 )
222 222
223 def start(self): 223 def start(self):
224 if self.args.name is not None: 224 if self.args.name is not None:
225 self.host.bridge.encryptionNamespaceGet(self.args.name, 225 self.host.bridge.encryptionNamespaceGet(self.args.name,
226 callback=self.encryptionNamespaceGetCb, 226 callback=self.encryptionNamespaceGetCb,
227 errback=partial(self.errback, 227 errback=partial(self.errback,
228 msg=_(u"Can't get encryption namespace: {}"), 228 msg=_("Can't get encryption namespace: {}"),
229 exit_code=C.EXIT_BRIDGE_ERRBACK, 229 exit_code=C.EXIT_BRIDGE_ERRBACK,
230 )) 230 ))
231 elif self.args.namespace is not None: 231 elif self.args.namespace is not None:
232 self.encryptionNamespaceGetCb(self.args.namespace) 232 self.encryptionNamespaceGetCb(self.args.namespace)
233 else: 233 else:
234 self.encryptionNamespaceGetCb(u"") 234 self.encryptionNamespaceGetCb("")
235 235
236 236
237 class EncryptionTrust(base.CommandBase): 237 class EncryptionTrust(base.CommandBase):
238 subcommands = (TrustUI,) 238 subcommands = (TrustUI,)
239 239
240 def __init__(self, host): 240 def __init__(self, host):
241 super(EncryptionTrust, self).__init__( 241 super(EncryptionTrust, self).__init__(
242 host, "trust", use_profile=False, help=_(u"trust manangement") 242 host, "trust", use_profile=False, help=_("trust manangement")
243 ) 243 )
244 244
245 245
246 class Encryption(base.CommandBase): 246 class Encryption(base.CommandBase):
247 subcommands = (EncryptionAlgorithms, EncryptionGet, EncryptionStart, EncryptionStop, 247 subcommands = (EncryptionAlgorithms, EncryptionGet, EncryptionStart, EncryptionStop,
248 EncryptionTrust) 248 EncryptionTrust)
249 249
250 def __init__(self, host): 250 def __init__(self, host):
251 super(Encryption, self).__init__( 251 super(Encryption, self).__init__(
252 host, "encryption", use_profile=False, help=_(u"encryption sessions handling") 252 host, "encryption", use_profile=False, help=_("encryption sessions handling")
253 ) 253 )