Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
BitchX
GitHub Repository: BitchX/BitchX1.3
Path: blob/master/dll/aim/toc/server.c
1074 views
1
#include <time.h>
2
#include <stdio.h>
3
#include <string.h>
4
#include <sys/types.h>
5
#include <sys/stat.h>
6
#include <sys/time.h>
7
#include <unistd.h>
8
#include "toc.h"
9
10
static time_t lastsent = 0;
11
time_t login_time = 0;
12
int my_evil;
13
int is_idle = 0;
14
int lag_ms = 0;
15
int time_to_idle = 600;
16
int is_away = 0;
17
static struct timeval lag_tv;
18
19
void serv_add_buddy(char *name)
20
{
21
char buf[1024];
22
snprintf(buf, sizeof(buf), "toc_add_buddy %s", normalize(name));
23
sflap_send(buf, -1, TYPE_DATA);
24
}
25
26
void serv_remove_buddy(char *name)
27
{
28
char buf[1024];
29
snprintf(buf, sizeof(buf), "toc_remove_buddy %s", normalize(name));
30
sflap_send(buf, -1, TYPE_DATA);
31
}
32
33
int serv_got_im(char *name, char *message, int away)
34
{
35
/*
36
struct conversation *cnv;
37
int is_idle = -1;
38
*/
39
char *nname;
40
41
nname = strdup(normalize(name));
42
43
if (!strcasecmp(normalize(name), nname)) {
44
if (!strcmp(message, LAGOMETER_STR)) {
45
struct timeval tv;
46
int ms;
47
48
gettimeofday(&tv, NULL);
49
50
ms = 1000000 * (tv.tv_sec - lag_tv.tv_sec);
51
52
ms += tv.tv_usec - lag_tv.tv_usec;
53
54
lag_ms = ms;
55
use_handler(TOC_HANDLE,TOC_LAG_UPDATE,NULL);
56
57
return -1;
58
}
59
60
}
61
62
/*
63
cnv = find_conversation(name);
64
65
if (cnv == NULL)
66
cnv = new_conversation(name);
67
68
if (away)
69
write_to_conv(cnv, message, WFLAG_AUTO | WFLAG_RECV);
70
else
71
write_to_conv(cnv, message, WFLAG_RECV);
72
73
74
if (cnv->makesound && extrasounds)
75
play_sound(RECEIVE);
76
77
78
if (awaymessage != NULL) {
79
time_t t;
80
81
time(&t);
82
83
84
if ((cnv == NULL) || (t - cnv->sent_away) < 120)
85
return;
86
87
cnv->sent_away = t;
88
89
if (is_idle)
90
is_idle = -1;
91
92
93
serv_send_im(name, awaymessage->message, 1);
94
95
if (is_idle == -1)
96
is_idle = 1;
97
98
write_to_conv(cnv, awaymessage->message, WFLAG_SEND | WFLAG_AUTO);
99
}
100
*/
101
toc_debug_printf("Received im from %s : %s\n",name,message);
102
return 1;
103
}
104
105
void serv_finish_login()
106
{
107
char *buf;
108
109
buf = strdup(user_info);
110
escape_text(buf);
111
serv_set_info(buf);
112
free(buf);
113
114
use_handler(TOC_HANDLE,TOC_REINSTALL_TIMER,NULL);
115
116
time(&login_time);
117
serv_touch_idle();
118
119
serv_add_buddy(aim_username);
120
121
if (!registered)
122
{
123
/* show_register_dialog(); */
124
save_prefs();
125
}
126
}
127
128
void serv_add_buddies(LL buddies)
129
{
130
char buf[MSG_LEN];
131
LLE e;
132
int n, num = 0;
133
134
n = snprintf(buf, sizeof(buf), "toc_add_buddy");
135
for ( TLL(buddies,e) ) {
136
if (num == 20) {
137
sflap_send(buf, -1, TYPE_DATA);
138
n = snprintf(buf, sizeof(buf), "toc_add_buddy");
139
num = 0;
140
}
141
++num;
142
n += snprintf(buf + n, sizeof(buf) - n, " %s", normalize((char *)e->key));
143
}
144
sflap_send(buf, -1, TYPE_DATA);
145
}
146
147
148
149
150
void serv_got_update(char *name, int loggedin, int evil, time_t signon, time_t idle, int type)
151
{
152
struct buddy *b;
153
char *nname,**args;
154
155
b = find_buddy(name);
156
157
nname = strdup(normalize(name));
158
if (!strcasecmp(nname, normalize(aim_username))) {
159
/*
160
correction_time = (int)(signon - login_time);
161
update_all_buddies();
162
*/
163
my_evil = evil;
164
if (!b)
165
return;
166
}
167
168
169
if (!b) {
170
toc_debug_printf("Error, no such person\n");
171
return;
172
}
173
174
/* This code will 'align' the name from the TOC */
175
/* server with what's in our record. We want to */
176
/* store things how THEY want it... */
177
/*
178
if (strcmp(name, b->name)) {
179
GList *cnv = conversations;
180
struct conversation *cv;
181
182
char *who = g_malloc(80);
183
184
strcpy(who, normalize(name));
185
186
while(cnv) {
187
cv = (struct conversation *)cnv->data;
188
if (!strcasecmp(who, normalize(cv->name))) {
189
g_snprintf(cv->name, sizeof(cv->name), "%s", name);
190
if (find_log_info(name) || log_all_conv)
191
g_snprintf(who, 63, LOG_CONVERSATION_TITLE, name);
192
else
193
g_snprintf(who, 63, CONVERSATION_TITLE, name);
194
gtk_window_set_title(GTK_WINDOW(cv->window), who);
195
*/
196
/* no free 'who', set_title needs it.
197
*/
198
/*
199
break;
200
}
201
cnv = cnv->next;
202
}
203
204
g_snprintf(b->name, sizeof(b->name), "%s", name); */
205
/*gtk_label_set_text(GTK_LABEL(b->label), b->name);*/
206
207
/* okay lets save the new config... */
208
209
/* } */
210
211
b->idle = idle;
212
b->evil = evil;
213
b->uc = type;
214
215
b->signon = signon;
216
217
if (loggedin) {
218
if (!b->present) {
219
b->present = 1;
220
args = (char **) malloc(sizeof(char *)*1);
221
args[0] = strdup(b->name);
222
use_handler(TOC_HANDLE,TOC_BUDDY_LOGGED_ON,args);
223
free(args[0]); free(args);
224
/* do_pounce(b->name); */
225
}
226
} else {
227
if ( b->present ) {
228
args = (char **) malloc(sizeof(char *)*1);
229
args[0] = strdup(b->name);
230
use_handler(TOC_HANDLE,TOC_BUDDY_LOGGED_OFF,args);
231
free(args[0]); free(args);
232
}
233
b->present = 0;
234
}
235
/*
236
set_buddy(b);
237
*/
238
}
239
240
241
void serv_send_im(char *name, char *message)
242
{
243
char buf[MSG_LEN - 7];
244
snprintf(buf, MSG_LEN - 8, "toc_send_im %s \"%s\"%s", normalize(name),
245
message, ((is_away) ? " auto" : ""));
246
sflap_send(buf, strlen(buf), TYPE_DATA);
247
248
if (!is_away && strcasecmp(message,LAGOMETER_STR) != 0)
249
serv_touch_idle();
250
251
}
252
253
void serv_close()
254
{
255
toc_close();
256
}
257
258
void serv_save_config()
259
{
260
char *buf = malloc(BUF_LONG);
261
char *buf2 = malloc(MSG_LEN);
262
toc_build_config(buf, BUF_LONG / 2);
263
snprintf(buf2, MSG_LEN, "toc_set_config {%s}", buf);
264
sflap_send(buf2, -1, TYPE_DATA);
265
free(buf2);
266
free(buf);
267
}
268
269
void serv_warn(char *name, int anon)
270
{
271
char *send = malloc(256);
272
snprintf(send, 255, "toc_evil %s %s", name,
273
((anon) ? "anon" : "norm"));
274
sflap_send(send, -1, TYPE_DATA);
275
free(send);
276
}
277
278
void serv_add_permit(char *name)
279
{
280
char buf[1024];
281
snprintf(buf, sizeof(buf), "toc_add_permit %s", normalize(name));
282
sflap_send(buf, -1, TYPE_DATA);
283
}
284
285
286
287
void serv_add_deny(char *name)
288
{
289
char buf[1024];
290
snprintf(buf, sizeof(buf), "toc_add_deny %s", normalize(name));
291
sflap_send(buf, -1, TYPE_DATA);
292
}
293
294
295
296
void serv_set_permit_deny()
297
{
298
char buf[MSG_LEN];
299
char type[30];
300
int at;
301
LLE t;
302
LL l;
303
/* FIXME! We flash here. */
304
if (permdeny == PERMIT_PERMITALL || permdeny == PERMIT_PERMITSOME) {
305
strcpy(type,"toc_add_permit");
306
l = permit;
307
} else {
308
strcpy(type,"toc_add_deny");
309
l = deny;
310
311
}
312
sflap_send(type, -1, TYPE_DATA);
313
314
if ( permdeny == PERMIT_DENYALL || permdeny == PERMIT_PERMITALL ) {
315
if ( permdeny == PERMIT_DENYALL )
316
strcpy(type,"toc_add_permit");
317
else
318
strcpy(type,"toc_add_deny");
319
sflap_send(type, -1, TYPE_DATA);
320
return;
321
}
322
323
at = snprintf(buf, sizeof(buf), "%s",type);
324
for ( TLL(l,t) ) {
325
at += snprintf(&buf[at], sizeof(buf) - at, " %s", normalize(t->key));
326
}
327
buf[at] = 0;
328
sflap_send(buf, -1, TYPE_DATA);
329
330
}
331
332
void serv_got_joined_chat(int id, char *name)
333
{
334
struct buddy_chat *b;
335
336
b = (struct buddy_chat *) malloc(sizeof(struct buddy_chat));
337
b->ignored = CreateLL();
338
b->in_room = CreateLL();
339
b->id = id;
340
b->init_chat = 0;
341
snprintf(b->name, 80, "%s", name);
342
343
AddToLL(buddy_chats,name,b);
344
}
345
346
void serv_got_chat_left(int id)
347
{
348
LLE t;
349
struct buddy_chat *b = NULL;
350
351
for ( TLL(buddy_chats,t) ) {
352
b = (struct buddy_chat *)t->data;
353
if (id == b->id) {
354
break;
355
}
356
b = NULL;
357
}
358
359
if (!b)
360
return;
361
362
RemoveFromLLByKey(buddy_chats,b->name);
363
364
toc_debug_printf("leaking memory in serv_got_chat_left");
365
}
366
367
void serv_accept_chat(int i)
368
{
369
char *buf = malloc(256);
370
snprintf(buf, 255, "toc_chat_accept %d", i);
371
sflap_send(buf, -1, TYPE_DATA);
372
free(buf);
373
}
374
375
void serv_join_chat(int exchange, char *name)
376
{
377
char buf[BUF_LONG];
378
snprintf(buf, sizeof(buf)/2, "toc_chat_join %d \"%s\"", exchange, name);
379
sflap_send(buf, -1, TYPE_DATA);
380
}
381
382
void serv_chat_invite(int id, char *message, char *name)
383
{
384
char buf[BUF_LONG];
385
snprintf(buf, sizeof(buf)/2, "toc_chat_invite %d \"%s\" %s", id, message, normalize(name));
386
sflap_send(buf, -1, TYPE_DATA);
387
}
388
389
void serv_chat_leave(int id)
390
{
391
char *buf = malloc(256);
392
snprintf(buf, 255, "toc_chat_leave %d", id);
393
sflap_send(buf, -1, TYPE_DATA);
394
free(buf);
395
}
396
397
void serv_chat_whisper(int id, char *who, char *message)
398
{
399
char buf2[MSG_LEN];
400
snprintf(buf2, sizeof(buf2), "toc_chat_whisper %d %s \"%s\"", id, who, message);
401
sflap_send(buf2, -1, TYPE_DATA);
402
}
403
404
void serv_chat_send(int id, char *message)
405
{
406
char buf[MSG_LEN];
407
snprintf(buf, sizeof(buf), "toc_chat_send %d \"%s\"",id, message);
408
sflap_send(buf, -1, TYPE_DATA);
409
serv_touch_idle();
410
}
411
412
void serv_chat_warn(int id, char *user, int anon) {
413
char send[256];
414
snprintf(send, 255, "toc_chat_evil %d %s %s", id, user, ((anon) ? "anon" : "norm"));
415
sflap_send(send, -1, TYPE_DATA);
416
}
417
418
419
void serv_get_dir(char *name) {
420
char buf[MSG_LEN];
421
snprintf(buf, MSG_LEN, "toc_get_dir %s", normalize(name));
422
sflap_send(buf, -1, TYPE_DATA);
423
}
424
425
void serv_set_dir(char *first, char *middle, char *last, char *maiden,
426
char *city, char *state, char *country, char *email, int web)
427
{
428
char buf2[BUF_LEN], buf[BUF_LEN];
429
/* sending email seems to mess this up? */
430
snprintf(buf2, sizeof(buf2), "%s:%s:%s:%s:%s:%s:%s:%s", first,
431
middle, last, maiden, city, state, country,
432
(web == 1) ? "Y" : "");
433
escape_text(buf2);
434
snprintf(buf, sizeof(buf), "toc_set_dir %s", buf2);
435
sflap_send(buf, -1, TYPE_DATA);
436
}
437
438
void serv_dir_search(char *first, char *middle, char *last, char *maiden,
439
char *city, char *state, char *country, char *email)
440
{
441
char buf[BUF_LONG];
442
snprintf(buf, sizeof(buf)/2, "toc_dir_search %s:%s:%s:%s:%s:%s:%s:%s", first, middle, last, maiden, city, state, country, email);
443
toc_debug_printf("Searching for: %s,%s,%s,%s,%s,%s,%s\n", first, middle, last, maiden, city, state, country);
444
sflap_send(buf, -1, TYPE_DATA);
445
}
446
447
448
void serv_get_info(char *name)
449
{
450
char buf[MSG_LEN];
451
snprintf(buf, MSG_LEN, "toc_get_info %s", normalize(name));
452
sflap_send(buf, -1, TYPE_DATA);
453
}
454
455
void serv_set_info(char *info)
456
{
457
char buf[MSG_LEN];
458
snprintf(buf, sizeof(buf), "toc_set_info \"%s\n\"", info);
459
sflap_send(buf, -1, TYPE_DATA);
460
}
461
462
void serv_touch_idle() {
463
/* Are we idle? If so, not anymore */
464
if (is_idle > 0) {
465
is_idle = 0;
466
serv_set_idle(0);
467
use_handler(TOC_HANDLE,TOC_WENT_IDLE,NULL);
468
}
469
time(&lastsent);
470
}
471
472
void serv_set_idle(int time)
473
{
474
char buf[256];
475
snprintf(buf, sizeof(buf), "toc_set_idle %d", time);
476
sflap_send(buf, -1, TYPE_DATA);
477
}
478
479
int check_idle() {
480
time_t t;
481
482
time(&t);
483
484
use_handler(TOC_HANDLE,TOC_REINSTALL_TIMER,NULL);
485
gettimeofday(&lag_tv, NULL);
486
serv_send_im(aim_username, LAGOMETER_STR);
487
488
/*
489
if (report_idle != IDLE_GAIM)
490
return TRUE;
491
*/
492
493
494
if (is_idle || is_away)
495
return 1;
496
497
toc_debug_printf("time_to_idle = %d, current idle = %d, t = %d, last_sent = %d",time_to_idle,(t - lastsent),t,lastsent);
498
if ((t - lastsent) > time_to_idle) { /* 10 minutes! */
499
serv_set_idle((int)t - lastsent);
500
toc_debug_printf("went idle wieth time_to_idle = %d",time_to_idle);
501
use_handler(TOC_HANDLE,TOC_WENT_IDLE,NULL);
502
is_idle = 1;
503
}
504
505
506
return 1;
507
508
}
509
510
void serv_set_away(char *message) {
511
char buf[MSG_LEN];
512
if ( ! is_away && message ) {
513
is_away = 1;
514
snprintf(buf, MSG_LEN, "toc_set_away \"%s\"", message);
515
} else {
516
is_away = 0;
517
snprintf(buf, MSG_LEN, "toc_set_away");
518
}
519
sflap_send(buf, -1, TYPE_DATA);
520
}
521
522