File: ibb.tcl

package info (click to toggle)
coccinella 0.96.20-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid, trixie
  • size: 13,184 kB
  • sloc: tcl: 124,744; xml: 206; makefile: 66; sh: 62
file content (491 lines) | stat: -rw-r--r-- 12,745 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
#  ibb.tcl --
#  
#      This file is part of the jabberlib. 
#      It provides support for the ibb stuff (In Band Bytestreams).
#      
#  Copyright (c) 2005  Mats Bengtsson
#  
# This file is distributed under BSD style license.
#  
# $Id: ibb.tcl,v 1.22 2007-11-30 14:38:34 matben Exp $
# 
############################# USAGE ############################################
#
#   NAME
#      ibb - convenience command library for the ibb part of XMPP.
#      
#   SYNOPSIS
#      jlib::ibb::init jlibname
#
#   OPTIONS
#   
#	
#   INSTANCE COMMANDS
#      jlibName ib send_set jid command ?-key value?
#      
############################# CHANGES ##########################################
#
#       0.1         first version

package require jlib
package require base64     ; # tcllib
package require jlib::disco
package require jlib::si

package provide jlib::ibb 0.1

namespace eval jlib::ibb {

    variable inited 0
    variable xmlns
    set xmlns(ibb) "http://jabber.org/protocol/ibb"
    set xmlns(amp) "http://jabber.org/protocol/amp"

    jlib::si::registertransport $xmlns(ibb) $xmlns(ibb) 80  \
      [namespace current]::si_open   \
      [namespace current]::si_close
    
    jlib::disco::registerfeature $xmlns(ibb)

    # Note: jlib::ensamble_register is last in this file!
}

# jlib::ibb::init --
# 
#       Sets up jabberlib handlers and makes a new instance if an ibb object.
  
proc jlib::ibb::init {jlibname args} {

    #puts "jlib::ibb::init"
    
    variable inited
    variable xmlns
    
    if {!$inited} {
	InitOnce
    }    

    # Keep different state arrays for initiator (i) and target (t).
    namespace eval ${jlibname}::ibb {
	variable priv
	variable opts
	variable istate
	variable tstate
    }
    upvar ${jlibname}::ibb::priv  priv
    upvar ${jlibname}::ibb::opts  opts
    
    array set opts {
	-block-size     4096
    }
    array set opts $args
    
    # Each base64 byte takes 6 bits; need to translate to binary bytes.
    set binblock [expr {(6 * $opts(-block-size))/8}]
    set priv(binblock) [expr {6 * ($binblock/6)}]
    
    # Register some standard iq handlers that is handled internally.
    $jlibname iq_register    set $xmlns(ibb) [namespace current]::handle_set
    $jlibname message_register * $xmlns(ibb) [namespace current]::message_handler

    return
}

proc jlib::ibb::InitOnce { } {
    
    variable ampElem
    variable inited
    variable xmlns
    
    set rule1 [wrapper::createtag "rule"   \
      -attrlist {condition deliver-at value stored action error}]
    set rule2 [wrapper::createtag "rule"   \
      -attrlist {condition match-resource value exact action error}]
    set ampElem [wrapper::createtag "amp"  \
      -attrlist [list xmlns $xmlns(amp)]   \
      -subtags [list $rule1 $rule2]]

    set inited 1
}

# jlib::ibb::cmdproc --
#
#       Just dispatches the command to the right procedure.
#
# Arguments:
#       jlibname:   the instance of this jlib.
#       cmd:        
#       args:       all args to the cmd procedure.
#       
# Results:
#       none.

proc jlib::ibb::cmdproc {jlibname cmd args} {
    
    # Which command? Just dispatch the command to the right procedure.
    return [eval {$cmd $jlibname} $args]
}

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#
# These are all functions to use by a initiator (sender).

# jlib::ibb::si_open, si_close --
# 
#       Bindings for si.

proc jlib::ibb::si_open {jlibname jid sid args} {
    
    upvar ${jlibname}::ibb::istate istate
    #puts "jlib::ibb::si_open (i)"
    
    set istate($sid,sid) $sid
    set istate($sid,jid) $jid
    set istate($sid,seq) 0
    set istate($sid,status) ""
    set si_open_cb [namespace current]::si_open_cb
    eval {send_open $jlibname $jid $sid $si_open_cb} $args
    return
}

proc jlib::ibb::si_open_cb {jlibname sid type subiq args} {
    
    upvar ${jlibname}::ibb::istate istate
    #puts "jlib::ibb::si_open_cb (i)"    
    
    # Since this is an async call we may have been reset.
    if {![info exists istate($sid,sid)]} {
	return
    }    
    jlib::si::transport_open_cb $jlibname $sid $type $subiq
    
    # If all went well this far we initiate the read/write data process.
    if {$type eq "result"} {
	
	# Tell the profile to prepare to read data (open file).
	jlib::si::open_data $jlibname $sid
	si_read $jlibname $sid
    }    
}

proc jlib::ibb::si_read {jlibname sid} {
    
    upvar ${jlibname}::ibb::istate istate
    #puts "jlib::ibb::si_read (i)"
    
    # Since this is an async call we may have been reset.
    if {![info exists istate($sid,sid)]} {
	return
    }    
    
    # We have been reset or something.
    if {$istate($sid,status) eq "close"} {
	return
    }
    set data [jlib::si::read_data $jlibname $sid]
    set len [string length $data]

    if {$len > 0} {
	si_send $jlibname $sid $data
    } else {
	
	# Empty data from the reader means that we are done.
	jlib::si::close_data $jlibname $sid
    }
}

proc jlib::ibb::si_send {jlibname sid data} {
    
    upvar ${jlibname}::ibb::istate istate
    #puts "jlib::ibb::si_send (i)"
    
    set jid $istate($sid,jid)
    send_data $jlibname $jid $sid $data [namespace current]::si_send_cb

    # Trick to avoid UI blocking.
    # @@@ We should have a method to detect if xmpp socket writable.
    after idle [list after 0 [list \
      [namespace current]::si_read $jlibname $sid]]
}

# jlib::ibb::si_send_cb --
# 
#       XEP says that we SHOULD track each mesage, in case of error.

proc jlib::ibb::si_send_cb {jlibname sid type subiq args} {

    upvar ${jlibname}::ibb::istate istate
    #puts "jlib::ibb::si_send_cb (i)"
    
    # We get this async so we may have been reset or something.
    if {![info exists istate($sid,sid)]} {
	return
    }
    if {[string equal $type "error"]} {
	jlib::si::close_data $jlibname $sid error
	ifree $jlibname $sid
    }
}

# jlib::ibb::si_close --
# 
#       The profile closes us down. It could be a reset.

proc jlib::ibb::si_close {jlibname sid} {
    
    upvar ${jlibname}::ibb::istate istate
    #puts "jlib::ibb::si_close (i)"

    # Keep a status so we can stop sending messages right away.
    set istate($sid,status) "close"
    set jid $istate($sid,jid)
    set cmd [namespace current]::si_close_cb

    send_close $jlibname $jid $sid $cmd
}

# jlib::ibb::si_close_cb --
# 
#       This is our destructor that ends it all.

proc jlib::ibb::si_close_cb {jlibname sid type subiq args} {
    
    upvar ${jlibname}::ibb::istate istate
    #puts "jlib::ibb::si_close_cb (i)"

    set jid $istate($sid,jid)
    
    jlib::si::transport_close_cb $jlibname $sid $type $subiq
    ifree $jlibname $sid
}

proc jlib::ibb::ifree {jlibname sid} {
    
    upvar ${jlibname}::ibb::istate istate
    #puts "jlib::ibb::ifree (i)"   

    array unset istate $sid,*
}

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

proc jlib::ibb::configure {jlibname args} {
    
    upvar ${jlibname}::ibb::opts opts

    # @@@ TODO
    
}

# jlib::ibb::send_open --
# 
#       Initiates a file transport. We must be able to configure 'block-size'
#       from the file-transfer profile.
#
# Arguments:
# 

proc jlib::ibb::send_open {jlibname jid sid cmd args} {    
    variable xmlns
    upvar ${jlibname}::ibb::opts opts
    
    #puts "jlib::ibb::send_open (i)"
    
    array set arr [list -block-size $opts(-block-size)]
    array set arr $args
        
    set openElem [wrapper::createtag "open"  \
      -attrlist [list sid $sid block-size $arr(-block-size) xmlns $xmlns(ibb)]]
    jlib::send_iq $jlibname set [list $openElem] -to $jid  \
      -command [concat $cmd [list $jlibname $sid]]
    return
}

# jlib::ibb::send_data --
# 
# 

proc jlib::ibb::send_data {jlibname jid sid data cmd} {    
    variable xmlns
    variable ampElem
    upvar ${jlibname}::ibb::istate istate
    #puts "jlib::ibb::send_data (i) sid=$sid, cmd=$cmd"

    set jid $istate($sid,jid)
    set seq $istate($sid,seq)
    set edata [base64::encode $data]
    set dataElem [wrapper::createtag "data"  \
      -attrlist [list xmlns $xmlns(ibb) sid $sid seq $seq]  \
      -chdata $edata]
    set istate($sid,seq) [expr {($seq + 1) % 65536}]

    jlib::send_message $jlibname $jid -xlist [list $dataElem $ampElem]  \
      -command [concat $cmd [list $jlibname $sid]]
}

# jlib::ibb::send_close --
# 
#       Sends the close tag.
#
# Arguments:
# 

proc jlib::ibb::send_close {jlibname jid sid cmd} {    
    variable xmlns
    #puts "jlib::ibb::send_close (i)"

    set closeElem [wrapper::createtag "close"  \
      -attrlist [list sid $sid xmlns $xmlns(ibb)]]
    jlib::send_iq $jlibname set [list $closeElem] -to $jid  \
      -command [concat $cmd [list $jlibname $sid]]
    return
}

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#
# These are all functions to use by a target (receiver) of a stream.

# jlib::ibb::handle_set --
# 
#       Parse incoming ibb iq-set open/close element.
#       It is being assumed that we already have accepted a stream initiation.

proc jlib::ibb::handle_set {jlibname from subiq args} {

    variable xmlns
    upvar ${jlibname}::ibb::tstate tstate
    
    #puts "jlib::ibb::handle_set (t)"
    
    set tag [wrapper::gettag $subiq]
    array set attr [wrapper::getattrlist $subiq]
    array set argsArr $args
    if {![info exists argsArr(-id)] || ![info exists attr(sid)]} {
	# We can't do more here.
	return 0
    }
    set sid $attr(sid)
    
    # We make sure that we have already got a si with this sid.
    if {![jlib::si::havesi $jlibname $sid]} {
	send_error $jlibname $from $argsArr(-id) $sid 404 cancel item-not-found
	return 1
    }

    switch -- $tag {
	open {
	    if {![info exists attr(block-size)]} {
		# @@@ better stanza!
		send_error $jlibname $from $argsArr(-id) $sid 501 cancel  \
		  feature_not_implemented
		return
	    }
	    set tstate($sid,sid)        $sid
	    set tstate($sid,jid)        $from
	    set tstate($sid,block-size) $attr(block-size)
	    set tstate($sid,seq)        0
	    
	    # Make a success response on open.
	    jlib::send_iq $jlibname "result" {} -to $from -id $argsArr(-id)
	}
	close {
	    
	    # Make a success response on close.
	    jlib::send_iq $jlibname "result" {} -to $from -id $argsArr(-id)
	    jlib::si::stream_closed $jlibname $sid
	    tfree $jlibname $sid
	}
	default {
	    return 0
	}
    }
    return 1
}

# jlib::ibb::message_handler --
# 
#       Message handler for incoming http://jabber.org/protocol/ibb elements.

proc jlib::ibb::message_handler {jlibname ns msgElem args} {

    variable xmlns
    upvar ${jlibname}::ibb::tstate tstate
    
    array set argsArr $args
    #puts "jlib::ibb::message_handler (t) ns=$ns"
    
    set jid [wrapper::getattribute $msgElem "from"]
    
    # Pack up the data and deliver to si.
    set dataElems [wrapper::getchildswithtagandxmlns $msgElem data $xmlns(ibb)]
    foreach dataElem $dataElems {
	array set attr [wrapper::getattrlist $dataElem]
	set sid $attr(sid)
	set seq $attr(seq)
		
	# We make sure that we have already got a si with this sid.
	# Since there can be many of these, reply with error only to first.
	if {![jlib::si::havesi $jlibname $sid]  \
	  || ![info exists tstate($sid,sid)]} {
	    if {[info exists argsArr(-id)]} {
		set id $argsArr(-id)
		jlib::send_message_error $jlibname $jid $id 404 cancel  \
		  item-not-found
	    }
	    return 1
	}
	
	# Check that no packets have been lost.
	if {$seq != $tstate($sid,seq)} {
	    if {[info exists argsArr(-id)]} {
		#puts "\t seq=$seq, expectseq=$expectseq"
		set id $argsArr(-id)
		jlib::send_message_error $jlibname $jid $id 400 cancel  \
		  bad-request
	    }
	    return 1
	}
	
	set encdata [wrapper::getcdata $dataElem]
	if {[catch {
	    set data [base64::decode $encdata]
	}]} {
	    if {[info exists argsArr(-id)]} {
		jlib::send_message_error $jlibname $jid $id 400 cancel bad-request
	    }
	    return 1
	}
	
	# Next expected 'seq'.
	set tstate($sid,seq) [expr {($seq + 1) % 65536}]

	# Deliver to si for further processing.
	jlib::si::stream_recv $jlibname $sid $data
    }
    return 1
}

proc jlib::ibb::send_error {jlibname jid id sid errcode errtype stanza} {

    jlib::send_iq_error $jlibname $jid $id $errcode $errtype $stanza    
    tfree $jlibname $sid
}

proc jlib::ibb::tfree {jlibname sid} {
    
    upvar ${jlibname}::ibb::tstate tstate
    #puts "jlib::ibb::tfree (t)"   

    array unset tstate $sid,*
}

# We have to do it here since need the initProc before doing this.

namespace eval jlib::ibb {

    jlib::ensamble_register ibb   \
      [namespace current]::init   \
      [namespace current]::cmdproc
}

#-------------------------------------------------------------------------------