-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmppcmd.bash
executable file
·1243 lines (1048 loc) · 27.1 KB
/
xmppcmd.bash
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# Author: Craig Hesling <[email protected]>
# Date: June 30, 2015
# September 1, 2016
#
# Usage:
# . xmppcmd.sh [jid] [pass] [pubsub_host]
# The script can also get it's jid, password, and pubsub host from
# the environment variables XMPP_JID, XMPP_PASS, and XMPP_PUBSUB
# OR from the defaults specified in a xmpprc file.
#
# Description:
# Upon sourcing, the startup script will try to assign user, host, pass,
# and pubsub server from the environment variables XMPP_JID, XMPP_PASS, and
# XMPP_PUBSUB. If this fails, the default values from a local or
# ~/.config/xmpprc file are used. Next, the startup script will try to
# override the user, host, pass, and pubsub host with argument supplied jid,
# pass, and pubsub host. The previously set values are used if no arguments
# are supplied.
# Settings:
# Command line args override environmental vars, environmental vars override
# an xmpprc in the working directory, and an xmpprc in the working directory
# overrides the ~/.config/xmpprc
XMPPCMD_VERSION=2.1
# Master list of user commands
XMPP_CMDS=( )
XMPP_CMDS+=( xmpphelp , )
XMPP_CMDS+=( pretty , )
XMPP_CMDS+=( get_config get_jid get_pass get_pubsub , )
XMPP_CMDS+=( message create delete publish retract purge , )
XMPP_CMDS+=( subscribe unsubscribe , )
XMPP_CMDS+=( get_features , )
XMPP_CMDS+=( get_nodes get_items get_item , )
XMPP_CMDS+=( get_subscriptions get_subscribers set_subscribers , )
XMPP_CMDS+=( get_affiliations get_affiliates get_options set_affiliations , )
XMPP_CMDS+=( get_vcard set_vcard , )
XMPP_CMDS+=( send send_stanza_iq stanza_pubsub , )
XMPP_CMDS+=( list_nodes , )
XMPP_CMDS+=( recv )
# Show the help message
alias xhelp='xmpphelp'
xmpphelp() {
# check args
if (( $# < 0 )) || [[ "$1" =~ --help ]] || [[ "$1" =~ -h ]]; then
echo "Usage: xmpphelp [command1 [command2 [...]]]"
echo "Get help with one or many commands"
return 0
fi
if [ $# -gt 0 ]; then
# Get help for certain commands
for i; do
echo "\$ $i --help"
$i --help
echo
done
else
# Print help message
echo "Tip: You can use $(font yellow)xhelp$(font off) in place of xmpphelp."
# List off all commands
font bold
echo "Valid commands:"
font off
for i in ${XMPP_CMDS[@]}; do
if [ "$i" = "," ]; then
echo
else
echo " $i"
fi
done
fi
}
# DEPRECIATED method
# Using the commandline utility sendxmpp
# echo "raw xml" | send
# xmpp_user=tom echo "raw xml" | send
send_sendxmpp() {
# Check if sendxmpp is installed
if ! hash sendxmpp &>/dev/null; then
font red bold >&2
echo "Error - sendxmpp is not installed">&2
font off >&2
return
fi
if (( DEBUG > 0 )); then
font yellow >&2
echo "Sending from: $xmpp_user@$xmpp_host" >&2
font off >&2
fi
if (( DEBUG < 1 )); then
sendxmpp --raw -u $xmpp_user -j $xmpp_host -p $xmpp_pass >&2
elif (( DEBUG < 2)); then
sendxmpp --raw -v -u $xmpp_user -j $xmpp_host -p $xmpp_pass >&2
else
sendxmpp --raw -d -u $xmpp_user -j $xmpp_host -p $xmpp_pass >&2
fi
}
# Using the custom commandline program xmppsend for two-way comm
# send_xmpp [stanza_id]
send_xmppsend() {
local stanza_id=$1
if (( DEBUG > 0 )); then
font yellow >&2
echo "Sending from: $xmpp_user@$xmpp_host" >&2
font off >&2
fi
if (( DEBUG < 1 )); then
# Silent Mode - Throw away debug info
timeout $SEND_TIMEOUT \
$XMPPTOOLS_DIR/xmppsend "$(get_jid)" "$(get_pass)" ${stanza_id} 2>/dev/null
else
# Show Debug Info - Display in blue
font blue >&2
timeout $SEND_TIMEOUT \
$XMPPTOOLS_DIR/xmppsend "$(get_jid)" "$(get_pass)" ${stanza_id}
font off >&2
fi
}
flatten_and_check() {
xmllint --dropdtd --nowrap --noblanks - | tail -n +2
}
# Format and print xml given on stdin if enabled
# echo "raw xml" | xml_prettyprint
xml_prettyprint() {
# bypass pretty print util when disabled
if (( ! xml_pretty_enable )); then
cat
return 0
fi
case $XML_PRETTYPRINT_UTIL in
xmllint)
# tail will kill the first line, which is an inserted xml version line
# we also kill errors emitted by xmllint
xmllint --format --recover --nowarning - | tail -n +2
;;
*)
$XML_PRETTYPRINT_UTIL
;;
esac
}
# echo "raw xml" | send
# xmpp_user=tom echo "raw xml" | send
send() {
# check args
if (( $# < 0 )) || [[ "$1" =~ --help ]] || [[ "$1" =~ -h ]]; then
echo "Usage: send"
return 0
fi
# Test if the JID is properly set
if ! get_jid > /dev/null; then
if (( DEBUG > 0 )); then
echo "Error - JID not set" >&2
fi
return 1
fi
if [ $# -gt 0 ]; then
# wait for response and format response
flatten_and_check | send_xmppsend $@ | xml_prettyprint
else
# no response
flatten_and_check | send_xmppsend
fi
}
# Open stream and listen on jid
# recv < -s [name [type [ns]]] | -p [node] >
recv() {
# check args
if (( $# < 0 )) || [[ "$1" =~ --help ]] || [[ "$1" =~ -h ]]; then
echo "Usage: recv <-s [name [type [ns]]] | -p [node]>"
echo "Open a stream and listen on JID"
return 0
fi
$XMPPTOOLS_DIR/xmpprecv "${xmpp_user}@${xmpp_host}" ${xmpp_pass} $@
}
# This function allows you to input an unqualified jid, like bob
# and get the local qualified jid [email protected]
# Example: jid=$(qualify_jid bob)
qualify_jid() {
local to=$1
if [[ "$to" =~ "@" ]]; then
# already qualified - pass through
echo "${to}"
elif [[ "$to" =~ "pubsub" ]]; then
# special pubsub - pass through
echo "${to}"
elif [ "$to" == "" ]; then
# special blank - pass through
echo "$to"
else
# unqualified jid - automatically qualify
echo "${to}@${xmpp_host}"
fi
}
# Generate a unique id
newid() {
echo "xmppsend$RANDOM"
}
##########################################################################
# XML Stanza Manipulation Primitives #
##########################################################################
# Sends an EOF to terminate a stream of stanzas
eof() {
printf ""
}
# Core XML stanza building block
# stanza <tag> [key=value [key2=value2 [...]]]
stanza() {
local tag=$1
local attrs=""
shift 1
for attr; do
# place 's around the key's value
attrs+=" $(echo $attr | sed "s/=/='/g;s/\$/'/g")"
done
# try first char
read -N1 firstchar
if [ -z "$firstchar" ]; then
printf "<${tag}${attrs:- }/>"
else
printf "<${tag}${attrs}>"
printf "${firstchar}"
cat
printf "</${tag}>"
fi
}
# stanza_iq <type> <id> [to]
stanza_iq() {
local typ=$1
local id=$2
local to=$(qualify_jid ${3-$xmpp_pubsub})
# check args
if (( $# < 2 )) || [[ "$1" =~ --help ]] || [[ "$1" =~ -h ]]; then
echo "Usage: stanza_iq <type> <id> [to]"
return 0
fi
if [ -n "$to" ]; then
stanza iq "type=$typ" "id=$id" "to=$to"
else
stanza iq "type=$typ" "id=$id"
fi
}
# send_stanza_iq <type> [to]
# Special functionality: if [to] is "", the to attribute is omitted
# Example: echo "<atom>blah</atom>" | send_stanza_iq get
send_stanza_iq() {
local typ=$1
local to=$(qualify_jid ${2-$xmpp_pubsub})
# check args
if (( $# < 1 )) || [[ "$1" =~ --help ]] || [[ "$1" =~ -h ]]; then
echo "Usage: send_stanza_iq <type> [to]"
return 0
fi
local id=`newid`
stanza_iq "$typ" "$id" "$to" | send "$id"
}
# stanza_pubsub [owner]
stanza_pubsub() {
# check args
if (( $# < 0 )) || [[ "$1" =~ --help ]] || [[ "$1" =~ -h ]]; then
echo "Usage: stanza_pubsub [owner]"
return 0
fi
local ns="http://jabber.org/protocol/pubsub"
if [ $# -gt 0 ]; then
ns+="#owner"
fi
stanza pubsub "xmlns=$ns"
}
# stanza_query <items|info> [node]
stanza_query() {
local typ=$1
local node=$2
if [ -n "$node" ]; then
stanza query "xmlns=http://jabber.org/protocol/disco#$typ" "node=$node"
else
stanza query "xmlns=http://jabber.org/protocol/disco#$typ"
fi
}
##########################################################################
# Runtime Config Interface #
##########################################################################
# Set whether xml pretty printing is on or off
pretty() {
local opt=$1
# check args
if (( $# < 0 )) || [[ "$1" =~ --help ]] || [[ "$1" =~ -h ]]; then
echo "Usage: pretty [on | off]"
echo "Set whether xml pretty printing is on or off"
return 0
fi
case $opt in
"")
(( xml_pretty_enable )) && echo on || echo off
;;
on|1)
xml_pretty_enable=1
;;
off|0)
xml_pretty_enable=0
;;
*)
pretty --help
return 1
;;
esac
}
# Print out the active jid
# returns with 1 if JID not set
get_jid() {
# check args
if (( $# < 0 )) || [[ "$1" =~ --help ]] || [[ "$1" =~ -h ]]; then
echo "Usage: get_jid"
echo "Print out the active JID being used"
return 0
fi
if [ -z "${xmpp_user}" -o -z "${xmpp_host}" ]; then
return 1
fi
echo ${xmpp_user}@${xmpp_host}
}
# Set the active jid
set_jid() {
local jid=( ${1/@/ } ) # separate at @
jid=( ${jid[@]/\// } ) # further separate at /
# check args
if (( $# < 1 )) || [[ "$1" =~ --help ]] || [[ "$1" =~ -h ]]; then
echo "Usage: set_jid <jid>"
echo "Set the active JID to use"
return 0
fi
# Check that we have exactly two parts
if [ ${#jid[@]} -lt 2 -o ${#jid[@]} -gt 3 ]; then
echo "Error - Invalid jid"
return 1
fi
xmpp_user=${jid[0]}
xmpp_host=${jid[1]}
# future support of resources starts here
#xmpp_res=${jid[2]}
}
# Print out the active password
get_pass() {
# check args
if (( $# < 0 )) || [[ "$1" =~ --help ]] || [[ "$1" =~ -h ]]; then
echo "Usage: get_pass"
echo "Print out the active password being used"
return 0
fi
echo ${xmpp_pass}
}
# Set the active password
set_pass() {
local password="$1"
# check args
if (( $# < 1 )) || [[ "$1" =~ --help ]] || [[ "$1" =~ -h ]]; then
echo "Usage: set_pass <password>"
echo "Set the active password to use"
return 0
fi
xmpp_pass="${password}"
}
# Print out the active pubsub host
get_pubsub() {
# check args
if (( $# < 0 )) || [[ "$1" =~ --help ]] || [[ "$1" =~ -h ]]; then
echo "Usage: get_pubsub"
echo "Print out the active pubsub host being used"
return 0
fi
echo ${xmpp_pubsub}
}
# Set the active pubsub host
set_pubsub() {
local pubsub=$1
# check args
if (( $# < 1 )) || [[ "$1" =~ --help ]] || [[ "$1" =~ -h ]]; then
echo "Usage: set_pubsub < - | <pubsub_host> >"
echo "Set the active pubsub host to use"
echo " Specify \"-\" to use the default pubsub for your host"
return 0
fi
if [ "$pubsub" = "-" ]; then
xmpp_pubsub="pubsub.${xmpp_host}"
else
xmpp_pubsub=${pubsub}
fi
}
# Show all current configuration settings
get_config() {
# check args
if (( $# < 0 )) || [[ "$1" =~ --help ]] || [[ "$1" =~ -h ]]; then
echo "Usage: get_config"
return 0
fi
font bold
echo "# General Settings #"
font off
echo "DEBUG=$DEBUG"
font bold
echo "# XMPP User Settings #"
font off
echo "user=$xmpp_user"
echo "host=$xmpp_host"
echo "pass=$xmpp_pass"
echo "pubsub=$xmpp_pubsub"
font bold
echo "# Extra Info #"
font off
echo "Run $(font yellow)xmpphelp$(font off) to show allowed commands."
echo -n "Run any command with $(font yellow)--help$(font off) to "
echo "get usage information."
echo "Version $XMPPCMD_VERSION"
}
##########################################################################
# XMPP/PubSub Interface Level #
##########################################################################
# message <to> <message_body | ->
message() {
local to=$(qualify_jid $1)
# check args
if (( $# < 2 )) || [[ "$1" =~ --help ]] || [[ "$1" =~ -h ]]; then
echo "Usage: message <to> <message_body | ->"
echo "Specify - to read body from stdin"
return 0
fi
shift 1
local id=`newid`
if [ "$*" = "-" ]; then
stanza body \
| stanza message "type=chat" "id=$id" "to=$to" \
| send
else
echo -n $* \
| stanza body \
| stanza message "type=chat" "id=$id" "to=$to" \
| send
fi
}
# Create a node
# create <node>
create() {
local node=$1
# check args
if (( $# < 1 )) || [[ "$1" =~ --help ]] || [[ "$1" =~ -h ]]; then
echo "Usage: create <node>"
echo "Create a node"
return 0
fi
eof \
| stanza create "node=$node" \
| stanza_pubsub \
| send_stanza_iq set
}
# Delete a node
# delete <node>
delete() {
local node=$1
# check args
if (( $# < 1 )) || [[ "$1" =~ --help ]] || [[ "$1" =~ -h ]]; then
echo "Usage: delete <node>"
echo "Delete a node"
return 0
fi
eof \
| stanza delete "node=$node" \
| stanza_pubsub owner \
| send_stanza_iq set
}
# publish <node> <item_id> < item_content | - >
publish() {
local node=$1
local item_id=$2
# check args
if (( $# < 3 )) || [[ "$1" =~ --help ]] || [[ "$1" =~ -h ]]; then
echo "Usage: publish <node> <item_id> < item_content | - > "
echo "Publish content to a pubsub node"
echo " Using the \"-\" indicates using stdin as item_content"
return 0
fi
shift 2
# Emit content to publish
if [ "$*" = "-" ]; then
cat
else
echo -n $*
fi \
| stanza item "id=$item_id" \
| stanza publish "node=$node" \
| stanza_pubsub \
| send_stanza_iq set
}
# Delete a node's item
# retract <node> <item_id>
retract() {
local node=$1
local item_id=$2
# check args
if (( $# < 2 )) || [[ "$1" =~ --help ]] || [[ "$1" =~ -h ]]; then
echo "Usage: retract <node> <item_id>"
echo "Deletes the item with \"item_id\" from \"node\""
return 0
fi
eof \
| stanza item "id=$item_id" \
| stanza retract "node=$node" \
| stanza_pubsub \
| send_stanza_iq set
}
# Purge all node items
# purge <node>
purge() {
local node=$1
# check args
if (( $# < 1 )) || [[ "$1" =~ --help ]] || [[ "$1" =~ -h ]]; then
echo "Usage: purge <node>"
return 0
fi
eof \
| stanza purge "node=$node" \
| stanza_pubsub owner \
| send_stanza_iq set
}
# subscribe <node> [jid]
subscribe() {
local node=$1
local jid=$(qualify_jid ${2-$xmpp_user})
# check args
if (( $# < 1 )) || [[ "$1" =~ --help ]] || [[ "$1" =~ -h ]]; then
echo "Usage: subscribe <node> [jid]"
echo "Subscribe \"jid\" to the pubsub \"node\""
return 0
fi
eof \
| stanza subscribe "node=$node" "jid=$jid" \
| stanza_pubsub \
| send_stanza_iq set
}
# unsubscribe <node> [jid [subid]]
unsubscribe() {
local node=$1
local jid=$(qualify_jid ${2-$xmpp_user})
local subid=$3
# check args
if (( $# < 1 )) || [[ "$1" =~ --help ]] || [[ "$1" =~ -h ]]; then
echo "Usage: unsubscribe <node> [jid [subid]]"
echo "Unsubscribe \"jid\" from the pubsub \"node\" with optional \"subid\""
return 0
fi
if [ -n "$subid" ]; then
eof \
| stanza unsubscribe "node=$node" "jid=$jid" "subid=$subid" \
| stanza_pubsub \
| send_stanza_iq set
else
eof \
| stanza unsubscribe "node=$node" "jid=$jid" \
| stanza_pubsub \
| send_stanza_iq set
fi
}
# Discover server features
# get_features
get_features() {
# check args
if (( $# < 0 )) || [[ "$1" =~ --help ]] || [[ "$1" =~ -h ]]; then
echo "Usage: get_features"
echo "Discover server features"
return 0
fi
{
eof | stanza identity "category=pubsub" "type=service"
eof | stanza feature "var=http://jabber.org/protocol/pubsub"
} \
| stanza_query info \
| send_stanza_iq get
}
# Get all nodes on the pubsub server
# get_nodes
get_nodes() {
# check args
if (( $# < 0 )) || [[ "$1" =~ --help ]] || [[ "$1" =~ -h ]]; then
echo "Usage: get_nodes"
echo "Dump all nodes on the pubsub host"
return 0
fi
eof \
| stanza_query items \
| send_stanza_iq get
}
# Get your subscription list OR subscriptions with a certain node
# get_subscriptions [node]
get_subscriptions() {
local node=$1
# check args
if (( $# < 0 )) || [[ "$1" =~ --help ]] || [[ "$1" =~ -h ]]; then
echo "Usage: get_subscriptions [node]"
echo "Get your subscription list OR subscriptions with a certain node"
return 0
fi
if [ -n "$node" ]; then
eof \
| stanza subscriptions "node=$node" \
| stanza_pubsub \
| send_stanza_iq get
else
eof \
| stanza subscriptions \
| stanza_pubsub \
| send_stanza_iq get
fi
}
# Get a list of subscribers of a node
# get_subscribers <node>
get_subscribers() {
local node=$1
# check args
if (( $# < 1 )) || [[ "$1" =~ --help ]] || [[ "$1" =~ -h ]]; then
echo "Usage: get_subscribers <node>"
echo "Get a list of subscribers of a given node"
echo "You must be owner of the node"
return 0
fi
eof \
| stanza subscriptions "node=$node" \
| stanza_pubsub owner \
| send_stanza_iq get
}
# Set subscribers for a given node
# Subscription states can be "none", "pending", "unconfigured", or "subscribed"
# See http://www.xmpp.org/extensions/xep-0060.html#substates
# set_subscribers <node> <jid> <subscription_state> [<jid2> <subscription_state2>] [...]
set_subscribers() {
local node=$1
# check args
if (( $# < 3 )) || [[ "$1" =~ --help ]] || [[ "$1" =~ -h ]]; then
echo "Usage: set_subscribers <node> <jid> <affiliation> [<jid2> <affiliation2>] [...]" >&2
return 0
fi
shift 1
while [ $# -gt 0 ]; do
if [ $# -lt 2 ]; then
echo "Error - Impropper number of arguments" >&2
return 1
fi
local jid=$(qualify_jid $1)
local subscription=$2
shift 2
eof | stanza subscription "jid=$jid" "subscription=$subscription"
done \
| stanza subscriptions "node=$node" \
| stanza_pubsub owner \
| send_stanza_iq set
}
# Get you own affiliations list OR affiliation with a certain node"
# get_affiliations [node]
get_affiliations() {
local node=$1
# check args
if (( $# < 0 )) || [[ "$1" =~ --help ]] || [[ "$1" =~ -h ]]; then
echo "Usage: get_affiliations [node]"
echo "Get you own affiliations list OR affiliation with a certain node"
return 0
fi
if [ -n "$node" ]; then
eof \
| stanza affiliations "node=$node"\
| stanza_pubsub \
| send_stanza_iq get
else
eof \
| stanza affiliations \
| stanza_pubsub \
| send_stanza_iq get
fi
}
# Get ALL affiliations of a certain node
# get_affiliates <node>
get_affiliates() {
local node=$1
# check args
if (( $# < 1 )) || [[ "$1" =~ --help ]] || [[ "$1" =~ -h ]]; then
echo "Usage: get_affiliates <node>"
echo "Get ALL affiliations of a certain node."
echo "You must be an owner of the node"
return 0
fi
eof \
| stanza affiliations "node=$node" \
| stanza_pubsub owner \
| send_stanza_iq get
}
# Request the subscription options for a node
# get_options <node>
get_options() {
local node=$1
# check args
if (( $# < 1 )) || [[ "$1" =~ --help ]] || [[ "$1" =~ -h ]]; then
echo "Usage: get_options <node>"
echo "Request the subscription options for a node"
return 0
fi
eof \
| stanza options "node=$node" "jid=$(get_jid)" \
| stanza_pubsub \
| send_stanza_iq get
}
# Set affiliations for a given node
# Affiliation can be "owner", "member", "publisher", "publish-only", "outcast", or "none"
# set_affiliations <node> <jid> <affiliation> [<jid2> <affiliation2>] [...]
set_affiliations() {
local node=$1
# check args
if (( $# < 3 )) || [[ "$1" =~ --help ]] || [[ "$1" =~ -h ]]; then
echo "Usage: set_affiliations <node> <jid> <affiliation> [<jid2> <affiliation2>] [...]" >&2
return 0
fi
shift 1
while [ $# -gt 0 ]; do
if [ $# -lt 2 ]; then
echo "Error - Impropper number of arguments" >&2
return 1
fi
local jid=$(qualify_jid $1)
local affiliation=$2
shift 2
eof | stanza affiliation "jid=$jid" "affiliation=$affiliation"
done \
| stanza affiliations "node=$node" \
| stanza_pubsub owner \
| send_stanza_iq set
}
# Get items for a node
# get_items <node>
get_items() {
local node=$1
# check args
if (( $# < 1 )) || [[ "$1" =~ --help ]] || [[ "$1" =~ -h ]]; then
echo "Usage: get_items <node>"
return 0
fi
eof \
| stanza_query items "$node" \
| send_stanza_iq get
}
# Get item(s) for a node
# get_item <node> <item_id> [item_id2 [item_id3...]]
get_item() {
local node=$1
# check args
if (( $# < 1 )) || [[ "$1" =~ --help ]] || [[ "$1" =~ -h ]]; then
echo "Usage: get_item <node> <item_id> [item_id2 [item_id3...]]"
return 0
fi
# Add in secret shortcut to get all items
# Just type: get_items <node>
if (( $# == 1 )); then
get_items $node
return 0
fi
shift 1
while [ $# -gt 0 ]; do
local item_id=$1
shift 1
eof | stanza item "id=$item_id"
done \
| stanza items "node=$node" \
| stanza_pubsub \
| send_stanza_iq get
}
# Get vCard info for jid
# get_vcard <jid>
get_vcard() {
local jid=$(qualify_jid $1)
# check args
if (( $# < 1 )) || [[ "$1" =~ --help ]] || [[ "$1" =~ -h ]]; then
echo "Usage: get_vcard <jid>"
return 0
fi
eof \
| stanza vCard "xmlns=vcard-temp" \
| send_stanza_iq get $jid
}
# Set vCard info
# cat <vcard_file> | set_vcard
set_vcard() {
# check args
if (( $# < 0 )) || [[ "$1" =~ --help ]] || [[ "$1" =~ -h ]]; then
echo "Usage: cat <vcard_file> | set_vcard"
return 0
fi
# file should have <vCard xmlns='vcard-temp'></vCard> inside
send_stanza_iq set ""
}
##########################################################################
# High Level Functions #
##########################################################################
list_nodes() {
get_nodes \
| xmllint --xpath "/*[local-name() = 'iq']/*[local-name() = 'query']/*[local-name() = 'item']/@node" - \
| sed 's/ node=/\n/g;s/\"//g'
echo
}
##########################################################################
# BASH Completion Handlers #
##########################################################################
_NODES_CACHE=( )
_update_nodes_cache() {
_NODES_CACHE=( $( get_nodes 2>/dev/null | xmllint --xpath "/*[local-name() = 'iq']/*[local-name() = 'query']/*[local-name() = 'item']/@node" - 2>/dev/null | sed 's/node=//g;s/\"//g' 2>/dev/null ) )
}
_node_items() {
local node=$1
get_items $node 2>/dev/null | xmllint --xpath "/*[local-name() = 'iq']/*[local-name() = 'query']/*[local-name() = 'item']/@name" - | sed 's/ name=/\n/g;s/\"//g;s/$/\n/g'
}
_compgen_node() {
local cur=$1
_update_nodes_cache
compgen -W "${_NODES_CACHE[*]}" $cur
}
_complete_node() {
local cur
cur=${COMP_WORDS[COMP_CWORD]}
COMPREPLY+=( $( _compgen_node $cur ) )
}
_compgen_node_item() {
local node=$1
local cur=$2
IFS=$'\n' compgen -W "$(_node_items $node)" -- $cur
}
_complete_node_item() {
local node=$1
local cur
cur=${COMP_WORDS[COMP_CWORD]}
while read comp; do
COMPREPLY+=( "$comp" )
done < <(_compgen_node_item $node $cur)
}