-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathres_xmpp.c
4673 lines (3941 loc) · 153 KB
/
res_xmpp.c
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
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 2012, Digium, Inc.
*
* Joshua Colp <[email protected]>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*! \file
*
* \brief XMPP client and component module.
*
* \author Joshua Colp <[email protected]>
*
* Iksemel http://code.google.com/p/iksemel/
*
* A reference module for interfacting Asterisk directly as a client or component with
* an XMPP/Jabber compliant server.
*
* This module is based upon the original res_jabber as done by Matt O'Gorman.
*
*/
/*! \li \ref res_xmpp.c uses the configuration file \ref xmpp.conf and \ref jabber.conf
* \addtogroup configuration_file Configuration Files
*/
/*!
* \page xmpp.conf xmpp.conf
* \verbinclude xmpp.conf.sample
*/
/*** MODULEINFO
<depend>iksemel</depend>
<use type="external">openssl</use>
<support_level>core</support_level>
***/
#include "asterisk.h"
ASTERISK_REGISTER_FILE()
#include <ctype.h>
#include <iksemel.h>
#include "asterisk/xmpp.h"
#include "asterisk/module.h"
#include "asterisk/manager.h"
#include "asterisk/app.h"
#include "asterisk/message.h"
#include "asterisk/manager.h"
#include "asterisk/cli.h"
#include "asterisk/config_options.h"
/*** DOCUMENTATION
<application name="JabberSend" language="en_US" module="res_xmpp">
<synopsis>
Sends an XMPP message to a buddy.
</synopsis>
<syntax>
<parameter name="account" required="true">
<para>The local named account to listen on (specified in
xmpp.conf)</para>
</parameter>
<parameter name="jid" required="true">
<para>Jabber ID of the buddy to send the message to. It can be a
bare JID (username@domain) or a full JID (username@domain/resource).</para>
</parameter>
<parameter name="message" required="true">
<para>The message to send.</para>
</parameter>
</syntax>
<description>
<para>Sends the content of <replaceable>message</replaceable> as text message
from the given <replaceable>account</replaceable> to the buddy identified by
<replaceable>jid</replaceable></para>
<para>Example: JabberSend(asterisk,[email protected],Hello world) sends "Hello world"
to <replaceable>[email protected]</replaceable> as an XMPP message from the account
<replaceable>asterisk</replaceable>, configured in xmpp.conf.</para>
</description>
<see-also>
<ref type="function" module="res_xmpp">JABBER_STATUS</ref>
<ref type="function" module="res_xmpp">JABBER_RECEIVE</ref>
</see-also>
</application>
<function name="JABBER_RECEIVE" language="en_US" module="res_xmpp">
<synopsis>
Reads XMPP messages.
</synopsis>
<syntax>
<parameter name="account" required="true">
<para>The local named account to listen on (specified in
xmpp.conf)</para>
</parameter>
<parameter name="jid" required="true">
<para>Jabber ID of the buddy to receive message from. It can be a
bare JID (username@domain) or a full JID (username@domain/resource).</para>
</parameter>
<parameter name="timeout">
<para>In seconds, defaults to <literal>20</literal>.</para>
</parameter>
</syntax>
<description>
<para>Receives a text message on the given <replaceable>account</replaceable>
from the buddy identified by <replaceable>jid</replaceable> and returns the contents.</para>
<para>Example: ${JABBER_RECEIVE(asterisk,[email protected])} returns an XMPP message
sent from <replaceable>[email protected]</replaceable> (or nothing in case of a time out), to
the <replaceable>asterisk</replaceable> XMPP account configured in xmpp.conf.</para>
</description>
<see-also>
<ref type="function" module="res_xmpp">JABBER_STATUS</ref>
<ref type="application" module="res_xmpp">JabberSend</ref>
</see-also>
</function>
<function name="JABBER_STATUS" language="en_US" module="res_xmpp">
<synopsis>
Retrieves a buddy's status.
</synopsis>
<syntax>
<parameter name="account" required="true">
<para>The local named account to listen on (specified in
xmpp.conf)</para>
</parameter>
<parameter name="jid" required="true">
<para>Jabber ID of the buddy to receive message from. It can be a
bare JID (username@domain) or a full JID (username@domain/resource).</para>
</parameter>
</syntax>
<description>
<para>Retrieves the numeric status associated with the buddy identified
by <replaceable>jid</replaceable>.
If the buddy does not exist in the buddylist, returns 7.</para>
<para>Status will be 1-7.</para>
<para>1=Online, 2=Chatty, 3=Away, 4=XAway, 5=DND, 6=Offline</para>
<para>If not in roster variable will be set to 7.</para>
<para>Example: ${JABBER_STATUS(asterisk,[email protected])} returns 1 if
<replaceable>[email protected]</replaceable> is online. <replaceable>asterisk</replaceable> is
the associated XMPP account configured in xmpp.conf.</para>
</description>
<see-also>
<ref type="function" module="res_xmpp">JABBER_RECEIVE</ref>
<ref type="application" module="res_xmpp">JabberSend</ref>
</see-also>
</function>
<application name="JabberSendGroup" language="en_US" module="res_xmpp">
<synopsis>
Send a Jabber Message to a specified chat room
</synopsis>
<syntax>
<parameter name="Jabber" required="true">
<para>Client or transport Asterisk uses to connect to Jabber.</para>
</parameter>
<parameter name="RoomJID" required="true">
<para>XMPP/Jabber JID (Name) of chat room.</para>
</parameter>
<parameter name="Message" required="true">
<para>Message to be sent to the chat room.</para>
</parameter>
<parameter name="Nickname" required="false">
<para>The nickname Asterisk uses in the chat room.</para>
</parameter>
</syntax>
<description>
<para>Allows user to send a message to a chat room via XMPP.</para>
<note><para>To be able to send messages to a chat room, a user must have previously joined it. Use the <replaceable>JabberJoin</replaceable> function to do so.</para></note>
</description>
</application>
<application name="JabberJoin" language="en_US" module="res_xmpp">
<synopsis>
Join a chat room
</synopsis>
<syntax>
<parameter name="Jabber" required="true">
<para>Client or transport Asterisk uses to connect to Jabber.</para>
</parameter>
<parameter name="RoomJID" required="true">
<para>XMPP/Jabber JID (Name) of chat room.</para>
</parameter>
<parameter name="Nickname" required="false">
<para>The nickname Asterisk will use in the chat room.</para>
<note><para>If a different nickname is supplied to an already joined room, the old nick will be changed to the new one.</para></note>
</parameter>
</syntax>
<description>
<para>Allows Asterisk to join a chat room.</para>
</description>
</application>
<application name="JabberLeave" language="en_US" module="res_xmpp">
<synopsis>
Leave a chat room
</synopsis>
<syntax>
<parameter name="Jabber" required="true">
<para>Client or transport Asterisk uses to connect to Jabber.</para>
</parameter>
<parameter name="RoomJID" required="true">
<para>XMPP/Jabber JID (Name) of chat room.</para>
</parameter>
<parameter name="Nickname" required="false">
<para>The nickname Asterisk uses in the chat room.</para>
</parameter>
</syntax>
<description>
<para>Allows Asterisk to leave a chat room.</para>
</description>
</application>
<application name="JabberStatus" language="en_US" module="res_xmpp">
<synopsis>
Retrieve the status of a jabber list member
</synopsis>
<syntax>
<parameter name="Jabber" required="true">
<para>Client or transport Asterisk users to connect to Jabber.</para>
</parameter>
<parameter name="JID" required="true">
<para>XMPP/Jabber JID (Name) of recipient.</para>
</parameter>
<parameter name="Variable" required="true">
<para>Variable to store the status of requested user.</para>
</parameter>
</syntax>
<description>
<para>This application is deprecated. Please use the JABBER_STATUS() function instead.</para>
<para>Retrieves the numeric status associated with the specified buddy <replaceable>JID</replaceable>.
The return value in the <replaceable>Variable</replaceable>will be one of the following.</para>
<enumlist>
<enum name="1">
<para>Online.</para>
</enum>
<enum name="2">
<para>Chatty.</para>
</enum>
<enum name="3">
<para>Away.</para>
</enum>
<enum name="4">
<para>Extended Away.</para>
</enum>
<enum name="5">
<para>Do Not Disturb.</para>
</enum>
<enum name="6">
<para>Offline.</para>
</enum>
<enum name="7">
<para>Not In Roster.</para>
</enum>
</enumlist>
</description>
</application>
<manager name="JabberSend" language="en_US" module="res_xmpp">
<synopsis>
Sends a message to a Jabber Client.
</synopsis>
<syntax>
<xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
<parameter name="Jabber" required="true">
<para>Client or transport Asterisk uses to connect to JABBER.</para>
</parameter>
<parameter name="JID" required="true">
<para>XMPP/Jabber JID (Name) of recipient.</para>
</parameter>
<parameter name="Message" required="true">
<para>Message to be sent to the buddy.</para>
</parameter>
</syntax>
<description>
<para>Sends a message to a Jabber Client.</para>
</description>
</manager>
<info name="XMPPMessageToInfo" language="en_US" tech="XMPP">
<para>Specifying a prefix of <literal>xmpp:</literal> will send the
message as an XMPP chat message.</para>
</info>
<info name="XMPPMessageFromInfo" language="en_US" tech="XMPP">
<para>Specifying a prefix of <literal>xmpp:</literal> will specify the
account defined in <literal>xmpp.conf</literal> to send the message from.
Note that this field is required for XMPP messages.</para>
</info>
<configInfo name="res_xmpp" language="en_US">
<synopsis>XMPP Messaging</synopsis>
<configFile name="xmpp.conf">
<configObject name="global">
<synopsis>Global configuration settings</synopsis>
<configOption name="debug">
<synopsis>Enable/disable XMPP message debugging</synopsis>
</configOption>
<configOption name="autoprune">
<synopsis>Auto-remove users from buddy list.</synopsis>
<description><para>Auto-remove users from buddy list. Depending on the setup
(e.g., using your personal Gtalk account for a test) this could cause loss of
the contact list.
</para></description>
</configOption>
<configOption name="autoregister">
<synopsis>Auto-register users from buddy list</synopsis>
</configOption>
<configOption name="collection_nodes">
<synopsis>Enable support for XEP-0248 for use with distributed device state</synopsis>
</configOption>
<configOption name="pubsub_autocreate">
<synopsis>Whether or not the PubSub server supports/is using auto-create for nodes</synopsis>
</configOption>
<configOption name="auth_policy">
<synopsis>Whether to automatically accept or deny users' subscription requests</synopsis>
</configOption>
</configObject>
<configObject name="client">
<synopsis>Configuration options for an XMPP client</synopsis>
<configOption name="username">
<synopsis>XMPP username with optional resource</synopsis>
</configOption>
<configOption name="secret">
<synopsis>XMPP password</synopsis>
</configOption>
<configOption name="serverhost">
<synopsis>Route to server, e.g. talk.google.com</synopsis>
</configOption>
<configOption name="statusmessage">
<synopsis>Custom status message</synopsis>
</configOption>
<configOption name="pubsub_node">
<synopsis>Node for publishing events via PubSub</synopsis>
</configOption>
<configOption name="context">
<synopsis>Dialplan context to send incoming messages to</synopsis>
</configOption>
<configOption name="priority">
<synopsis>XMPP resource priority</synopsis>
</configOption>
<configOption name="port">
<synopsis>XMPP server port</synopsis>
</configOption>
<configOption name="timeout">
<synopsis>Timeout in seconds to hold incoming messages</synopsis>
<description><para>Timeout (in seconds) on the message stack. Messages stored longer
than this value will be deleted by Asterisk. This option applies to incoming messages only
which are intended to be processed by the <literal>JABBER_RECEIVE</literal> dialplan function.
</para></description>
</configOption>
<configOption name="debug">
<synopsis>Enable debugging</synopsis>
</configOption>
<configOption name="type">
<synopsis>Connection is either a client or a component</synopsis>
</configOption>
<configOption name="distribute_events">
<synopsis>Whether or not to distribute events using this connection</synopsis>
</configOption>
<configOption name="usetls">
<synopsis>Whether to use TLS for the connection or not</synopsis>
</configOption>
<configOption name="usesasl">
<synopsis>Whether to use SASL for the connection or not</synopsis>
</configOption>
<configOption name="forceoldssl">
<synopsis>Force the use of old-style SSL for the connection</synopsis>
</configOption>
<configOption name="keepalive">
<synopsis>If enabled, periodically send an XMPP message from this client with an empty message</synopsis>
</configOption>
<configOption name="autoprune">
<synopsis>Auto-remove users from buddy list.</synopsis>
<description><para>Auto-remove users from buddy list. Depending on the setup
(e.g., using your personal Gtalk account for a test) this could cause loss of
the contact list.
</para></description>
</configOption>
<configOption name="autoregister">
<synopsis>Auto-register users bfrom buddy list</synopsis>
</configOption>
<configOption name="auth_policy">
<synopsis>Whether to automatically accept or deny users' subscription requests</synopsis>
</configOption>
<configOption name="sendtodialplan">
<synopsis>Send incoming messages into the dialplan</synopsis>
</configOption>
<configOption name="status">
<synopsis>Default XMPP status for the client</synopsis>
<description><para>Can be one of the following XMPP statuses:</para>
<enumlist>
<enum name="chat"/>
<enum name="available"/>
<enum name="away"/>
<enum name="xaway"/>
<enum name="dnd"/>
</enumlist>
</description>
</configOption>
<configOption name="buddy">
<synopsis>Manual addition of buddy to list</synopsis>
<description><para>
Manual addition of buddy to the buddy list. For distributed events, these budies are
automatically added in the whitelist as 'owners' of the node(s).
</para></description>
</configOption>
</configObject>
</configFile>
</configInfo>
***/
/*! \brief Supported general configuration flags */
enum {
XMPP_AUTOPRUNE = (1 << 0),
XMPP_AUTOREGISTER = (1 << 1),
XMPP_AUTOACCEPT = (1 << 2),
XMPP_DEBUG = (1 << 3),
XMPP_USETLS = (1 << 4),
XMPP_USESASL = (1 << 5),
XMPP_FORCESSL = (1 << 6),
XMPP_KEEPALIVE = (1 << 7),
XMPP_COMPONENT = (1 << 8),
XMPP_SEND_TO_DIALPLAN = (1 << 9),
XMPP_DISTRIBUTE_EVENTS = (1 << 10),
};
/*! \brief Supported pubsub configuration flags */
enum {
XMPP_XEP0248 = (1 << 0),
XMPP_PUBSUB = (1 << 1),
XMPP_PUBSUB_AUTOCREATE = (1 << 2),
};
/*! \brief Number of buckets for client connections */
#define CLIENT_BUCKETS 53
/*! \brief Number of buckets for buddies (per client) */
#define BUDDY_BUCKETS 53
/*! \brief Number of buckets for resources (per buddy) */
#define RESOURCE_BUCKETS 53
/*! \brief Namespace for TLS support */
#define XMPP_TLS_NS "urn:ietf:params:xml:ns:xmpp-tls"
/*! \brief Status for a disappearing buddy */
#define STATUS_DISAPPEAR 6
/*! \brief Global debug status */
static int debug;
/*! \brief XMPP Global Configuration */
struct ast_xmpp_global_config {
struct ast_flags general; /*!< General configuration options */
struct ast_flags pubsub; /*!< Pubsub related configuration options */
};
/*! \brief XMPP Client Configuration */
struct ast_xmpp_client_config {
AST_DECLARE_STRING_FIELDS(
AST_STRING_FIELD(name); /*!< Name of the client connection */
AST_STRING_FIELD(user); /*!< Username to use for authentication */
AST_STRING_FIELD(password); /*!< Password to use for authentication */
AST_STRING_FIELD(server); /*!< Server hostname */
AST_STRING_FIELD(statusmsg); /*!< Status message for presence */
AST_STRING_FIELD(pubsubnode); /*!< Pubsub node */
AST_STRING_FIELD(context); /*!< Context for incoming messages */
);
int port; /*!< Port to use when connecting to server */
int message_timeout; /*!< Timeout for messages */
int priority; /*!< Resource priority */
struct ast_flags flags; /*!< Various options that have been set */
struct ast_flags mod_flags; /*!< Global options that have been modified */
enum ikshowtype status; /*!< Presence status */
struct ast_xmpp_client *client; /*!< Pointer to the client */
struct ao2_container *buddies; /*!< Configured buddies */
};
struct xmpp_config {
struct ast_xmpp_global_config *global; /*!< Global configuration options */
struct ao2_container *clients; /*!< Configured clients */
};
static AO2_GLOBAL_OBJ_STATIC(globals);
static int xmpp_client_request_tls(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, int type, iks *node);
static int xmpp_client_requested_tls(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, int type, iks *node);
static int xmpp_client_authenticate(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, int type, iks *node);
static int xmpp_client_authenticating(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, int type, iks *node);
static int xmpp_component_authenticate(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, int type, iks *node);
static int xmpp_component_authenticating(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, int type, iks *node);
/*! \brief Defined handlers for XMPP client states */
static const struct xmpp_state_handler {
int state;
int component;
int (*handler)(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, int type, iks *node);
} xmpp_state_handlers[] = {
{ XMPP_STATE_REQUEST_TLS, 0, xmpp_client_request_tls, },
{ XMPP_STATE_REQUESTED_TLS, 0, xmpp_client_requested_tls, },
{ XMPP_STATE_AUTHENTICATE, 0, xmpp_client_authenticate, },
{ XMPP_STATE_AUTHENTICATING, 0, xmpp_client_authenticating, },
{ XMPP_STATE_AUTHENTICATE, 1, xmpp_component_authenticate, },
{ XMPP_STATE_AUTHENTICATING, 1, xmpp_component_authenticating, },
};
static int xmpp_pak_message(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, iks *node, ikspak *pak);
static int xmpp_pak_presence(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, iks *node, ikspak *pak);
static int xmpp_pak_s10n(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, iks *node, ikspak *pak);
/*! \brief Defined handlers for different PAK types */
static const struct xmpp_pak_handler {
int type;
int (*handler)(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, iks *node, ikspak *pak);
} xmpp_pak_handlers[] = {
{ IKS_PAK_MESSAGE, xmpp_pak_message, },
{ IKS_PAK_PRESENCE, xmpp_pak_presence, },
{ IKS_PAK_S10N, xmpp_pak_s10n, },
};
static const char *app_ajisend = "JabberSend";
static const char *app_ajisendgroup = "JabberSendGroup";
static const char *app_ajistatus = "JabberStatus";
static const char *app_ajijoin = "JabberJoin";
static const char *app_ajileave = "JabberLeave";
static ast_cond_t message_received_condition;
static ast_mutex_t messagelock;
static int xmpp_client_config_post_apply(void *obj, void *arg, int flags);
/*! \brief Destructor function for configuration */
static void ast_xmpp_client_config_destructor(void *obj)
{
struct ast_xmpp_client_config *cfg = obj;
ast_string_field_free_memory(cfg);
ao2_cleanup(cfg->client);
ao2_cleanup(cfg->buddies);
}
/*! \brief Destroy function for XMPP messages */
static void xmpp_message_destroy(struct ast_xmpp_message *message)
{
if (message->from) {
ast_free(message->from);
}
if (message->message) {
ast_free(message->message);
}
ast_free(message);
}
/*! \brief Destructor callback function for XMPP client */
static void xmpp_client_destructor(void *obj)
{
struct ast_xmpp_client *client = obj;
struct ast_xmpp_message *message;
ast_xmpp_client_disconnect(client);
ast_endpoint_shutdown(client->endpoint);
ao2_cleanup(client->endpoint);
client->endpoint = NULL;
if (client->filter) {
iks_filter_delete(client->filter);
}
if (client->stack) {
iks_stack_delete(client->stack);
}
ao2_cleanup(client->buddies);
while ((message = AST_LIST_REMOVE_HEAD(&client->messages, list))) {
xmpp_message_destroy(message);
}
AST_LIST_HEAD_DESTROY(&client->messages);
}
/*! \brief Hashing function for XMPP buddy */
static int xmpp_buddy_hash(const void *obj, const int flags)
{
const struct ast_xmpp_buddy *buddy = obj;
const char *id = obj;
return ast_str_hash(flags & OBJ_KEY ? id : buddy->id);
}
/*! \brief Comparator function for XMPP buddy */
static int xmpp_buddy_cmp(void *obj, void *arg, int flags)
{
struct ast_xmpp_buddy *buddy1 = obj, *buddy2 = arg;
const char *id = arg;
return !strcmp(buddy1->id, flags & OBJ_KEY ? id : buddy2->id) ? CMP_MATCH | CMP_STOP : 0;
}
/*! \brief Internal function which changes the XMPP client state */
static void xmpp_client_change_state(struct ast_xmpp_client *client, int state)
{
if (state == client->state) {
return;
}
client->state = state;
if (client->state == XMPP_STATE_DISCONNECTED) {
ast_endpoint_set_state(client->endpoint, AST_ENDPOINT_OFFLINE);
} else if (client->state == XMPP_STATE_CONNECTED) {
ast_endpoint_set_state(client->endpoint, AST_ENDPOINT_ONLINE);
}
}
/*! \brief Allocator function for ast_xmpp_client */
static struct ast_xmpp_client *xmpp_client_alloc(const char *name)
{
struct ast_xmpp_client *client;
if (!(client = ao2_alloc(sizeof(*client), xmpp_client_destructor))) {
return NULL;
}
AST_LIST_HEAD_INIT(&client->messages);
client->thread = AST_PTHREADT_NULL;
client->endpoint = ast_endpoint_create("XMPP", name);
if (!client->endpoint) {
ao2_ref(client, -1);
return NULL;
}
if (!(client->buddies = ao2_container_alloc(BUDDY_BUCKETS, xmpp_buddy_hash, xmpp_buddy_cmp))) {
ast_log(LOG_ERROR, "Could not initialize buddy container for '%s'\n", name);
ao2_ref(client, -1);
return NULL;
}
if (ast_string_field_init(client, 512)) {
ast_log(LOG_ERROR, "Could not initialize stringfields for '%s'\n", name);
ao2_ref(client, -1);
return NULL;
}
if (!(client->stack = iks_stack_new(8192, 8192))) {
ast_log(LOG_ERROR, "Could not create an Iksemel stack for '%s'\n", name);
ao2_ref(client, -1);
return NULL;
}
ast_string_field_set(client, name, name);
client->timeout = 50;
xmpp_client_change_state(client, XMPP_STATE_DISCONNECTED);
ast_copy_string(client->mid, "aaaaa", sizeof(client->mid));
return client;
}
/*! \brief Find function for configuration */
static void *xmpp_config_find(struct ao2_container *tmp_container, const char *category)
{
return ao2_find(tmp_container, category, OBJ_KEY);
}
/*! \brief Look up existing client or create a new one */
static void *xmpp_client_find_or_create(const char *category)
{
RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, category))) {
return xmpp_client_alloc(category);
}
ao2_ref(clientcfg->client, +1);
return clientcfg->client;
}
/*! \brief Allocator function for configuration */
static void *ast_xmpp_client_config_alloc(const char *cat)
{
struct ast_xmpp_client_config *cfg;
if (!(cfg = ao2_alloc(sizeof(*cfg), ast_xmpp_client_config_destructor))) {
return NULL;
}
if (ast_string_field_init(cfg, 512)) {
ao2_ref(cfg, -1);
return NULL;
}
if (!(cfg->client = xmpp_client_find_or_create(cat))) {
ao2_ref(cfg, -1);
return NULL;
}
if (!(cfg->buddies = ao2_container_alloc(BUDDY_BUCKETS, xmpp_buddy_hash, xmpp_buddy_cmp))) {
ao2_ref(cfg, -1);
return NULL;
}
ast_string_field_set(cfg, name, cat);
return cfg;
}
/*! \brief Destructor for XMPP configuration */
static void xmpp_config_destructor(void *obj)
{
struct xmpp_config *cfg = obj;
ao2_cleanup(cfg->global);
ao2_cleanup(cfg->clients);
}
/*! \brief Hashing function for configuration */
static int xmpp_config_hash(const void *obj, const int flags)
{
const struct ast_xmpp_client_config *cfg = obj;
const char *name = (flags & OBJ_KEY) ? obj : cfg->name;
return ast_str_case_hash(name);
}
/*! \brief Comparator function for configuration */
static int xmpp_config_cmp(void *obj, void *arg, int flags)
{
struct ast_xmpp_client_config *one = obj, *two = arg;
const char *match = (flags & OBJ_KEY) ? arg : two->name;
return strcasecmp(one->name, match) ? 0 : (CMP_MATCH | CMP_STOP);
}
/*! \brief Allocator for XMPP configuration */
static void *xmpp_config_alloc(void)
{
struct xmpp_config *cfg;
if (!(cfg = ao2_alloc(sizeof(*cfg), xmpp_config_destructor))) {
return NULL;
}
if (!(cfg->global = ao2_alloc(sizeof(*cfg->global), NULL))) {
goto error;
}
if (!(cfg->clients = ao2_container_alloc(1, xmpp_config_hash, xmpp_config_cmp))) {
goto error;
}
return cfg;
error:
ao2_ref(cfg, -1);
return NULL;
}
static int xmpp_config_prelink(void *newitem)
{
struct ast_xmpp_client_config *clientcfg = newitem;
RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
RAII_VAR(struct ast_xmpp_client_config *, oldclientcfg, NULL, ao2_cleanup);
if (ast_strlen_zero(clientcfg->user)) {
ast_log(LOG_ERROR, "No user specified on client '%s'\n", clientcfg->name);
return -1;
} else if (ast_strlen_zero(clientcfg->password)) {
ast_log(LOG_ERROR, "No password specified on client '%s'\n", clientcfg->name);
return -1;
} else if (ast_strlen_zero(clientcfg->server)) {
ast_log(LOG_ERROR, "No server specified on client '%s'\n", clientcfg->name);
return -1;
}
/* If this is a new connection force a reconnect */
if (!cfg || !cfg->clients || !(oldclientcfg = xmpp_config_find(cfg->clients, clientcfg->name))) {
clientcfg->client->reconnect = 1;
return 0;
}
/* If any configuration options are changing that would require reconnecting set the bit so we will do so if possible */
if (strcmp(clientcfg->user, oldclientcfg->user) ||
strcmp(clientcfg->password, oldclientcfg->password) ||
strcmp(clientcfg->server, oldclientcfg->server) ||
(clientcfg->port != oldclientcfg->port) ||
(ast_test_flag(&clientcfg->flags, XMPP_COMPONENT) != ast_test_flag(&oldclientcfg->flags, XMPP_COMPONENT)) ||
(clientcfg->priority != oldclientcfg->priority)) {
clientcfg->client->reconnect = 1;
} else {
clientcfg->client->reconnect = 0;
}
return 0;
}
static void xmpp_config_post_apply(void)
{
RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
ao2_callback(cfg->clients, OBJ_NODATA | OBJ_MULTIPLE, xmpp_client_config_post_apply, NULL);
}
static struct aco_type global_option = {
.type = ACO_GLOBAL,
.name = "global",
.item_offset = offsetof(struct xmpp_config, global),
.category_match = ACO_WHITELIST,
.category = "^general$",
};
struct aco_type *global_options[] = ACO_TYPES(&global_option);
static struct aco_type client_option = {
.type = ACO_ITEM,
.name = "client",
.category_match = ACO_BLACKLIST,
.category = "^(general)$",
.item_alloc = ast_xmpp_client_config_alloc,
.item_find = xmpp_config_find,
.item_prelink = xmpp_config_prelink,
.item_offset = offsetof(struct xmpp_config, clients),
};
struct aco_type *client_options[] = ACO_TYPES(&client_option);
struct aco_file res_xmpp_conf = {
.filename = "xmpp.conf",
.alias = "jabber.conf",
.types = ACO_TYPES(&global_option, &client_option),
};
CONFIG_INFO_STANDARD(cfg_info, globals, xmpp_config_alloc,
.files = ACO_FILES(&res_xmpp_conf),
.post_apply_config = xmpp_config_post_apply,
);
/*! \brief Destructor callback function for XMPP resource */
static void xmpp_resource_destructor(void *obj)
{
struct ast_xmpp_resource *resource = obj;
if (resource->description) {
ast_free(resource->description);
}
}
/*! \brief Hashing function for XMPP resource */
static int xmpp_resource_hash(const void *obj, const int flags)
{
const struct ast_xmpp_resource *resource = obj;
return flags & OBJ_KEY ? -1 : resource->priority;
}
/*! \brief Comparator function for XMPP resource */
static int xmpp_resource_cmp(void *obj, void *arg, int flags)
{
struct ast_xmpp_resource *resource1 = obj;
const char *resource = arg;
return !strcmp(resource1->resource, resource) ? CMP_MATCH | CMP_STOP : 0;
}
/*! \brief Destructor callback function for XMPP buddy */
static void xmpp_buddy_destructor(void *obj)
{
struct ast_xmpp_buddy *buddy = obj;
if (buddy->resources) {
ao2_ref(buddy->resources, -1);
}
}
/*! \brief Helper function which returns whether an XMPP client connection is secure or not */
static int xmpp_is_secure(struct ast_xmpp_client *client)
{
#ifdef HAVE_OPENSSL
return client->stream_flags & SECURE;
#else
return 0;
#endif
}
struct ast_xmpp_client *ast_xmpp_client_find(const char *name)
{
RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, name))) {
return NULL;
}
ao2_ref(clientcfg->client, +1);
return clientcfg->client;
}
void ast_xmpp_client_unref(struct ast_xmpp_client *client)
{
ao2_ref(client, -1);
}
void ast_xmpp_client_lock(struct ast_xmpp_client *client)
{
ao2_lock(client);
}
void ast_xmpp_client_unlock(struct ast_xmpp_client *client)
{
ao2_unlock(client);
}
/*! \brief Internal function used to send a message to a user or chatroom */
static int xmpp_client_send_message(struct ast_xmpp_client *client, int group, const char *nick, const char *address, const char *message)
{
RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
int res = 0;
char from[XMPP_MAX_JIDLEN];
iks *message_packet;
if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, client->name)) ||
!(message_packet = iks_make_msg(group ? IKS_TYPE_GROUPCHAT : IKS_TYPE_CHAT, address, message))) {
return -1;
}
if (!ast_strlen_zero(nick) && ast_test_flag(&clientcfg->flags, XMPP_COMPONENT)) {
snprintf(from, sizeof(from), "%s@%s/%s", nick, client->jid->full, nick);
} else {
snprintf(from, sizeof(from), "%s", client->jid->full);
}
iks_insert_attrib(message_packet, "from", from);
res = ast_xmpp_client_send(client, message_packet);
iks_delete(message_packet);
return res;
}
int ast_xmpp_client_send_message(struct ast_xmpp_client *client, const char *user, const char *message)
{
return xmpp_client_send_message(client, 0, NULL, user, message);
}
int ast_xmpp_chatroom_invite(struct ast_xmpp_client *client, const char *user, const char *room, const char *message)
{
int res = 0;
iks *invite, *body = NULL, *namespace = NULL;
if (!(invite = iks_new("message")) || !(body = iks_new("body")) || !(namespace = iks_new("x"))) {
res = -1;
goto done;
}
iks_insert_attrib(invite, "to", user);
ast_xmpp_client_lock(client);
iks_insert_attrib(invite, "id", client->mid);
ast_xmpp_increment_mid(client->mid);
ast_xmpp_client_unlock(client);
iks_insert_cdata(body, message, 0);
iks_insert_node(invite, body);
iks_insert_attrib(namespace, "xmlns", "jabber:x:conference");
iks_insert_attrib(namespace, "jid", room);
iks_insert_node(invite, namespace);
res = ast_xmpp_client_send(client, invite);
done:
iks_delete(namespace);
iks_delete(body);
iks_delete(invite);
return res;
}
static int xmpp_client_set_group_presence(struct ast_xmpp_client *client, const char *room, int level, const char *nick)
{
RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
int res = 0;
iks *presence = NULL, *x = NULL;
char from[XMPP_MAX_JIDLEN], roomid[XMPP_MAX_JIDLEN];
if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, client->name)) ||
!(presence = iks_make_pres(level, NULL)) || !(x = iks_new("x"))) {
res = -1;
goto done;
}
if (ast_test_flag(&clientcfg->flags, XMPP_COMPONENT)) {
snprintf(from, sizeof(from), "%s@%s/%s", nick, client->jid->full, nick);
snprintf(roomid, sizeof(roomid), "%s/%s", room, nick);
} else {
snprintf(from, sizeof(from), "%s", client->jid->full);
snprintf(roomid, sizeof(roomid), "%s/%s", room, S_OR(nick, client->jid->user));
}
iks_insert_attrib(presence, "to", roomid);
iks_insert_attrib(presence, "from", from);
iks_insert_attrib(x, "xmlns", "http://jabber.org/protocol/muc");
iks_insert_node(presence, x);