-
Notifications
You must be signed in to change notification settings - Fork 0
/
aim.c
526 lines (447 loc) · 9.13 KB
/
aim.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
/**
** bsflite - bs-free AIM client
**
** (C) 2003-2007 by Claudio Leite <leitec at leitec dot org>
**
** NO WARRANTY. Read the file COPYING for more details.
**/
#include "bsf.h"
extern struct Conn *conn;
extern struct BuddyList *buddylist;
extern struct Waiting *waiting;
/* PROTO */
void
getmessage(void *c, const char *who, const int automessage, const char *message)
{
char *msg, *tempmsg;
char *sname;
struct Waiting *wtemp, *wptr = NULL;
int offset, foundWaiting = 0;
tempmsg = strip_html(message);
if (tempmsg == NULL)
return;
if (strlen(tempmsg) == 0) {
free(tempmsg);
return;
}
if (conn->netspeak_filter) {
msg = undo_netspeak(tempmsg);
free(tempmsg);
} else {
msg = tempmsg;
}
if (msg == NULL)
return;
if (strlen(msg) == 0) {
free(msg);
return;
}
if (conn->istyping == 0) {
if (conn->lastsn != NULL)
free(conn->lastsn);
conn->lastsn = simplify_sn(who);
}
sname = simplify_sn(who);
if (waiting == NULL) {
waiting = malloc(sizeof(struct Waiting));
wptr = waiting;
} else {
for (wtemp = waiting; wtemp != NULL; wtemp = wtemp->next)
if (imcomm_compare_nicks(c, wtemp->sn, who)) {
foundWaiting = 1;
wptr = wtemp;
break;
}
if (!foundWaiting) {
for (wtemp = waiting; wtemp->next != NULL;
wtemp = wtemp->next);
wtemp->next = malloc(sizeof(struct Waiting));
wptr = wtemp->next;
}
}
if (!foundWaiting) {
wptr->sn = strdup(who);
wptr->next = NULL;
if (conn->isaway && !automessage) {
if ((conn->respond_idle_only && conn->isidle)
|| (!conn->respond_idle_only)) {
imcomm_im_send_message(c, who, conn->awaymsg, 1);
eraseline();
b_echostr_s();
if (conn->timestamps) {
addts();
putchar(' ');
}
printf("Sent auto-response to %s.\n", who);
show_prompt();
}
}
}
#ifdef MESSAGE_QUEUE
if (conn->isaway)
wptr->mqueue = addToMQueue(wptr->mqueue, msg, who);
#endif /* MESSAGE_QUEUE */
eraseline();
if (conn->bell_on_incoming)
putchar('\a');
if (conn->timestamps) {
addts();
putchar(' ');
offset = 11;
} else {
offset = 0;
}
offset += strlen(who) + 2;
if (automessage) {
set_color(COLOR_AUTORESPONSE);
printf("*AUTO RESPONSE* ");
set_color(0);
offset += 16;
}
set_color(COLOR_INCOMING_IM);
printf("%s", who);
set_color(0);
printf(": ");
wordwrap_print(msg, offset);
if (automessage)
log_event(EVENT_IM_AUTORESPONSE, sname, msg);
else
log_event(EVENT_IM, sname, msg);
free(msg);
free(sname);
show_prompt();
}
/* PROTO */
void
buddy_idle(void *c, const char *who, long idletime)
{
struct BuddyList *trav;
int changed = 1;
char *sn = simplify_sn(who);
trav = buddylist;
while (trav != NULL) {
if (strcmp(trav->sn, sn) == 0) {
trav->idletime = idletime;
if (idletime >= 10) {
if (trav->idle)
changed = 0;
trav->idle = 1;
} else {
if (trav->idle == 0)
changed = 0;
trav->idle = 0;
}
break;
}
trav = trav->next;
}
free(sn);
/*
* in case for whatever reason the buddy isn't in the list (shouldn't
* happen)
*/
if (trav == NULL) {
return;
}
if (!changed) {
return;
}
if (conn->squelchidle)
return;
eraseline();
b_echostr();
if (conn->timestamps) {
putchar(' ');
addts();
}
set_color(COLOR_BUDDY_IDLE);
printf(" %s ", who);
set_color(0);
printf("is %s idle.\n", (trav->idle ? "now" : "no longer"));
show_prompt();
return;
}
/* PROTO */
void
buddy_away(void *c, const char *who)
{
struct BuddyList *trav;
char *sn = simplify_sn(who);
trav = buddylist;
while (trav != NULL) {
if (strcmp(trav->sn, sn) == 0) {
trav->away = 1;
break;
}
trav = trav->next;
}
free(sn);
if (conn->squelchaway)
return;
eraseline();
b_echostr();
if (conn->timestamps) {
putchar(' ');
addts();
}
set_color(COLOR_BUDDY_AWAY);
printf(" %s ", who);
set_color(0);
printf("is away.\n");
show_prompt();
return;
}
/* PROTO */
void
buddy_unaway(void *c, const char *who)
{
struct BuddyList *trav;
char *sn = simplify_sn(who);
trav = buddylist;
while (trav != NULL) {
if (strcmp(sn, trav->sn) == 0) {
trav->away = 0;
break;
}
trav = trav->next;
}
free(sn);
if (conn->squelchaway)
return;
eraseline();
b_echostr();
if (conn->timestamps) {
putchar(' ');
addts();
}
set_color(COLOR_BUDDY_AWAY);
printf(" %s ", who);
set_color(0);
printf("is no longer away.\n");
show_prompt();
return;
}
/* PROTO */
void
buddy_online(void *c, const char *who)
{
struct BuddyList *trav, *newbuddy;
char *sname;
trav = buddylist;
sname = simplify_sn(who);
if (buddylist == NULL) {
buddylist = malloc(sizeof(struct BuddyList));
newbuddy = buddylist;
newbuddy->prev = NULL;
newbuddy->next = NULL;
} else {
for (trav = buddylist; trav != NULL; trav = trav->next) {
if (strcmp(sname, trav->sn) < 0)
break;
}
newbuddy = malloc(sizeof(struct BuddyList));
if (trav == NULL) { /* if it's the last entry */
for (trav = buddylist; trav->next != NULL; trav = trav->next);
trav->next = newbuddy;
newbuddy->prev = trav;
newbuddy->next = NULL;
} else {
if (trav == buddylist) {
buddylist->prev = newbuddy;
newbuddy->prev = NULL;
newbuddy->next = buddylist;
buddylist = newbuddy;
} else {
trav->prev->next = newbuddy;
newbuddy->prev = trav->prev;
newbuddy->next = trav;
trav->prev = newbuddy;
}
}
}
newbuddy->sn = strdup(sname);
newbuddy->formattedsn = strdup(who);
newbuddy->away = 0;
newbuddy->idle = 0;
conn->buddiesonline++;
if (conn->squelchconnect) {
free(sname);
return;
}
eraseline();
b_echostr();
if (conn->timestamps) {
putchar(' ');
addts();
}
set_color(COLOR_BUDDY_SIGNON);
printf(" %s ", who);
set_color(0);
printf("is now online.\n");
log_event(EVENT_SIGNON, sname, NULL);
free(sname);
show_prompt();
}
/* PROTO */
void
remove_from_list(const char *who)
{
char *sname;
struct BuddyList *trav;
sname = simplify_sn(who);
trav = buddylist;
while (trav != NULL) {
if (strcmp(trav->sn, sname) == 0) {
conn->buddiesonline--;
if (trav->prev == NULL) {
buddylist = buddylist->next;
if (buddylist != NULL)
buddylist->prev = NULL;
free(trav->sn);
free(trav->formattedsn);
free(trav);
break;
} else {
trav->prev->next = trav->next;
if (trav->next != NULL)
trav->next->prev = trav->prev;
free(trav->sn);
free(trav->formattedsn);
free(trav);
break;
}
}
trav = trav->next;
}
free(sname);
}
/* PROTO */
void
buddy_offline(void *c, const char *who)
{
char *sname;
int found = 0;
struct BuddyList *trav;
sname = simplify_sn(who);
trav = buddylist;
while (trav != NULL) {
if (strcmp(trav->sn, sname) == 0) {
found = 1;
if (trav->prev == NULL) {
buddylist = buddylist->next;
if (buddylist != NULL)
buddylist->prev = NULL;
free(trav->sn);
free(trav->formattedsn);
free(trav);
break;
} else {
trav->prev->next = trav->next;
if (trav->next != NULL)
trav->next->prev = trav->prev;
free(trav->sn);
free(trav->formattedsn);
free(trav);
break;
}
}
trav = trav->next;
}
if (!found) {
free(sname);
return;
}
conn->buddiesonline--;
if (conn->squelchconnect) {
free(sname);
return;
}
eraseline();
b_echostr();
if (conn->timestamps) {
putchar(' ');
addts();
}
set_color(COLOR_BUDDY_SIGNOFF);
printf(" %s ", who);
set_color(0);
printf("has signed off.\n");
log_event(EVENT_SIGNOFF, sname, NULL);
free(sname);
show_prompt();
}
/* PROTO */
void
buddy_profile(void *c, const char *who, const char *msg)
{
char *ptext;
long days, hours, minutes;
struct BuddyList *trav;
char *sname;
#ifdef DUMP_PROFILE
FILE *profile_dump;
#endif
sname = simplify_sn(who);
eraseline();
for (trav = buddylist; trav; trav = trav->next) {
if (strcmp(trav->sn, sname) == 0) {
if (trav->idle) {
b_echostr_s();
printf("[%s] Idle: ", who);
days = trav->idletime / 1440;
hours = (trav->idletime % 1440) / 60;
minutes = trav->idletime % 60;
if (days > 0)
printf("%ld days, ", days);
if (hours > 0)
printf("%ld hour%s, ", hours, (hours != 1 ? "s" : ""));
printf("%ld minute%s\n", minutes,
(minutes != 1 ? "s" : ""));
break;
}
}
}
b_echostr_s();
printf("[%s] Info:\n", who);
free(sname);
ptext = strip_html(msg);
prettyprint_echostr(ptext, PROFILE_ECHOSTR);
free(ptext);
#ifdef DUMP_PROFILE
if ((profile_dump = fopen(PROFILE_DUMP_PATH, "w")) != NULL) {
fprintf(profile_dump, "%s", msg);
fclose(profile_dump);
}
#endif
show_prompt();
}
/* PROTO */
void
buddy_awaymsg(void *c, const char *who, const char *msg)
{
char *ptext;
eraseline();
b_echostr_s();
printf("[%s] Away: ", who);
ptext = strip_html(msg);
prettyprint(ptext, 12 + strlen(who));
if (ptext[0] == 0)
printf("\n");
free(ptext);
show_prompt();
}
/* PROTO */
void
delete_buddylist(struct BuddyList * buddylist)
{
struct BuddyList *tr, *tmp;
for (tr = buddylist; tr;) {
if (tr->sn)
free(tr->sn);
if (tr->formattedsn)
free(tr->formattedsn);
tmp = tr;
tr = tr->next;
free(tmp);
}
}