annotate mcabber/utils.c @ 0:b3b2332715fb

Tailorization of /trunk Import of the upstream sources from Repository: file:///tmp/svn-mcabber Module: /trunk Revision: 15
author tailor@frmp8452
date Thu, 30 Jun 2005 21:39:31 +0000
parents
children 4dcabf02f474
rev   line source
0
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
1 #include <stdio.h>
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
2 #include <string.h>
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
3 #include <stdlib.h>
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
4 #include <ncurses.h>
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
5 #include <stdarg.h>
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
6 #include <time.h>
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
7
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
8 /* Variables globales a UTILS.C */
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
9 int DebugEnabled = 0;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
10
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
11 void ut_InitDebug(int level)
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
12 {
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
13 FILE *fp = fopen("/tmp/mcabberlog", "w");
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
14
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
15 DebugEnabled = level;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
16 fprintf(fp, "Debug inicializado...\n"
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
17 "-----------------------------------\n");
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
18 fclose(fp);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
19 }
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
20
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
21 void ut_WriteLog(const char *fmt, ...)
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
22 {
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
23 FILE *fp = NULL;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
24 time_t ahora;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
25 va_list ap;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
26 char *buffer = NULL;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
27
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
28 if (DebugEnabled) {
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
29 fp = fopen("/tmp/mcabberlog", "a+");
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
30 buffer = (char *) calloc(1, 8192);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
31
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
32 ahora = time(NULL);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
33 strftime(buffer, 1024, "[%H:%M:%S] ", localtime(&ahora));
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
34 fprintf(fp, "%s", buffer);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
35
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
36 va_start(ap, fmt);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
37 vfprintf(fp, fmt, ap);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
38 va_end(ap);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
39
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
40 free(buffer);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
41 fclose(fp);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
42 }
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
43 }
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
44
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
45 char **ut_SplitMessage(char *mensaje, int *nsubmsgs, unsigned int maxlong)
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
46 {
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
47 /* BUGs: recorta la palabra si la longitud maxlong es menor que la palabra
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
48 // maxlong = 4
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
49 // mensaje = "peaso bug!"
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
50 // submsgs[0] = "peas"
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
51 // submsgs[1] = "bug!"
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
52 // por lo demas, rula de arte. De todos modos, podrias verificarla ???
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
53 */
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
54 char *running;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
55 char *aux;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
56 char *aux2;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
57 char **submsgs;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
58 char *buffer = (char *) malloc(strlen(mensaje) * 2);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
59 int i = 0;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
60
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
61 submsgs = (char **) malloc(50 * sizeof(char *)); /* limitamos, a priori, el maximo de lineas devueltas... */
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
62
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
63 running = strdup(mensaje); /* duplicamos mensaje */
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
64 aux2 = strdup(mensaje); /* hacemos otra copia */
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
65 while (strlen(aux2) > maxlong) { /* mintras quede texto... */
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
66 memset(buffer, 0, strlen(mensaje) * 2); /* borramos el buffer */
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
67 running[maxlong] = '\0'; /* cortamos la cadena a la maxima longitud */
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
68 aux = rindex(running, ' '); /* posicion del ultimo blanco */
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
69 if (aux != NULL) /* hay blanco! */
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
70 strncpy(buffer, running, strlen(running) - strlen(aux));
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
71 else
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
72 strcpy(buffer, running); /* se supone que esto es pa evitar el bug explicado arriba, pero no rula */
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
73
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
74 submsgs[i] = (char *) malloc(strlen(buffer) + 1); /*reservamos memoria */
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
75 strcpy(submsgs[i], buffer); /*copiamos el buffer de arriba */
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
76 i++; /*aumentamos numero de mensajillos */
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
77 aux2 += strlen(buffer) + 1; /*eliminamos texto particionado */
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
78 sprintf(running, "%s", aux2); /*y lo copiamos de nuevo a la string de "curro" */
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
79 }
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
80 /* la ultima parte del mensaje, si la hay ;-) */
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
81 if (strlen(aux2) > 0) {
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
82 submsgs[i] = (char *) malloc(strlen(aux2) + 1);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
83 strcpy(submsgs[i], aux2);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
84 i++;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
85 }
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
86 (*nsubmsgs) = i;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
87 free(buffer);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
88 return submsgs;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
89 }
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
90
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
91 /* Desc: get the rightmost substring
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
92 *
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
93 * In : string, match
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
94 * Out : ptr to substring (or NULL if not found)
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
95 *
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
96 * Note: this one has no namespace, cos it belongs to <string.h>
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
97 */
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
98 char *ut_strrstr(const char *s1, const char *s2)
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
99 {
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
100 int l = strlen(s2);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
101
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
102 if (l) {
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
103 const char *s = strchr(s1, '\0') - l;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
104 while (s >= s1) {
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
105 if (*s == *s2) {
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
106 int _l = l - 1;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
107 const char *_s = s + 1, *_s2 = s2 + 1;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
108 while (_l) {
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
109 if (*_s++ != *_s2++) {
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
110 break;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
111 }
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
112 _l--;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
113 }
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
114 if (!_l) {
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
115 return (char *) s;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
116 }
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
117 }
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
118 s--;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
119 }
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
120 }
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
121
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
122 return NULL;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
123 }
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
124
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
125 char *gettag(char *buffer, char *what)
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
126 {
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
127 char *aux;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
128 char *aux2;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
129 char *result = (char *) malloc(1024);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
130 char *tmp = (char *) malloc(1024);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
131 memset(result, 0, 1024);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
132 memset(tmp, 0, 1024);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
133
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
134 sprintf(tmp, "<%s>", what);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
135 aux = strstr(buffer, tmp);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
136 if (aux) {
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
137 aux += strlen(tmp);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
138 sprintf(tmp, "</%s>", what);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
139 aux2 = strstr(aux, tmp);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
140 if (aux2) {
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
141 strncpy(result, aux, strlen(aux) - strlen(aux2));
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
142 free(tmp);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
143 return result;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
144 }
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
145 }
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
146 free(tmp);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
147 free(result);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
148 return "UNKtag";
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
149 }
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
150
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
151
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
152 char *getattr(char *buffer, char *what)
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
153 {
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
154 char *aux;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
155 char *aux2;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
156 char *result = (char *) malloc(1024);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
157 memset(result, 0, 1024);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
158
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
159 aux = strstr(buffer, what);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
160 if (aux) {
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
161 aux += strlen(what);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
162 aux2 = strstr(aux, "'");
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
163 if (aux2) {
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
164 strncpy(result, aux, strlen(aux) - strlen(aux2));
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
165 return result;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
166 }
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
167 }
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
168 free(result);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
169 return "UNKattr";
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
170 }
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
171
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
172 void ut_CenterMessage(char *text, int width, char *output)
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
173 {
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
174 char *blank;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
175 int ntest, nn;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
176
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
177 memset(output, 0, strlen(output));
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
178
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
179 ntest = (width - strlen(text)) / 2;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
180 blank = (char *) malloc(ntest + 1);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
181
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
182 for (nn = 0; nn < ntest; nn++)
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
183 blank[nn] = ' ';
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
184 blank[ntest] = '\0';
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
185
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
186 strcpy(output, blank);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
187 strcat(output, text);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
188 strcat(output, blank);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
189
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
190 free(blank);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
191 }