-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpurpled.c
2317 lines (1976 loc) · 60.9 KB
/
purpled.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
/*
* purpleD
*
* A daemon program to provide a socket interface into libpurple.
*
* 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 "purple.h"
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <pwd.h>
#include <glib.h>
#include <glib/gstdio.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <argp.h>
#include "defines.h"
/* --version string */
const char *argp_program_version = PURPLED_VERSION;
/* disable message history? */
int disable_history;
/* push accounts to clients? */
int push_accounts;
/* filter own messages? */
int filter_own;
/* logging functions */
void log_debug(const char *format, ...);
void log_info(const char *format, ...);
void log_warn(const char *format, ...);
void log_error(const char *format, ...);
void client_command(client* ptr, char *mesg);
void purpld_process_client(client* ptr);
void purpld_client_send(client* ptr, const char *mesg);
static void quit_purpled();
int total_c = 0;
int listenfd;
GList *clients = NULL;
struct purpld_dirs purpld_dirs;
/**
* The following eventloop functions are used in both pidgin and purple-text.
* If your application uses glib mainloop, you can safely use this verbatim.
*/
#define PURPLE_GLIB_READ_COND (G_IO_IN | G_IO_HUP | G_IO_ERR)
#define PURPLE_GLIB_WRITE_COND (G_IO_OUT | G_IO_HUP | G_IO_ERR | G_IO_NVAL)
typedef struct _PurpleGLibIOClosure {
PurpleInputFunction function;
guint result;
gpointer data;
} PurpleGLibIOClosure;
static void purple_glib_io_destroy(gpointer data)
{
g_free(data);
}
static gboolean purple_glib_io_invoke(GIOChannel *source,
GIOCondition condition, gpointer data)
{
PurpleGLibIOClosure *closure = data;
PurpleInputCondition purple_cond = 0;
if (condition & PURPLE_GLIB_READ_COND)
purple_cond |= PURPLE_INPUT_READ;
if (condition & PURPLE_GLIB_WRITE_COND)
purple_cond |= PURPLE_INPUT_WRITE;
closure->function(closure->data, g_io_channel_unix_get_fd(source),
purple_cond);
return TRUE;
}
static guint glib_input_add(gint fd, GIOCondition cond, gpointer function)
{
GIOChannel *channel;
channel = g_io_channel_unix_new(fd);
g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, cond,
function, 0, purple_glib_io_destroy);
g_io_channel_unref(channel);
return 0;
}
static guint purple_glib_input_add(gint fd, PurpleInputCondition condition,
PurpleInputFunction function, gpointer data)
{
PurpleGLibIOClosure *closure = g_new0(PurpleGLibIOClosure, 1);
GIOChannel *channel;
GIOCondition cond = 0;
closure->function = function;
closure->data = data;
if (condition & PURPLE_INPUT_READ)
cond |= PURPLE_GLIB_READ_COND;
if (condition & PURPLE_INPUT_WRITE)
cond |= PURPLE_GLIB_WRITE_COND;
channel = g_io_channel_unix_new(fd);
closure->result = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, cond,
purple_glib_io_invoke, closure,
purple_glib_io_destroy);
g_io_channel_unref(channel);
return closure->result;
}
static PurpleEventLoopUiOps glib_eventloops =
{
g_timeout_add,
g_source_remove,
purple_glib_input_add,
g_source_remove,
NULL,
#if GLIB_CHECK_VERSION(2,14,0)
g_timeout_add_seconds,
#else
NULL,
#endif
/* padding */
NULL,
NULL,
NULL
};
/*** End of the eventloop functions. ***/
void purpld_inform_client(PurpleAccount *account, char *message) {
GList *iter;
for (iter = clients; iter; iter = iter->next) {
client *cli = iter->data;
// TODO: if account IN cli->accounts THEN
purpld_client_send(cli, message);
// ENDIF
}
}
/*** Account uiops ***/
static void*
purpld_accounts_request_authorize(PurpleAccount *account,
const char *remote_user, const char *id,
const char *alias, const char *message,
gboolean on_list,
PurpleAccountRequestAuthorizationCb auth_cb,
PurpleAccountRequestAuthorizationCb deny_cb,
void *user_data)
{
auth_cb(user_data);
return NULL;
}
static PurpleAccountUiOps purpld_accounts_uiops =
{
NULL, //notify_added
NULL, //status changed
NULL, //request_add,
purpld_accounts_request_authorize,
NULL, //close_account_request
NULL,
NULL,
NULL,
NULL
};
/*** Notify uiops ***/
static void *
purpld_notify_userinfo(PurpleConnection *gc, const char *who,
PurpleNotifyUserInfo *user_info)
{
GList *iter;
int n;
PurpleAccount *account = purple_connection_get_account(gc);
n = g_list_index(purple_accounts_get_all(), account);
for (iter = purple_notify_user_info_get_entries(user_info); iter;
iter=iter->next) {
PurpleNotifyUserInfoEntry *ent = iter->data;
gchar *info = g_strdup_printf(
"info: %d %s %s = %s\r\n", n, who,
purple_notify_user_info_entry_get_label(ent),
purple_notify_user_info_entry_get_value(ent));
//TODO: resolve bottle-neck - inform_client shouldn't be in loop
purpld_inform_client(account, info);
g_free(info);
}
return NULL;
}
static PurpleNotifyUiOps purpld_notify_uiops =
{
NULL, /* message */
NULL, /* email */
NULL, /* emails */
NULL, /* formatted */
NULL, /* search */
NULL, /* search new rows */
purpld_notify_userinfo, /* user_info ! */
NULL, /* uri */
NULL, /* close! */
NULL,
NULL,
NULL,
NULL
};
/*** Request uiops ***/
static void*
purpld_request_file(const char *title, const char *filename,
gboolean savedialog, GCallback ok_cb, GCallback cancel_cb,
PurpleAccount *account, const char *who,
PurpleConversation *conv, void *user_data)
{
PurpleXfer *xfer = user_data;
PurpleRequestFileCb callback = (gpointer)ok_cb;
int n = 0;
gchar *dir;
dir = g_build_filename(purpld_dirs.file_dir, account->alias, who, NULL);
if (!g_file_test(dir, G_FILE_TEST_IS_DIR)) {
g_mkdir_with_parents(dir, S_IRUSR | S_IWUSR | S_IXUSR);
}
gchar *path;
path = g_build_filename(dir, xfer->filename, NULL);
n = g_list_index(purple_accounts_get_all(), account);
gchar *msg = g_strdup_printf("info: %d) FILE %d %s %s\r\n", n,
(int) time(NULL), who, path);
purpld_inform_client(account, msg) ;
if (!conv) {
conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM,
who, account);
if (!conv)
conv = purple_conversation_new(PURPLE_CONV_TYPE_IM,
account, who);
}
purple_conversation_write(conv, who, msg, PURPLE_MESSAGE_RECV,
time(NULL));
g_free(msg);
callback(xfer, path);
g_free(path);
g_free(dir);
return NULL;
}
static void*
purpld_request_input(const char *title, const char *primary,
const char *secondary, const char *default_value,
gboolean multiline, gboolean masked, gchar *hint,
const char *ok_text, GCallback ok_cb,
const char *cancel_text, GCallback cancel_cb,
PurpleAccount *account, const char *who,
PurpleConversation *conv, void *user_data)
{
printf("libpurple input request: \"%s - %s - %s - %s\"\n", title,
primary, secondary, default_value);
PurpleRequestInputCb callback = (PurpleRequestInputCb)ok_cb;
callback(user_data, default_value);
return NULL;
}
static void*
purpld_request_action(const char *title, const char *primary,
const char *secondary, int default_value,
PurpleAccount *account, const char *who,
PurpleConversation *conv, void *user_data,
size_t actioncount, va_list actions)
{
gboolean done = FALSE;
int i;
printf("libpurple request:\"%s - %s - %s\"", title, primary,
secondary);
for (i = 0; i < actioncount; i++)
{
const char *text = va_arg(actions, const char *);
PurpleRequestActionCb callback = va_arg(actions,
PurpleRequestActionCb);
/* Hack -- what if it's not called accept next time? */
if (!strcmp(text, "Accept") || !strcmp(text, "_Accept") ||
!strcmp(text, "Yes") || !strcmp(text, "_Yes")) {
printf(" [ok] \n");
callback(user_data, i);
done = TRUE;
break;
}
}
if (!done) {
printf (" [fail] \n");
}
return NULL;
}
static PurpleRequestUiOps purpld_request_uiops =
{
purpld_request_input,
NULL,
purpld_request_action,
NULL,
purpld_request_file,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
};
/*** Conversation uiops ***/
static void
purpld_create_conv(PurpleConversation *conv)
{
/* if disable-history is set, turn off logging */
if (disable_history) {
purple_conversation_set_logging(conv, FALSE);
}
}
static void
purpld_write_conv(PurpleConversation *conv, const char *who, const char *alias,
const char *message, PurpleMessageFlags flags,
time_t mtime)
{
gchar *text, *escaped_newlines, *escaped;
PurpleAccount *account;
gchar *buf;
int n;
account = purple_conversation_get_account(conv);
/* remove all html tags, then escape all special chars */
text = purple_markup_strip_html(message);
escaped_newlines = purple_markup_escape_text(text, strlen(text));
escaped = purple_strdup_withhtml(escaped_newlines);
n = g_list_index(purple_accounts_get_all(), account);
if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_CHAT) {
PurpleConvChat *data = purple_conversation_get_chat_data(conv);
const char *nick = purple_conv_chat_get_nick(data);
/* rewrite sender of own messages to "<self>" */
if (purple_strequal(who, nick)) {
who = "<self>";
}
buf = g_strdup_printf("chat: msg: %d %s %d %s %s\r\n", n,
purple_conversation_get_name(conv),
(int) mtime, who, escaped);
/* only show messages from others, not own messages */
if (!filter_own || !purple_strequal(who, nick))
purpld_inform_client(account, buf);
} else {
buf = g_strdup_printf("message: %d %s %d %s %s\r\n", n,
purple_conversation_get_name(conv),
(int) mtime, who, escaped);
purpld_inform_client(account, buf);
}
/* free all string buffers */
g_free(escaped_newlines);
g_free(escaped);
g_free(text);
g_free(buf);
}
/* chat users have joined a chat room */
static void
purpld_chat_add_users(PurpleConversation *conv, GList *cbuddies,
gboolean new_arrivals)
{
PurpleAccount *account;
int account_id;
GList *iter;
if (!new_arrivals)
return;
/* get account and its id */
account = purple_conversation_get_account(conv);
account_id = g_list_index(purple_accounts_get_all(), account);
/* send list of new chat users to client */
for (iter = g_list_first(cbuddies); iter; iter=iter->next) {
PurpleConvChatBuddy *user = iter->data;
gchar *reply;
/* construct message and send it */
reply = g_strdup_printf("chat: user: %d %s %s %s %s\r\n",
account_id, conv->name,
user->alias ? user->alias : user->name,
user->name, "join");
//TODO: resolve bottle-neck - inform_client shouldn't be in loop
purpld_inform_client(account, reply);
g_free(reply);
}
}
/* chat users have left a chat room */
static void
purpld_chat_remove_users(PurpleConversation *conv, GList *users)
{
PurpleAccount *account;
int account_id;
GList *iter;
/* get account and its id */
account = purple_conversation_get_account(conv);
account_id = g_list_index(purple_accounts_get_all(), account);
/* send list of parted chat users to client */
for (iter = g_list_first(users); iter; iter=iter->next) {
char *user_name = iter->data;
gchar *reply;
/* construct message and send it */
reply = g_strdup_printf("chat: user: %d %s %s %s %s\r\n",
account_id, conv->name, user_name,
user_name, "part");
//TODO: resolve bottle-neck - inform_client shouldn't be in loop
purpld_inform_client(account, reply);
g_free(reply);
}
}
/* chat user has changed its name */
static void
purpld_chat_rename_user(PurpleConversation *conv, const char *old_name, const
char *new_name, const char *new_alias)
{
PurpleAccount *account;
int account_id;
gchar *reply;
/* get account and its id */
account = purple_conversation_get_account(conv);
account_id = g_list_index(purple_accounts_get_all(), account);
/* construct message and send it */
reply = g_strdup_printf("chat: user: %d %s %s %s %s\r\n",
account_id, conv->name,
new_alias ? new_alias : new_name,
old_name, "rename");
//TODO: resolve bottle-neck - inform_client shouldn't be in loop
purpld_inform_client(account, reply);
g_free(reply);
}
static PurpleConversationUiOps purpld_conv_uiops =
{
purpld_create_conv, /* create_conversation */
NULL, /* destroy_conversation */
NULL, /* write_chat */
NULL, /* write_im */
purpld_write_conv, /* write_conv */
purpld_chat_add_users, /* chat_add_users */
purpld_chat_rename_user, /* chat_rename_user */
purpld_chat_remove_users, /* chat_remove_users */
NULL, /* chat_update_user */
NULL, /* present */
NULL, /* has_focus */
NULL, /* custom_smiley_add */
NULL, /* custom_smiley_write */
NULL, /* custom_smiley_close */
NULL, /* send_confirm */
NULL,
NULL,
NULL,
NULL
};
char *strleft(char *str, int len, int off) {
int i;
for (i = off; i < len; i++)
str[i-off] = str[i];
str[len-off] = '\0';
return str;
}
int strpos(char *str, char substr) {
int i;
for (i=0; str[i]; i++)
if (str[i] == substr)
return i;
return -1;
}
int strrpos(char *str, char substr, int len) {
int i;
for (i=len; i>0; i--)
if (str[i] == substr)
return i;
return -1;
}
int strcrep(char *str, char src, char dst) {
char *ptr = str;
while (*ptr != '\0') {
if(*ptr == src)
*ptr = dst;
ptr++;
}
return 0;
}
static gint find_account(gconstpointer a, gconstpointer b) {
PurpleAccount *account = (PurpleAccount *)a;
if (account->alias && !g_strcmp0(account->alias, (char*)b)) return 0;
return 1;
}
static gint find_client(gconstpointer a, gconstpointer b) {
int* connfd2 = (int*) b;
client *cli = (client *)a;
if (cli->connfd == *connfd2) return 0;
return 1;
}
void client_set_instance(client* ptr) {
GList *iter;
client *cli;
int min_i = 0, min_a = 0;
time_t max_c = 0;
for (iter = g_list_first(clients);iter;iter = iter->next) {
cli = iter->data;
if (cli != ptr && !strcmp(cli->user, ptr->user)) {
if (cli->instance > min_i)
min_i = cli->instance;
if (cli->instance < min_a || min_a == 0)
min_a = cli->instance;
if (cli->lastcollect > max_c)
max_c = cli->lastcollect;
}
}
if (!max_c)
max_c = time(NULL);
ptr->lastcollect = max_c;
if (min_a > 1)
ptr->instance = min_a - 1;
else
ptr->instance = min_i + 1;
}
void _create_account_msg(char *buf, PurpleAccount *account) {
PurplePresence *pres = purple_account_get_presence(account);
PurpleStatus *stat = purple_presence_get_active_status(pres);
sprintf(buf, "account: %d %s %s %s [%s]\r\n",
g_list_index(purple_accounts_get_all(), account),
purple_account_get_alias(account),
purple_account_get_protocol_name(account),
account->username,
(purple_account_is_connected(account) ?
purple_status_get_name(stat) :
(purple_account_is_connecting(account) ? "Connecting" :
"Offline")));
}
gboolean respond_to_login(client* ptr, char *mesg, char **args,
gpointer user_data) {
if (!ptr->auth || !ptr->instance) {
if (!strcasecmp(args[0], "USER") ) {
if (args[1]) {
strcpy(ptr->user, args[1]);
if (args[2]) {
strcpy(ptr->host, args[2]);
if (args[3])
strcpy(ptr->server, args[3]);
}
}
} else {
if (args[1]) {
strcpy(ptr->pass, args[1]);
}
}
}
if (ptr->conntype == CONNECTION_IRC) {
sprintf(mesg, ":%s 001 %s :purpleD\n", ptr->server, ptr->user);
send(ptr->connfd, mesg, strlen(mesg), 0);
sprintf(mesg, ":%s!%s@%s JOIN :&root\n", ptr->user, ptr->host,
ptr->server);
send(ptr->connfd, mesg, strlen(mesg), 0);
sprintf(mesg, ":%s 353 %s = &root :@daemon %s\n", ptr->server,
ptr->user, ptr->user);
send(ptr->connfd, mesg, strlen(mesg), 0);
}
client_set_instance(ptr);
ptr->auth = TRUE;
return TRUE;
}
gboolean respond_irc_generic(client* ptr, char *mesg, char **args,
gpointer user_data) {
if (!strcasecmp(args[0], "NICK") ) {
ptr->conntype = CONNECTION_IRC;
strcpy(ptr->user, args[1]);
} else if (!strcmp(args[0], "PRIVMSG") && !strcmp(args[1], "&root") ) {
char buf[PD_SMALL_BUFFER];
strcpy(buf, args[2]);
if (buf[0] == ':') strleft(buf, strlen(buf),1);
client_command(ptr, buf) ;
} else if (!strcmp(args[0], "PING")) {
sprintf(mesg, "PONG %s\n", args[1]);
send(ptr->connfd, mesg, strlen(mesg), 0);
}
return TRUE;
}
gboolean respond_http_command(client* ptr, char *mesg, char **args,
gpointer user_data) {
char buf[PD_SMALL_BUFFER];
strcpy(buf, args[1]);
client_command(ptr, buf) ;
return TRUE;
}
gboolean respond_http_content(client* ptr, char *mesg, char **args,
gpointer user_data) {
ptr->conntype = CONNECTION_HTTP;
ptr->content_length = atoi(args[1]);
if (strlen(ptr->buffer) == ptr->content_length) {
strcat(ptr->buffer, "\n");
}
purpld_process_client(ptr);
return TRUE;
}
gboolean respond_http_generic(client* ptr, char *mesg, char **args,
gpointer user_data) {
ptr->conntype = CONNECTION_HTTP;
gchar *msg;
msg = g_strdup_printf("%s 200 OK\r\n", args[2]);
purpld_client_send(ptr, msg);
g_free(msg);
msg = g_strdup_printf("Content-type: text/html\r\n\r\n");
purpld_client_send(ptr, msg);
g_free(msg);
/* Get command from GET */
if (strlen(args[1]) > 1) {
char buf[PD_SMALL_BUFFER];
bzero(&buf, PD_SMALL_BUFFER);
strcpy(buf, args[1]);
strcrep(buf, '+', ' ');
strleft(buf, strlen(buf),1);
client_command(ptr, buf) ;
}
ptr->kill = TRUE;
return TRUE;
}
gboolean respond_generic_dummy(client* ptr, char *mesg, char **args,
gpointer user_data) {
int i = 0;
log_info("Command parser sample function.\n");
for (i = 0; args[i]; i++) {
if (ptr->conntype == CONNECTION_IRC && args[i][0] == ':')
strleft(args[i], strlen(args[i]),1);
log_info("%c) %s\n", 63+i, args[i]);
}
return TRUE;
}
/* handle "bye" command, disconnect client */
gboolean respond_command_bye(client* ptr, char *mesg, char **args,
gpointer user_data) {
ptr->kill = TRUE;
return TRUE;
}
/* handle "quit" command, quit purpled */
gboolean respond_command_quit(client* ptr, char *mesg, char **args,
gpointer user_data) {
quit_purpled();
return TRUE;
}
/* handle "help" command, return help message */
gboolean respond_command_help(client* ptr, char *mesg, char **args,
gpointer user_data) {
static char *help_msg =
"List of commands and their description:\n"
"account list\n"
" list all accounts and their account ids.\n"
"account add <protocol> <user> <password>\n"
" add a new account for chat protocol <protocol> with "
"user name <user> and\n"
" the password <password>. The supported chat protocol(s) "
"are backend\n"
" specific. The user name is chat protocol specific. "
"An account id is\n"
" assigned to the account that can be shown with "
"\"account list\".\n"
"account <id> delete\n"
" delete the account with the account id <id>.\n"
"account <id> buddies [online]\n"
" list all buddies on the account with the "
"account id <id>. Optionally, show\n"
" only online buddies with the extra parameter \"online\".\n"
"account <id> collect\n"
" collect all messages received on the account with "
"the account id <id>.\n"
"account <id> send <user> <msg>\n"
" send a message to the user <user> on the account with "
"the account id <id>.\n"
"account <id> status get\n"
" get the status of the account with the account id <id>.\n"
"account <id> status set <status>\n"
" set the status of the account with the account id <id> "
"to <status>.\n"
"account <id> chat list\n"
" list all group chats on the account with "
"the account id <id>.\n"
"account <id> chat join <chat>\n"
" join the group chat <chat> on the account with "
"the account id <id>.\n"
"account <id> chat part <chat>\n"
" leave the group chat <chat> on the account with "
"the account id <id>.\n"
"account <id> chat send <chat> <msg>\n"
" send the message <msg> to the group chat <chat> on "
"the account with the\n"
" account id <id>.\n"
"account <id> chat users <chat>\n"
" list the users in the group chat <chat> on "
"the account with the\n"
" account id <id>.\n"
"account <id> chat invite <chat> <user>\n"
" invite the user <user> to the group chat <chat> on "
"the account with the\n"
" account id <id>.\n"
"version\n"
" get version of the backend\n"
"bye\n"
" disconnect from backend\n"
"quit\n"
" quit backend\n"
"help\n"
" show this help";
gchar *buf;
buf = g_strdup_printf("info: %s\r\n", help_msg);
purpld_client_send(ptr, buf);
g_free(buf);
return TRUE;
}
gboolean respond_command_ver(client* ptr, char *mesg, char **args,
gpointer user_data) {
gchar *buf;
buf = g_strdup_printf("info: %s (libpurple %d.%d.%d)\r\n",
PURPLED_VERSION, PURPLE_MAJOR_VERSION,
PURPLE_MINOR_VERSION, PURPLE_MICRO_VERSION);
purpld_client_send(ptr, buf);
g_free(buf);
return TRUE;
}
gboolean respond_command_who(client* ptr, char *mesg, char **args,
gpointer user_data) {
GList *iter;
client *cli;
gchar *buf;
for (iter = g_list_first(clients);iter;iter = iter->next) {
cli = iter->data;
char *conn_type = (cli->conntype == CONNECTION_IRC ? "irc" :
(cli->conntype == CONNECTION_HTTP ? "http" :
"raw"));
char *cli_addr = inet_ntoa(cli->addr.sin_addr);
buf = g_strdup_printf ("info: %10s/%s%d %s\r\n",
(cli->user[0] ? cli->user : "????"),
conn_type, cli->instance, cli_addr);
purpld_client_send(ptr, buf);
g_free(buf);
}
return TRUE;
}
gboolean respond_account_join(client* ptr, char *mesg, char **args,
gpointer user_data) {
PurpleAccount *account = user_data;
GHashTable *comps = NULL;
PurpleChat *chat;
PurpleConnection *con = purple_account_get_connection(account);
PurplePluginProtocolInfo *info =
PURPLE_PLUGIN_PROTOCOL_INFO(purple_connection_get_prpl(con));
if (!purple_account_is_connected(account))
return TRUE;
if (info->chat_info_defaults != NULL)
comps = info->chat_info_defaults(con, args[1]);
chat = purple_blist_find_chat(account, args[1]);
if (!chat) {
chat = purple_chat_new(account, args[1], comps);
purple_blist_add_chat(chat, NULL, NULL);
} else {
comps = purple_chat_get_components(chat);
}
serv_join_chat(con, comps);
return TRUE;
}
gboolean respond_account_part(client* ptr, char *mesg, char **args,
gpointer user_data) {
PurpleAccount *account = user_data;
PurpleChat *chat = NULL;
PurpleConversation *conv = NULL;
chat = purple_blist_find_chat(account, args[1]);
if (chat) {
purple_blist_remove_chat(chat);
}
conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_CHAT,
args[1], account );
if (conv) {
purple_conversation_destroy(conv);
}
return TRUE;
}
gboolean respond_account_forget(client* ptr, char *mesg, char **args,
gpointer user_data) {
PurpleAccount *account = user_data;
PurpleConnection *con = purple_account_get_connection(account);
PurpleBuddy *buddy;
PurpleGroup *grp = NULL;
if (!con || purple_account_is_connecting(account)) {
return TRUE;
}
buddy = purple_find_buddy(account, args[1]);
if (buddy) {
grp = purple_buddy_get_group(buddy);
purple_account_remove_buddy(account, buddy, grp);
purple_blist_remove_buddy(buddy);
}
return TRUE;
}
gboolean respond_account_send(client* ptr, char *mesg, char **args,
gpointer user_data) {
PurpleAccount *account = user_data;
PurpleConnection *con = purple_account_get_connection(account);
PurpleConversation *conv = NULL;
PurpleBuddy *buddy;
gchar *escaped;
if (!con || purple_account_is_connecting(account)) {
gchar *error = g_strdup_printf(
"error: Failed to message \"%s\": "
"Account %s offline\r\n",
args[1], account->username);
purpld_client_send(ptr, error);
g_free(error);
return TRUE;
}
if (purple_account_get_ui_bool(account, UI_ID, "log_self", FALSE)) {
conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM,
args[1], account);
if (!conv)
conv = purple_find_conversation_with_account(
PURPLE_CONV_TYPE_ANY,
args[1], account);
if (!conv)
conv = purple_conversation_new(PURPLE_CONV_TYPE_IM,
account, args[1]);
}
if (purple_account_get_ui_bool(account, UI_ID, "add_buddy_on_send",
TRUE)) {
buddy = purple_find_buddy(account, args[1]);
if (!buddy) {
buddy = purple_buddy_new(account, args[1], NULL);
purple_blist_add_buddy(buddy, NULL, NULL, NULL);
purple_account_add_buddy(account, buddy);
}
}
PurpleMessageFlags flags = PURPLE_MESSAGE_SEND;
time_t mtime = time(NULL);
/* unescape message and send it */
escaped = purple_unescape_html(args[2]);
serv_send_im(con, args[1], escaped, flags);
g_free(escaped);
if (conv)
purple_conversation_write (conv, args[1], args[2], flags, mtime);
return TRUE;
}
/* get a list of chat rooms */
gboolean respond_account_chat_list(client* ptr, char *mesg, char **args,
gpointer user_data) {
PurpleAccount *account = user_data;
GList *chats = purple_get_chats();
int account_id;
GList *iter;
/* get account id */
account_id = g_list_index(purple_accounts_get_all(), account);
/* get list of chat rooms and send each back as reply */
for (iter = g_list_first(chats); iter; iter=iter->next) {
PurpleConversation *conv = iter->data;
PurpleConvChat *chat_conv;
int conv_acc_id;
gchar *reply;
/* skip other accounts; only show requested account's chats */
conv_acc_id = g_list_index(purple_accounts_get_all(),
conv->account);
if (conv_acc_id != account_id)
continue;
/* construct message and send it */
chat_conv = purple_conversation_get_chat_data(conv);
reply = g_strdup_printf("chat: list: %d %s %s %s\r\n",
conv_acc_id, conv->name, conv->name,
chat_conv->nick);
//TODO: resolve bottle-neck - inform_client shouldn't be in loop
purpld_inform_client(account, reply);
g_free(reply);
}
return TRUE;
}
/* send a message to a chat room */
gboolean respond_account_chat_send(client* ptr, char *mesg, char **args,
gpointer user_data) {
PurpleAccount *account = user_data;
PurpleConnection *con = purple_account_get_connection(account);
PurpleConvChat *chat_conv = NULL;
PurpleConversation *conv = NULL;
gchar *escaped;
int chat_id;
/* check if account is ready */
if (!con || purple_account_is_connecting(account)) {
gchar *error = g_strdup_printf("error: Failed to message "