forked from processone/xmpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmpp.erl
1152 lines (966 loc) · 39 KB
/
xmpp.erl
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
%%%-------------------------------------------------------------------
%%% @author Evgeny Khramtsov <[email protected]>
%%% @doc
%%%
%%% @end
%%% Created : 9 Dec 2015 by Evgeny Khramtsov <[email protected]>
%%%
%%%
%%% Copyright (C) 2002-2021 ProcessOne, SARL. All Rights Reserved.
%%%
%%% Licensed under the Apache License, Version 2.0 (the "License");
%%% you may not use this file except in compliance with the License.
%%% You may obtain a copy of the License at
%%%
%%% http://www.apache.org/licenses/LICENSE-2.0
%%%
%%% Unless required by applicable law or agreed to in writing, software
%%% distributed under the License is distributed on an "AS IS" BASIS,
%%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%%% See the License for the specific language governing permissions and
%%% limitations under the License.
%%%
%%%-------------------------------------------------------------------
-module(xmpp).
-behaviour(application).
-dialyzer({nowarn_function, get_els/1}).
%% API
%% Application callbacks
-export([start/2, stop/1]).
-export([make_iq_result/1, make_iq_result/2, make_error/2,
decode/1, decode/3, encode/1, encode/2,
get_type/1, get_to/1, get_from/1, get_id/1,
get_lang/1, get_error/1, get_els/1, get_ns/1, get_meta/1, get_meta/2,
get_meta/3, set_type/2, set_to/2, set_from/2, set_id/2,
set_lang/2, set_error/2, set_els/2, set_from_to/3,
set_meta/2, put_meta/3, update_meta/3, del_meta/2,
format_error/1, io_format_error/1, is_stanza/1, try_subtag/2,
set_subtag/2, get_subtag/2, remove_subtag/2, has_subtag/2,
decode_els/1, decode_els/3, pp/1, get_name/1, get_text/1,
get_text/2, mk_text/1, mk_text/2, is_known_tag/1, is_known_tag/2,
append_subtags/2, prep_lang/1, register_codec/1, unregister_codec/1,
set_tr_callback/1, format_stanza_error/1, format_stanza_error/2,
format_stream_error/1, format_stream_error/2, format_sasl_error/1,
format_sasl_error/2, set_config/1, get_config/0, get_subtags/2,
try_subtags/2]).
%% XMPP errors
-export([err_bad_request/0, err_bad_request/2,
err_conflict/0, err_conflict/2,
err_feature_not_implemented/0, err_feature_not_implemented/2,
err_forbidden/0, err_forbidden/2,
err_gone/1, err_gone/3,
err_internal_server_error/0, err_internal_server_error/2,
err_item_not_found/0, err_item_not_found/2,
err_jid_malformed/0, err_jid_malformed/2,
err_not_acceptable/0, err_not_acceptable/2,
err_not_allowed/0, err_not_allowed/2,
err_not_authorized/0, err_not_authorized/2,
err_payment_required/0, err_payment_required/2,
err_policy_violation/0, err_policy_violation/2,
err_recipient_unavailable/0, err_recipient_unavailable/2,
err_redirect/1, err_redirect/3,
err_registration_required/0, err_registration_required/2,
err_remote_server_not_found/0, err_remote_server_not_found/2,
err_remote_server_timeout/0, err_remote_server_timeout/2,
err_resource_constraint/0, err_resource_constraint/2,
err_service_unavailable/0, err_service_unavailable/2,
err_subscription_required/0, err_subscription_required/2,
err_undefined_condition/1, err_undefined_condition/3,
err_unexpected_request/0, err_unexpected_request/2]).
%% XMPP stream errors
-export([serr_bad_format/0, serr_bad_format/2,
serr_bad_namespace_prefix/0, serr_bad_namespace_prefix/2,
serr_conflict/0, serr_conflict/2,
serr_connection_timeout/0, serr_connection_timeout/2,
serr_host_gone/0, serr_host_gone/2,
serr_host_unknown/0, serr_host_unknown/2,
serr_improper_addressing/0, serr_improper_addressing/2,
serr_internal_server_error/0, serr_internal_server_error/2,
serr_invalid_from/0, serr_invalid_from/2,
serr_invalid_id/0, serr_invalid_id/2,
serr_invalid_namespace/0, serr_invalid_namespace/2,
serr_invalid_xml/0, serr_invalid_xml/2,
serr_not_authorized/0, serr_not_authorized/2,
serr_not_well_formed/0, serr_not_well_formed/2,
serr_policy_violation/0, serr_policy_violation/2,
serr_remote_connection_failed/0, serr_remote_connection_failed/2,
serr_reset/0, serr_reset/2,
serr_resource_constraint/0, serr_resource_constraint/2,
serr_restricted_xml/0, serr_restricted_xml/2,
serr_see_other_host/1, serr_see_other_host/3,
serr_system_shutdown/0, serr_system_shutdown/2,
serr_undefined_condition/0, serr_undefined_condition/2,
serr_unsupported_encoding/0, serr_unsupported_encoding/2,
serr_unsupported_feature/0, serr_unsupported_feature/2,
serr_unsupported_stanza_type/0, serr_unsupported_stanza_type/2,
serr_unsupported_version/0, serr_unsupported_version/2]).
-include("xmpp.hrl").
-type reason_text() :: binary() | {io:format(), list()}.
-type lang() :: binary().
-type decode_option() :: ignore_els.
-type config_option() :: {debug, boolean()} | {fqdn, [binary()]}.
-export_type([decode_option/0, config_option/0]).
%%%===================================================================
%%% Application callbacks
%%%===================================================================
start(_StartType, _StartArgs) ->
try
ok = jid:start(),
ok = xmpp_uri:start(),
ok = xmpp_lang:start(),
p1_options:start_link(xmpp_config),
p1_options:insert(xmpp_config, debug, global, false),
p1_options:insert(xmpp_config, fqdn, global, []),
xmpp_sup:start_link()
catch _:{badmatch, Err} ->
Err
end.
stop(_State) ->
ok.
%%%===================================================================
%%% API
%%%===================================================================
-spec make_iq_result(iq()) -> iq().
make_iq_result(IQ) ->
make_iq_result(IQ, undefined).
-spec make_iq_result(iq(), xmpp_element() | xmlel() | undefined) -> iq().
make_iq_result(#iq{type = Type, from = From, to = To} = IQ, El)
when Type == get; Type == set ->
SubEls = if El == undefined -> [];
true -> [El]
end,
IQ#iq{type = result, to = From, from = To, sub_els = SubEls}.
-spec make_error(message(), stanza_error() | xmlel()) -> message();
(presence(), stanza_error() | xmlel()) -> presence();
(iq(), stanza_error() | xmlel()) -> iq();
(xmlel(), stanza_error() | xmlel()) -> xmlel().
make_error(#message{type = Type, from = From, to = To, sub_els = Els} = Msg,
Err) when Type /= error ->
Msg#message{type = error, from = To, to = From, sub_els = Els ++ [Err]};
make_error(#presence{type = Type, from = From, to = To, sub_els = Els} = Pres,
Err) when Type /= error ->
Pres#presence{type = error, from = To, to = From, sub_els = Els ++ [Err]};
make_error(#iq{type = Type, from = From, to = To, sub_els = Els} = IQ,
Err) when Type /= result, Type /= error ->
IQ#iq{type = error, from = To, to = From, sub_els = Els ++ [Err]};
make_error(#xmlel{attrs = Attrs, children = Els} = El, Err) ->
To = fxml:get_attr(<<"to">>, Attrs),
From = fxml:get_attr(<<"from">>, Attrs),
Attrs1 = case To of
{value, T} ->
lists:keystore(<<"from">>, 1, Attrs, {<<"from">>, T});
_ ->
lists:keydelete(<<"from">>, 1, Attrs)
end,
Attrs2 = case From of
{value, F} ->
lists:keystore(<<"to">>, 1, Attrs1, {<<"to">>, F});
_ ->
lists:keydelete(<<"to">>, 1, Attrs1)
end,
Attrs3 = lists:keystore(<<"type">>, 1, Attrs2, {<<"type">>, <<"error">>}),
El#xmlel{attrs = Attrs3, children = Els ++ [encode(Err, ?NS_CLIENT)]}.
-spec get_id(stanza() | xmlel()) -> binary().
get_id(#iq{id = ID}) -> ID;
get_id(#message{id = ID}) -> ID;
get_id(#presence{id = ID}) -> ID;
get_id(#xmlel{attrs = Attrs}) -> fxml:get_attr_s(<<"id">>, Attrs).
-spec get_type(iq()) -> iq_type();
(message()) -> message_type();
(presence()) -> presence_type();
(xmlel()) -> binary().
get_type(#iq{type = T}) -> T;
get_type(#message{type = T}) -> T;
get_type(#presence{type = T}) -> T;
get_type(#xmlel{attrs = Attrs}) -> fxml:get_attr_s(<<"type">>, Attrs).
-spec get_lang(stanza() | xmlel()) -> binary().
get_lang(#iq{lang = L}) -> L;
get_lang(#message{lang = L}) -> L;
get_lang(#presence{lang = L}) -> L;
get_lang(#xmlel{attrs = Attrs}) -> fxml:get_attr_s(<<"xml:lang">>, Attrs).
-spec get_from(stanza()) -> undefined | jid:jid().
get_from(#iq{from = J}) -> J;
get_from(#message{from = J}) -> J;
get_from(#presence{from = J}) -> J.
-spec get_to(stanza()) -> undefined | jid:jid().
get_to(#iq{to = J}) -> J;
get_to(#message{to = J}) -> J;
get_to(#presence{to = J}) -> J.
-spec get_error(xmpp_element()) -> undefined | stanza_error().
get_error(Pkt) ->
case get_subtag(Pkt, #stanza_error{type = cancel}) of
false -> undefined;
Error -> Error
end.
-spec get_els(xmpp_element()) -> [xmpp_element() | xmlel()];
(xmlel()) -> [xmlel()].
get_els(#xmlel{children = Els}) ->
[El || El = #xmlel{} <- Els];
get_els(Pkt) ->
xmpp_codec:get_els(Pkt).
-spec set_id(iq(), binary()) -> iq();
(message(), binary()) -> message();
(presence(), binary()) -> presence().
set_id(#iq{} = IQ, I) -> IQ#iq{id = I};
set_id(#message{} = Msg, I) -> Msg#message{id = I};
set_id(#presence{} = Pres, I) -> Pres#presence{id = I}.
-spec set_type(iq(), iq_type()) -> iq();
(message(), message_type()) -> message();
(presence(), presence_type()) -> presence().
set_type(#iq{} = IQ, T) -> IQ#iq{type = T};
set_type(#message{} = Msg, T) -> Msg#message{type = T};
set_type(#presence{} = Pres, T) -> Pres#presence{type = T}.
-spec set_lang(iq(), binary()) -> iq();
(message(), binary()) -> message();
(presence(), binary()) -> presence().
set_lang(#iq{} = IQ, L) -> IQ#iq{lang = L};
set_lang(#message{} = Msg, L) -> Msg#message{lang = L};
set_lang(#presence{} = Pres, L) -> Pres#presence{lang = L}.
-spec set_from(iq(), jid:jid() | undefined) -> iq();
(message(), jid:jid() | undefined) -> message();
(presence(), jid:jid() | undefined) -> presence().
set_from(#iq{} = IQ, J) -> IQ#iq{from = J};
set_from(#message{} = Msg, J) -> Msg#message{from = J};
set_from(#presence{} = Pres, J) -> Pres#presence{from = J}.
-spec set_to(iq(), jid:jid() | undefined) -> iq();
(message(), jid:jid() | undefined) -> message();
(presence(), jid:jid() | undefined) -> presence().
set_to(#iq{} = IQ, J) -> IQ#iq{to = J};
set_to(#message{} = Msg, J) -> Msg#message{to = J};
set_to(#presence{} = Pres, J) -> Pres#presence{to = J}.
-spec set_from_to(iq(), undefined | jid:jid(), undefined | jid:jid()) -> iq();
(message(), undefined | jid:jid(), undefined | jid:jid()) -> message();
(presence(), undefined | jid:jid(), undefined | jid:jid()) -> presence().
set_from_to(#iq{} = IQ, F, T) -> IQ#iq{from = F, to = T};
set_from_to(#message{} = Msg, F, T) -> Msg#message{from = F, to = T};
set_from_to(#presence{} = Pres, F, T) -> Pres#presence{from = F, to = T}.
-spec set_error(iq(), stanza_error()) -> iq();
(message(), stanza_error()) -> message();
(presence(), stanza_error()) -> presence().
set_error(Stanza, E) -> set_subtag(Stanza, E).
-spec set_els(xmpp_element(), [xmpp_element() | xmlel()]) -> xmpp_element().
set_els(Pkt, Els) ->
xmpp_codec:set_els(Pkt, Els).
-spec get_ns(xmpp_element() | xmlel()) -> binary().
get_ns(#xmlel{attrs = Attrs}) ->
fxml:get_attr_s(<<"xmlns">>, Attrs);
get_ns(Pkt) ->
xmpp_codec:get_ns(Pkt).
-spec get_name(xmpp_element() | xmlel()) -> binary().
get_name(#xmlel{name = Name}) ->
Name;
get_name(Pkt) ->
xmpp_codec:get_name(Pkt).
-spec get_meta(stanza()) -> map().
get_meta(#iq{meta = M}) -> M;
get_meta(#message{meta = M}) -> M;
get_meta(#presence{meta = M}) -> M.
-spec get_meta(stanza(), any()) -> any().
get_meta(#iq{meta = M}, K) -> maps:get(K, M);
get_meta(#message{meta = M}, K) -> maps:get(K, M);
get_meta(#presence{meta = M}, K) -> maps:get(K, M).
-spec get_meta(stanza(), any(), any()) -> any().
get_meta(#iq{meta = M}, K, Def) -> maps:get(K, M, Def);
get_meta(#message{meta = M}, K, Def) -> maps:get(K, M, Def);
get_meta(#presence{meta = M}, K, Def) -> maps:get(K, M, Def).
-spec set_meta(iq(), map()) -> iq();
(message(), map()) -> message();
(presence(), map()) -> presence().
set_meta(#iq{} = IQ, M) -> IQ#iq{meta = M};
set_meta(#message{} = Msg, M) -> Msg#message{meta = M};
set_meta(#presence{} = Pres, M) -> Pres#presence{meta = M}.
-spec put_meta(iq(), any(), any()) -> iq();
(message(), any(), any()) -> message();
(presence(), any(), any()) -> presence().
put_meta(#iq{meta = M} = IQ, K, V) ->
IQ#iq{meta = maps:put(K, V, M)};
put_meta(#message{meta = M} = Msg, K, V) ->
Msg#message{meta = maps:put(K, V, M)};
put_meta(#presence{meta = M} = Pres, K, V) ->
Pres#presence{meta = maps:put(K, V, M)}.
-spec update_meta(iq(), any(), any()) -> iq();
(message(), any(), any()) -> message();
(presence(), any(), any()) -> presence().
update_meta(#iq{meta = M} = IQ, K, V) ->
IQ#iq{meta = maps:update(K, V, M)};
update_meta(#message{meta = M} = Msg, K, V) ->
Msg#message{meta = maps:update(K, V, M)};
update_meta(#presence{meta = M} = Pres, K, V) ->
Pres#presence{meta = maps:update(K, V, M)}.
-spec del_meta(iq(), any()) -> iq();
(message(), any()) -> message();
(presence(), any()) -> presence().
del_meta(#iq{meta = M} = IQ, K) ->
IQ#iq{meta = maps:remove(K, M)};
del_meta(#message{meta = M} = Msg, K) ->
Msg#message{meta = maps:remove(K, M)};
del_meta(#presence{meta = M} = Pres, K) ->
Pres#presence{meta = maps:remove(K, M)}.
-spec decode(xmlel() | xmpp_element()) -> xmpp_element().
decode(El) ->
decode(El, ?NS_CLIENT, []).
-spec decode(xmlel() | xmpp_element(), binary(), [decode_option()]) ->
xmpp_element().
decode(#xmlel{} = El, TopXMLNS, Opts) ->
xmpp_codec:decode(El, TopXMLNS, Opts);
decode(Pkt, _, _) ->
Pkt.
-spec decode_els(xmpp_element()) -> xmpp_element().
decode_els(Pkt) ->
decode_els(Pkt, ?NS_CLIENT, fun is_known_tag/1).
-type match_fun() :: fun((xmlel()) -> boolean()).
-spec decode_els(xmpp_element(), binary(), match_fun()) -> xmpp_element().
decode_els(Pkt, TopXMLNS, MatchFun) ->
Els = lists:map(
fun(#xmlel{} = El) ->
case MatchFun(El) of
true -> decode(El, TopXMLNS, []);
false -> El
end;
(El) ->
El
end, get_els(Pkt)),
set_els(Pkt, Els).
-spec encode(xmpp_element() | xmlel()) -> xmlel().
encode(Pkt) ->
encode(Pkt, <<>>).
-spec encode(xmpp_element() | xmlel(), binary()) -> xmlel().
encode(Pkt, TopXMLNS) ->
xmpp_codec:encode(Pkt, TopXMLNS).
-spec is_known_tag(xmlel()) -> boolean().
is_known_tag(El) ->
is_known_tag(El, ?NS_CLIENT).
-spec is_known_tag(xmlel(), binary()) -> boolean().
is_known_tag(El, TopXMLNS) ->
xmpp_codec:is_known_tag(El, TopXMLNS).
-spec format_error(_) -> binary().
format_error(Reason) ->
xmpp_codec:format_error(Reason).
-spec io_format_error(_) -> {binary(), [binary()]}.
io_format_error(Reason) ->
xmpp_codec:io_format_error(Reason).
-spec format_stanza_error(stanza_error()) -> binary().
format_stanza_error(Err) ->
format_stanza_error(Err, <<"en">>).
-spec format_stanza_error(stanza_error(), binary()) -> binary().
format_stanza_error(#stanza_error{reason = Reason, text = Text}, Lang) ->
format_s_error(Reason, Text, Lang).
-spec format_stream_error(stream_error()) -> binary().
format_stream_error(Err) ->
format_stream_error(Err, <<"en">>).
-spec format_stream_error(stream_error(), binary()) -> binary().
format_stream_error(#stream_error{reason = Reason, text = Text}, Lang) ->
format_s_error(Reason, Text, Lang).
-spec format_sasl_error(sasl_failure()) -> binary().
format_sasl_error(Err) ->
format_sasl_error(Err, <<"en">>).
-spec format_sasl_error(sasl_failure(), binary()) -> binary().
format_sasl_error(#sasl_failure{reason = Reason, text = Text}, Lang) ->
format_s_error(Reason, Text, Lang).
-spec is_stanza(any()) -> boolean().
is_stanza(#message{}) -> true;
is_stanza(#iq{}) -> true;
is_stanza(#presence{}) -> true;
is_stanza(#xmlel{name = Name}) ->
(Name == <<"iq">>) or (Name == <<"message">>) or (Name == <<"presence">>);
is_stanza(_) -> false.
-spec set_subtag(xmpp_element(), xmpp_element()) -> xmpp_element().
set_subtag(Pkt, Tag) ->
TagName = xmpp_codec:get_name(Tag),
TopXMLNS = xmpp_codec:get_ns(Pkt),
XMLNS = xmpp_codec:get_ns(Tag),
Els = get_els(Pkt),
NewEls = set_subtag(Els, Tag, TagName, XMLNS, TopXMLNS),
set_els(Pkt, NewEls).
set_subtag([El|Els], Tag, TagName, XMLNS, TopXMLNS) ->
case match_tag(El, TagName, XMLNS, TopXMLNS) of
true ->
[Tag|Els];
false ->
[El|set_subtag(Els, Tag, TagName, XMLNS, TopXMLNS)]
end;
set_subtag([], Tag, _, _, _) ->
[Tag].
-spec get_subtag(xmpp_element(), xmpp_element()) -> xmpp_element() | false.
get_subtag(Pkt, Tag) ->
Els = get_els(Pkt),
TopXMLNS = xmpp_codec:get_ns(Pkt),
TagName = xmpp_codec:get_name(Tag),
XMLNS = xmpp_codec:get_ns(Tag),
get_subtag(Els, TagName, XMLNS, TopXMLNS).
get_subtag([El|Els], TagName, XMLNS, TopXMLNS) ->
case match_tag(El, TagName, XMLNS, TopXMLNS) of
true ->
try
decode(El)
catch _:{xmpp_codec, _Why} ->
get_subtag(Els, TagName, XMLNS, TopXMLNS)
end;
false ->
get_subtag(Els, TagName, XMLNS, TopXMLNS)
end;
get_subtag([], _, _, _) ->
false.
-spec get_subtags(xmpp_element(), xmpp_element()) -> [xmpp_element()].
get_subtags(Pkt, Tag) ->
Els = get_els(Pkt),
TopXMLNS = xmpp_codec:get_ns(Pkt),
TagName = xmpp_codec:get_name(Tag),
XMLNS = xmpp_codec:get_ns(Tag),
get_subtags(Els, TagName, XMLNS, TopXMLNS, []).
get_subtags([El|Els], TagName, XMLNS, TopXMLNS, Acc) ->
case match_tag(El, TagName, XMLNS, TopXMLNS) of
true ->
try
get_subtags(Els, TagName, XMLNS, TopXMLNS, [decode(El) | Acc])
catch _:{xmpp_codec, _Why} ->
get_subtags(Els, TagName, XMLNS, TopXMLNS, Acc)
end;
false ->
get_subtags(Els, TagName, XMLNS, TopXMLNS, Acc)
end;
get_subtags([], _, _, _, Acc) ->
lists:reverse(Acc).
-spec try_subtag(xmpp_element(), xmpp_element()) -> xmpp_element() | false.
try_subtag(Pkt, Tag) ->
Els = get_els(Pkt),
TopXMLNS = xmpp_codec:get_ns(Pkt),
TagName = xmpp_codec:get_name(Tag),
XMLNS = xmpp_codec:get_ns(Tag),
try_subtag(Els, TagName, XMLNS, TopXMLNS).
try_subtag([El|Els], TagName, XMLNS, TopXMLNS) ->
case match_tag(El, TagName, XMLNS, TopXMLNS) of
true ->
decode(El);
false ->
try_subtag(Els, TagName, XMLNS, TopXMLNS)
end;
try_subtag([], _, _, _) ->
false.
-spec try_subtags(xmpp_element(), xmpp_element()) -> [xmpp_element()].
try_subtags(Pkt, Tag) ->
Els = get_els(Pkt),
TopXMLNS = xmpp_codec:get_ns(Pkt),
TagName = xmpp_codec:get_name(Tag),
XMLNS = xmpp_codec:get_ns(Tag),
try_subtags(Els, TagName, XMLNS, TopXMLNS, []).
try_subtags([El|Els], TagName, XMLNS, TopXMLNS, Acc) ->
case match_tag(El, TagName, XMLNS, TopXMLNS) of
true ->
try_subtags(Els, TagName, XMLNS, TopXMLNS, [decode(El) | Acc]);
false ->
try_subtags(Els, TagName, XMLNS, TopXMLNS, Acc)
end;
try_subtags([], _, _, _, Acc) ->
lists:reverse(Acc).
-spec remove_subtag(xmpp_element(), xmpp_element()) -> xmpp_element().
remove_subtag(Pkt, Tag) ->
Els = get_els(Pkt),
TagName = xmpp_codec:get_name(Tag),
TopXMLNS = xmpp_codec:get_ns(Pkt),
XMLNS = xmpp_codec:get_ns(Tag),
NewEls = remove_subtag(Els, TagName, XMLNS, TopXMLNS),
set_els(Pkt, NewEls).
remove_subtag([El|Els], TagName, XMLNS, TopXMLNS) ->
case match_tag(El, TagName, XMLNS, TopXMLNS) of
true ->
remove_subtag(Els, TagName, XMLNS, TopXMLNS);
false ->
[El|remove_subtag(Els, TagName, XMLNS, TopXMLNS)]
end;
remove_subtag([], _, _, _) ->
[].
-spec has_subtag(xmpp_element(), xmpp_element()) -> boolean().
has_subtag(Pkt, Tag) ->
Els = get_els(Pkt),
TagName = xmpp_codec:get_name(Tag),
TopXMLNS = xmpp_codec:get_ns(Pkt),
XMLNS = xmpp_codec:get_ns(Tag),
has_subtag(Els, TagName, XMLNS, TopXMLNS).
has_subtag([El|Els], TagName, XMLNS, TopXMLNS) ->
case match_tag(El, TagName, XMLNS, TopXMLNS) of
true ->
true;
false ->
has_subtag(Els, TagName, XMLNS, TopXMLNS)
end;
has_subtag([], _, _, _) ->
false.
-spec append_subtags(xmpp_element(), [xmpp_element() | xmlel()]) -> xmpp_element().
append_subtags(Pkt, Tags) ->
Els = get_els(Pkt),
set_els(Pkt, Els ++ Tags).
-spec get_text([text()]) -> binary().
get_text(Text) ->
get_text(Text, <<"en">>).
-spec get_text([text()], binary()) -> binary().
get_text(Text, Lang) ->
get_text(Text, Lang, <<>>).
-spec mk_text(reason_text()) -> [text()].
mk_text(Text) ->
mk_text(Text, <<"">>).
-spec mk_text(reason_text(), lang()) -> [text()].
mk_text(<<"">>, _Lang) ->
[];
mk_text({Format, Args}, Lang) ->
InFormat = iolist_to_binary(Format),
OutFormat = xmpp_tr:tr(Lang, InFormat),
InTxt = iolist_to_binary(io_lib:format(InFormat, Args)),
if InFormat == OutFormat ->
[#text{data = InTxt, lang = <<"en">>}];
true ->
OutTxt = iolist_to_binary(io_lib:format(OutFormat, Args)),
[#text{data = OutTxt, lang = Lang},
#text{data = InTxt, lang = <<"en">>}]
end;
mk_text(InTxt, Lang) ->
OutTxt = xmpp_tr:tr(Lang, InTxt),
if OutTxt == InTxt ->
[#text{data = InTxt, lang = <<"en">>}];
true ->
[#text{data = OutTxt, lang = Lang},
#text{data = InTxt, lang = <<"en">>}]
end.
-spec pp(any()) -> iodata().
pp(Term) ->
io_lib_pretty:print(Term, fun pp/2).
-spec register_codec(module()) -> ok.
register_codec(Mod) ->
xmpp_codec:register_module(Mod).
-spec unregister_codec(module()) -> ok.
unregister_codec(Mod) ->
xmpp_codec:unregister_module(Mod).
-spec set_tr_callback({module(), atom()} | undefined) -> ok.
set_tr_callback(Callback) ->
Forms = get_tr_forms(Callback),
{ok, Code} = case compile:forms(Forms, []) of
{ok, xmpp_tr, Bin} -> {ok, Bin};
{ok, xmpp_tr, Bin, _Warnings} -> {ok, Bin};
Error -> Error
end,
{module, xmpp_tr} = code:load_binary(xmpp_tr, "nofile", Code),
ok.
-spec set_config([config_option()]) -> ok.
set_config(Options) ->
lists:foreach(
fun({debug, Bool}) when is_boolean(Bool) ->
p1_options:insert(xmpp_config, debug, global, Bool);
({fqdn, Domains}) ->
case lists:all(fun erlang:is_binary/1, Domains) of
true ->
p1_options:insert(xmpp_config, fqdn, global, Domains);
false ->
erlang:error(badarg)
end;
(_) ->
erlang:error(badarg)
end, Options),
p1_options:compile(xmpp_config).
-spec get_config() -> [config_option()].
get_config() ->
{ok, Debug} = xmpp_config:debug(global),
{ok, Domains} = xmpp_config:fqdn(global),
[{debug, Debug}, {fqdn, Domains}].
%%%===================================================================
%%% Functions to construct general XMPP errors
%%%===================================================================
-spec err_bad_request() -> stanza_error().
err_bad_request() ->
err(modify, 'bad-request', 400).
-spec err_bad_request(reason_text(), lang()) -> stanza_error().
err_bad_request(Text, Lang) ->
err(modify, 'bad-request', 400, Text, Lang).
-spec err_conflict() -> stanza_error().
err_conflict() ->
err(cancel, 'conflict', 409).
-spec err_conflict(reason_text(), lang()) -> stanza_error().
err_conflict(Text, Lang) ->
err(cancel, 'conflict', 409, Text, Lang).
-spec err_feature_not_implemented() -> stanza_error().
err_feature_not_implemented() ->
err(cancel, 'feature-not-implemented', 501).
-spec err_feature_not_implemented(reason_text(), lang()) -> stanza_error().
err_feature_not_implemented(Text, Lang) ->
err(cancel, 'feature-not-implemented', 501, Text, Lang).
-spec err_forbidden() -> stanza_error().
err_forbidden() ->
err(auth, 'forbidden', 403).
-spec err_forbidden(reason_text(), lang()) -> stanza_error().
err_forbidden(Text, Lang) ->
err(auth, 'forbidden', 403, Text, Lang).
%% RFC 6120 says error type SHOULD be "cancel".
%% RFC 3920 and XEP-0082 says it SHOULD be "modify".
-spec err_gone(binary()) -> stanza_error().
err_gone(URI) ->
err(modify, #gone{uri = URI}, 302).
-spec err_gone(binary(), reason_text(), lang()) -> stanza_error().
err_gone(URI, Text, Lang) ->
err(modify, #gone{uri = URI}, 302, Text, Lang).
%% RFC 6120 says error type SHOULD be "cancel".
%% RFC 3920 and XEP-0082 says it SHOULD be "wait".
-spec err_internal_server_error() -> stanza_error().
err_internal_server_error() ->
err(wait, 'internal-server-error', 500).
-spec err_internal_server_error(reason_text(), lang()) -> stanza_error().
err_internal_server_error(Text, Lang) ->
err(wait, 'internal-server-error', 500, Text, Lang).
-spec err_item_not_found() -> stanza_error().
err_item_not_found() ->
err(cancel, 'item-not-found', 404).
-spec err_item_not_found(reason_text(), lang()) -> stanza_error().
err_item_not_found(Text, Lang) ->
err(cancel, 'item-not-found', 404, Text, Lang).
-spec err_jid_malformed() -> stanza_error().
err_jid_malformed() ->
err(modify, 'jid-malformed', 400).
-spec err_jid_malformed(reason_text(), lang()) -> stanza_error().
err_jid_malformed(Text, Lang) ->
err(modify, 'jid-malformed', 400, Text, Lang).
-spec err_not_acceptable() -> stanza_error().
err_not_acceptable() ->
err(modify, 'not-acceptable', 406).
-spec err_not_acceptable(reason_text(), lang()) -> stanza_error().
err_not_acceptable(Text, Lang) ->
err(modify, 'not-acceptable', 406, Text, Lang).
-spec err_not_allowed() -> stanza_error().
err_not_allowed() ->
err(cancel, 'not-allowed', 405).
-spec err_not_allowed(reason_text(), lang()) -> stanza_error().
err_not_allowed(Text, Lang) ->
err(cancel, 'not-allowed', 405, Text, Lang).
-spec err_not_authorized() -> stanza_error().
err_not_authorized() ->
err(auth, 'not-authorized', 401).
-spec err_not_authorized(reason_text(), lang()) -> stanza_error().
err_not_authorized(Text, Lang) ->
err(auth, 'not-authorized', 401, Text, Lang).
-spec err_payment_required() -> stanza_error().
err_payment_required() ->
err(auth, 'not-authorized', 402).
-spec err_payment_required(reason_text(), lang()) -> stanza_error().
err_payment_required(Text, Lang) ->
err(auth, 'not-authorized', 402, Text, Lang).
%% <policy-violation/> is defined in neither RFC 3920 nor XEP-0086.
%% We choose '403' error code (as in <forbidden/>).
-spec err_policy_violation() -> stanza_error().
err_policy_violation() ->
err(modify, 'policy-violation', 403).
-spec err_policy_violation(reason_text(), lang()) -> stanza_error().
err_policy_violation(Text, Lang) ->
err(modify, 'policy-violation', 403, Text, Lang).
-spec err_recipient_unavailable() -> stanza_error().
err_recipient_unavailable() ->
err(wait, 'recipient-unavailable', 404).
-spec err_recipient_unavailable(reason_text(), lang()) -> stanza_error().
err_recipient_unavailable(Text, Lang) ->
err(wait, 'recipient-unavailable', 404, Text, Lang).
-spec err_redirect(binary()) -> stanza_error().
err_redirect(URI) ->
err(modify, #redirect{uri = URI}, 302).
-spec err_redirect(binary(), reason_text(), lang()) -> stanza_error().
err_redirect(URI, Text, Lang) ->
err(modify, #redirect{uri = URI}, 302, Text, Lang).
-spec err_registration_required() -> stanza_error().
err_registration_required() ->
err(auth, 'registration-required', 407).
-spec err_registration_required(reason_text(), lang()) -> stanza_error().
err_registration_required(Text, Lang) ->
err(auth, 'registration-required', 407, Text, Lang).
-spec err_remote_server_not_found() -> stanza_error().
err_remote_server_not_found() ->
err(cancel, 'remote-server-not-found', 404).
-spec err_remote_server_not_found(reason_text(), lang()) -> stanza_error().
err_remote_server_not_found(Text, Lang) ->
err(cancel, 'remote-server-not-found', 404, Text, Lang).
-spec err_remote_server_timeout() -> stanza_error().
err_remote_server_timeout() ->
err(wait, 'remote-server-timeout', 504).
-spec err_remote_server_timeout(reason_text(), lang()) -> stanza_error().
err_remote_server_timeout(Text, Lang) ->
err(wait, 'remote-server-timeout', 504, Text, Lang).
-spec err_resource_constraint() -> stanza_error().
err_resource_constraint() ->
err(wait, 'resource-constraint', 500).
-spec err_resource_constraint(reason_text(), lang()) -> stanza_error().
err_resource_constraint(Text, Lang) ->
err(wait, 'resource-constraint', 500, Text, Lang).
-spec err_service_unavailable() -> stanza_error().
err_service_unavailable() ->
err(cancel, 'service-unavailable', 503).
-spec err_service_unavailable(reason_text(), lang()) -> stanza_error().
err_service_unavailable(Text, Lang) ->
err(cancel, 'service-unavailable', 503, Text, Lang).
-spec err_subscription_required() -> stanza_error().
err_subscription_required() ->
err(auth, 'subscription-required', 407).
-spec err_subscription_required(reason_text(), lang()) -> stanza_error().
err_subscription_required(Text, Lang) ->
err(auth, 'subscription-required', 407, Text, Lang).
%% No error type is defined for <undefined-confition/>.
%% Let user provide the type.
-spec err_undefined_condition('auth' | 'cancel' | 'continue' |
'modify' | 'wait') -> stanza_error().
err_undefined_condition(Type) ->
err(Type, 'undefined-condition', 500).
-spec err_undefined_condition('auth' | 'cancel' | 'continue' | 'modify' | 'wait',
reason_text(), lang()) -> stanza_error().
err_undefined_condition(Type, Text, Lang) ->
err(Type, 'undefined-condition', 500, Text, Lang).
%% RFC 6120 says error type SHOULD be "wait" or "modify".
%% RFC 3920 and XEP-0082 says it SHOULD be "wait".
-spec err_unexpected_request() -> stanza_error().
err_unexpected_request() ->
err(wait, 'unexpected-request', 400).
-spec err_unexpected_request(reason_text(), lang()) -> stanza_error().
err_unexpected_request(Text, Lang) ->
err(wait, 'unexpected-request', 400, Text, Lang).
%%%===================================================================
%%% Functions to construct stream errors
%%%===================================================================
-spec serr_bad_format() -> stream_error().
serr_bad_format() ->
serr('bad-format').
-spec serr_bad_format(reason_text(), lang()) -> stream_error().
serr_bad_format(Text, Lang) ->
serr('bad-format', Text, Lang).
-spec serr_bad_namespace_prefix() -> stream_error().
serr_bad_namespace_prefix() ->
serr('bad-namespace-prefix').
-spec serr_bad_namespace_prefix(reason_text(), lang()) -> stream_error().
serr_bad_namespace_prefix(Text, Lang) ->
serr('bad-namespace-prefix', Text, Lang).
-spec serr_conflict() -> stream_error().
serr_conflict() ->
serr('conflict').
-spec serr_conflict(reason_text(), lang()) -> stream_error().
serr_conflict(Text, Lang) ->
serr('conflict', Text, Lang).
-spec serr_connection_timeout() -> stream_error().
serr_connection_timeout() ->
serr('connection-timeout').
-spec serr_connection_timeout(reason_text(), lang()) -> stream_error().
serr_connection_timeout(Text, Lang) ->
serr('connection-timeout', Text, Lang).
-spec serr_host_gone() -> stream_error().
serr_host_gone() ->
serr('host-gone').
-spec serr_host_gone(reason_text(), lang()) -> stream_error().
serr_host_gone(Text, Lang) ->
serr('host-gone', Text, Lang).
-spec serr_host_unknown() -> stream_error().
serr_host_unknown() ->
serr('host-unknown').
-spec serr_host_unknown(reason_text(), lang()) -> stream_error().
serr_host_unknown(Text, Lang) ->
serr('host-unknown', Text, Lang).
-spec serr_improper_addressing() -> stream_error().
serr_improper_addressing() ->
serr('improper-addressing').
-spec serr_improper_addressing(reason_text(), lang()) -> stream_error().
serr_improper_addressing(Text, Lang) ->
serr('improper-addressing', Text, Lang).
-spec serr_internal_server_error() -> stream_error().
serr_internal_server_error() ->
serr('internal-server-error').
-spec serr_internal_server_error(reason_text(), lang()) -> stream_error().
serr_internal_server_error(Text, Lang) ->
serr('internal-server-error', Text, Lang).
-spec serr_invalid_from() -> stream_error().
serr_invalid_from() ->
serr('invalid-from').
-spec serr_invalid_from(reason_text(), lang()) -> stream_error().
serr_invalid_from(Text, Lang) ->
serr('invalid-from', Text, Lang).
-spec serr_invalid_id() -> stream_error().
serr_invalid_id() ->
serr('invalid-id').
-spec serr_invalid_id(reason_text(), lang()) -> stream_error().
serr_invalid_id(Text, Lang) ->
serr('invalid-id', Text, Lang).
-spec serr_invalid_namespace() -> stream_error().
serr_invalid_namespace() ->
serr('invalid-namespace').
-spec serr_invalid_namespace(reason_text(), lang()) -> stream_error().
serr_invalid_namespace(Text, Lang) ->
serr('invalid-namespace', Text, Lang).
-spec serr_invalid_xml() -> stream_error().
serr_invalid_xml() ->
serr('invalid-xml').
-spec serr_invalid_xml(reason_text(), lang()) -> stream_error().
serr_invalid_xml(Text, Lang) ->
serr('invalid-xml', Text, Lang).
-spec serr_not_authorized() -> stream_error().
serr_not_authorized() ->
serr('not-authorized').
-spec serr_not_authorized(reason_text(), lang()) -> stream_error().
serr_not_authorized(Text, Lang) ->
serr('not-authorized', Text, Lang).
-spec serr_not_well_formed() -> stream_error().
serr_not_well_formed() ->
serr('not-well-formed').
-spec serr_not_well_formed(reason_text(), lang()) -> stream_error().
serr_not_well_formed(Text, Lang) ->
serr('not-well-formed', Text, Lang).
-spec serr_policy_violation() -> stream_error().
serr_policy_violation() ->
serr('policy-violation').
-spec serr_policy_violation(reason_text(), lang()) -> stream_error().
serr_policy_violation(Text, Lang) ->
serr('policy-violation', Text, Lang).
-spec serr_remote_connection_failed() -> stream_error().
serr_remote_connection_failed() ->
serr('remote-connection-failed').
-spec serr_remote_connection_failed(reason_text(), lang()) -> stream_error().
serr_remote_connection_failed(Text, Lang) ->
serr('remote-connection-failed', Text, Lang).
-spec serr_reset() -> stream_error().
serr_reset() ->
serr('reset').
-spec serr_reset(reason_text(), lang()) -> stream_error().
serr_reset(Text, Lang) ->
serr('reset', Text, Lang).
-spec serr_resource_constraint() -> stream_error().
serr_resource_constraint() ->
serr('resource-constraint').
-spec serr_resource_constraint(reason_text(), lang()) -> stream_error().
serr_resource_constraint(Text, Lang) ->
serr('resource-constraint', Text, Lang).
-spec serr_restricted_xml() -> stream_error().
serr_restricted_xml() ->
serr('restricted-xml').
-spec serr_restricted_xml(reason_text(), lang()) -> stream_error().
serr_restricted_xml(Text, Lang) ->
serr('restricted-xml', Text, Lang).
-spec serr_see_other_host(xmpp_host()) -> stream_error().
serr_see_other_host(HostPort) ->
serr(#'see-other-host'{host = HostPort}).