15
15
# limitations under the License.
16
16
17
17
""" Starts a synapse client console. """
18
+ from __future__ import print_function
18
19
19
20
from twisted .internet import reactor , defer , threads
20
21
from http import TwistedHttpClient
@@ -109,7 +110,7 @@ def do_config(self, line):
109
110
by using $. E.g. 'config roomid room1' then 'raw get /rooms/$roomid'.
110
111
"""
111
112
if len (line ) == 0 :
112
- print json .dumps (self .config , indent = 4 )
113
+ print ( json .dumps (self .config , indent = 4 ) )
113
114
return
114
115
115
116
try :
@@ -123,8 +124,8 @@ def do_config(self, line):
123
124
]
124
125
for key , valid_vals in config_rules :
125
126
if key == args ["key" ] and args ["val" ] not in valid_vals :
126
- print "%s value must be one of %s" % (args ["key" ],
127
- valid_vals )
127
+ print ( "%s value must be one of %s" % (args ["key" ],
128
+ valid_vals ))
128
129
return
129
130
130
131
# toggle the http client verbosity
@@ -133,11 +134,11 @@ def do_config(self, line):
133
134
134
135
# assign the new config
135
136
self .config [args ["key" ]] = args ["val" ]
136
- print json .dumps (self .config , indent = 4 )
137
+ print ( json .dumps (self .config , indent = 4 ) )
137
138
138
139
save_config (self .config )
139
140
except Exception as e :
140
- print e
141
+ print ( e )
141
142
142
143
def do_register (self , line ):
143
144
"""Registers for a new account: "register <userid> <noupdate>"
@@ -153,7 +154,7 @@ def do_register(self, line):
153
154
pwd = getpass .getpass ("Type a password for this user: " )
154
155
pwd2 = getpass .getpass ("Retype the password: " )
155
156
if pwd != pwd2 or len (pwd ) == 0 :
156
- print "Password mismatch."
157
+ print ( "Password mismatch." )
157
158
pwd = None
158
159
else :
159
160
password = pwd
@@ -174,12 +175,12 @@ def _do_register(self, data, update_config):
174
175
# check the registration flows
175
176
url = self ._url () + "/register"
176
177
json_res = yield self .http_client .do_request ("GET" , url )
177
- print json .dumps (json_res , indent = 4 )
178
+ print ( json .dumps (json_res , indent = 4 ) )
178
179
179
180
passwordFlow = None
180
181
for flow in json_res ["flows" ]:
181
182
if flow ["type" ] == "m.login.recaptcha" or ("stages" in flow and "m.login.recaptcha" in flow ["stages" ]):
182
- print "Unable to register: Home server requires captcha."
183
+ print ( "Unable to register: Home server requires captcha." )
183
184
return
184
185
if flow ["type" ] == "m.login.password" and "stages" not in flow :
185
186
passwordFlow = flow
@@ -189,7 +190,7 @@ def _do_register(self, data, update_config):
189
190
return
190
191
191
192
json_res = yield self .http_client .do_request ("POST" , url , data = data )
192
- print json .dumps (json_res , indent = 4 )
193
+ print ( json .dumps (json_res , indent = 4 ) )
193
194
if update_config and "user_id" in json_res :
194
195
self .config ["user" ] = json_res ["user_id" ]
195
196
self .config ["token" ] = json_res ["access_token" ]
@@ -215,7 +216,7 @@ def do_login(self, line):
215
216
reactor .callFromThread (self ._do_login , user , p )
216
217
#print " got %s " % p
217
218
except Exception as e :
218
- print e
219
+ print ( e )
219
220
220
221
@defer .inlineCallbacks
221
222
def _do_login (self , user , password ):
@@ -227,13 +228,13 @@ def _do_login(self, user, password):
227
228
}
228
229
url = self ._url () + path
229
230
json_res = yield self .http_client .do_request ("POST" , url , data = data )
230
- print json_res
231
+ print ( json_res )
231
232
232
233
if "access_token" in json_res :
233
234
self .config ["user" ] = user
234
235
self .config ["token" ] = json_res ["access_token" ]
235
236
save_config (self .config )
236
- print "Login successful."
237
+ print ( "Login successful." )
237
238
238
239
@defer .inlineCallbacks
239
240
def _check_can_login (self ):
@@ -242,10 +243,10 @@ def _check_can_login(self):
242
243
# submitting!
243
244
url = self ._url () + path
244
245
json_res = yield self .http_client .do_request ("GET" , url )
245
- print json_res
246
+ print ( json_res )
246
247
247
248
if "flows" not in json_res :
248
- print "Failed to find any login flows."
249
+ print ( "Failed to find any login flows." )
249
250
defer .returnValue (False )
250
251
251
252
flow = json_res ["flows" ][0 ] # assume first is the one we want.
@@ -275,9 +276,9 @@ def _do_emailrequest(self, args):
275
276
276
277
json_res = yield self .http_client .do_request ("POST" , url , data = urllib .urlencode (args ), jsonreq = False ,
277
278
headers = {'Content-Type' : ['application/x-www-form-urlencoded' ]})
278
- print json_res
279
+ print ( json_res )
279
280
if 'sid' in json_res :
280
- print "Token sent. Your session ID is %s" % (json_res ['sid' ])
281
+ print ( "Token sent. Your session ID is %s" % (json_res ['sid' ]) )
281
282
282
283
def do_emailvalidate (self , line ):
283
284
"""Validate and associate a third party ID
@@ -297,7 +298,7 @@ def _do_emailvalidate(self, args):
297
298
298
299
json_res = yield self .http_client .do_request ("POST" , url , data = urllib .urlencode (args ), jsonreq = False ,
299
300
headers = {'Content-Type' : ['application/x-www-form-urlencoded' ]})
300
- print json_res
301
+ print ( json_res )
301
302
302
303
def do_3pidbind (self , line ):
303
304
"""Validate and associate a third party ID
@@ -317,23 +318,23 @@ def _do_3pidbind(self, args):
317
318
318
319
json_res = yield self .http_client .do_request ("POST" , url , data = urllib .urlencode (args ), jsonreq = False ,
319
320
headers = {'Content-Type' : ['application/x-www-form-urlencoded' ]})
320
- print json_res
321
+ print ( json_res )
321
322
322
323
def do_join (self , line ):
323
324
"""Joins a room: "join <roomid>" """
324
325
try :
325
326
args = self ._parse (line , ["roomid" ], force_keys = True )
326
327
self ._do_membership_change (args ["roomid" ], "join" , self ._usr ())
327
328
except Exception as e :
328
- print e
329
+ print ( e )
329
330
330
331
def do_joinalias (self , line ):
331
332
try :
332
333
args = self ._parse (line , ["roomname" ], force_keys = True )
333
334
path = "/join/%s" % urllib .quote (args ["roomname" ])
334
335
reactor .callFromThread (self ._run_and_pprint , "POST" , path , {})
335
336
except Exception as e :
336
- print e
337
+ print ( e )
337
338
338
339
def do_topic (self , line ):
339
340
""""topic [set|get] <roomid> [<newtopic>]"
@@ -343,17 +344,17 @@ def do_topic(self, line):
343
344
try :
344
345
args = self ._parse (line , ["action" , "roomid" , "topic" ])
345
346
if "action" not in args or "roomid" not in args :
346
- print "Must specify set|get and a room ID."
347
+ print ( "Must specify set|get and a room ID." )
347
348
return
348
349
if args ["action" ].lower () not in ["set" , "get" ]:
349
- print "Must specify set|get, not %s" % args ["action" ]
350
+ print ( "Must specify set|get, not %s" % args ["action" ])
350
351
return
351
352
352
353
path = "/rooms/%s/topic" % urllib .quote (args ["roomid" ])
353
354
354
355
if args ["action" ].lower () == "set" :
355
356
if "topic" not in args :
356
- print "Must specify a new topic."
357
+ print ( "Must specify a new topic." )
357
358
return
358
359
body = {
359
360
"topic" : args ["topic" ]
@@ -362,7 +363,7 @@ def do_topic(self, line):
362
363
elif args ["action" ].lower () == "get" :
363
364
reactor .callFromThread (self ._run_and_pprint , "GET" , path )
364
365
except Exception as e :
365
- print e
366
+ print ( e )
366
367
367
368
def do_invite (self , line ):
368
369
"""Invite a user to a room: "invite <userid> <roomid>" """
@@ -373,7 +374,7 @@ def do_invite(self, line):
373
374
374
375
reactor .callFromThread (self ._do_invite , args ["roomid" ], user_id )
375
376
except Exception as e :
376
- print e
377
+ print ( e )
377
378
378
379
@defer .inlineCallbacks
379
380
def _do_invite (self , roomid , userstring ):
@@ -393,29 +394,29 @@ def _do_invite(self, roomid, userstring):
393
394
if 'public_key' in pubKeyObj :
394
395
pubKey = nacl .signing .VerifyKey (pubKeyObj ['public_key' ], encoder = nacl .encoding .HexEncoder )
395
396
else :
396
- print "No public key found in pubkey response!"
397
+ print ( "No public key found in pubkey response!" )
397
398
398
399
sigValid = False
399
400
400
401
if pubKey :
401
402
for signame in json_res ['signatures' ]:
402
403
if signame not in TRUSTED_ID_SERVERS :
403
- print "Ignoring signature from untrusted server %s" % (signame )
404
+ print ( "Ignoring signature from untrusted server %s" % (signame ) )
404
405
else :
405
406
try :
406
407
verify_signed_json (json_res , signame , pubKey )
407
408
sigValid = True
408
- print "Mapping %s -> %s correctly signed by %s" % (userstring , json_res ['mxid' ], signame )
409
+ print ( "Mapping %s -> %s correctly signed by %s" % (userstring , json_res ['mxid' ], signame ) )
409
410
break
410
411
except SignatureVerifyException as e :
411
- print "Invalid signature from %s" % (signame )
412
- print e
412
+ print ( "Invalid signature from %s" % (signame ) )
413
+ print ( e )
413
414
414
415
if sigValid :
415
- print "Resolved 3pid %s to %s" % (userstring , json_res ['mxid' ])
416
+ print ( "Resolved 3pid %s to %s" % (userstring , json_res ['mxid' ]) )
416
417
mxid = json_res ['mxid' ]
417
418
else :
418
- print "Got association for %s but couldn't verify signature" % (userstring )
419
+ print ( "Got association for %s but couldn't verify signature" % (userstring ) )
419
420
420
421
if not mxid :
421
422
mxid = "@" + userstring + ":" + self ._domain ()
@@ -428,7 +429,7 @@ def do_leave(self, line):
428
429
args = self ._parse (line , ["roomid" ], force_keys = True )
429
430
self ._do_membership_change (args ["roomid" ], "leave" , self ._usr ())
430
431
except Exception as e :
431
- print e
432
+ print ( e )
432
433
433
434
def do_send (self , line ):
434
435
"""Sends a message. "send <roomid> <body>" """
@@ -453,10 +454,10 @@ def do_list(self, line):
453
454
"""
454
455
args = self ._parse (line , ["type" , "roomid" , "qp" ])
455
456
if not "type" in args or not "roomid" in args :
456
- print "Must specify type and room ID."
457
+ print ( "Must specify type and room ID." )
457
458
return
458
459
if args ["type" ] not in ["members" , "messages" ]:
459
- print "Unrecognised type: %s" % args ["type" ]
460
+ print ( "Unrecognised type: %s" % args ["type" ])
460
461
return
461
462
room_id = args ["roomid" ]
462
463
path = "/rooms/%s/%s" % (urllib .quote (room_id ), args ["type" ])
@@ -468,7 +469,7 @@ def do_list(self, line):
468
469
key_value = key_value_str .split ("=" )
469
470
qp [key_value [0 ]] = key_value [1 ]
470
471
except :
471
- print "Bad query param: %s" % key_value
472
+ print ( "Bad query param: %s" % key_value )
472
473
return
473
474
474
475
reactor .callFromThread (self ._run_and_pprint , "GET" , path ,
@@ -508,14 +509,14 @@ def do_raw(self, line):
508
509
args = self ._parse (line , ["method" , "path" , "data" ])
509
510
# sanity check
510
511
if "method" not in args or "path" not in args :
511
- print "Must specify path and method."
512
+ print ( "Must specify path and method." )
512
513
return
513
514
514
515
args ["method" ] = args ["method" ].upper ()
515
516
valid_methods = ["PUT" , "GET" , "POST" , "DELETE" ,
516
517
"XPUT" , "XGET" , "XPOST" , "XDELETE" ]
517
518
if args ["method" ] not in valid_methods :
518
- print "Unsupported method: %s" % args ["method" ]
519
+ print ( "Unsupported method: %s" % args ["method" ])
519
520
return
520
521
521
522
if "data" not in args :
@@ -524,7 +525,7 @@ def do_raw(self, line):
524
525
try :
525
526
args ["data" ] = json .loads (args ["data" ])
526
527
except Exception as e :
527
- print "Data is not valid JSON. %s" % e
528
+ print ( "Data is not valid JSON. %s" % e )
528
529
return
529
530
530
531
qp = {"access_token" : self ._tok ()}
@@ -553,7 +554,7 @@ def do_stream(self, line):
553
554
try :
554
555
timeout = int (args ["timeout" ])
555
556
except ValueError :
556
- print "Timeout must be in milliseconds."
557
+ print ( "Timeout must be in milliseconds." )
557
558
return
558
559
reactor .callFromThread (self ._do_event_stream , timeout )
559
560
@@ -566,7 +567,7 @@ def _do_event_stream(self, timeout):
566
567
"timeout" : str (timeout ),
567
568
"from" : self .event_stream_token
568
569
})
569
- print json .dumps (res , indent = 4 )
570
+ print ( json .dumps (res , indent = 4 ) )
570
571
571
572
if "chunk" in res :
572
573
for event in res ["chunk" ]:
@@ -669,9 +670,9 @@ def _run_and_pprint(self, method, path, data=None,
669
670
data = data ,
670
671
qparams = query_params )
671
672
if alt_text :
672
- print alt_text
673
+ print ( alt_text )
673
674
else :
674
- print json .dumps (json_res , indent = 4 )
675
+ print ( json .dumps (json_res , indent = 4 ) )
675
676
676
677
677
678
def save_config (config ):
@@ -680,16 +681,16 @@ def save_config(config):
680
681
681
682
682
683
def main (server_url , identity_server_url , username , token , config_path ):
683
- print "Synapse command line client"
684
- print "==========================="
685
- print "Server: %s" % server_url
686
- print "Type 'help' to get started."
687
- print "Close this console with CTRL+C then CTRL+D."
684
+ print ( "Synapse command line client" )
685
+ print ( "===========================" )
686
+ print ( "Server: %s" % server_url )
687
+ print ( "Type 'help' to get started." )
688
+ print ( "Close this console with CTRL+C then CTRL+D." )
688
689
if not username or not token :
689
- print "- 'register <username>' - Register an account"
690
- print "- 'stream' - Connect to the event stream"
691
- print "- 'create <roomid>' - Create a room"
692
- print "- 'send <roomid> <message>' - Send a message"
690
+ print ( "- 'register <username>' - Register an account" )
691
+ print ( "- 'stream' - Connect to the event stream" )
692
+ print ( "- 'create <roomid>' - Create a room" )
693
+ print ( "- 'send <roomid> <message>' - Send a message" )
693
694
http_client = TwistedHttpClient ()
694
695
695
696
# the command line client
@@ -705,7 +706,7 @@ def main(server_url, identity_server_url, username, token, config_path):
705
706
http_client .verbose = "on" == syn_cmd .config ["verbose" ]
706
707
except :
707
708
pass
708
- print "Loaded config from %s" % config_path
709
+ print ( "Loaded config from %s" % config_path )
709
710
except :
710
711
pass
711
712
@@ -736,7 +737,7 @@ def main(server_url, identity_server_url, username, token, config_path):
736
737
args = parser .parse_args ()
737
738
738
739
if not args .server :
739
- print "You must supply a server URL to communicate with."
740
+ print ( "You must supply a server URL to communicate with." )
740
741
parser .print_help ()
741
742
sys .exit (1 )
742
743
0 commit comments