-
-
Notifications
You must be signed in to change notification settings - Fork 224
Expand file tree
/
Copy pathiq.c
More file actions
3031 lines (2545 loc) · 98.7 KB
/
iq.c
File metadata and controls
3031 lines (2545 loc) · 98.7 KB
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
/*
* iq.c
* vim: expandtab:ts=4:sts=4:sw=4
*
* Copyright (C) 2012 - 2019 James Booth <boothj5@gmail.com>
*
* SPDX-License-Identifier: GPL-3.0-or-later WITH OpenSSL-exception
*/
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <glib.h>
#include <strophe.h>
#include "profanity.h"
#include "log.h"
#include "config/preferences.h"
#include "event/server_events.h"
#include "plugins/plugins.h"
#include "tools/http_upload.h"
#include "ui/ui.h"
#include "ui/window_list.h"
#include "xmpp/xmpp.h"
#include "xmpp/connection.h"
#include "xmpp/session.h"
#include "xmpp/iq.h"
#include "xmpp/capabilities.h"
#include "xmpp/blocking.h"
#include "xmpp/session.h"
#include "xmpp/stanza.h"
#include "xmpp/form.h"
#include "xmpp/roster_list.h"
#include "xmpp/roster.h"
#include "xmpp/muc.h"
#include "src/database.h"
#include "ui/window.h"
#ifdef HAVE_OMEMO
#include "omemo/omemo.h"
#endif
typedef struct p_room_info_data_t
{
char* room;
gboolean display;
} ProfRoomInfoData;
typedef struct p_iq_handle_t
{
ProfIqCallback func;
ProfIqFreeCallback free_func;
void* userdata;
} ProfIqHandler;
typedef struct privilege_set_t
{
char* item;
char* privilege;
} ProfPrivilegeSet;
typedef struct affiliation_list_t
{
char* affiliation;
bool show_ui_message;
} ProfAffiliationList;
typedef struct command_config_data_t
{
char* sessionid;
char* command;
} CommandConfigData;
typedef struct mam_rsm_userdata
{
char* barejid;
char* start_datestr;
char* end_datestr;
gboolean fetch_next;
ProfChatWin* win;
} MamRsmUserdata;
typedef struct late_delivery_userdata
{
ProfChatWin* win;
GDateTime* enddate;
GDateTime* startdate;
} LateDeliveryUserdata;
static int _iq_handler(xmpp_conn_t* const conn, xmpp_stanza_t* const stanza, void* const userdata);
static void _error_handler(xmpp_stanza_t* const stanza);
static void _disco_info_get_handler(xmpp_stanza_t* const stanza);
static void _disco_items_get_handler(xmpp_stanza_t* const stanza);
static void _disco_items_result_handler(xmpp_stanza_t* const stanza);
static void _last_activity_get_handler(xmpp_stanza_t* const stanza);
static void _version_get_handler(xmpp_stanza_t* const stanza);
static void _ping_get_handler(xmpp_stanza_t* const stanza);
static int _version_result_id_handler(xmpp_stanza_t* const stanza, void* const userdata);
static int _disco_info_response_id_handler(xmpp_stanza_t* const stanza, void* const userdata);
static int _disco_info_response_id_handler_onconnect(xmpp_stanza_t* const stanza, void* const userdata);
static int _http_upload_response_id_handler(xmpp_stanza_t* const stanza, void* const upload_ctx);
static int _last_activity_response_id_handler(xmpp_stanza_t* const stanza, void* const userdata);
static int _room_info_response_id_handler(xmpp_stanza_t* const stanza, void* const userdata);
static int _destroy_room_result_id_handler(xmpp_stanza_t* const stanza, void* const userdata);
static int _room_config_id_handler(xmpp_stanza_t* const stanza, void* const userdata);
static int _room_config_submit_id_handler(xmpp_stanza_t* const stanza, void* const userdata);
static int _room_affiliation_list_result_id_handler(xmpp_stanza_t* const stanza, void* const userdata);
static int _room_affiliation_set_result_id_handler(xmpp_stanza_t* const stanza, void* const userdata);
static int _room_role_set_result_id_handler(xmpp_stanza_t* const stanza, void* const userdata);
static int _room_role_list_result_id_handler(xmpp_stanza_t* const stanza, void* const userdata);
static int _room_kick_result_id_handler(xmpp_stanza_t* const stanza, void* const userdata);
static int _enable_carbons_id_handler(xmpp_stanza_t* const stanza, void* const userdata);
static int _disable_carbons_id_handler(xmpp_stanza_t* const stanza, void* const userdata);
static int _manual_pong_id_handler(xmpp_stanza_t* const stanza, void* const userdata);
static int _caps_response_id_handler(xmpp_stanza_t* const stanza, void* const userdata);
static int _caps_response_for_jid_id_handler(xmpp_stanza_t* const stanza, void* const userdata);
static int _caps_response_legacy_id_handler(xmpp_stanza_t* const stanza, void* const userdata);
static int _auto_pong_id_handler(xmpp_stanza_t* const stanza, void* const userdata);
static int _room_list_id_handler(xmpp_stanza_t* const stanza, void* const userdata);
static int _command_list_result_handler(xmpp_stanza_t* const stanza, void* const userdata);
static int _command_exec_response_handler(xmpp_stanza_t* const stanza, void* const userdata);
static int _mam_rsm_id_handler(xmpp_stanza_t* const stanza, void* const userdata);
static int _register_change_password_result_id_handler(xmpp_stanza_t* const stanza, void* const userdata);
static void _iq_mam_request(ProfChatWin* win, GDateTime* startdate, GDateTime* enddate);
static void _iq_free_room_data(ProfRoomInfoData* roominfo);
static void _iq_free_affiliation_set(ProfPrivilegeSet* affiliation_set);
static void _iq_free_affiliation_list(ProfAffiliationList* affiliation_list);
static void _iq_id_handler_free(ProfIqHandler* handler);
// scheduled
static int _autoping_timed_send(xmpp_conn_t* const conn, void* const userdata);
static void _identity_destroy(DiscoIdentity* identity);
static void _item_destroy(DiscoItem* item);
static gboolean autoping_wait = FALSE;
static GTimer* autoping_time = NULL;
static GHashTable* id_handlers;
static GHashTable* rooms_cache = NULL;
static GSList* late_delivery_windows = NULL;
static gboolean received_disco_items = FALSE;
static int
_iq_handler(xmpp_conn_t* const conn, xmpp_stanza_t* const stanza, void* const userdata)
{
log_debug("iq stanza handler fired");
autoping_timer_extend();
char* text;
size_t text_size;
xmpp_stanza_to_text(stanza, &text, &text_size);
gboolean cont = plugins_on_iq_stanza_receive(text);
xmpp_free(connection_get_ctx(), text);
if (!cont || !id_handlers) {
return 1;
}
const char* type = xmpp_stanza_get_type(stanza);
if (g_strcmp0(type, STANZA_TYPE_ERROR) == 0) {
_error_handler(stanza);
}
xmpp_stanza_t* discoinfo = xmpp_stanza_get_child_by_ns(stanza, XMPP_NS_DISCO_INFO);
if (discoinfo && (g_strcmp0(type, STANZA_TYPE_GET) == 0)) {
_disco_info_get_handler(stanza);
}
xmpp_stanza_t* discoitems = xmpp_stanza_get_child_by_ns(stanza, XMPP_NS_DISCO_ITEMS);
if (discoitems && (g_strcmp0(type, STANZA_TYPE_GET) == 0)) {
_disco_items_get_handler(stanza);
}
if (discoitems && (g_strcmp0(type, STANZA_TYPE_RESULT) == 0)) {
_disco_items_result_handler(stanza);
}
xmpp_stanza_t* lastactivity = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_LASTACTIVITY);
if (lastactivity && (g_strcmp0(type, STANZA_TYPE_GET) == 0)) {
_last_activity_get_handler(stanza);
}
xmpp_stanza_t* version = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_VERSION);
if (version && (g_strcmp0(type, STANZA_TYPE_GET) == 0)) {
_version_get_handler(stanza);
}
xmpp_stanza_t* ping = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_PING);
if (ping && (g_strcmp0(type, STANZA_TYPE_GET) == 0)) {
_ping_get_handler(stanza);
}
xmpp_stanza_t* roster = xmpp_stanza_get_child_by_ns(stanza, XMPP_NS_ROSTER);
if (roster && (g_strcmp0(type, STANZA_TYPE_SET) == 0)) {
roster_set_handler(stanza);
}
if (roster && (g_strcmp0(type, STANZA_TYPE_RESULT) == 0)) {
roster_result_handler(stanza);
}
xmpp_stanza_t* blocking = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_BLOCKING);
if (blocking && (g_strcmp0(type, STANZA_TYPE_SET) == 0)) {
blocked_set_handler(stanza);
}
const char* id = xmpp_stanza_get_id(stanza);
if (id) {
ProfIqHandler* handler = g_hash_table_lookup(id_handlers, id);
if (handler) {
int keep = handler->func(stanza, handler->userdata);
if (!keep) {
g_hash_table_remove(id_handlers, id);
}
}
}
return 1;
}
static void
_xmpp_stanza_release_destroy_notify(gpointer data)
{
xmpp_stanza_release((xmpp_stanza_t*)data);
}
void
iq_handlers_init(void)
{
xmpp_conn_t* const conn = connection_get_conn();
xmpp_ctx_t* const ctx = connection_get_ctx();
xmpp_handler_add(conn, _iq_handler, NULL, STANZA_NAME_IQ, NULL, ctx);
if (prefs_get_autoping() != 0) {
int millis = prefs_get_autoping() * 1000;
xmpp_timed_handler_add(conn, _autoping_timed_send, millis, ctx);
}
received_disco_items = FALSE;
iq_handlers_clear();
id_handlers = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)_iq_id_handler_free);
rooms_cache = g_hash_table_new_full(g_str_hash, g_str_equal, free, _xmpp_stanza_release_destroy_notify);
}
struct iq_win_finder
{
gsize max, cur;
char** to_be_removed;
};
static void
_win_find(char* key,
ProfIqHandler* handler,
struct iq_win_finder* finder)
{
if (handler->func == _mam_rsm_id_handler) {
if (finder->cur >= finder->max) {
finder->max *= 2;
finder->to_be_removed = g_realloc_n(finder->to_be_removed, finder->max, sizeof(char*));
}
finder->to_be_removed[finder->cur++] = g_strdup(key);
}
}
static void
_free_late_delivery_userdata(LateDeliveryUserdata* d)
{
if (!d)
return;
if (d->enddate)
g_date_time_unref(d->enddate);
if (d->startdate)
g_date_time_unref(d->startdate);
free(d);
}
void
iq_handlers_remove_win(ProfWin* window)
{
log_debug("Remove window %p of type %d", window, window ? window->type : (win_type_t)-1);
if (!window)
return;
GSList *cur = late_delivery_windows, *next;
while (cur) {
LateDeliveryUserdata* del_data = cur->data;
next = g_slist_next(cur);
if (del_data->win == (void*)window) {
late_delivery_windows = g_slist_delete_link(late_delivery_windows,
cur);
_free_late_delivery_userdata(del_data);
}
cur = next;
}
struct iq_win_finder st = { 0 };
st.max = g_hash_table_size(id_handlers);
if (st.max == 0)
return;
st.to_be_removed = g_new(char*, st.max);
g_hash_table_foreach(id_handlers, (GHFunc)_win_find, &st);
for (gsize n = 0; n < st.cur; ++n) {
g_hash_table_remove(id_handlers, st.to_be_removed[n]);
g_free(st.to_be_removed[n]);
}
g_free(st.to_be_removed);
}
void
iq_handlers_clear(void)
{
iq_rooms_cache_clear();
if (id_handlers) {
g_hash_table_destroy(id_handlers);
id_handlers = NULL;
}
if (late_delivery_windows) {
g_slist_free_full(late_delivery_windows, (GDestroyNotify)_free_late_delivery_userdata);
late_delivery_windows = NULL;
}
}
static void
_iq_id_handler_free(ProfIqHandler* handler)
{
if (handler == NULL) {
return;
}
if (handler->free_func && handler->userdata) {
handler->free_func(handler->userdata);
}
free(handler);
}
void
iq_id_handler_add(const char* const id, ProfIqCallback func, ProfIqFreeCallback free_func, void* userdata)
{
if (id == NULL) {
if (free_func) {
free_func(userdata);
}
return;
}
ProfIqHandler* handler = g_new0(ProfIqHandler, 1);
handler->func = func;
handler->free_func = free_func;
handler->userdata = userdata;
g_hash_table_insert(id_handlers, strdup(id), handler);
}
void
iq_autoping_timer_cancel(void)
{
autoping_wait = FALSE;
if (autoping_time) {
g_timer_destroy(autoping_time);
autoping_time = NULL;
}
}
void
iq_autoping_check(void)
{
if (connection_get_status() != JABBER_CONNECTED) {
return;
}
if (autoping_wait == FALSE) {
return;
}
if (autoping_time == NULL) {
return;
}
gdouble elapsed = g_timer_elapsed(autoping_time, NULL);
gint seconds_elapsed = elapsed * 1.0;
gint timeout = prefs_get_autoping_timeout();
if (timeout > 0 && seconds_elapsed >= timeout) {
cons_show("Autoping response timed out after %u seconds.", timeout);
log_debug("Autoping check: timed out after %u seconds, disconnecting", timeout);
iq_autoping_timer_cancel();
session_autoping_fail();
}
}
void
iq_set_autoping(const int seconds)
{
if (connection_get_status() != JABBER_CONNECTED) {
return;
}
xmpp_conn_t* const conn = connection_get_conn();
xmpp_timed_handler_delete(conn, _autoping_timed_send);
if (seconds == 0) {
return;
}
int millis = seconds * 1000;
xmpp_ctx_t* const ctx = connection_get_ctx();
xmpp_timed_handler_add(conn, _autoping_timed_send, millis, ctx);
}
void
iq_rooms_cache_clear(void)
{
if (rooms_cache) {
g_hash_table_destroy(rooms_cache);
rooms_cache = NULL;
}
}
void
iq_room_list_request(const char* conferencejid, char* filter)
{
if (g_hash_table_contains(rooms_cache, conferencejid)) {
log_debug("Rooms request cached for: %s", conferencejid);
_room_list_id_handler(g_hash_table_lookup(rooms_cache, conferencejid), filter);
return;
}
log_debug("Rooms request not cached for: %s", conferencejid);
xmpp_ctx_t* const ctx = connection_get_ctx();
char* id = connection_create_stanza_id();
xmpp_stanza_t* iq = stanza_create_disco_items_iq(ctx, id, conferencejid, NULL);
iq_id_handler_add(id, _room_list_id_handler, NULL, filter);
iq_send_stanza(iq);
xmpp_stanza_release(iq);
}
void
iq_enable_carbons(void)
{
xmpp_ctx_t* const ctx = connection_get_ctx();
xmpp_stanza_t* iq = stanza_enable_carbons(ctx);
const char* id = xmpp_stanza_get_id(iq);
iq_id_handler_add(id, _enable_carbons_id_handler, NULL, NULL);
iq_send_stanza(iq);
xmpp_stanza_release(iq);
}
void
iq_disable_carbons(void)
{
xmpp_ctx_t* const ctx = connection_get_ctx();
xmpp_stanza_t* iq = stanza_disable_carbons(ctx);
const char* id = xmpp_stanza_get_id(iq);
iq_id_handler_add(id, _disable_carbons_id_handler, NULL, NULL);
iq_send_stanza(iq);
xmpp_stanza_release(iq);
}
void
iq_http_upload_request(HTTPUpload* upload)
{
const char* jid = connection_jid_for_feature(STANZA_NS_HTTP_UPLOAD);
if (jid == NULL) {
cons_show_error("XEP-0363 HTTP File Upload is not supported by the server");
return;
}
xmpp_ctx_t* const ctx = connection_get_ctx();
auto_char char* id = connection_create_stanza_id();
xmpp_stanza_t* iq = stanza_create_http_upload_request(ctx, id, jid, upload);
iq_id_handler_add(id, _http_upload_response_id_handler, NULL, upload);
iq_send_stanza(iq);
xmpp_stanza_release(iq);
return;
}
void
iq_disco_info_request(const char* jid)
{
xmpp_ctx_t* const ctx = connection_get_ctx();
auto_char char* id = connection_create_stanza_id();
xmpp_stanza_t* iq = stanza_create_disco_info_iq(ctx, id, jid, NULL);
iq_id_handler_add(id, _disco_info_response_id_handler, NULL, NULL);
iq_send_stanza(iq);
xmpp_stanza_release(iq);
}
void
iq_disco_info_request_onconnect(const char* jid)
{
xmpp_ctx_t* const ctx = connection_get_ctx();
auto_char char* id = connection_create_stanza_id();
xmpp_stanza_t* iq = stanza_create_disco_info_iq(ctx, id, jid, NULL);
iq_id_handler_add(id, _disco_info_response_id_handler_onconnect, NULL, NULL);
iq_send_stanza(iq);
xmpp_stanza_release(iq);
}
void
iq_last_activity_request(const char* jid)
{
xmpp_ctx_t* const ctx = connection_get_ctx();
auto_char char* id = connection_create_stanza_id();
xmpp_stanza_t* iq = stanza_create_last_activity_iq(ctx, id, jid);
iq_id_handler_add(id, _last_activity_response_id_handler, NULL, NULL);
iq_send_stanza(iq);
xmpp_stanza_release(iq);
}
void
iq_room_info_request(const char* const room, gboolean display_result)
{
xmpp_ctx_t* const ctx = connection_get_ctx();
auto_char char* id = connection_create_stanza_id();
xmpp_stanza_t* iq = stanza_create_disco_info_iq(ctx, id, room, NULL);
ProfRoomInfoData* cb_data = g_new0(ProfRoomInfoData, 1);
if (cb_data) {
cb_data->room = strdup(room);
cb_data->display = display_result;
iq_id_handler_add(id, _room_info_response_id_handler, (ProfIqFreeCallback)_iq_free_room_data, cb_data);
iq_send_stanza(iq);
xmpp_stanza_release(iq);
}
}
void
iq_send_caps_request_for_jid(const char* const to, const char* const id,
const char* const node, const char* const ver)
{
xmpp_ctx_t* const ctx = connection_get_ctx();
if (!node) {
log_error("Could not create caps request, no node");
return;
}
if (!ver) {
log_error("Could not create caps request, no ver");
return;
}
GString* node_str = g_string_new("");
g_string_printf(node_str, "%s#%s", node, ver);
xmpp_stanza_t* iq = stanza_create_disco_info_iq(ctx, id, to, node_str->str);
g_string_free(node_str, TRUE);
iq_id_handler_add(id, _caps_response_for_jid_id_handler, free, strdup(to));
iq_send_stanza(iq);
xmpp_stanza_release(iq);
}
void
iq_send_caps_request(const char* const to, const char* const id,
const char* const node, const char* const ver)
{
xmpp_ctx_t* const ctx = connection_get_ctx();
if (!node) {
log_error("Could not create caps request, no node");
return;
}
if (!ver) {
log_error("Could not create caps request, no ver");
return;
}
GString* node_str = g_string_new("");
g_string_printf(node_str, "%s#%s", node, ver);
xmpp_stanza_t* iq = stanza_create_disco_info_iq(ctx, id, to, node_str->str);
g_string_free(node_str, TRUE);
iq_id_handler_add(id, _caps_response_id_handler, NULL, NULL);
iq_send_stanza(iq);
xmpp_stanza_release(iq);
}
void
iq_send_caps_request_legacy(const char* const to, const char* const id,
const char* const node, const char* const ver)
{
xmpp_ctx_t* const ctx = connection_get_ctx();
if (!node) {
log_error("Could not create caps request, no node");
return;
}
if (!ver) {
log_error("Could not create caps request, no ver");
return;
}
gchar* node_str = g_strdup_printf("%s#%s", node, ver);
xmpp_stanza_t* iq = stanza_create_disco_info_iq(ctx, id, to, node_str);
iq_id_handler_add(id, _caps_response_legacy_id_handler, g_free, node_str);
iq_send_stanza(iq);
xmpp_stanza_release(iq);
}
void
iq_disco_items_request(const char* jid)
{
xmpp_ctx_t* const ctx = connection_get_ctx();
xmpp_stanza_t* iq = stanza_create_disco_items_iq(ctx, "discoitemsreq", jid, NULL);
iq_send_stanza(iq);
xmpp_stanza_release(iq);
}
void
iq_disco_items_request_onconnect(const char* jid)
{
xmpp_ctx_t* const ctx = connection_get_ctx();
xmpp_stanza_t* iq = stanza_create_disco_items_iq(ctx, "discoitemsreq_onconnect", jid, NULL);
iq_send_stanza(iq);
xmpp_stanza_release(iq);
}
void
iq_send_software_version(const char* const fulljid)
{
xmpp_ctx_t* const ctx = connection_get_ctx();
xmpp_stanza_t* iq = stanza_create_software_version_iq(ctx, fulljid);
const char* id = xmpp_stanza_get_id(iq);
iq_id_handler_add(id, _version_result_id_handler, free, strdup(fulljid));
iq_send_stanza(iq);
xmpp_stanza_release(iq);
}
void
iq_confirm_instant_room(const char* const room_jid)
{
xmpp_ctx_t* const ctx = connection_get_ctx();
xmpp_stanza_t* iq = stanza_create_instant_room_request_iq(ctx, room_jid);
iq_send_stanza(iq);
xmpp_stanza_release(iq);
}
void
iq_destroy_room(const char* const room_jid)
{
xmpp_ctx_t* const ctx = connection_get_ctx();
xmpp_stanza_t* iq = stanza_create_instant_room_destroy_iq(ctx, room_jid);
const char* id = xmpp_stanza_get_id(iq);
iq_id_handler_add(id, _destroy_room_result_id_handler, NULL, NULL);
iq_send_stanza(iq);
xmpp_stanza_release(iq);
}
void
iq_request_room_config_form(const char* const room_jid)
{
xmpp_ctx_t* const ctx = connection_get_ctx();
xmpp_stanza_t* iq = stanza_create_room_config_request_iq(ctx, room_jid);
const char* id = xmpp_stanza_get_id(iq);
iq_id_handler_add(id, _room_config_id_handler, NULL, NULL);
iq_send_stanza(iq);
xmpp_stanza_release(iq);
}
void
iq_submit_room_config(ProfConfWin* confwin)
{
xmpp_ctx_t* const ctx = connection_get_ctx();
xmpp_stanza_t* iq = stanza_create_room_config_submit_iq(ctx, confwin->roomjid, confwin->form);
const char* id = xmpp_stanza_get_id(iq);
iq_id_handler_add(id, _room_config_submit_id_handler, NULL, NULL);
iq_send_stanza(iq);
xmpp_stanza_release(iq);
}
void
iq_room_config_cancel(ProfConfWin* confwin)
{
xmpp_ctx_t* const ctx = connection_get_ctx();
xmpp_stanza_t* iq = stanza_create_room_config_cancel_iq(ctx, confwin->roomjid);
iq_send_stanza(iq);
xmpp_stanza_release(iq);
}
void
iq_room_affiliation_list(const char* const room, char* affiliation, bool show_ui_message)
{
xmpp_ctx_t* const ctx = connection_get_ctx();
xmpp_stanza_t* iq = stanza_create_room_affiliation_list_iq(ctx, room, affiliation);
const char* id = xmpp_stanza_get_id(iq);
ProfAffiliationList* affiliation_list = g_new0(ProfAffiliationList, 1);
if (affiliation_list) {
affiliation_list->affiliation = strdup(affiliation);
affiliation_list->show_ui_message = show_ui_message;
iq_id_handler_add(id, _room_affiliation_list_result_id_handler, (ProfIqFreeCallback)_iq_free_affiliation_list, affiliation_list);
iq_send_stanza(iq);
xmpp_stanza_release(iq);
}
}
void
iq_room_kick_occupant(const char* const room, const char* const nick, const char* const reason)
{
xmpp_ctx_t* const ctx = connection_get_ctx();
xmpp_stanza_t* iq = stanza_create_room_kick_iq(ctx, room, nick, reason);
const char* id = xmpp_stanza_get_id(iq);
iq_id_handler_add(id, _room_kick_result_id_handler, free, strdup(nick));
iq_send_stanza(iq);
xmpp_stanza_release(iq);
}
void
iq_room_affiliation_set(const char* const room, const char* const jid, char* affiliation,
const char* const reason)
{
xmpp_ctx_t* const ctx = connection_get_ctx();
xmpp_stanza_t* iq = stanza_create_room_affiliation_set_iq(ctx, room, jid, affiliation, reason);
const char* id = xmpp_stanza_get_id(iq);
ProfPrivilegeSet* affiliation_set = g_new0(struct privilege_set_t, 1);
if (affiliation_set) {
affiliation_set->item = strdup(jid);
affiliation_set->privilege = strdup(affiliation);
iq_id_handler_add(id, _room_affiliation_set_result_id_handler, (ProfIqFreeCallback)_iq_free_affiliation_set, affiliation_set);
iq_send_stanza(iq);
xmpp_stanza_release(iq);
}
}
void
iq_room_role_set(const char* const room, const char* const nick, char* role,
const char* const reason)
{
xmpp_ctx_t* const ctx = connection_get_ctx();
xmpp_stanza_t* iq = stanza_create_room_role_set_iq(ctx, room, nick, role, reason);
const char* id = xmpp_stanza_get_id(iq);
struct privilege_set_t* role_set = g_new0(ProfPrivilegeSet, 1);
if (role_set) {
role_set->item = strdup(nick);
role_set->privilege = strdup(role);
iq_id_handler_add(id, _room_role_set_result_id_handler, (ProfIqFreeCallback)_iq_free_affiliation_set, role_set);
iq_send_stanza(iq);
xmpp_stanza_release(iq);
}
}
void
iq_room_role_list(const char* const room, char* role)
{
xmpp_ctx_t* const ctx = connection_get_ctx();
xmpp_stanza_t* iq = stanza_create_room_role_list_iq(ctx, room, role);
const char* id = xmpp_stanza_get_id(iq);
iq_id_handler_add(id, _room_role_list_result_id_handler, free, strdup(role));
iq_send_stanza(iq);
xmpp_stanza_release(iq);
}
void
iq_send_ping(const char* const target)
{
xmpp_ctx_t* const ctx = connection_get_ctx();
xmpp_stanza_t* iq = stanza_create_ping_iq(ctx, target);
const char* id = xmpp_stanza_get_id(iq);
GDateTime* now = g_date_time_new_now_local();
iq_id_handler_add(id, _manual_pong_id_handler, (ProfIqFreeCallback)g_date_time_unref, now);
iq_send_stanza(iq);
xmpp_stanza_release(iq);
}
void
iq_command_list(const char* const target)
{
xmpp_ctx_t* const ctx = connection_get_ctx();
const char* id = connection_create_stanza_id();
xmpp_stanza_t* iq = stanza_create_disco_items_iq(ctx, id, target, STANZA_NS_COMMAND);
iq_id_handler_add(id, _command_list_result_handler, NULL, NULL);
iq_send_stanza(iq);
xmpp_stanza_release(iq);
}
void
iq_command_exec(const char* const target, const char* const command)
{
xmpp_ctx_t* const ctx = connection_get_ctx();
xmpp_stanza_t* iq = stanza_create_command_exec_iq(ctx, target, command);
const char* id = xmpp_stanza_get_id(iq);
iq_id_handler_add(id, _command_exec_response_handler, free, strdup(command));
iq_send_stanza(iq);
xmpp_stanza_release(iq);
}
void
iq_submit_command_config(ProfConfWin* confwin)
{
xmpp_ctx_t* const ctx = connection_get_ctx();
CommandConfigData* data = (CommandConfigData*)confwin->userdata;
xmpp_stanza_t* iq = stanza_create_command_config_submit_iq(ctx, confwin->roomjid, data->command, data->sessionid, confwin->form);
const char* id = xmpp_stanza_get_id(iq);
iq_id_handler_add(id, _command_exec_response_handler, free, strdup(data->command));
iq_send_stanza(iq);
xmpp_stanza_release(iq);
free(data->sessionid);
free(data->command);
free(data);
}
void
iq_cancel_command_config(ProfConfWin* confwin)
{
xmpp_ctx_t* const ctx = connection_get_ctx();
CommandConfigData* data = (CommandConfigData*)confwin->userdata;
xmpp_stanza_t* iq = stanza_create_room_config_cancel_iq(ctx, confwin->roomjid);
iq_send_stanza(iq);
xmpp_stanza_release(iq);
free(data->sessionid);
free(data->command);
free(data);
}
static void
_error_handler(xmpp_stanza_t* const stanza)
{
const char* id = xmpp_stanza_get_id(stanza);
auto_char char* error_msg = stanza_get_error_message(stanza);
if (id) {
log_debug("IQ error handler fired, id: %s, error: %s", id, error_msg);
log_error("IQ error received, id: %s, error: %s", id, error_msg);
} else {
log_debug("IQ error handler fired, error: %s", error_msg);
log_error("IQ error received, error: %s", error_msg);
}
}
static int
_caps_response_id_handler(xmpp_stanza_t* const stanza, void* const userdata)
{
const char* id = xmpp_stanza_get_id(stanza);
xmpp_stanza_t* query = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_QUERY);
const char* type = xmpp_stanza_get_type(stanza);
// ignore non result
if ((g_strcmp0(type, "get") == 0) || (g_strcmp0(type, "set") == 0)) {
return 1;
}
if (id) {
log_debug("Capabilities response handler fired for id %s", id);
} else {
log_debug("Capabilities response handler fired");
}
const char* from = xmpp_stanza_get_from(stanza);
if (!from) {
log_info("_caps_response_id_handler(): No from attribute");
return 0;
}
// handle error responses
if (g_strcmp0(type, STANZA_TYPE_ERROR) == 0) {
auto_char char* error_message = stanza_get_error_message(stanza);
log_warning("Error received for capabilities response from %s: ", from, error_message);
return 0;
}
if (query == NULL) {
log_info("_caps_response_id_handler(): No query element found.");
return 0;
}
const char* node = xmpp_stanza_get_attribute(query, STANZA_ATTR_NODE);
if (node == NULL) {
log_info("_caps_response_id_handler(): No node attribute found");
return 0;
}
// validate sha1
auto_gcharv gchar** split = g_strsplit(node, "#", -1);
char* given_sha1 = split[1];
auto_gchar gchar* generated_sha1 = stanza_create_caps_sha1_from_query(query);
if (g_strcmp0(given_sha1, generated_sha1) != 0) {
log_warning("Generated sha-1 does not match given:");
log_warning("Generated : %s", generated_sha1);
log_warning("Given : %s", given_sha1);
} else {
log_debug("Valid SHA-1 hash found: %s", given_sha1);
if (caps_cache_contains(given_sha1)) {
log_debug("Capabilities already cached: %s", given_sha1);
} else {
log_debug("Capabilities not cached: %s, storing", given_sha1);
EntityCapabilities* capabilities = stanza_create_caps_from_query_element(query);
caps_add_by_ver(given_sha1, capabilities);
caps_destroy(capabilities);
}
caps_map_jid_to_ver(from, given_sha1);
}
return 0;
}
static int
_caps_response_for_jid_id_handler(xmpp_stanza_t* const stanza, void* const userdata)
{
char* jid = (char*)userdata;
const char* id = xmpp_stanza_get_id(stanza);
xmpp_stanza_t* query = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_QUERY);
const char* type = xmpp_stanza_get_type(stanza);
// ignore non result
if ((g_strcmp0(type, "get") == 0) || (g_strcmp0(type, "set") == 0)) {
return 1;
}
if (id) {
log_debug("Capabilities response handler fired for id %s", id);
} else {
log_debug("Capabilities response handler fired");
}
const char* from = xmpp_stanza_get_from(stanza);
if (!from) {
if (jid) {
log_info("_caps_response_for_jid_id_handler(): No from attribute for %s", jid);
} else {
log_info("_caps_response_for_jid_id_handler(): No from attribute");
}
return 0;
}
// handle error responses
if (g_strcmp0(type, STANZA_TYPE_ERROR) == 0) {
auto_char char* error_message = stanza_get_error_message(stanza);
log_warning("Error received for capabilities response from %s: ", from, error_message);
return 0;
}
if (query == NULL) {
if (jid) {
log_info("_caps_response_for_jid_id_handler(): No query element found for %s.", jid);
} else {
log_info("_caps_response_for_jid_id_handler(): No query element found.");
}
return 0;
}
const char* node = xmpp_stanza_get_attribute(query, STANZA_ATTR_NODE);
if (node == NULL) {
if (jid) {
log_info("_caps_response_for_jid_id_handler(): No node attribute found for %s", jid);