summaryrefslogtreecommitdiff
path: root/libpurple/server.c
blob: 39a188a35bc6c25f6b44fd47adfd135ce3280700 (plain)
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
/*
 * 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
 *
 */

/* This file is the fullcrap */

#include "internal.h"
#include "blist.h"
#include "conversation.h"
#include "debug.h"
#include "log.h"
#include "notify.h"
#include "prefs.h"
#include "privacy.h"
#include "prpl.h"
#include "request.h"
#include "signals.h"
#include "server.h"
#include "status.h"
#include "util.h"

#define SECS_BEFORE_RESENDING_AUTORESPONSE 600
#define SEX_BEFORE_RESENDING_AUTORESPONSE "Only after you're married"

unsigned int
serv_send_typing(PurpleConnection *gc, const char *name, PurpleTypingState state)
{
	PurplePlugin *prpl = NULL;
	PurplePluginProtocolInfo *prpl_info = NULL;

	if(gc)
		prpl = purple_connection_get_prpl(gc);

	if(prpl)
		prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(prpl);

	if(prpl_info && prpl_info->send_typing)
		return prpl_info->send_typing(gc, name, state);

	return 0;
}

static GSList *last_auto_responses = NULL;
struct last_auto_response {
	PurpleConnection *gc;
	char name[80];
	time_t sent;
};

static gboolean
expire_last_auto_responses(gpointer data)
{
	GSList *tmp, *cur;
	struct last_auto_response *lar;

	tmp = last_auto_responses;

	while (tmp) {
		cur = tmp;
		tmp = tmp->next;
		lar = (struct last_auto_response *)cur->data;

		if ((time(NULL) - lar->sent) > SECS_BEFORE_RESENDING_AUTORESPONSE) {
			last_auto_responses = g_slist_remove(last_auto_responses, lar);
			g_free(lar);
		}
	}

	return FALSE; /* do not run again */
}

static struct last_auto_response *
get_last_auto_response(PurpleConnection *gc, const char *name)
{
	GSList *tmp;
	struct last_auto_response *lar;

	/* because we're modifying or creating a lar, schedule the
	 * function to expire them as the pref dictates */
	purple_timeout_add_seconds((SECS_BEFORE_RESENDING_AUTORESPONSE + 1), expire_last_auto_responses, NULL);

	tmp = last_auto_responses;

	while (tmp) {
		lar = (struct last_auto_response *)tmp->data;

		if (gc == lar->gc && !strncmp(name, lar->name, sizeof(lar->name)))
			return lar;

		tmp = tmp->next;
	}

	lar = (struct last_auto_response *)g_new0(struct last_auto_response, 1);
	g_snprintf(lar->name, sizeof(lar->name), "%s", name);
	lar->gc = gc;
	lar->sent = 0;
	last_auto_responses = g_slist_prepend(last_auto_responses, lar);

	return lar;
}

int serv_send_im(PurpleConnection *gc, const char *name, const char *message,
				 PurpleMessageFlags flags)
{
	PurpleConversation *conv = NULL;
	PurpleAccount *account = NULL;
	PurplePresence *presence = NULL;
	PurplePlugin *prpl = NULL;
	PurplePluginProtocolInfo *prpl_info = NULL;
	int val = -EINVAL;
	const gchar *auto_reply_pref = NULL;

	g_return_val_if_fail(gc != NULL, val);

	prpl = purple_connection_get_prpl(gc);

	g_return_val_if_fail(prpl != NULL, val);

	prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(prpl);

	account  = purple_connection_get_account(gc);
	presence = purple_account_get_presence(account);

	conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, name, account);

	if(prpl_info && prpl_info->send_im)
		val = prpl_info->send_im(gc, name, message, flags);

	/*
	 * XXX - If "only auto-reply when away & idle" is set, then shouldn't
	 * this only reset lar->sent if we're away AND idle?
	 */
	auto_reply_pref = purple_prefs_get_string("/purple/away/auto_reply");
	if((gc->flags & PURPLE_CONNECTION_AUTO_RESP) &&
			!purple_presence_is_available(presence) &&
			strcmp(auto_reply_pref, "never")) {

		struct last_auto_response *lar;
		lar = get_last_auto_response(gc, name);
		lar->sent = time(NULL);
	}

	if(conv && purple_conv_im_get_send_typed_timeout(PURPLE_CONV_IM(conv)))
		purple_conv_im_stop_send_typed_timeout(PURPLE_CONV_IM(conv));

	return val;
}

void serv_get_info(PurpleConnection *gc, const char *name)
{
	PurplePlugin *prpl = NULL;
	PurplePluginProtocolInfo *prpl_info = NULL;

	if(gc)
		prpl = purple_connection_get_prpl(gc);
	
	if(prpl)
		prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(prpl);

	if(gc && prpl_info && prpl_info->get_info)
		prpl_info->get_info(gc, name);
}

void serv_set_info(PurpleConnection *gc, const char *info)
{
	PurplePlugin *prpl = NULL;
	PurplePluginProtocolInfo *prpl_info = NULL;
	PurpleAccount *account = NULL;;

	if(gc)
		prpl = purple_connection_get_prpl(gc);
	
	if(prpl)
		prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(prpl);

	if(prpl_info && prpl_info->set_info) {

		account = purple_connection_get_account(gc);

		if(purple_signal_emit_return_1(purple_accounts_get_handle(),
									  "account-setting-info", account, info))
			return;

		prpl_info->set_info(gc, info);

		purple_signal_emit(purple_accounts_get_handle(),
						 "account-set-info", account, info);
	}
}

/*
 * Set buddy's alias on server roster/list
 */
void serv_alias_buddy(PurpleBuddy *b)
{
	PurpleAccount *account = NULL;
	PurpleConnection *gc = NULL;
	PurplePlugin *prpl = NULL;
	PurplePluginProtocolInfo *prpl_info = NULL;

	if(b)
		account = purple_buddy_get_account(b);

	if(account)
		gc = purple_account_get_connection(account);

	if(gc)
		prpl = purple_connection_get_prpl(gc);

	if(prpl)
		prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(prpl);

	if(b && prpl_info && prpl_info->alias_buddy) {
		prpl_info->alias_buddy(gc, b->name, b->alias);
	}
}

void
serv_got_alias(PurpleConnection *gc, const char *who, const char *alias)
{
	PurpleAccount *account;
	GSList *buddies;
	PurpleBuddy *b;
	PurpleConversation *conv;

	account = purple_connection_get_account(gc);
	buddies = purple_find_buddies(account, who);

	while (buddies != NULL)
	{
		b = buddies->data;
		buddies = g_slist_delete_link(buddies, buddies);

		if((b->server_alias == NULL && alias == NULL) ||
		    (b->server_alias && alias && !strcmp(b->server_alias, alias)))
		{
			continue;
		}

		purple_blist_server_alias_buddy(b, alias);

		conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, b->name, account);
		if(conv != NULL && alias != NULL && strcmp(alias, who))
		{
			char *tmp = g_strdup_printf(_("%s is now known as %s.\n"),
										who, alias);

			purple_conversation_write(conv, NULL, tmp,
					PURPLE_MESSAGE_SYSTEM | PURPLE_MESSAGE_NO_LINKIFY,
					time(NULL));

			g_free(tmp);
		}
	}
}

void
purple_serv_got_private_alias(PurpleConnection *gc, const char *who, const char *alias)
{
	PurpleAccount *account = NULL;
	GSList *buddies = NULL;
	PurpleBuddy *b = NULL;

	account = purple_connection_get_account(gc);
	buddies = purple_find_buddies(account, who);

	while(buddies != NULL) {
		b = buddies->data;

		buddies = g_slist_delete_link(buddies, buddies);

		if((!b->alias && !alias) || (b->alias && alias && !strcmp(b->alias, alias)))
			continue;

		purple_blist_alias_buddy(b, alias);
	}
}


PurpleAttentionType *purple_get_attention_type_from_code(PurpleAccount *account, guint type_code)
{
	PurplePlugin *prpl;
	PurpleAttentionType* attn;
	GList *(*get_attention_types)(PurpleAccount *);

	g_return_val_if_fail(account != NULL, NULL);

	prpl = purple_find_prpl(purple_account_get_protocol_id(account));

	/* Lookup the attention type in the protocol's attention_types list, if any. */
	get_attention_types = PURPLE_PLUGIN_PROTOCOL_INFO(prpl)->get_attention_types;
	if (get_attention_types) {
		GList *attention_types;

		attention_types = get_attention_types(account);
		attn = (PurpleAttentionType *)g_list_nth_data(attention_types, type_code);
	} else {
		attn = NULL;
	}

	return attn;
}

void
serv_send_attention(PurpleConnection *gc, const char *who, guint type_code)
{
	purple_prpl_send_attention(gc, who, type_code);
}

void
serv_got_attention(PurpleConnection *gc, const char *who, guint type_code)
{
	purple_prpl_got_attention(gc, who, type_code);
}


/*
 * Move a buddy from one group to another on server.
 *
 * Note: For now we'll not deal with changing gc's at the same time, but
 * it should be possible.  Probably needs to be done, someday.  Although,
 * the UI for that would be difficult, because groups are Purple-wide.
 */
void serv_move_buddy(PurpleBuddy *b, PurpleGroup *og, PurpleGroup *ng)
{
	PurpleAccount *account = NULL;
	PurpleConnection *gc = NULL;
	PurplePlugin *prpl = NULL;
	PurplePluginProtocolInfo *prpl_info = NULL;

	g_return_if_fail(b != NULL);
	g_return_if_fail(og != NULL);
	g_return_if_fail(ng != NULL);

	account = purple_buddy_get_account(b);
	gc = purple_account_get_connection(account);

	if(gc)
		prpl = purple_connection_get_prpl(gc);

	if(prpl)
		prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(prpl);

	if(gc && og && ng) {
		if (prpl_info && prpl_info->group_buddy) {
			prpl_info->group_buddy(gc, b->name, og->name, ng->name);
		}
	}
}

void serv_add_permit(PurpleConnection *gc, const char *name)
{
	PurplePlugin *prpl = NULL;
	PurplePluginProtocolInfo *prpl_info = NULL;

	if(gc)
		prpl = purple_connection_get_prpl(gc);

	if(prpl)
		prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(prpl);

	if(prpl_info && prpl_info->add_permit)
		prpl_info->add_permit(gc, name);
}

void serv_add_deny(PurpleConnection *gc, const char *name)
{
	PurplePlugin *prpl = NULL;
	PurplePluginProtocolInfo *prpl_info = NULL;

	if(gc)
		prpl = purple_connection_get_prpl(gc);

	if(prpl)
		prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(prpl);

	if(prpl_info && prpl_info->add_deny)
		prpl_info->add_deny(gc, name);
}

void serv_rem_permit(PurpleConnection *gc, const char *name)
{
	PurplePlugin *prpl = NULL;
	PurplePluginProtocolInfo *prpl_info = NULL;

	if(gc)
		prpl = purple_connection_get_prpl(gc);

	if(prpl)
		prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(prpl);

	if(prpl_info && prpl_info->rem_permit)
		prpl_info->rem_permit(gc, name);
}

void serv_rem_deny(PurpleConnection *gc, const char *name)
{
	PurplePlugin *prpl = NULL;
	PurplePluginProtocolInfo *prpl_info = NULL;

	if(gc)
		prpl = purple_connection_get_prpl(gc);

	if(prpl)
		prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(prpl);

	if(prpl_info && prpl_info->rem_deny)
		prpl_info->rem_deny(gc, name);
}

void serv_set_permit_deny(PurpleConnection *gc)
{
	PurplePlugin *prpl = NULL;
	PurplePluginProtocolInfo *prpl_info = NULL;

	if(gc)
		prpl = purple_connection_get_prpl(gc);

	if(prpl)
		prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(prpl);

	/*
	 * this is called when either you import a buddy list, and make lots
	 * of changes that way, or when the user toggles the permit/deny mode
	 * in the prefs. In either case you should probably be resetting and
	 * resending the permit/deny info when you get this.
	 */
	if(prpl_info && prpl_info->set_permit_deny)
		prpl_info->set_permit_deny(gc);
}

void serv_join_chat(PurpleConnection *gc, GHashTable *data)
{
	PurplePlugin *prpl = NULL;
	PurplePluginProtocolInfo *prpl_info = NULL;

	if(gc)
		prpl = purple_connection_get_prpl(gc);

	if(prpl)
		prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(prpl);

	if(prpl_info && prpl_info->join_chat)
		prpl_info->join_chat(gc, data);
}


void serv_reject_chat(PurpleConnection *gc, GHashTable *data)
{
	PurplePlugin *prpl = NULL;
	PurplePluginProtocolInfo *prpl_info = NULL;

	if(gc)
		prpl = purple_connection_get_prpl(gc);

	if(prpl)
		prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(prpl);

	if(prpl_info && prpl_info->reject_chat)
		prpl_info->reject_chat(gc, data);
}

void serv_chat_invite(PurpleConnection *gc, int id, const char *message, const char *name)
{
	PurplePlugin *prpl = NULL;
	PurplePluginProtocolInfo *prpl_info = NULL;
	PurpleConversation *conv;
	char *buffy = message && *message ? g_strdup(message) : NULL;

	conv = purple_find_chat(gc, id);

	if(conv == NULL)
		return;

	if(gc)
		prpl = purple_connection_get_prpl(gc);

	if(prpl)
		prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(prpl);

	purple_signal_emit(purple_conversations_get_handle(), "chat-inviting-user",
					 conv, name, &buffy);

	if (prpl_info && prpl_info->chat_invite)
		prpl_info->chat_invite(gc, id, buffy, name);

	purple_signal_emit(purple_conversations_get_handle(), "chat-invited-user",
					 conv, name, buffy);

	g_free(buffy);
}

/* Ya know, nothing uses this except purple_conversation_destroy(),
 * I think I'll just merge it into that later...
 * Then again, something might want to use this, from outside prpl-land
 * to leave a chat without destroying the conversation.
 */

void serv_chat_leave(PurpleConnection *gc, int id)
{
	PurplePlugin *prpl = NULL;
	PurplePluginProtocolInfo *prpl_info = NULL;

	prpl = purple_connection_get_prpl(gc);

	if(prpl)
		prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(prpl);

	if(prpl_info && prpl_info->chat_leave)
		prpl_info->chat_leave(gc, id);
}

void serv_chat_whisper(PurpleConnection *gc, int id, const char *who, const char *message)
{
	PurplePlugin *prpl = NULL;
	PurplePluginProtocolInfo *prpl_info = NULL;

	if(gc)
		prpl = purple_connection_get_prpl(gc);

	if(prpl)
		prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(prpl);

	if(prpl_info && prpl_info->chat_whisper)
		prpl_info->chat_whisper(gc, id, who, message);
}

int serv_chat_send(PurpleConnection *gc, int id, const char *message, PurpleMessageFlags flags)
{
	int val = -EINVAL;
	PurplePlugin *prpl = NULL;
	PurplePluginProtocolInfo *prpl_info = NULL;

	prpl = purple_connection_get_prpl(gc);

	if(prpl)
		prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(prpl);

	if(prpl_info && prpl_info->chat_send)
		val = prpl_info->chat_send(gc, id, message, flags);

	return val;
}

/*
 * woo. i'm actually going to comment this function. isn't that fun. make
 * sure to follow along, kids
 */
void serv_got_im(PurpleConnection *gc, const char *who, const char *msg,
				 PurpleMessageFlags flags, time_t mtime)
{
	PurpleAccount *account;
	PurpleConversation *conv;
	char *message, *name;
	char *angel, *buffy;
	int plugin_return;

	g_return_if_fail(msg != NULL);

	account  = purple_connection_get_account(gc);

	if (PURPLE_PLUGIN_PROTOCOL_INFO(purple_connection_get_prpl(gc))->set_permit_deny == NULL) {
		/* protocol does not support privacy, handle it ourselves */
		if (!purple_privacy_check(account, who)) {
			purple_signal_emit(purple_conversations_get_handle(), "blocked-im-msg",
					account, who, msg, flags, (unsigned int)mtime);
			return;
		}
	}

	/*
	 * We should update the conversation window buttons and menu,
	 * if it exists.
	 */
	conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, who, gc->account);

	/*
	 * Make copies of the message and the sender in case plugins want
	 * to free these strings and replace them with a modifed version.
	 */
	buffy = g_strdup(msg);
	angel = g_strdup(who);

	plugin_return = GPOINTER_TO_INT(
		purple_signal_emit_return_1(purple_conversations_get_handle(),
								  "receiving-im-msg", gc->account,
								  &angel, &buffy, conv, &flags));

	if (!buffy || !angel || plugin_return) {
		g_free(buffy);
		g_free(angel);
		return;
	}

	name = angel;
	message = buffy;

	purple_signal_emit(purple_conversations_get_handle(), "received-im-msg", gc->account,
					 name, message, conv, flags);

	/* search for conversation again in case it was created by received-im-msg handler */
	if (conv == NULL)
		conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, name, gc->account);

	/*
	 * XXX: Should we be setting this here, or relying on prpls to set it?
	 */
	flags |= PURPLE_MESSAGE_RECV;

	if (conv == NULL)
		conv = purple_conversation_new(PURPLE_CONV_TYPE_IM, account, name);

	purple_conv_im_write(PURPLE_CONV_IM(conv), name, message, flags, mtime);
	g_free(message);

	/*
	 * Don't autorespond if:
	 *
	 *  - it's not supported on this connection
	 *  - we are available
	 *  - or it's disabled
	 *  - or we're not idle and the 'only auto respond if idle' pref
	 *    is set
	 */
	if (gc->flags & PURPLE_CONNECTION_AUTO_RESP)
	{
		PurplePresence *presence;
		PurpleStatus *status;
		PurpleStatusType *status_type;
		PurpleStatusPrimitive primitive;
		const gchar *auto_reply_pref;
		const char *away_msg = NULL;
		gboolean mobile = FALSE;

		auto_reply_pref = purple_prefs_get_string("/purple/away/auto_reply");

		presence = purple_account_get_presence(account);
		status = purple_presence_get_active_status(presence);
		status_type = purple_status_get_type(status);
		primitive = purple_status_type_get_primitive(status_type);
		mobile = purple_presence_is_status_primitive_active(presence, PURPLE_STATUS_MOBILE);
		if ((primitive == PURPLE_STATUS_AVAILABLE) ||
			(primitive == PURPLE_STATUS_INVISIBLE) ||
			mobile ||
		    !strcmp(auto_reply_pref, "never") ||
		    (!purple_presence_is_idle(presence) && !strcmp(auto_reply_pref, "awayidle")))
		{
			g_free(name);
			return;
		}

		away_msg = purple_value_get_string(
			purple_status_get_attr_value(status, "message"));

		if ((away_msg != NULL) && (*away_msg != '\0')) {
			struct last_auto_response *lar;
			time_t now = time(NULL);

			/*
			 * This used to be based on the conversation window. But um, if
			 * you went away, and someone sent you a message and got your
			 * auto-response, and then you closed the window, and then they
			 * sent you another one, they'd get the auto-response back too
			 * soon. Besides that, we need to keep track of this even if we've
			 * got a queue. So the rest of this block is just the auto-response,
			 * if necessary.
			 */
			lar = get_last_auto_response(gc, name);
			if ((now - lar->sent) >= SECS_BEFORE_RESENDING_AUTORESPONSE)
			{
				/*
				 * We don't want to send an autoresponse in response to the other user's
				 * autoresponse.  We do, however, not want to then send one in response to the
				 * _next_ message, so we still set lar->sent to now.
				 */
				lar->sent = now;

				if (!(flags & PURPLE_MESSAGE_AUTO_RESP))
				{
					serv_send_im(gc, name, away_msg, PURPLE_MESSAGE_AUTO_RESP);

					purple_conv_im_write(PURPLE_CONV_IM(conv), NULL, away_msg,
									   PURPLE_MESSAGE_SEND | PURPLE_MESSAGE_AUTO_RESP,
									   mtime);
				}
			}
		}
	}

	g_free(name);
}

void serv_got_typing(PurpleConnection *gc, const char *name, int timeout,
					 PurpleTypingState state) {
	PurpleConversation *conv;
	PurpleConvIm *im = NULL;

	conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, name, gc->account);
	if (conv != NULL) {
		im = PURPLE_CONV_IM(conv);

		purple_conv_im_set_typing_state(im, state);
		purple_conv_im_update_typing(im);
	} else {
		switch (state)
		{
			case PURPLE_TYPING:
				purple_signal_emit(purple_conversations_get_handle(),
								   "buddy-typing", gc->account, name);
				break;
			case PURPLE_TYPED:
				purple_signal_emit(purple_conversations_get_handle(),
								   "buddy-typed", gc->account, name);
				break;
			case PURPLE_NOT_TYPING:
				purple_signal_emit(purple_conversations_get_handle(),
								   "buddy-typing-stopped", gc->account, name);
				break;
		}
	}

	if (conv != NULL && timeout > 0)
		purple_conv_im_start_typing_timeout(im, timeout);
}

void serv_got_typing_stopped(PurpleConnection *gc, const char *name) {

	PurpleConversation *conv;
	PurpleConvIm *im;

	conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, name, gc->account);
	if (conv != NULL)
	{
		im = PURPLE_CONV_IM(conv);

		if (im->typing_state == PURPLE_NOT_TYPING)
			return;

		purple_conv_im_stop_typing_timeout(im);
		purple_conv_im_set_typing_state(im, PURPLE_NOT_TYPING);
		purple_conv_im_update_typing(im);
	}
	else
	{
		purple_signal_emit(purple_conversations_get_handle(),
						 "buddy-typing-stopped", gc->account, name);
	}
}

struct chat_invite_data {
	PurpleConnection *gc;
	GHashTable *components;
};

static void chat_invite_data_free(struct chat_invite_data *cid)
{
	if (cid->components)
		g_hash_table_destroy(cid->components);
	g_free(cid);
}


static void chat_invite_reject(struct chat_invite_data *cid)
{
	serv_reject_chat(cid->gc, cid->components);
	chat_invite_data_free(cid);
}


static void chat_invite_accept(struct chat_invite_data *cid)
{
	serv_join_chat(cid->gc, cid->components);
	chat_invite_data_free(cid);
}



void serv_got_chat_invite(PurpleConnection *gc, const char *name,
						  const char *who, const char *message, GHashTable *data)
{
	PurpleAccount *account;
	char buf2[BUF_LONG];
	struct chat_invite_data *cid = g_new0(struct chat_invite_data, 1);
	int plugin_return;

	account = purple_connection_get_account(gc);
	if (PURPLE_PLUGIN_PROTOCOL_INFO(purple_connection_get_prpl(gc))->set_permit_deny == NULL) {
		/* protocol does not support privacy, handle it ourselves */
		if (!purple_privacy_check(account, who)) {
			purple_signal_emit(purple_conversations_get_handle(), "chat-invite-blocked",
					account, who, name, message, data);
			return;
		}
	}

	plugin_return = GPOINTER_TO_INT(purple_signal_emit_return_1(
					purple_conversations_get_handle(),
					"chat-invited", account, who, name, message, data));

	cid->gc = gc;
	cid->components = data;

	if (plugin_return == 0)
	{
		if (message != NULL)
		{
			g_snprintf(buf2, sizeof(buf2),
				   _("%s has invited %s to the chat room %s:\n%s"),
				   who, purple_account_get_username(account), name, message);
		}
		else
			g_snprintf(buf2, sizeof(buf2),
				   _("%s has invited %s to the chat room %s\n"),
				   who, purple_account_get_username(account), name);


		purple_request_accept_cancel(gc, NULL, _("Accept chat invitation?"), buf2,
							   PURPLE_DEFAULT_ACTION_NONE, account, who, NULL,
							   cid, G_CALLBACK(chat_invite_accept),
							   G_CALLBACK(chat_invite_reject));
	}
	else if (plugin_return > 0)
		chat_invite_accept(cid);
	else
		chat_invite_reject(cid);
}

PurpleConversation *serv_got_joined_chat(PurpleConnection *gc,
											   int id, const char *name)
{
	PurpleConversation *conv;
	PurpleConvChat *chat;
	PurpleAccount *account;

	account = purple_connection_get_account(gc);

	conv = purple_conversation_new(PURPLE_CONV_TYPE_CHAT, account, name);
	chat = PURPLE_CONV_CHAT(conv);

	if (!g_slist_find(gc->buddy_chats, conv))
		gc->buddy_chats = g_slist_append(gc->buddy_chats, conv);

	purple_conv_chat_set_id(chat, id);

	purple_signal_emit(purple_conversations_get_handle(), "chat-joined", conv);

	return conv;
}

void serv_got_chat_left(PurpleConnection *g, int id)
{
	GSList *bcs;
	PurpleConversation *conv = NULL;
	PurpleConvChat *chat = NULL;

	for (bcs = g->buddy_chats; bcs != NULL; bcs = bcs->next) {
		conv = (PurpleConversation *)bcs->data;

		chat = PURPLE_CONV_CHAT(conv);

		if (purple_conv_chat_get_id(chat) == id)
			break;

		conv = NULL;
	}

	if (!conv)
		return;

	purple_debug(PURPLE_DEBUG_INFO, "server", "Leaving room: %s\n",
			   purple_conversation_get_name(conv));

	g->buddy_chats = g_slist_remove(g->buddy_chats, conv);

	purple_conv_chat_left(PURPLE_CONV_CHAT(conv));

	purple_signal_emit(purple_conversations_get_handle(), "chat-left", conv);
}

void purple_serv_got_join_chat_failed(PurpleConnection *gc, GHashTable *data)
{
	purple_signal_emit(purple_conversations_get_handle(), "chat-join-failed",
					gc, data);
}

void serv_got_chat_in(PurpleConnection *g, int id, const char *who,
					  PurpleMessageFlags flags, const char *message, time_t mtime)
{
	GSList *bcs;
	PurpleConversation *conv = NULL;
	PurpleConvChat *chat = NULL;
	char *buffy, *angel;
	int plugin_return;

	g_return_if_fail(who != NULL);
	g_return_if_fail(message != NULL);

	for (bcs = g->buddy_chats; bcs != NULL; bcs = bcs->next) {
		conv = (PurpleConversation *)bcs->data;

		chat = PURPLE_CONV_CHAT(conv);

		if (purple_conv_chat_get_id(chat) == id)
			break;

		conv = NULL;
	}

	if (!conv)
		return;

	/*
	 * Make copies of the message and the sender in case plugins want
	 * to free these strings and replace them with a modifed version.
	 */
	buffy = g_strdup(message);
	angel = g_strdup(who);

	plugin_return = GPOINTER_TO_INT(
		purple_signal_emit_return_1(purple_conversations_get_handle(),
								  "receiving-chat-msg", g->account,
								  &angel, &buffy, conv, &flags));

	if (!buffy || !angel || plugin_return) {
		g_free(buffy);
		g_free(angel);
		return;
	}

	who = angel;
	message = buffy;

	purple_signal_emit(purple_conversations_get_handle(), "received-chat-msg", g->account,
					 who, message, conv, flags);

	purple_conv_chat_write(chat, who, message, flags, mtime);

	g_free(angel);
	g_free(buffy);
}

void serv_send_file(PurpleConnection *gc, const char *who, const char *file)
{
	PurplePlugin *prpl = NULL;
	PurplePluginProtocolInfo *prpl_info = NULL;

	if(gc)
		prpl = purple_connection_get_prpl(gc);

	if(prpl)
		prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(prpl);

	if (prpl_info && prpl_info->send_file) {
		if (!prpl_info->can_receive_file || prpl_info->can_receive_file(gc, who)) {
			prpl_info->send_file(gc, who, file);
		}
	}
}