Mercurial > pidgin
comparison src/util.c @ 1:2846a03bda67
[gaim-migrate @ 10]
The other missing files :)
committer: Tailor Script <tailor@pidgin.im>
author | Rob Flynn <gaim@robflynn.com> |
---|---|
date | Thu, 23 Mar 2000 03:13:54 +0000 |
parents | |
children | 34db9f242899 |
comparison
equal
deleted
inserted
replaced
0:a5ace2e037bc | 1:2846a03bda67 |
---|---|
1 /* | |
2 * gaim | |
3 * | |
4 * Copyright (C) 1998-1999, Mark Spencer <markster@marko.net> | |
5 * | |
6 * This program is free software; you can redistribute it and/or modify | |
7 * it under the terms of the GNU General Public License as published by | |
8 * the Free Software Foundation; either version 2 of the License, or | |
9 * (at your option) any later version. | |
10 * | |
11 * This program is distributed in the hope that it will be useful, | |
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 * GNU General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU General Public License | |
17 * along with this program; if not, write to the Free Software | |
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 * | |
20 */ | |
21 | |
22 #include <unistd.h> | |
23 #include <errno.h> | |
24 #include <stdio.h> | |
25 #include <stdlib.h> | |
26 #include <sys/time.h> | |
27 #include <sys/types.h> | |
28 #include <sys/stat.h> | |
29 #include <string.h> | |
30 #include <sys/wait.h> | |
31 #include <gtk/gtk.h> | |
32 #include <pixmaps/aimicon.xpm> | |
33 #include "gaim.h" | |
34 | |
35 static GdkPixmap *icon_pm = NULL; | |
36 static GdkBitmap *icon_bm = NULL; | |
37 static int state; | |
38 | |
39 gint badchar(char c) | |
40 { | |
41 if (c == ' ') | |
42 return 1; | |
43 if (c == ',') | |
44 return 1; | |
45 if (c == ')') | |
46 return 1; | |
47 if (c == '(') | |
48 return 1; | |
49 if (c == 0) | |
50 return 1; | |
51 if (c == '\n') | |
52 return 1; | |
53 return 0; | |
54 | |
55 | |
56 } | |
57 | |
58 | |
59 char *sec_to_text(int sec) | |
60 { | |
61 int hrs, min; | |
62 char minutes[64]; | |
63 char hours[64]; | |
64 char sep[16]; | |
65 char *ret = g_malloc(256); | |
66 | |
67 hrs = sec / 3600; | |
68 min = sec % 3600; | |
69 | |
70 min = min / 60; | |
71 sec = min % 60; | |
72 | |
73 if (min) { | |
74 if (min == 1) | |
75 g_snprintf(minutes, sizeof(minutes), "%d minute.", min); | |
76 else | |
77 g_snprintf(minutes, sizeof(minutes), "%d minutes.", min); | |
78 sprintf(sep, ", "); | |
79 } else { | |
80 if (!hrs) | |
81 g_snprintf(minutes, sizeof(minutes), "%d minutes.", min); | |
82 else { | |
83 minutes[0] = '.'; | |
84 minutes[1] = '\0'; | |
85 } | |
86 sep[0] = '\0'; | |
87 } | |
88 | |
89 if (hrs) { | |
90 if (hrs == 1) | |
91 g_snprintf(hours, sizeof(hours), "%d hour%s", hrs, sep); | |
92 else | |
93 g_snprintf(hours, sizeof(hours), "%d hours%s", hrs, sep); | |
94 } else | |
95 hours[0] = '\0'; | |
96 | |
97 | |
98 g_snprintf(ret, 256, "%s%s", hours, minutes); | |
99 | |
100 return ret; | |
101 } | |
102 | |
103 gint linkify_text(char *text) | |
104 { | |
105 char *c, *t; | |
106 char cpy[BUF_LONG]; | |
107 char url_buf[512]; | |
108 int cnt=0; | |
109 /* Assumes you have a buffer able to cary at least BUF_LEN * 2 bytes */ | |
110 | |
111 strncpy(cpy, text, strlen(text)); | |
112 cpy[strlen(text)] = 0; | |
113 c = cpy; | |
114 while(*c) { | |
115 if (!strncasecmp(c, "<A", 2)) { | |
116 while(1) { | |
117 if (!strncasecmp(c, "/A>", 3)) { | |
118 break; | |
119 } | |
120 text[cnt++] = *c; | |
121 c++; | |
122 if (!(*c)) | |
123 break; | |
124 } | |
125 } else if (!strncasecmp(c, "http://", 7)) { | |
126 t = c; | |
127 while(1) { | |
128 if (badchar(*t)) { | |
129 if (*(t-1) == '.') | |
130 t--; | |
131 strncpy(url_buf, c, t-c); | |
132 url_buf[t-c] = 0; | |
133 cnt += g_snprintf(&text[cnt++], 1024, "<A HREF=\"%s\">%s</A>", url_buf, url_buf); | |
134 cnt--; | |
135 c = t; | |
136 break; | |
137 } | |
138 if (!t) | |
139 break; | |
140 t++; | |
141 | |
142 } | |
143 } else if (!strncasecmp(c, "www.", 4)) { | |
144 if (strncasecmp(c, "www..", 5)) { | |
145 t = c; | |
146 while(1) { | |
147 if (badchar(*t)) { | |
148 if (t-c == 4) { | |
149 break; | |
150 } | |
151 if (*(t-1) == '.') | |
152 t--; | |
153 strncpy(url_buf, c, t-c); | |
154 url_buf[t-c] = 0; | |
155 cnt += g_snprintf(&text[cnt++], 1024, "<A HREF=\"http://%s\">%s</A>", url_buf, url_buf); | |
156 cnt--; | |
157 c = t; | |
158 break; | |
159 } | |
160 if (!t) | |
161 break; | |
162 t++; | |
163 } | |
164 } | |
165 } else if (!strncasecmp(c, "ftp://", 6)) { | |
166 t = c; | |
167 while(1) { | |
168 if (badchar(*t)) { | |
169 if (*(t-1) == '.') | |
170 t--; | |
171 strncpy(url_buf, c, t-c); | |
172 url_buf[t-c] = 0; | |
173 cnt += g_snprintf(&text[cnt++], 1024, "<A HREF=\"%s\">%s</A>", url_buf, url_buf); | |
174 cnt--; | |
175 c = t; | |
176 break; | |
177 } | |
178 if (!t) | |
179 break; | |
180 t++; | |
181 | |
182 } | |
183 } else if (!strncasecmp(c, "ftp.", 4)) { | |
184 t = c; | |
185 while(1) { | |
186 if (badchar(*t)) { | |
187 if (t-c == 4) { | |
188 break; | |
189 } | |
190 if (*(t-1) == '.') | |
191 t--; | |
192 strncpy(url_buf, c, t-c); | |
193 url_buf[t-c] = 0; | |
194 cnt += g_snprintf(&text[cnt++], 1024, "<A HREF=\"ftp://%s\">%s</A>", url_buf, url_buf); | |
195 cnt--; | |
196 c = t; | |
197 break; | |
198 } | |
199 if (!t) | |
200 break; | |
201 t++; | |
202 } | |
203 } else if (!strncasecmp(c, "mailto:", 7)) { | |
204 t = c; | |
205 while(1) { | |
206 if (badchar(*t)) { | |
207 if (*(t-1) == '.') | |
208 t--; | |
209 strncpy(url_buf, c, t-c); | |
210 url_buf[t-c] = 0; | |
211 cnt += g_snprintf(&text[cnt++], 1024, "<A HREF=\"%s\">%s</A>", url_buf, url_buf); | |
212 cnt--; | |
213 c = t; | |
214 break; | |
215 } | |
216 if (!t) | |
217 break; | |
218 t++; | |
219 | |
220 } | |
221 } else if (!strncasecmp(c, "@", 1)) { | |
222 char *tmp; | |
223 int flag; | |
224 int len=0; | |
225 url_buf[0] = 0; | |
226 | |
227 if (*(c-1) == ' ' || *(c+1) == ' ') | |
228 flag = 0; | |
229 else | |
230 flag = 1; | |
231 | |
232 t = c; | |
233 while(flag) { | |
234 if (badchar(*t)) { | |
235 cnt -= (len - 1); | |
236 break; | |
237 } else { | |
238 len++; | |
239 tmp = g_malloc(len + 1); | |
240 tmp[len] = 0; | |
241 tmp[0] = *t; | |
242 strncpy(tmp + 1, url_buf, len - 1); | |
243 strcpy(url_buf, tmp); | |
244 url_buf[len] = 0; | |
245 g_free(tmp); | |
246 t--; | |
247 if (t < cpy) { | |
248 cnt = 0; | |
249 break; | |
250 } | |
251 } | |
252 } | |
253 | |
254 | |
255 t = c + 1; | |
256 | |
257 while(flag) { | |
258 if (badchar(*t)) { | |
259 if (*(t-1) == '.') | |
260 t--; | |
261 cnt += g_snprintf(&text[cnt++], 1024, "<A HREF=\"mailto:%s\">%s</A>", url_buf, url_buf); | |
262 text[cnt]=0; | |
263 | |
264 | |
265 cnt--; | |
266 c = t; | |
267 | |
268 break; | |
269 } else { | |
270 strncat(url_buf, t, 1); | |
271 len++; | |
272 url_buf[len] = 0; | |
273 } | |
274 | |
275 t++; | |
276 | |
277 } | |
278 | |
279 | |
280 } | |
281 | |
282 if (*c == 0) | |
283 break; | |
284 | |
285 text[cnt++] = *c; | |
286 c++; | |
287 | |
288 } | |
289 text[cnt]=0; | |
290 return cnt; | |
291 } | |
292 | |
293 | |
294 FILE *open_log_file (struct conversation *c) | |
295 { | |
296 char *buf = g_malloc(BUF_LONG); | |
297 char *buf2 = g_malloc(BUF_LONG); | |
298 char log_all_file[256]; | |
299 struct log_conversation *l; | |
300 struct stat st; | |
301 int flag = 0; | |
302 FILE *fd; | |
303 int res; | |
304 | |
305 if (!(general_options & OPT_GEN_LOG_ALL)) { | |
306 | |
307 l = find_log_info(c->name); | |
308 if (!l) | |
309 return NULL; | |
310 | |
311 if (stat(l->filename, &st) < 0) | |
312 flag = 1; | |
313 | |
314 fd = fopen(l->filename, "a"); | |
315 | |
316 if (flag) { /* is a new file */ | |
317 fprintf(fd, "<HTML><HEAD><TITLE>" ); | |
318 fprintf(fd, "IM Sessions with %s", c->name ); | |
319 fprintf(fd, "</TITLE></HEAD><BODY BGCOLOR=\"ffffff\">\n" ); | |
320 } | |
321 | |
322 return fd; | |
323 } | |
324 | |
325 /* Dont log yourself */ | |
326 g_snprintf(log_all_file, 256, "%s/.gaim", getenv("HOME")); | |
327 | |
328 stat(log_all_file, &st); | |
329 if (!S_ISDIR(st.st_mode)) | |
330 unlink(log_all_file); | |
331 | |
332 fd = fopen(log_all_file, "r"); | |
333 | |
334 if (!fd) { | |
335 res = mkdir(log_all_file, S_IRUSR | S_IWUSR | S_IXUSR); | |
336 if (res < 0) { | |
337 g_snprintf(buf, BUF_LONG, "Unable to make directory %s for logging", log_all_file); | |
338 do_error_dialog(buf, "Error!"); | |
339 g_free(buf); | |
340 g_free(buf2); | |
341 return NULL; | |
342 } | |
343 } else | |
344 fclose(fd); | |
345 | |
346 g_snprintf(log_all_file, 256, "%s/.gaim/%s", getenv("HOME"), current_user->username); | |
347 | |
348 if (stat(log_all_file, &st) < 0) | |
349 flag = 1; | |
350 if (!S_ISDIR(st.st_mode)) | |
351 unlink(log_all_file); | |
352 | |
353 fd = fopen(log_all_file, "r"); | |
354 if (!fd) { | |
355 res = mkdir(log_all_file, S_IRUSR | S_IWUSR | S_IXUSR); | |
356 if (res < 0) { | |
357 g_snprintf(buf, BUF_LONG, "Unable to make directory %s for logging", log_all_file); | |
358 do_error_dialog(buf, "Error!"); | |
359 g_free(buf); | |
360 g_free(buf2); | |
361 return NULL; | |
362 } | |
363 } else | |
364 fclose(fd); | |
365 | |
366 | |
367 g_snprintf(log_all_file, 256, "%s/.gaim/%s/%s.log", getenv("HOME"), current_user->username, normalize(c->name)); | |
368 | |
369 if (stat(log_all_file, &st) < 0) | |
370 flag = 1; | |
371 | |
372 sprintf(debug_buff,"Logging to: \"%s\"\n", log_all_file); | |
373 debug_print(debug_buff); | |
374 | |
375 fd = fopen(log_all_file, "a"); | |
376 | |
377 if (flag) { /* is a new file */ | |
378 fprintf(fd, "<HTML><HEAD><TITLE>" ); | |
379 fprintf(fd, "IM Sessions with %s", c->name ); | |
380 fprintf(fd, "</TITLE></HEAD><BODY BGCOLOR=\"ffffff\">\n" ); | |
381 } | |
382 return fd; | |
383 } | |
384 | |
385 | |
386 int escape_message(char *msg) | |
387 { | |
388 char *c, *cpy; | |
389 int cnt=0; | |
390 /* Assumes you have a buffer able to cary at least BUF_LEN * 2 bytes */ | |
391 if (strlen(msg) > BUF_LEN) { | |
392 sprintf(debug_buff, "Warning: truncating message to 2048 bytes\n"); | |
393 debug_print(debug_buff); | |
394 msg[2047]='\0'; | |
395 } | |
396 | |
397 cpy = g_strdup(msg); | |
398 c = cpy; | |
399 while(*c) { | |
400 switch(*c) { | |
401 case '$': | |
402 case '[': | |
403 case ']': | |
404 case '(': | |
405 case ')': | |
406 case '#': | |
407 msg[cnt++]='\\'; | |
408 /* Fall through */ | |
409 default: | |
410 msg[cnt++]=*c; | |
411 } | |
412 c++; | |
413 } | |
414 msg[cnt]='\0'; | |
415 g_free(cpy); | |
416 return cnt; | |
417 } | |
418 | |
419 int escape_text(char *msg) | |
420 { | |
421 char *c, *cpy; | |
422 int cnt=0; | |
423 /* Assumes you have a buffer able to cary at least BUF_LEN * 2 bytes */ | |
424 if (strlen(msg) > BUF_LEN) { | |
425 fprintf(stderr, "Warning: truncating message to 2048 bytes\n"); | |
426 msg[2047]='\0'; | |
427 } | |
428 | |
429 cpy = g_strdup(msg); | |
430 c = cpy; | |
431 while(*c) { | |
432 switch(*c) { | |
433 case '\n': | |
434 msg[cnt++] = '<'; | |
435 msg[cnt++] = 'B'; | |
436 msg[cnt++] = 'R'; | |
437 msg[cnt++] = '>'; | |
438 break; | |
439 case '{': | |
440 case '}': | |
441 case '\\': | |
442 case '"': | |
443 msg[cnt++]='\\'; | |
444 /* Fall through */ | |
445 default: | |
446 msg[cnt++]=*c; | |
447 } | |
448 c++; | |
449 } | |
450 msg[cnt]='\0'; | |
451 g_free(cpy); | |
452 return cnt; | |
453 } | |
454 | |
455 char * escape_text2(char *msg) | |
456 { | |
457 char *c, *cpy; | |
458 char *woo; | |
459 int cnt=0; | |
460 /* Assumes you have a buffer able to cary at least BUF_LEN * 2 bytes */ | |
461 if (strlen(msg) > BUF_LEN) { | |
462 fprintf(stderr, "Warning: truncating message to 2048 bytes\n"); | |
463 msg[2047]='\0'; | |
464 } | |
465 | |
466 woo = (char *)malloc(strlen(msg) * 2); | |
467 cpy = g_strdup(msg); | |
468 c = cpy; | |
469 while(*c) { | |
470 switch(*c) { | |
471 case '\n': | |
472 woo[cnt++] = '<'; | |
473 woo[cnt++] = 'B'; | |
474 woo[cnt++] = 'R'; | |
475 woo[cnt++] = '>'; | |
476 break; | |
477 case '{': | |
478 case '}': | |
479 case '\\': | |
480 case '"': | |
481 woo[cnt++]='\\'; | |
482 /* Fall through */ | |
483 default: | |
484 woo[cnt++]=*c; | |
485 } | |
486 c++; | |
487 } | |
488 woo[cnt]='\0'; | |
489 return woo; | |
490 } | |
491 | |
492 | |
493 char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" | |
494 "0123456789+/"; | |
495 | |
496 | |
497 char *tobase64(char *text) | |
498 { | |
499 char *out = NULL; | |
500 char *c; | |
501 unsigned int tmp = 0; | |
502 int len = 0, n = 0; | |
503 | |
504 c = text; | |
505 | |
506 while(c) { | |
507 tmp = tmp << 8; | |
508 tmp += *c; | |
509 n++; | |
510 | |
511 if (n == 3) { | |
512 out = g_realloc(out, len+4); | |
513 out[len] = alphabet[(tmp >> 18) & 0x3f]; | |
514 out[len+1] = alphabet[(tmp >> 12) & 0x3f]; | |
515 out[len+2] = alphabet[(tmp >> 6) & 0x3f]; | |
516 out[len+3] = alphabet[tmp & 0x3f]; | |
517 len += 4; | |
518 tmp = 0; | |
519 n = 0; | |
520 } | |
521 c++; | |
522 } | |
523 switch(n) { | |
524 | |
525 case 2: | |
526 out = g_realloc(out, len+5); | |
527 out[len] = alphabet[(tmp >> 12) & 0x3f]; | |
528 out[len+1] = alphabet[(tmp >> 6) & 0x3f]; | |
529 out[len+2] = alphabet[tmp & 0x3f]; | |
530 out[len+3] = '='; | |
531 out[len+4] = 0; | |
532 break; | |
533 case 1: | |
534 out = g_realloc(out, len+4); | |
535 out[len] = alphabet[(tmp >> 6) & 0x3f]; | |
536 out[len+1] = alphabet[tmp & 0x3f]; | |
537 out[len+2] = '='; | |
538 out[len+3] = 0; | |
539 break; | |
540 case 0: | |
541 out = g_realloc(out, len+2); | |
542 out[len] = '='; | |
543 out[len+1] = 0; | |
544 break; | |
545 } | |
546 return out; | |
547 } | |
548 | |
549 | |
550 char *frombase64(char *text) | |
551 { | |
552 char *out = NULL; | |
553 char tmp = 0; | |
554 char *c; | |
555 gint32 tmp2 = 0; | |
556 int len = 0, n = 0; | |
557 | |
558 c = text; | |
559 | |
560 while(*c) { | |
561 if (*c >= 'A' && *c <= 'Z') { | |
562 tmp = *c - 'A'; | |
563 } else if (*c >= 'a' && *c <= 'z') { | |
564 tmp = 26 + (*c - 'a'); | |
565 } else if (*c >= '0' && *c <= 57) { | |
566 tmp = 52 + (*c - '0'); | |
567 } else if (*c == '+') { | |
568 tmp = 62; | |
569 } else if (*c == '/') { | |
570 tmp = 63; | |
571 } else if (*c == '=') { | |
572 if (n == 3) { | |
573 out = g_realloc(out, len + 2); | |
574 out[len] = (char)(tmp2 >> 10) & 0xff; | |
575 len++; | |
576 out[len] = (char)(tmp2 >> 2) & 0xff; | |
577 len++; | |
578 } else if (n == 2) { | |
579 out = g_realloc(out, len + 1); | |
580 out[len] = (char)(tmp2 >> 4) & 0xff; | |
581 len++; | |
582 } | |
583 break; | |
584 } | |
585 tmp2 = ((tmp2 << 6) | (tmp & 0xff)); | |
586 n++; | |
587 if (n == 4) { | |
588 out = g_realloc(out, len + 3); | |
589 out[len] = (char)((tmp2 >> 16) & 0xff); | |
590 len++; | |
591 out[len] = (char)((tmp2 >> 8) & 0xff); | |
592 len++; | |
593 out[len] = (char)(tmp2 & 0xff); | |
594 len++; | |
595 tmp2 = 0; | |
596 n = 0; | |
597 } | |
598 c++; | |
599 } | |
600 | |
601 g_realloc(out, len+1); | |
602 out[len] = 0; | |
603 | |
604 return out; | |
605 } | |
606 | |
607 | |
608 char *normalize(const char *s) | |
609 { | |
610 static char buf[BUF_LEN]; | |
611 char *t, *u; | |
612 int x=0; | |
613 | |
614 u = t = g_malloc(strlen(s) + 1); | |
615 | |
616 strcpy(t, s); | |
617 g_strdown(t); | |
618 | |
619 while(*t) { | |
620 if (*t != ' ') { | |
621 buf[x] = *t; | |
622 x++; | |
623 } | |
624 t++; | |
625 } | |
626 buf[x]='\0'; | |
627 g_free(u); | |
628 return buf; | |
629 } | |
630 | |
631 int query_state() | |
632 { | |
633 return state; | |
634 } | |
635 | |
636 void set_state(int i) | |
637 { | |
638 state = i; | |
639 } | |
640 | |
641 char *date() | |
642 { | |
643 static char date[80]; | |
644 time_t tme; | |
645 time(&tme); | |
646 strftime(date, sizeof(date), "%H:%M:%S", localtime(&tme)); | |
647 return date; | |
648 } | |
649 | |
650 | |
651 gint clean_pid(void *dummy) | |
652 { | |
653 int status; | |
654 pid_t pid; | |
655 | |
656 pid = waitpid(-1, &status, WNOHANG); | |
657 | |
658 if (pid == 0) | |
659 return TRUE; | |
660 | |
661 return FALSE; | |
662 } | |
663 | |
664 void aol_icon(GdkWindow *w) | |
665 { | |
666 #ifndef _WIN32 | |
667 if (icon_pm == NULL) { | |
668 icon_pm = gdk_pixmap_create_from_xpm_d(w, &icon_bm, | |
669 NULL, (gchar **)aimicon_xpm); | |
670 } | |
671 gdk_window_set_icon(w, NULL, icon_pm, icon_bm); | |
672 gdk_window_set_group(w, mainwindow->window); | |
673 #endif | |
674 } | |
675 | |
676 struct aim_user *find_user(const char *name) | |
677 { | |
678 char *who = g_strdup(normalize(name)); | |
679 GList *usr = aim_users; | |
680 struct aim_user *u; | |
681 | |
682 while(usr) { | |
683 u = (struct aim_user *)usr->data; | |
684 if (!strcmp(normalize(u->username), who)) { | |
685 g_free(who); | |
686 return u; | |
687 } | |
688 usr = usr->next; | |
689 } | |
690 g_free(who); | |
691 return NULL; | |
692 } |