-
Notifications
You must be signed in to change notification settings - Fork 3
/
conversation.c
2676 lines (2130 loc) · 71 KB
/
conversation.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
/*
* purple
*
* Purple is the legal property of its developers, whose names are too numerous
* to list here. Please refer to the COPYRIGHT file distributed with this
* source distribution.
*
* 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 "blist.h"
#include "cmds.h"
#include "conversation.h"
#include "dbus-maybe.h"
#include "debug.h"
#include "imgstore.h"
#include "notify.h"
#include "prefs.h"
#include "prpl.h"
#include "request.h"
#include "signals.h"
#include "util.h"
#define SEND_TYPED_TIMEOUT_SECONDS 5
static GList *conversations = NULL;
static GList *ims = NULL;
static GList *chats = NULL;
static PurpleConversationUiOps *default_ops = NULL;
/**
* A hash table used for efficient lookups of conversations by name.
* struct _purple_hconv => PurpleConversation*
*/
static GHashTable *conversation_cache = NULL;
struct _purple_hconv {
PurpleConversationType type;
char *name;
const PurpleAccount *account;
};
static guint _purple_conversations_hconv_hash(struct _purple_hconv *hc)
{
return g_str_hash(hc->name) ^ hc->type ^ g_direct_hash(hc->account);
}
static guint _purple_conversations_hconv_equal(struct _purple_hconv *hc1, struct _purple_hconv *hc2)
{
return (hc1->type == hc2->type &&
hc1->account == hc2->account &&
g_str_equal(hc1->name, hc2->name));
}
static void _purple_conversations_hconv_free_key(struct _purple_hconv *hc)
{
g_free(hc->name);
g_free(hc);
}
static guint _purple_conversation_user_hash(gconstpointer data)
{
const gchar *name = data;
gchar *collated;
guint hash;
collated = g_utf8_collate_key(name, -1);
hash = g_str_hash(collated);
g_free(collated);
return hash;
}
static gboolean _purple_conversation_user_equal(gconstpointer a, gconstpointer b)
{
return !g_utf8_collate(a, b);
}
void
purple_conversations_set_ui_ops(PurpleConversationUiOps *ops)
{
default_ops = ops;
}
static gboolean
reset_typing_cb(gpointer data)
{
PurpleConversation *c = (PurpleConversation *)data;
PurpleConvIm *im;
im = PURPLE_CONV_IM(c);
purple_conv_im_set_typing_state(im, PURPLE_NOT_TYPING);
purple_conv_im_stop_typing_timeout(im);
return FALSE;
}
static gboolean
send_typed_cb(gpointer data)
{
PurpleConversation *conv = (PurpleConversation *)data;
PurpleConnection *gc;
const char *name;
g_return_val_if_fail(conv != NULL, FALSE);
gc = purple_conversation_get_gc(conv);
name = purple_conversation_get_name(conv);
if (gc != NULL && name != NULL) {
/* We set this to 1 so that PURPLE_TYPING will be sent
* if the Purple user types anything else.
*/
purple_conv_im_set_type_again(PURPLE_CONV_IM(conv), 1);
serv_send_typing(gc, name, PURPLE_TYPED);
purple_debug(PURPLE_DEBUG_MISC, "conversation", "typed...\n");
}
return FALSE;
}
static void
common_send(PurpleConversation *conv, const char *message, PurpleMessageFlags msgflags)
{
PurpleConversationType type;
PurpleAccount *account;
PurpleConnection *gc;
char *displayed = NULL, *sent = NULL;
int err = 0;
if (*message == '\0')
return;
account = purple_conversation_get_account(conv);
gc = purple_conversation_get_gc(conv);
g_return_if_fail(account != NULL);
g_return_if_fail(gc != NULL);
type = purple_conversation_get_type(conv);
/* Always linkfy the text for display, unless we're
* explicitly asked to do otheriwse*/
if (!(msgflags & PURPLE_MESSAGE_INVISIBLE)) {
if(msgflags & PURPLE_MESSAGE_NO_LINKIFY)
displayed = g_strdup(message);
else
displayed = purple_markup_linkify(message);
}
if (displayed && (conv->features & PURPLE_CONNECTION_HTML) &&
!(msgflags & PURPLE_MESSAGE_RAW)) {
sent = g_strdup(displayed);
} else
sent = g_strdup(message);
msgflags |= PURPLE_MESSAGE_SEND;
if (type == PURPLE_CONV_TYPE_IM) {
PurpleConvIm *im = PURPLE_CONV_IM(conv);
purple_signal_emit(purple_conversations_get_handle(), "sending-im-msg",
account,
purple_conversation_get_name(conv), &sent);
if (sent != NULL && sent[0] != '\0') {
err = serv_send_im(gc, purple_conversation_get_name(conv),
sent, msgflags);
if ((err > 0) && (displayed != NULL))
purple_conv_im_write(im, NULL, displayed, msgflags, time(NULL));
purple_signal_emit(purple_conversations_get_handle(), "sent-im-msg",
account,
purple_conversation_get_name(conv), sent);
}
}
else {
purple_signal_emit(purple_conversations_get_handle(), "sending-chat-msg",
account, &sent,
purple_conv_chat_get_id(PURPLE_CONV_CHAT(conv)));
if (sent != NULL && sent[0] != '\0') {
err = serv_chat_send(gc, purple_conv_chat_get_id(PURPLE_CONV_CHAT(conv)), sent, msgflags);
purple_signal_emit(purple_conversations_get_handle(), "sent-chat-msg",
account, sent,
purple_conv_chat_get_id(PURPLE_CONV_CHAT(conv)));
}
}
if (err < 0) {
const char *who;
const char *msg;
who = purple_conversation_get_name(conv);
if (err == -E2BIG) {
msg = _("Unable to send message: The message is too large.");
if (!purple_conv_present_error(who, account, msg)) {
char *msg2 = g_strdup_printf(_("Unable to send message to %s."), who);
purple_notify_error(gc, NULL, msg2, _("The message is too large."));
g_free(msg2);
}
}
else if (err == -ENOTCONN) {
purple_debug(PURPLE_DEBUG_ERROR, "conversation",
"Not yet connected.\n");
}
else {
msg = _("Unable to send message.");
if (!purple_conv_present_error(who, account, msg)) {
char *msg2 = g_strdup_printf(_("Unable to send message to %s."), who);
purple_notify_error(gc, NULL, msg2, NULL);
g_free(msg2);
}
}
}
g_free(displayed);
g_free(sent);
}
static void
open_log(PurpleConversation *conv)
{
conv->logs = g_list_append(NULL, purple_log_new(conv->type == PURPLE_CONV_TYPE_CHAT ? PURPLE_LOG_CHAT :
PURPLE_LOG_IM, conv->name, conv->account,
conv, time(NULL), NULL));
}
/* Functions that deal with PurpleConvMessage */
static void
add_message_to_history(PurpleConversation *conv, const char *who, const char *alias,
const char *message, PurpleMessageFlags flags, time_t when)
{
PurpleConvMessage *msg;
PurpleConnection *gc;
gc = purple_account_get_connection(conv->account);
if (flags & PURPLE_MESSAGE_SEND) {
const char *me = NULL;
if (gc)
me = purple_connection_get_display_name(gc);
if (!me)
me = conv->account->username;
who = me;
}
msg = g_new0(PurpleConvMessage, 1);
PURPLE_DBUS_REGISTER_POINTER(msg, PurpleConvMessage);
msg->who = g_strdup(who);
msg->alias = g_strdup(alias);
msg->flags = flags;
msg->what = g_strdup(message);
msg->when = when;
msg->conv = conv;
conv->message_history = g_list_prepend(conv->message_history, msg);
}
static void
free_conv_message(PurpleConvMessage *msg)
{
g_free(msg->who);
g_free(msg->alias);
g_free(msg->what);
PURPLE_DBUS_UNREGISTER_POINTER(msg);
g_free(msg);
}
static void
message_history_free(GList *list)
{
g_list_foreach(list, (GFunc)free_conv_message, NULL);
g_list_free(list);
}
/**************************************************************************
* Conversation API
**************************************************************************/
static void
purple_conversation_chat_cleanup_for_rejoin(PurpleConversation *conv)
{
const char *disp;
PurpleAccount *account;
PurpleConnection *gc;
account = purple_conversation_get_account(conv);
purple_conversation_close_logs(conv);
open_log(conv);
gc = purple_account_get_connection(account);
if ((disp = purple_connection_get_display_name(gc)) != NULL)
purple_conv_chat_set_nick(PURPLE_CONV_CHAT(conv), disp);
else
{
purple_conv_chat_set_nick(PURPLE_CONV_CHAT(conv),
purple_account_get_username(account));
}
purple_conv_chat_clear_users(PURPLE_CONV_CHAT(conv));
purple_conv_chat_set_topic(PURPLE_CONV_CHAT(conv), NULL, NULL);
PURPLE_CONV_CHAT(conv)->left = FALSE;
purple_conversation_update(conv, PURPLE_CONV_UPDATE_CHATLEFT);
}
PurpleConversation *
purple_conversation_new(PurpleConversationType type, PurpleAccount *account,
const char *name)
{
PurpleConversation *conv;
PurpleConnection *gc;
PurpleConversationUiOps *ops;
struct _purple_hconv *hc;
g_return_val_if_fail(type != PURPLE_CONV_TYPE_UNKNOWN, NULL);
g_return_val_if_fail(account != NULL, NULL);
g_return_val_if_fail(name != NULL, NULL);
/* Check if this conversation already exists. */
if ((conv = purple_find_conversation_with_account(type, name, account)) != NULL)
{
if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_CHAT &&
!purple_conv_chat_has_left(PURPLE_CONV_CHAT(conv))) {
purple_debug_warning("conversation", "Trying to create multiple "
"chats (%s) with the same name is deprecated and will be "
"removed in libpurple 3.0.0", name);
}
/*
* This hack is necessary because some prpls (MSN) have unnamed chats
* that all use the same name. A PurpleConversation for one of those
* is only ever re-used if the user has left, so calls to
* purple_conversation_new need to fall-through to creating a new
* chat.
* TODO 3.0.0: Remove this workaround and mandate unique names.
*/
if (purple_conversation_get_type(conv) != PURPLE_CONV_TYPE_CHAT ||
purple_conv_chat_has_left(PURPLE_CONV_CHAT(conv)))
{
if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_CHAT)
purple_conversation_chat_cleanup_for_rejoin(conv);
return conv;
}
}
gc = purple_account_get_connection(account);
g_return_val_if_fail(gc != NULL, NULL);
conv = g_new0(PurpleConversation, 1);
PURPLE_DBUS_REGISTER_POINTER(conv, PurpleConversation);
conv->type = type;
conv->account = account;
conv->name = g_strdup(name);
conv->title = g_strdup(name);
conv->data = g_hash_table_new_full(g_str_hash, g_str_equal,
g_free, NULL);
/* copy features from the connection. */
conv->features = gc->flags;
if (type == PURPLE_CONV_TYPE_IM)
{
PurpleBuddyIcon *icon;
conv->u.im = g_new0(PurpleConvIm, 1);
conv->u.im->conv = conv;
PURPLE_DBUS_REGISTER_POINTER(conv->u.im, PurpleConvIm);
ims = g_list_prepend(ims, conv);
if ((icon = purple_buddy_icons_find(account, name)))
{
purple_conv_im_set_icon(conv->u.im, icon);
/* purple_conv_im_set_icon refs the icon. */
purple_buddy_icon_unref(icon);
}
if (purple_prefs_get_bool("/purple/logging/log_ims"))
{
purple_conversation_set_logging(conv, TRUE);
open_log(conv);
}
}
else if (type == PURPLE_CONV_TYPE_CHAT)
{
const char *disp;
conv->u.chat = g_new0(PurpleConvChat, 1);
conv->u.chat->conv = conv;
conv->u.chat->users = g_hash_table_new_full(_purple_conversation_user_hash,
_purple_conversation_user_equal, g_free, NULL);
PURPLE_DBUS_REGISTER_POINTER(conv->u.chat, PurpleConvChat);
chats = g_list_prepend(chats, conv);
if ((disp = purple_connection_get_display_name(account->gc)))
purple_conv_chat_set_nick(conv->u.chat, disp);
else
purple_conv_chat_set_nick(conv->u.chat,
purple_account_get_username(account));
if (purple_prefs_get_bool("/purple/logging/log_chats"))
{
purple_conversation_set_logging(conv, TRUE);
open_log(conv);
}
}
conversations = g_list_prepend(conversations, conv);
hc = g_new(struct _purple_hconv, 1);
hc->name = g_strdup(purple_normalize(account, conv->name));
hc->account = account;
hc->type = type;
g_hash_table_insert(conversation_cache, hc, conv);
/* Auto-set the title. */
purple_conversation_autoset_title(conv);
/* Don't move this.. it needs to be one of the last things done otherwise
* it causes mysterious crashes on my system.
* -- Gary
*/
ops = conv->ui_ops = default_ops;
if (ops != NULL && ops->create_conversation != NULL)
ops->create_conversation(conv);
purple_signal_emit(purple_conversations_get_handle(),
"conversation-created", conv);
return conv;
}
void
purple_conversation_destroy(PurpleConversation *conv)
{
PurplePluginProtocolInfo *prpl_info = NULL;
PurpleConversationUiOps *ops;
PurpleConnection *gc;
const char *name;
struct _purple_hconv hc;
g_return_if_fail(conv != NULL);
purple_request_close_with_handle(conv);
ops = purple_conversation_get_ui_ops(conv);
gc = purple_conversation_get_gc(conv);
name = purple_conversation_get_name(conv);
if (gc != NULL)
{
/* Still connected */
prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(purple_connection_get_prpl(gc));
if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_IM)
{
if (purple_prefs_get_bool("/purple/conversations/im/send_typing"))
serv_send_typing(gc, name, PURPLE_NOT_TYPING);
if (gc && prpl_info->convo_closed != NULL)
prpl_info->convo_closed(gc, name);
}
else if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_CHAT)
{
int chat_id = purple_conv_chat_get_id(PURPLE_CONV_CHAT(conv));
#if 0
/*
* This is unfortunately necessary, because calling
* serv_chat_leave() calls this purple_conversation_destroy(),
* which leads to two calls here.. We can't just return after
* this, because then it'll return on the next pass. So, since
* serv_got_chat_left(), which is eventually called from the
* prpl that serv_chat_leave() calls, removes this conversation
* from the gc's buddy_chats list, we're going to check to see
* if this exists in the list. If so, we want to return after
* calling this, because it'll be called again. If not, fall
* through, because it'll have already been removed, and we'd
* be on the 2nd pass.
*
* Long paragraph. <-- Short sentence.
*
* -- ChipX86
*/
if (gc && g_slist_find(gc->buddy_chats, conv) != NULL) {
serv_chat_leave(gc, chat_id);
return;
}
#endif
/*
* Instead of all of that, lets just close the window when
* the user tells us to, and let the prpl deal with the
* internals on it's own time. Don't do this if the prpl already
* knows it left the chat.
*/
if (!purple_conv_chat_has_left(PURPLE_CONV_CHAT(conv)))
serv_chat_leave(gc, chat_id);
/*
* If they didn't call serv_got_chat_left by now, it's too late.
* So we better do it for them before we destroy the thing.
*/
if (!purple_conv_chat_has_left(PURPLE_CONV_CHAT(conv)))
serv_got_chat_left(gc, chat_id);
}
}
/* remove from conversations and im/chats lists prior to emit */
conversations = g_list_remove(conversations, conv);
if(conv->type==PURPLE_CONV_TYPE_IM)
ims = g_list_remove(ims, conv);
else if(conv->type==PURPLE_CONV_TYPE_CHAT)
chats = g_list_remove(chats, conv);
hc.name = (gchar *)purple_normalize(conv->account, conv->name);
hc.account = conv->account;
hc.type = conv->type;
g_hash_table_remove(conversation_cache, &hc);
purple_signal_emit(purple_conversations_get_handle(),
"deleting-conversation", conv);
g_free(conv->name);
g_free(conv->title);
conv->name = NULL;
conv->title = NULL;
if (conv->type == PURPLE_CONV_TYPE_IM) {
purple_conv_im_stop_typing_timeout(conv->u.im);
purple_conv_im_stop_send_typed_timeout(conv->u.im);
purple_buddy_icon_unref(conv->u.im->icon);
conv->u.im->icon = NULL;
PURPLE_DBUS_UNREGISTER_POINTER(conv->u.im);
g_free(conv->u.im);
conv->u.im = NULL;
}
else if (conv->type == PURPLE_CONV_TYPE_CHAT) {
g_hash_table_destroy(conv->u.chat->users);
conv->u.chat->users = NULL;
g_list_foreach(conv->u.chat->in_room, (GFunc)purple_conv_chat_cb_destroy, NULL);
g_list_free(conv->u.chat->in_room);
g_list_foreach(conv->u.chat->ignored, (GFunc)g_free, NULL);
g_list_free(conv->u.chat->ignored);
conv->u.chat->in_room = NULL;
conv->u.chat->ignored = NULL;
g_free(conv->u.chat->who);
conv->u.chat->who = NULL;
g_free(conv->u.chat->topic);
conv->u.chat->topic = NULL;
g_free(conv->u.chat->nick);
PURPLE_DBUS_UNREGISTER_POINTER(conv->u.chat);
g_free(conv->u.chat);
conv->u.chat = NULL;
}
g_hash_table_destroy(conv->data);
conv->data = NULL;
if (ops != NULL && ops->destroy_conversation != NULL)
ops->destroy_conversation(conv);
conv->ui_data = NULL;
purple_conversation_close_logs(conv);
purple_conversation_clear_message_history(conv);
PURPLE_DBUS_UNREGISTER_POINTER(conv);
g_free(conv);
conv = NULL;
}
void
purple_conversation_present(PurpleConversation *conv) {
PurpleConversationUiOps *ops;
g_return_if_fail(conv != NULL);
ops = purple_conversation_get_ui_ops(conv);
if(ops && ops->present)
ops->present(conv);
}
void
purple_conversation_set_features(PurpleConversation *conv, PurpleConnectionFlags features)
{
g_return_if_fail(conv != NULL);
conv->features = features;
purple_conversation_update(conv, PURPLE_CONV_UPDATE_FEATURES);
}
PurpleConnectionFlags
purple_conversation_get_features(PurpleConversation *conv)
{
g_return_val_if_fail(conv != NULL, 0);
return conv->features;
}
PurpleConversationType
purple_conversation_get_type(const PurpleConversation *conv)
{
g_return_val_if_fail(conv != NULL, PURPLE_CONV_TYPE_UNKNOWN);
return conv->type;
}
void
purple_conversation_set_ui_ops(PurpleConversation *conv,
PurpleConversationUiOps *ops)
{
g_return_if_fail(conv != NULL);
if (conv->ui_ops == ops)
return;
if (conv->ui_ops != NULL && conv->ui_ops->destroy_conversation != NULL)
conv->ui_ops->destroy_conversation(conv);
conv->ui_data = NULL;
conv->ui_ops = ops;
}
PurpleConversationUiOps *
purple_conversation_get_ui_ops(const PurpleConversation *conv)
{
g_return_val_if_fail(conv != NULL, NULL);
return conv->ui_ops;
}
void
purple_conversation_set_account(PurpleConversation *conv, PurpleAccount *account)
{
g_return_if_fail(conv != NULL);
if (account == purple_conversation_get_account(conv))
return;
conv->account = account;
purple_conversation_update(conv, PURPLE_CONV_UPDATE_ACCOUNT);
}
PurpleAccount *
purple_conversation_get_account(const PurpleConversation *conv)
{
g_return_val_if_fail(conv != NULL, NULL);
return conv->account;
}
PurpleConnection *
purple_conversation_get_gc(const PurpleConversation *conv)
{
PurpleAccount *account;
g_return_val_if_fail(conv != NULL, NULL);
account = purple_conversation_get_account(conv);
if (account == NULL)
return NULL;
return account->gc;
}
void
purple_conversation_set_title(PurpleConversation *conv, const char *title)
{
g_return_if_fail(conv != NULL);
g_return_if_fail(title != NULL);
g_free(conv->title);
conv->title = g_strdup(title);
purple_conversation_update(conv, PURPLE_CONV_UPDATE_TITLE);
}
const char *
purple_conversation_get_title(const PurpleConversation *conv)
{
g_return_val_if_fail(conv != NULL, NULL);
return conv->title;
}
void
purple_conversation_autoset_title(PurpleConversation *conv)
{
PurpleAccount *account;
PurpleBuddy *b;
PurpleChat *chat;
const char *text = NULL, *name;
g_return_if_fail(conv != NULL);
account = purple_conversation_get_account(conv);
name = purple_conversation_get_name(conv);
if(purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_IM) {
if(account && ((b = purple_find_buddy(account, name)) != NULL))
text = purple_buddy_get_contact_alias(b);
} else if(purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_CHAT) {
if(account && ((chat = purple_blist_find_chat(account, name)) != NULL))
text = purple_chat_get_name(chat);
}
if(text == NULL)
text = name;
purple_conversation_set_title(conv, text);
}
void
purple_conversation_foreach(void (*func)(PurpleConversation *conv))
{
PurpleConversation *conv;
GList *l;
g_return_if_fail(func != NULL);
for (l = purple_get_conversations(); l != NULL; l = l->next) {
conv = (PurpleConversation *)l->data;
func(conv);
}
}
void
purple_conversation_set_name(PurpleConversation *conv, const char *name)
{
struct _purple_hconv *hc;
g_return_if_fail(conv != NULL);
hc = g_new(struct _purple_hconv, 1);
hc->type = conv->type;
hc->account = conv->account;
hc->name = (gchar *)purple_normalize(conv->account, conv->name);
g_hash_table_remove(conversation_cache, hc);
g_free(conv->name);
conv->name = g_strdup(name);
hc->name = g_strdup(purple_normalize(conv->account, conv->name));
g_hash_table_insert(conversation_cache, hc, conv);
purple_conversation_autoset_title(conv);
}
const char *
purple_conversation_get_name(const PurpleConversation *conv)
{
g_return_val_if_fail(conv != NULL, NULL);
return conv->name;
}
void
purple_conversation_set_logging(PurpleConversation *conv, gboolean log)
{
g_return_if_fail(conv != NULL);
if (conv->logging != log)
{
conv->logging = log;
purple_conversation_update(conv, PURPLE_CONV_UPDATE_LOGGING);
}
}
gboolean
purple_conversation_is_logging(const PurpleConversation *conv)
{
g_return_val_if_fail(conv != NULL, FALSE);
return conv->logging;
}
void
purple_conversation_close_logs(PurpleConversation *conv)
{
g_return_if_fail(conv != NULL);
g_list_foreach(conv->logs, (GFunc)purple_log_free, NULL);
g_list_free(conv->logs);
conv->logs = NULL;
}
PurpleConvIm *
purple_conversation_get_im_data(const PurpleConversation *conv)
{
g_return_val_if_fail(conv != NULL, NULL);
if (purple_conversation_get_type(conv) != PURPLE_CONV_TYPE_IM)
return NULL;
return conv->u.im;
}
PurpleConvChat *
purple_conversation_get_chat_data(const PurpleConversation *conv)
{
g_return_val_if_fail(conv != NULL, NULL);
if (purple_conversation_get_type(conv) != PURPLE_CONV_TYPE_CHAT)
return NULL;
return conv->u.chat;
}
void
purple_conversation_set_data(PurpleConversation *conv, const char *key,
gpointer data)
{
g_return_if_fail(conv != NULL);
g_return_if_fail(key != NULL);
g_hash_table_replace(conv->data, g_strdup(key), data);
}
gpointer
purple_conversation_get_data(PurpleConversation *conv, const char *key)
{
g_return_val_if_fail(conv != NULL, NULL);
g_return_val_if_fail(key != NULL, NULL);
return g_hash_table_lookup(conv->data, key);
}
GList *
purple_get_conversations(void)
{
return conversations;
}
GList *
purple_get_ims(void)
{
return ims;
}
GList *
purple_get_chats(void)
{
return chats;
}
PurpleConversation *
purple_find_conversation_with_account(PurpleConversationType type,
const char *name,
const PurpleAccount *account)
{
PurpleConversation *c = NULL;
struct _purple_hconv hc;
g_return_val_if_fail(name != NULL, NULL);
hc.name = (gchar *)purple_normalize(account, name);
hc.account = account;
hc.type = type;
switch (type) {
case PURPLE_CONV_TYPE_IM:
case PURPLE_CONV_TYPE_CHAT:
c = g_hash_table_lookup(conversation_cache, &hc);
break;
case PURPLE_CONV_TYPE_ANY:
hc.type = PURPLE_CONV_TYPE_IM;
c = g_hash_table_lookup(conversation_cache, &hc);
if (!c) {
hc.type = PURPLE_CONV_TYPE_CHAT;
c = g_hash_table_lookup(conversation_cache, &hc);
}
break;
default:
g_return_val_if_reached(NULL);
}
return c;
}
void
purple_conversation_write(PurpleConversation *conv, const char *who,
const char *message, PurpleMessageFlags flags,
time_t mtime)
{
PurplePluginProtocolInfo *prpl_info = NULL;
PurpleConnection *gc = NULL;
PurpleAccount *account;
PurpleConversationUiOps *ops;
const char *alias;
char *displayed = NULL;
PurpleBuddy *b;
int plugin_return;
PurpleConversationType type;
/* int logging_font_options = 0; */
g_return_if_fail(conv != NULL);
g_return_if_fail(message != NULL);
ops = purple_conversation_get_ui_ops(conv);
account = purple_conversation_get_account(conv);
type = purple_conversation_get_type(conv);
if (account != NULL)
gc = purple_account_get_connection(account);
if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_CHAT &&
(gc != NULL && !g_slist_find(gc->buddy_chats, conv)))
return;
if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_IM &&
!g_list_find(purple_get_conversations(), conv))
return;
displayed = g_strdup(message);
if (who == NULL || *who == '\0')
who = purple_conversation_get_name(conv);
alias = who;
plugin_return =
GPOINTER_TO_INT(purple_signal_emit_return_1(
purple_conversations_get_handle(),
(type == PURPLE_CONV_TYPE_IM ? "writing-im-msg" : "writing-chat-msg"),
account, who, &displayed, conv, flags));
if (displayed == NULL)
return;
if (plugin_return) {
g_free(displayed);
return;
}
if (account != NULL) {
prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(purple_find_prpl(purple_account_get_protocol_id(account)));
if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_IM ||
!(prpl_info->options & OPT_PROTO_UNIQUE_CHATNAME)) {
if (flags & PURPLE_MESSAGE_SEND) {
b = purple_find_buddy(account,
purple_account_get_username(account));
if (purple_account_get_alias(account) != NULL)
alias = account->alias;
else if (b != NULL && !purple_strequal(purple_buddy_get_name(b), purple_buddy_get_contact_alias(b)))
alias = purple_buddy_get_contact_alias(b);
else if (purple_connection_get_display_name(gc) != NULL)
alias = purple_connection_get_display_name(gc);
else
alias = purple_account_get_username(account);
}