-
Notifications
You must be signed in to change notification settings - Fork 2
/
prpl.c
637 lines (504 loc) · 16.6 KB
/
prpl.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
/*
* 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 "conversation.h"
#include "debug.h"
#include "network.h"
#include "notify.h"
#include "prpl.h"
#include "request.h"
#include "util.h"
/**************************************************************************/
/** @name Attention Type API */
/**************************************************************************/
PurpleAttentionType *
purple_attention_type_new(const char *ulname, const char *name,
const char *inc_desc, const char *out_desc)
{
PurpleAttentionType *attn = g_new0(PurpleAttentionType, 1);
purple_attention_type_set_name(attn, name);
purple_attention_type_set_incoming_desc(attn, inc_desc);
purple_attention_type_set_outgoing_desc(attn, out_desc);
purple_attention_type_set_unlocalized_name(attn, ulname);
return attn;
}
void
purple_attention_type_set_name(PurpleAttentionType *type, const char *name)
{
g_return_if_fail(type != NULL);
type->name = name;
}
void
purple_attention_type_set_incoming_desc(PurpleAttentionType *type, const char *desc)
{
g_return_if_fail(type != NULL);
type->incoming_description = desc;
}
void
purple_attention_type_set_outgoing_desc(PurpleAttentionType *type, const char *desc)
{
g_return_if_fail(type != NULL);
type->outgoing_description = desc;
}
void
purple_attention_type_set_icon_name(PurpleAttentionType *type, const char *name)
{
g_return_if_fail(type != NULL);
type->icon_name = name;
}
void
purple_attention_type_set_unlocalized_name(PurpleAttentionType *type, const char *ulname)
{
g_return_if_fail(type != NULL);
type->unlocalized_name = ulname;
}
const char *
purple_attention_type_get_name(const PurpleAttentionType *type)
{
g_return_val_if_fail(type != NULL, NULL);
return type->name;
}
const char *
purple_attention_type_get_incoming_desc(const PurpleAttentionType *type)
{
g_return_val_if_fail(type != NULL, NULL);
return type->incoming_description;
}
const char *
purple_attention_type_get_outgoing_desc(const PurpleAttentionType *type)
{
g_return_val_if_fail(type != NULL, NULL);
return type->outgoing_description;
}
const char *
purple_attention_type_get_icon_name(const PurpleAttentionType *type)
{
g_return_val_if_fail(type != NULL, NULL);
if(type->icon_name == NULL || *(type->icon_name) == '\0')
return NULL;
return type->icon_name;
}
const char *
purple_attention_type_get_unlocalized_name(const PurpleAttentionType *type)
{
g_return_val_if_fail(type != NULL, NULL);
return type->unlocalized_name;
}
/**************************************************************************/
/** @name Protocol Plugin API */
/**************************************************************************/
void
purple_prpl_got_account_idle(PurpleAccount *account, gboolean idle,
time_t idle_time)
{
g_return_if_fail(account != NULL);
g_return_if_fail(purple_account_is_connected(account));
purple_presence_set_idle(purple_account_get_presence(account),
idle, idle_time);
}
void
purple_prpl_got_account_login_time(PurpleAccount *account, time_t login_time)
{
PurplePresence *presence;
g_return_if_fail(account != NULL);
g_return_if_fail(purple_account_is_connected(account));
if (login_time == 0)
login_time = time(NULL);
presence = purple_account_get_presence(account);
purple_presence_set_login_time(presence, login_time);
}
void
purple_prpl_got_account_status(PurpleAccount *account, const char *status_id, ...)
{
PurplePresence *presence;
PurpleStatus *status;
va_list args;
g_return_if_fail(account != NULL);
g_return_if_fail(status_id != NULL);
g_return_if_fail(purple_account_is_connected(account));
presence = purple_account_get_presence(account);
status = purple_presence_get_status(presence, status_id);
g_return_if_fail(status != NULL);
va_start(args, status_id);
purple_status_set_active_with_attrs(status, TRUE, args);
va_end(args);
}
void
purple_prpl_got_account_actions(PurpleAccount *account)
{
g_return_if_fail(account != NULL);
g_return_if_fail(purple_account_is_connected(account));
purple_signal_emit(purple_accounts_get_handle(), "account-actions-changed",
account);
}
void
purple_prpl_got_user_idle(PurpleAccount *account, const char *name,
gboolean idle, time_t idle_time)
{
PurplePresence *presence;
GSList *list;
g_return_if_fail(account != NULL);
g_return_if_fail(name != NULL);
g_return_if_fail(purple_account_is_connected(account) || purple_account_is_connecting(account));
if ((list = purple_find_buddies(account, name)) == NULL)
return;
while (list) {
presence = purple_buddy_get_presence(list->data);
list = g_slist_delete_link(list, list);
purple_presence_set_idle(presence, idle, idle_time);
}
}
void
purple_prpl_got_user_login_time(PurpleAccount *account, const char *name,
time_t login_time)
{
GSList *list;
PurplePresence *presence;
g_return_if_fail(account != NULL);
g_return_if_fail(name != NULL);
if ((list = purple_find_buddies(account, name)) == NULL)
return;
if (login_time == 0)
login_time = time(NULL);
while (list) {
PurpleBuddy *buddy = list->data;
presence = purple_buddy_get_presence(buddy);
list = g_slist_delete_link(list, list);
if (purple_presence_get_login_time(presence) != login_time)
{
purple_presence_set_login_time(presence, login_time);
purple_signal_emit(purple_blist_get_handle(), "buddy-got-login-time", buddy);
}
}
}
void
purple_prpl_got_user_status(PurpleAccount *account, const char *name,
const char *status_id, ...)
{
GSList *list, *l;
PurpleBuddy *buddy;
PurplePresence *presence;
PurpleStatus *status;
PurpleStatus *old_status;
va_list args;
g_return_if_fail(account != NULL);
g_return_if_fail(name != NULL);
g_return_if_fail(status_id != NULL);
g_return_if_fail(purple_account_is_connected(account) || purple_account_is_connecting(account));
if((list = purple_find_buddies(account, name)) == NULL)
return;
for(l = list; l != NULL; l = l->next) {
buddy = l->data;
presence = purple_buddy_get_presence(buddy);
status = purple_presence_get_status(presence, status_id);
if(NULL == status)
/*
* TODO: This should never happen, right? We should call
* g_warning() or something.
*/
continue;
old_status = purple_presence_get_active_status(presence);
va_start(args, status_id);
purple_status_set_active_with_attrs(status, TRUE, args);
va_end(args);
purple_blist_update_buddy_status(buddy, old_status);
}
g_slist_free(list);
/* The buddy is no longer online, they are therefore by definition not
* still typing to us. */
if (!purple_status_is_online(status)) {
serv_got_typing_stopped(purple_account_get_connection(account), name);
purple_prpl_got_media_caps(account, name);
}
}
void purple_prpl_got_user_status_deactive(PurpleAccount *account, const char *name,
const char *status_id)
{
GSList *list, *l;
PurpleBuddy *buddy;
PurplePresence *presence;
PurpleStatus *status;
g_return_if_fail(account != NULL);
g_return_if_fail(name != NULL);
g_return_if_fail(status_id != NULL);
g_return_if_fail(purple_account_is_connected(account) || purple_account_is_connecting(account));
if((list = purple_find_buddies(account, name)) == NULL)
return;
for(l = list; l != NULL; l = l->next) {
buddy = l->data;
presence = purple_buddy_get_presence(buddy);
status = purple_presence_get_status(presence, status_id);
if(NULL == status)
continue;
if (purple_status_is_active(status)) {
purple_status_set_active(status, FALSE);
purple_blist_update_buddy_status(buddy, status);
}
}
g_slist_free(list);
}
static void
do_prpl_change_account_status(PurpleAccount *account,
PurpleStatus *old_status, PurpleStatus *new_status)
{
PurplePlugin *prpl;
PurplePluginProtocolInfo *prpl_info;
if (purple_status_is_online(new_status) &&
purple_account_is_disconnected(account) &&
purple_network_is_available())
{
purple_account_connect(account);
return;
}
if (!purple_status_is_online(new_status))
{
if (!purple_account_is_disconnected(account))
purple_account_disconnect(account);
/* Clear out the unsaved password if we're already disconnected and we switch to offline status */
else if (!purple_account_get_remember_password(account))
purple_account_set_password(account, NULL);
return;
}
if (purple_account_is_connecting(account))
/*
* We don't need to call the set_status PRPL function because
* the PRPL will take care of setting its status during the
* connection process.
*/
return;
prpl = purple_find_prpl(purple_account_get_protocol_id(account));
if (prpl == NULL)
return;
prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(prpl);
if (!purple_account_is_disconnected(account) && prpl_info->set_status != NULL)
{
prpl_info->set_status(account, new_status);
}
}
void
purple_prpl_change_account_status(PurpleAccount *account,
PurpleStatus *old_status, PurpleStatus *new_status)
{
g_return_if_fail(account != NULL);
g_return_if_fail(new_status != NULL);
g_return_if_fail(!purple_status_is_exclusive(new_status) || old_status != NULL);
do_prpl_change_account_status(account, old_status, new_status);
purple_signal_emit(purple_accounts_get_handle(), "account-status-changed",
account, old_status, new_status);
}
GList *
purple_prpl_get_statuses(PurpleAccount *account, PurplePresence *presence)
{
GList *statuses = NULL;
GList *l;
PurpleStatus *status;
g_return_val_if_fail(account != NULL, NULL);
g_return_val_if_fail(presence != NULL, NULL);
for (l = purple_account_get_status_types(account); l != NULL; l = l->next)
{
status = purple_status_new((PurpleStatusType *)l->data, presence);
statuses = g_list_prepend(statuses, status);
}
statuses = g_list_reverse(statuses);
return statuses;
}
static void
purple_prpl_attention(PurpleConversation *conv, const char *who,
guint type, PurpleMessageFlags flags, time_t mtime)
{
PurpleAccount *account = purple_conversation_get_account(conv);
purple_signal_emit(purple_conversations_get_handle(),
flags == PURPLE_MESSAGE_SEND ? "sent-attention" : "got-attention",
account, who, conv, type);
}
void
purple_prpl_send_attention(PurpleConnection *gc, const char *who, guint type_code)
{
PurpleAttentionType *attn;
PurpleMessageFlags flags;
PurplePlugin *prpl;
PurpleConversation *conv;
gboolean (*send_attention)(PurpleConnection *, const char *, guint);
PurpleBuddy *buddy;
const char *alias;
gchar *description;
time_t mtime;
g_return_if_fail(gc != NULL);
g_return_if_fail(who != NULL);
prpl = purple_find_prpl(purple_account_get_protocol_id(gc->account));
send_attention = PURPLE_PLUGIN_PROTOCOL_INFO(prpl)->send_attention;
g_return_if_fail(send_attention != NULL);
mtime = time(NULL);
attn = purple_get_attention_type_from_code(gc->account, type_code);
if ((buddy = purple_find_buddy(purple_connection_get_account(gc), who)) != NULL)
alias = purple_buddy_get_contact_alias(buddy);
else
alias = who;
if (attn && purple_attention_type_get_outgoing_desc(attn)) {
description = g_strdup_printf(purple_attention_type_get_outgoing_desc(attn), alias);
} else {
description = g_strdup_printf(_("Requesting %s's attention..."), alias);
}
flags = PURPLE_MESSAGE_SEND | PURPLE_MESSAGE_NOTIFY | PURPLE_MESSAGE_SYSTEM;
purple_debug_info("server", "serv_send_attention: sending '%s' to %s\n",
description, who);
if (!send_attention(gc, who, type_code))
return;
conv = purple_conversation_new(PURPLE_CONV_TYPE_IM, gc->account, who);
purple_conv_im_write(PURPLE_CONV_IM(conv), NULL, description, flags, mtime);
purple_prpl_attention(conv, who, type_code, PURPLE_MESSAGE_SEND, time(NULL));
g_free(description);
}
static void
got_attention(PurpleConnection *gc, int id, const char *who, guint type_code)
{
PurpleMessageFlags flags;
PurpleAttentionType *attn;
PurpleBuddy *buddy;
const char *alias;
gchar *description;
time_t mtime;
mtime = time(NULL);
attn = purple_get_attention_type_from_code(gc->account, type_code);
/* PURPLE_MESSAGE_NOTIFY is for attention messages. */
flags = PURPLE_MESSAGE_SYSTEM | PURPLE_MESSAGE_NOTIFY | PURPLE_MESSAGE_RECV;
/* TODO: if (attn->icon_name) is non-null, use it to lookup an emoticon and display
* it next to the attention command. And if it is null, display a generic icon. */
if ((buddy = purple_find_buddy(purple_connection_get_account(gc), who)) != NULL)
alias = purple_buddy_get_contact_alias(buddy);
else
alias = who;
if (attn && purple_attention_type_get_incoming_desc(attn)) {
description = g_strdup_printf(purple_attention_type_get_incoming_desc(attn), alias);
} else {
description = g_strdup_printf(_("%s has requested your attention!"), alias);
}
purple_debug_info("server", "got_attention: got '%s' from %s\n",
description, who);
if (id == -1)
serv_got_im(gc, who, description, flags, mtime);
else
serv_got_chat_in(gc, id, who, flags, description, mtime);
/* TODO: sounds (depending on PurpleAttentionType), shaking, etc. */
g_free(description);
}
void
purple_prpl_got_attention(PurpleConnection *gc, const char *who, guint type_code)
{
PurpleConversation *conv = NULL;
PurpleAccount *account = purple_connection_get_account(gc);
got_attention(gc, -1, who, type_code);
conv =
purple_find_conversation_with_account(PURPLE_CONV_TYPE_ANY, who, account);
if (conv)
purple_prpl_attention(conv, who, type_code, PURPLE_MESSAGE_RECV,
time(NULL));
}
void
purple_prpl_got_attention_in_chat(PurpleConnection *gc, int id, const char *who, guint type_code)
{
got_attention(gc, id, who, type_code);
}
gboolean
purple_prpl_initiate_media(PurpleAccount *account,
const char *who,
PurpleMediaSessionType type)
{
#ifdef USE_VV
PurpleConnection *gc = NULL;
PurplePlugin *prpl = NULL;
PurplePluginProtocolInfo *prpl_info = NULL;
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 (prpl_info && PURPLE_PROTOCOL_PLUGIN_HAS_FUNC(prpl_info, initiate_media)) {
/* should check that the protocol supports this media type here? */
return prpl_info->initiate_media(account, who, type);
} else
#endif
return FALSE;
}
PurpleMediaCaps
purple_prpl_get_media_caps(PurpleAccount *account, const char *who)
{
#ifdef USE_VV
PurpleConnection *gc = NULL;
PurplePlugin *prpl = NULL;
PurplePluginProtocolInfo *prpl_info = NULL;
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 (prpl_info && PURPLE_PROTOCOL_PLUGIN_HAS_FUNC(prpl_info,
get_media_caps)) {
return prpl_info->get_media_caps(account, who);
}
#endif
return PURPLE_MEDIA_CAPS_NONE;
}
void
purple_prpl_got_media_caps(PurpleAccount *account, const char *name)
{
#ifdef USE_VV
GSList *list;
g_return_if_fail(account != NULL);
g_return_if_fail(name != NULL);
if ((list = purple_find_buddies(account, name)) == NULL)
return;
while (list) {
PurpleBuddy *buddy = list->data;
PurpleMediaCaps oldcaps = purple_buddy_get_media_caps(buddy);
PurpleMediaCaps newcaps = 0;
const gchar *bname = purple_buddy_get_name(buddy);
list = g_slist_delete_link(list, list);
newcaps = purple_prpl_get_media_caps(account, bname);
purple_buddy_set_media_caps(buddy, newcaps);
if (oldcaps == newcaps)
continue;
purple_signal_emit(purple_blist_get_handle(),
"buddy-caps-changed", buddy,
newcaps, oldcaps);
}
#endif
}
/**************************************************************************
* Protocol Plugin Subsystem API
**************************************************************************/
PurplePlugin *
purple_find_prpl(const char *id)
{
GList *l;
PurplePlugin *plugin;
g_return_val_if_fail(id != NULL, NULL);
for (l = purple_plugins_get_protocols(); l != NULL; l = l->next) {
plugin = (PurplePlugin *)l->data;
if (purple_strequal(plugin->info->id, id))
return plugin;
}
return NULL;
}