-
Notifications
You must be signed in to change notification settings - Fork 20
/
fetion.c
1726 lines (1488 loc) · 45 KB
/
fetion.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
/**
* @file fetion.c
*
* purple
*
* Copyright (C) 2005 Thomas Butter <[email protected]>
*
* ***
* Thanks to Google's Summer of Code Program and the helpful mentors
* ***
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
*/
#include "internal.h"
#include "accountopt.h"
#include "blist.h"
#include "conversation.h"
#include "dnsquery.h"
#include "debug.h"
#include "notify.h"
#include "privacy.h"
#include "prpl.h"
#include "plugin.h"
#include "util.h"
#include "version.h"
#include "network.h"
#include "xmlnode.h"
#include "request.h"
#include "imgstore.h"
#include "sslconn.h"
#include "cmds.h"
#include "sipmsg.h"
#include "dnssrv.h"
#include "ntlm.h"
#include "sipmsg.h"
#include "fetion.h"
#include "f_buddy.h"
#include "f_chat.h"
#include "f_group.h"
#include "f_login.h"
#include "f_sysmsg.h"
#include "f_portrait.h"
#include "f_user.h"
#include "f_util.h"
#include "f_gchat.h"
gint g_callid;
static const char *fetion_list_icon(PurpleAccount * a, PurpleBuddy * b)
{
return "fetion";
}
static void fetion_keep_alive(PurpleConnection * gc)
{
struct fetion_account_data *sip = gc->proto_data;
time_t curtime = time(NULL);
/* register again if first registration expires */
if (sip->reregister < curtime) {
do_register(sip);
}
return;
}
static void fetion_set_status(PurpleAccount * account, PurpleStatus * status)
{
/*
Away = 100,
BeRightBack = 300,
Busy = 600,
DoNotDisturb = 800,
InTheMeeting = 850,
Invisible = 0x383,
Offline = 0,
Online = 400,
OnThePhone = 500,
OutToLunch = 150,
SmsOnline = 1,
Unknown = -1
*/
const char *status_id;
char *body;
int status_code;
status_id = purple_status_get_id(status);
if (!strcmp(status_id, "away"))
status_code = 100;
else if (!strcmp(status_id, "brb"))
status_code = 300;
else if (!strcmp(status_id, "busy"))
status_code = 600;
else if (!strcmp(status_id, "phone"))
status_code = 500;
else if (!strcmp(status_id, "lunch"))
status_code = 150;
else if (!strcmp(status_id, "invisible"))
status_code = 0x383;
else
status_code = 400;
body =
g_strdup_printf
("<args><presence><basic value=\"%d\" /></presence></args>",
status_code);
send_sip_request(account->gc, "S", "", "", "N: SetPresence\r\n", body,
NULL, NULL);
g_free(body);
//<args><presence><basic value="400" /></presence></args>
}
static struct sip_connection *connection_find(struct fetion_account_data
*sip, int fd)
{
struct sip_connection *ret = NULL;
GSList *entry = sip->openconns;
while (entry) {
ret = entry->data;
if (ret->fd == fd)
return ret;
entry = entry->next;
}
return NULL;
}
static struct sip_connection *connection_create(struct fetion_account_data
*sip, int fd)
{
struct sip_connection *ret = g_new0(struct sip_connection, 1);
ret->fd = fd;
sip->openconns = g_slist_append(sip->openconns, ret);
return ret;
}
static void connection_remove(struct fetion_account_data *sip, int fd)
{
struct sip_connection *conn = connection_find(sip, fd);
sip->openconns = g_slist_remove(sip->openconns, conn);
if (conn->inputhandler)
purple_input_remove(conn->inputhandler);
g_free(conn->inbuf);
g_free(conn);
}
static void connection_free_all(struct fetion_account_data *sip)
{
struct sip_connection *ret = NULL;
GSList *entry = sip->openconns;
while (entry) {
ret = entry->data;
connection_remove(sip, ret->fd);
entry = sip->openconns;
}
}
static GList *fetion_status_types(PurpleAccount * acc)
{
PurpleStatusType *status;
GList *types = NULL;
status = purple_status_type_new_full(PURPLE_STATUS_AVAILABLE,
"available", NULL, FALSE, TRUE,
FALSE);
types = g_list_append(types, status);
status = purple_status_type_new_full(PURPLE_STATUS_AWAY,
"away", NULL, FALSE, TRUE, FALSE);
types = g_list_append(types, status);
status = purple_status_type_new_full(PURPLE_STATUS_AWAY,
"brb", _("Be Right Back"), FALSE,
TRUE, FALSE);
types = g_list_append(types, status);
status = purple_status_type_new_full(PURPLE_STATUS_UNAVAILABLE,
"busy", _("Busy"), FALSE, TRUE,
FALSE);
types = g_list_append(types, status);
status = purple_status_type_new_full(PURPLE_STATUS_UNAVAILABLE,
"phone", _("On the Phone"), FALSE,
TRUE, FALSE);
types = g_list_append(types, status);
status = purple_status_type_new_full(PURPLE_STATUS_AWAY,
"lunch", _("Out to Lunch"), FALSE,
TRUE, FALSE);
types = g_list_append(types, status);
status = purple_status_type_new_full(PURPLE_STATUS_INVISIBLE,
NULL, NULL, FALSE, TRUE, FALSE);
types = g_list_append(types, status);
status = purple_status_type_new_full(PURPLE_STATUS_OFFLINE,
NULL, NULL, FALSE, TRUE, FALSE);
types = g_list_append(types, status);
status = purple_status_type_new_full(PURPLE_STATUS_MOBILE,
"mobile", _("Mobile on Line"),
FALSE, TRUE, FALSE);
types = g_list_append(types, status);
return types;
}
static void
fetion_canwrite_cb(gpointer data, gint source, PurpleInputCondition cond)
{
PurpleConnection *gc = data;
struct fetion_account_data *sip = gc->proto_data;
gsize max_write;
gssize written;
max_write = purple_circ_buffer_get_max_read(sip->txbuf);
if (max_write == 0) {
purple_input_remove(sip->tx_handler);
sip->tx_handler = 0;
return;
}
written = write(sip->fd, sip->txbuf->outptr, max_write);
if (written < 0 && errno == EAGAIN)
written = 0;
else if (written <= 0) {
/*TODO: do we really want to disconnect on a failure to write? */
purple_connection_error_reason(gc,
PURPLE_CONNECTION_ERROR_NETWORK_ERROR,
_("Could not write"));
return;
}
purple_circ_buffer_mark_read(sip->txbuf, written);
}
static void send_later_cb(gpointer data, gint source, const gchar * error)
{
PurpleConnection *gc = data;
struct fetion_account_data *sip;
struct sip_connection *conn;
if (!PURPLE_CONNECTION_IS_VALID(gc)) {
if (source >= 0)
close(source);
return;
}
if (source < 0) {
purple_connection_error_reason(gc,
PURPLE_CONNECTION_ERROR_NETWORK_ERROR,
_("Could not connect"));
return;
}
sip = gc->proto_data;
sip->fd = source;
sip->connecting = FALSE;
fetion_canwrite_cb(gc, sip->fd, PURPLE_INPUT_WRITE);
/* If there is more to write now, we need to register a handler */
if (sip->txbuf->bufused > 0)
sip->tx_handler = purple_input_add(sip->fd, PURPLE_INPUT_WRITE,
fetion_canwrite_cb, gc);
conn = connection_create(sip, source);
conn->inputhandler =
purple_input_add(sip->fd, PURPLE_INPUT_READ, fetion_input_cb, gc);
}
static void sendlater(PurpleConnection * gc, const char *buf)
{
struct fetion_account_data *sip = gc->proto_data;
if (!sip->connecting) {
purple_debug_info("fetion", "connecting to %s port %d\n",
sip->
realhostname ? sip->realhostname : "{NULL}",
sip->realport);
if (purple_proxy_connect
(gc, sip->account, sip->realhostname, sip->realport,
send_later_cb, gc) == NULL) {
purple_connection_error_reason(gc,
PURPLE_CONNECTION_ERROR_NETWORK_ERROR,
_
("Couldn't create socket"));
}
sip->connecting = TRUE;
}
if (purple_circ_buffer_get_max_read(sip->txbuf) > 0)
purple_circ_buffer_append(sip->txbuf, "\r\n", 2);
purple_circ_buffer_append(sip->txbuf, buf, strlen(buf));
}
static void sendout_pkt(PurpleConnection * gc, const gchar * buf)
{
struct fetion_account_data *sip = gc->proto_data;
time_t currtime = time(NULL);
gint writelen = strlen(buf);
gint ret;
purple_debug(PURPLE_DEBUG_MISC, "fetion",
"\n\nsending - %s\n######\n%s\n######\n\n",
ctime(&currtime), buf);
if (sip->fd < 0) {
sendlater(gc, buf);
return;
}
if (sip->tx_handler) {
ret = -1;
errno = EAGAIN;
} else
ret = write(sip->fd, buf, writelen);
if (ret < 0 && errno == EAGAIN)
ret = 0;
else if (ret <= 0) { /* XXX: When does this happen legitimately? */
sendlater(gc, buf);
return;
}
if (ret < writelen) {
if (!sip->tx_handler)
sip->tx_handler = purple_input_add(sip->fd,
PURPLE_INPUT_WRITE,
fetion_canwrite_cb,
gc);
/* XXX: is it OK to do this? You might get part of a request sent
with part of another. */
if (sip->txbuf->bufused > 0)
purple_circ_buffer_append(sip->txbuf, "\r\n", 2);
purple_circ_buffer_append(sip->txbuf, buf + ret,
writelen - ret);
}
}
static int fetion_send_raw(PurpleConnection * gc, const char *buf, int len)
{
sendout_pkt(gc, buf);
return len;
}
/*
static void sendout_sipmsg(struct fetion_account_data *sip, struct sipmsg *msg)
{
GSList *tmp = msg->headers;
gchar *name;
gchar *value;
GString *outstr = g_string_new("");
g_string_append_printf(outstr, "%s %s SIP/2.0\r\n", msg->method, msg->target);
while(tmp)
{
name = ((struct siphdrelement*) (tmp->data))->name;
value = ((struct siphdrelement*) (tmp->data))->value;
g_string_append_printf(outstr, "%s: %s\r\n", name, value);
tmp = g_slist_next(tmp);
}
g_string_append_printf(outstr, "\r\n%s", msg->body ? msg->body : "");
sendout_pkt(sip->gc, outstr->str);
g_string_free(outstr, TRUE);
}
*/
void
send_sip_response(PurpleConnection * gc, struct sipmsg *msg, int code,
const char *text, const char *body)
{
GSList *tmp = msg->headers;
gchar *name;
gchar *value;
GString *outstr = g_string_new("");
/* When sending the acknowlegements and errors, the content length from the original
message is still here, but there is no body; we need to make sure we're sending the
correct content length */
sipmsg_remove_header(msg, "L");
if (body) {
gchar len[12];
sprintf(len, "%d", (int)strlen(body));
sipmsg_add_header(msg, "L", len);
}
g_string_append_printf(outstr, "SIP-C/2.0 %d %s\r\n", code, text);
while (tmp) {
name = ((struct siphdrelement *)(tmp->data))->name;
value = ((struct siphdrelement *)(tmp->data))->value;
g_string_append_printf(outstr, "%s: %s\r\n", name, value);
tmp = g_slist_next(tmp);
}
g_string_append_printf(outstr, "\r\n%s", body ? body : "");
sendout_pkt(gc, outstr->str);
g_string_free(outstr, TRUE);
}
static void transaction_timeout(gpointer data)
{
struct transaction *trans;
g_return_if_fail(data != NULL);
trans = data;
if (trans->callback) {
/* call the callback to process response */
(trans->callback) (trans->sip, trans->msg, trans);
}
if (trans->timer)
purple_timeout_remove(trans->timer);
}
static void
transactions_remove(struct fetion_account_data *sip, struct transaction *trans)
{
if (trans->msg)
sipmsg_free(trans->msg);
if (trans->timer)
purple_timeout_remove(trans->timer);
sip->transactions = g_slist_remove(sip->transactions, trans);
g_free(trans);
}
static void
transactions_add_buf(struct fetion_account_data *sip, const gchar * buf,
void *callback)
{
struct transaction *trans = g_new0(struct transaction, 1);
trans->sip = sip;
trans->time = time(NULL);
trans->msg = sipmsg_parse_msg(buf);
trans->cseq = sipmsg_find_header(trans->msg, "Q");
trans->callback = callback;
if (!strcmp(trans->msg->method, "M"))
trans->timer =
purple_timeout_add(60000, (GSourceFunc) transaction_timeout,
trans);
sip->transactions = g_slist_append(sip->transactions, trans);
}
static void transactions_free_all(struct fetion_account_data *sip)
{
GSList *entry;
while ((entry = sip->transactions) != NULL) {
transactions_remove(sip, entry->data);
}
}
static struct transaction *transactions_find(struct fetion_account_data
*sip, struct sipmsg *msg)
{
struct transaction *trans;
GSList *transactions = sip->transactions;
const gchar *cseq = sipmsg_find_header(msg, "Q");
if (cseq) {
while (transactions) {
trans = transactions->data;
if (!strcmp(trans->cseq, cseq)) {
return trans;
}
transactions = transactions->next;
}
} else {
purple_debug(PURPLE_DEBUG_MISC, "fetion",
"Received message contains no CSeq header.\n");
}
return NULL;
}
void
send_sip_request(PurpleConnection * gc, const gchar * method,
const gchar * url, const gchar * to,
const gchar * addheaders, const gchar * body,
struct sip_dialog *dialog, TransCallback tc)
{
struct fetion_account_data *sip = gc->proto_data;
gchar *callid = dialog ? g_strdup(dialog->callid) : gencallid();
const gchar *addh = "";
GString *outstr = g_string_new("");
if (!strcmp(method, "R")) {
if (sip->regcallid) {
g_free(callid);
callid = g_strdup(sip->regcallid);
} else
sip->regcallid = g_strdup(callid);
}
if (addheaders)
addh = addheaders;
g_string_append_printf(outstr, "%s fetion.com.cn SIP-C/2.0\r\n"
"F: %s\r\n"
"I: %s\r\n"
"Q: %d %s\r\n"
"%s%s",
method,
sip->username,
callid, ++sip->cseq, method, to, addh);
if (body)
g_string_append_printf(outstr, "L: %d\r\n\r\n%s",
(int)strlen(body), body);
else
g_string_append_printf(outstr, "\r\n\r\n");
g_free(callid);
/* add to ongoing transactions */
transactions_add_buf(sip, outstr->str, tc);
sendout_pkt(gc, outstr->str);
g_string_free(outstr, TRUE);
}
gboolean
process_subscribe_response(struct fetion_account_data *sip,
struct sipmsg *msg, struct transaction *tc)
{
purple_debug_info("fetion", "process subscribe response[%s]\n",
msg->body);
return TRUE;
}
static void
fetion_unsubscribe(char *name, struct fetion_buddy *buddy,
struct fetion_account_data *sip)
{
if (buddy->dialog) {
purple_debug_info("fetion", "Unsubscribing from %s\n", name);
fetion_subscribe_exp(sip, buddy);
}
}
static int
fetion_im_send(PurpleConnection * gc, const char *who, const char *what,
PurpleMessageFlags flags)
{
struct fetion_account_data *sip = gc->proto_data;
char *to = g_strdup(who);
char *text = purple_unescape_html(what);
fetion_send_message(sip, to, text, NULL, FALSE);
g_free(to);
g_free(text);
return 1;
}
static void conversation_created_cb(PurpleConversation *g_conv,
struct fetion_account_data * sip)
{
//PurpleAccount *account = purple_conversation_get_account(conv);
//PurpleConnection *gc = purple_account_get_connection(account);
PurplePresence *presence;
PurpleBuddy *b;
struct fetion_buddy *buddy = NULL;
const gchar *to;
to = purple_conversation_get_name(g_conv);
buddy = g_hash_table_lookup(sip->buddies, to);
if (buddy == NULL) {
buddy = g_new0(struct fetion_buddy, 1);
buddy->name = g_strdup(to);
g_hash_table_insert(sip->buddies, buddy->name, buddy);
}
if (buddy->dialog == NULL) {
buddy->dialog = g_new0(struct sip_dialog, 1);
buddy->dialog->callid = g_strdup_printf("%d", -1);
}
if (strcmp(sip->uri, to) != 0) {
b = purple_find_buddy(sip->account, to);
presence = purple_buddy_get_presence(b);
if (!purple_presence_is_status_primitive_active
(presence, PURPLE_STATUS_MOBILE)) {
//if (strncmp(buddy->dialog->callid, "-1", 2) ==
// 0) {
g_free(buddy->dialog->callid);
buddy->dialog->callid = gencallid();
SendInvite(sip, to);
//}
}
}
}
static void conversation_deleting_cb(PurpleConversation *g_conv,
struct fetion_account_data * sip)
{
PurplePresence *presence;
PurpleBuddy *b;
struct fetion_buddy *buddy = NULL;
gchar *fullto;
const gchar *to;
to = purple_conversation_get_name(g_conv);
buddy = g_hash_table_lookup(sip->buddies, to);
if (buddy == NULL)
return;
if (buddy->dialog == NULL)
return;
if (strncmp("sip:", to, 4) == 0)
fullto = g_strdup_printf("T: %s\r\n", to);
else
return;
if (strcmp(sip->uri, to) != 0) {
b = purple_find_buddy(sip->account, to);
presence = purple_buddy_get_presence(b);
if (!purple_presence_is_status_primitive_active
(presence, PURPLE_STATUS_MOBILE)) {
send_sip_request(sip->gc, "B", "", fullto, NULL, NULL, buddy->dialog,
NULL);
}
}
free(fullto);
}
gboolean
process_register_response(struct fetion_account_data * sip,
struct sipmsg * msg, struct transaction * tc)
{
const gchar *tmp;
const gchar *szExpire;
purple_debug(PURPLE_DEBUG_MISC, "fetion",
"in process register response response: %d\n",
msg->response);
switch (msg->response) {
case 200:
if (sip->registerstatus < FETION_REGISTER_COMPLETE) {
/* get buddies from blist */
GetPersonalInfo(sip);
if (sip->GetContactTimeOut)
purple_timeout_remove(sip->GetContactTimeOut);
sip->GetContactTimeOut =
purple_timeout_add(5000,
(GSourceFunc) GetContactList,
sip);
GetContactList(sip);
purple_signal_disconnect(purple_conversations_get_handle(),
"conversation-created", sip,
PURPLE_CALLBACK(conversation_created_cb));
purple_signal_disconnect(purple_conversations_get_handle(),
"deleting-conversation", sip,
PURPLE_CALLBACK(conversation_deleting_cb));
/* start watching for new conversations */
purple_signal_connect(purple_conversations_get_handle(),
"conversation-created", sip,
PURPLE_CALLBACK(conversation_created_cb), sip);
/* start watching for deleting conversations */
purple_signal_connect(purple_conversations_get_handle(),
"deleting-conversation", sip,
PURPLE_CALLBACK(conversation_deleting_cb), sip);
}
sip->registerstatus = FETION_REGISTER_COMPLETE;
szExpire = sipmsg_find_header(msg, "X");
if (szExpire != NULL)
sip->registerexpire = atoi(szExpire);
purple_debug_info("Register:", "[%s]", szExpire);
purple_connection_set_state(sip->gc, PURPLE_CONNECTED);
break;
case 401:
if (sip->registerstatus != FETION_REGISTER_RETRY) {
purple_debug_info("fetion", "REGISTER retries %d\n",
sip->registrar.retries);
if (sip->registrar.retries > FETION_REGISTER_RETRY_MAX) {
if (!purple_account_get_remember_password
(sip->gc->account))
purple_account_set_password(sip->
gc->account,
NULL);
purple_connection_error_reason(sip->gc,
PURPLE_CONNECTION_ERROR_AUTHENTICATION_FAILED,
_
("Incorrect password."));
return TRUE;
}
tmp = sipmsg_find_header(msg, "W");
purple_debug_info("befor fill_auth:", "%s\n", tmp);
fill_auth(sip, tmp, &sip->registrar);
sip->registerstatus = FETION_REGISTER_RETRY;
do_register(sip);
}
break;
default:
if (sip->registerstatus != FETION_REGISTER_RETRY) {
purple_debug_info("fetion",
"Unrecognized return code for REGISTER.%d\n",
msg->response);
if (sip->registrar.retries > FETION_REGISTER_RETRY_MAX) {
purple_connection_error_reason(sip->gc,
PURPLE_CONNECTION_ERROR_OTHER_ERROR,
_
("Unknown server response."));
return TRUE;
}
//sip->registerstatus = FETION_REGISTER_RETRY;
//do_register(sip);
}
break;
}
return TRUE;
}
static gboolean dialog_match(struct sip_dialog *dialog, struct sipmsg *msg)
{
const gchar *fromhdr;
const gchar *tohdr;
const gchar *callid;
gchar *ourtag, *theirtag;
gboolean match = FALSE;
fromhdr = sipmsg_find_header(msg, "F");
tohdr = sipmsg_find_header(msg, "T");
callid = sipmsg_find_header(msg, "I");
if (!fromhdr || !tohdr || !callid)
return FALSE;
ourtag = find_tag(tohdr);
theirtag = find_tag(fromhdr);
if (ourtag && theirtag &&
!strcmp(dialog->callid, callid) &&
!strcmp(dialog->ourtag, ourtag) &&
!strcmp(dialog->theirtag, theirtag))
match = TRUE;
g_free(ourtag);
g_free(theirtag);
return match;
}
static void
process_incoming_notify(struct fetion_account_data *sip, struct sipmsg *msg)
{
gchar *from;
const gchar *fromhdr;
gchar *basicstatus_data;
xmlnode *pidf;
xmlnode *basicstatus = NULL, *tuple, *status;
gboolean isonline = FALSE;
struct fetion_buddy *b = NULL;
const gchar *sshdr = NULL;
fromhdr = sipmsg_find_header(msg, "F");
from = parse_from(fromhdr);
if (!from)
return;
b = g_hash_table_lookup(sip->buddies, from);
if (!b) {
g_free(from);
purple_debug_info("fetion", "Could not find the buddy.\n");
return;
}
if (b->dialog && !dialog_match(b->dialog, msg)) {
/* We only accept notifies from people that
* we already have a dialog with.
*/
purple_debug_info("fetion",
"No corresponding dialog for notify--discard\n");
g_free(from);
return;
}
pidf = xmlnode_from_str(msg->body, msg->bodylen);
if (!pidf) {
purple_debug_info("fetion",
"process_incoming_notify: no parseable pidf\n");
sshdr = sipmsg_find_header(msg, "Subscription-State");
if (sshdr) {
int i = 0;
gchar **ssparts = g_strsplit(sshdr, ":", 0);
while (ssparts[i]) {
g_strchug(ssparts[i]);
if (g_str_has_prefix(ssparts[i], "terminated")) {
purple_debug_info("fetion",
"Subscription expired!");
g_free(b->dialog->ourtag);
g_free(b->dialog->theirtag);
g_free(b->dialog->callid);
g_free(b->dialog);
b->dialog = NULL;
purple_prpl_got_user_status
(sip->account, from, "offline",
NULL);
break;
}
i++;
}
g_strfreev(ssparts);
}
send_sip_response(sip->gc, msg, 200, "OK", NULL);
g_free(from);
return;
}
if ((tuple = xmlnode_get_child(pidf, "tuple")))
if ((status = xmlnode_get_child(tuple, "status")))
basicstatus = xmlnode_get_child(status, "basic");
if (!basicstatus) {
purple_debug_info("fetion",
"process_incoming_notify: no basic found\n");
xmlnode_free(pidf);
g_free(from);
return;
}
basicstatus_data = xmlnode_get_data(basicstatus);
if (!basicstatus_data) {
purple_debug_info("fetion",
"process_incoming_notify: no basic data found\n");
xmlnode_free(pidf);
g_free(from);
return;
}
if (strstr(basicstatus_data, "open"))
isonline = TRUE;
if (isonline)
purple_prpl_got_user_status(sip->account, from, "available",
NULL);
else
purple_prpl_got_user_status(sip->account, from, "offline",
NULL);
xmlnode_free(pidf);
g_free(from);
g_free(basicstatus_data);
send_sip_response(sip->gc, msg, 200, "OK", NULL);
}
gchar *find_tag(const gchar * hdr)
{
const gchar *tmp = strstr(hdr, ";tag="), *tmp2;
if (!tmp)
return NULL;
tmp += 5;
if ((tmp2 = strchr(tmp, ';'))) {
return g_strndup(tmp, tmp2 - tmp);
}
return g_strdup(tmp);
}
static void
process_input_message(struct fetion_account_data *sip, struct sipmsg *msg)
{
gboolean found = FALSE;
if (msg->response == 0) { /* request */
if (!strcmp(msg->method, "M")) {
process_incoming_message(sip, msg);
found = TRUE;
} else if (!strcmp(msg->method, "BN")) {
process_incoming_BN(sip, msg);
found = TRUE;
} else if (!strcmp(msg->method, "I")) {
process_incoming_invite(sip, msg);
found = TRUE;
} else if (!strcmp(msg->method, "A")) {
} else if (!strcmp(msg->method, "IN")) {
const gchar *from;
from = sipmsg_find_header(msg, "F");
serv_got_attention(sip->gc, from, 0);
found = TRUE;
} else if (!strcmp(msg->method, "N")) {
process_incoming_notify(sip, msg);
found = TRUE;
} else if (!strcmp(msg->method, "B")) {
send_sip_response(sip->gc, msg, 200, "OK", NULL);
found = TRUE;
} else if (!strcmp(msg->method, "O")) {
send_sip_response(sip->gc, msg, 200, "OK", NULL);
found = TRUE;
} else {
purple_debug_info("fetion:", "not implemented:\n%s\n",
msg->body);
}
} else { /* response */
struct transaction *trans = transactions_find(sip, msg);
if (trans) {
if (msg->response == 407) {
gchar *resend, *auth;
const gchar *ptmp;
if (sip->proxy.retries > 3)
return;
sip->proxy.retries++;
/* do proxy authentication */
ptmp =
sipmsg_find_header(msg,
"Proxy-Authenticate");
fill_auth(sip, ptmp, &sip->proxy);
auth =
auth_header(sip, &sip->proxy,
trans->msg->method,
trans->msg->target);
sipmsg_remove_header(trans->msg,
"Proxy-Authorization");
sipmsg_add_header(trans->msg,
"Proxy-Authorization", auth);
g_free(auth);
resend = sipmsg_to_string(trans->msg);
/* resend request */
sendout_pkt(sip->gc, resend);
g_free(resend);
} else if (msg->response == 522) {
if (!strcmp(trans->msg->method, "S")) {
purple_debug_info("fetion:",
"AddBuddy:522\n");
if (trans->callback)
(trans->callback) (sip, msg,
trans);
}
found = TRUE;
} else if (msg->response == 406 || msg->response == 480
|| msg->response == 400) {
//406 Not Acceptable
purple_connection_error_reason(sip->gc,
PURPLE_CONNECTION_ERROR_NETWORK_ERROR,
_
("Error Connect.Try reconnect."));
} else if ((!strcmp(trans->msg->method, "M"))
&& (msg->response == 200
|| msg->response == 280)) {
transactions_remove(sip, trans);
found = TRUE;
} else {
if (msg->response == 100) {
/* ignore provisional response */
purple_debug_info("fetion",
"got trying response\n");
} else {
sip->proxy.retries = 0;
if (strcmp(trans->msg->method, "R") ==
0) {
if (msg->response != 200) {
/* denied for some other reason! */
sip->
registrar.retries++;
} else {
const gchar *callid;
callid =
sipmsg_find_header
(msg, "I");
sip->registrar.retries =
0;
sip->regcallid =
g_strdup(callid);
}
} else {
if (msg->response == 401) {
/* This is encountered when a generic (MESSAGE, NOTIFY, etc)
* was denied until further authorization is provided.
*/
} else {
/* Reset any count of retries that may have