comparison src/util.c @ 5136:381da05cb5ed

[gaim-migrate @ 5500] this started out as simply adding an option to right-click on a jabber buddy and re-request authorization. Then I ended up chasing the disgusting mess of const vs non-const parameters all over gaim. The end result is that you can now right-click on jabber buddies and re-request auth like you can for ICQ. Also, a lot more things are const that should be, I fixed a bug or two, and I cleaned up one of my least favorite functions in gaim (linkify_text). It is now decidedly less evil. committer: Tailor Script <tailor@pidgin.im>
author Nathan Walp <nwalp@pidgin.im>
date Tue, 15 Apr 2003 04:18:00 +0000
parents 89c0c811befa
children 91ea5ef2278b
comparison
equal deleted inserted replaced
5135:102135caa225 5136:381da05cb5ed
135 } 135 }
136 136
137 return ret; 137 return ret;
138 } 138 }
139 139
140 gint linkify_text(char *text) 140 char *linkify_text(const char *text)
141 { 141 {
142 char *c, *t, *q = NULL; 142 const char *c, *t, *q = NULL;
143 char *cpy = g_malloc(strlen(text) * 3 + 1); 143 char *tmp;
144 char url_buf[BUF_LEN * 4]; 144 char url_buf[BUF_LEN * 4];
145 int cnt = 0; 145 GString *ret = g_string_new("");
146 /* Assumes you have a buffer able to cary at least BUF_LEN * 2 bytes */ 146 /* Assumes you have a buffer able to cary at least BUF_LEN * 2 bytes */
147 147
148 strncpy(cpy, text, strlen(text)); 148 c = text;
149 cpy[strlen(text)] = 0;
150 c = cpy;
151 while (*c) { 149 while (*c) {
152 if(!q && (*c == '\"' || *c == '\'')) { 150 if(!q && (*c == '\"' || *c == '\'')) {
153 q = c; 151 q = c;
154 } else if(q) { 152 } else if(q) {
155 if(*c == *q) 153 if(*c == *q)
157 } else if (!g_ascii_strncasecmp(c, "<A", 2)) { 155 } else if (!g_ascii_strncasecmp(c, "<A", 2)) {
158 while (1) { 156 while (1) {
159 if (!g_ascii_strncasecmp(c, "/A>", 3)) { 157 if (!g_ascii_strncasecmp(c, "/A>", 3)) {
160 break; 158 break;
161 } 159 }
162 text[cnt++] = *c; 160 ret = g_string_append_c(ret, *c);
163 c++; 161 c++;
164 if (!(*c)) 162 if (!(*c))
165 break; 163 break;
166 } 164 }
167 } else if ((*c=='h') && (!g_ascii_strncasecmp(c, "http://", 7) || (!g_ascii_strncasecmp(c, "https://", 8)))) { 165 } else if ((*c=='h') && (!g_ascii_strncasecmp(c, "http://", 7) ||
166 (!g_ascii_strncasecmp(c, "https://", 8)))) {
168 t = c; 167 t = c;
169 while (1) { 168 while (1) {
170 if (badchar(*t)) { 169 if (badchar(*t)) {
171 170
172 if (*(t) == ',' && (*(t + 1) != ' ')) { 171 if (*(t) == ',' && (*(t + 1) != ' ')) {
176 175
177 if (*(t - 1) == '.') 176 if (*(t - 1) == '.')
178 t--; 177 t--;
179 strncpy(url_buf, c, t - c); 178 strncpy(url_buf, c, t - c);
180 url_buf[t - c] = 0; 179 url_buf[t - c] = 0;
181 cnt += g_snprintf(&text[cnt], 1024, "<A HREF=\"%s\">%s</A>", 180 g_string_append_printf(ret, "<A HREF=\"%s\">%s</A>",
182 url_buf, url_buf); 181 url_buf, url_buf);
183 c = t; 182 c = t;
184 break; 183 break;
185 } 184 }
186 if (!t) 185 if (!t)
187 break; 186 break;
204 203
205 if (*(t - 1) == '.') 204 if (*(t - 1) == '.')
206 t--; 205 t--;
207 strncpy(url_buf, c, t - c); 206 strncpy(url_buf, c, t - c);
208 url_buf[t - c] = 0; 207 url_buf[t - c] = 0;
209 cnt += g_snprintf(&text[cnt], 1024, 208 g_string_append_printf(ret,
210 "<A HREF=\"http://%s\">%s</A>", url_buf, 209 "<A HREF=\"http://%s\">%s</A>", url_buf,
211 url_buf); 210 url_buf);
212 c = t; 211 c = t;
213 break; 212 break;
214 } 213 }
215 if (!t) 214 if (!t)
216 break; 215 break;
223 if (badchar(*t)) { 222 if (badchar(*t)) {
224 if (*(t - 1) == '.') 223 if (*(t - 1) == '.')
225 t--; 224 t--;
226 strncpy(url_buf, c, t - c); 225 strncpy(url_buf, c, t - c);
227 url_buf[t - c] = 0; 226 url_buf[t - c] = 0;
228 cnt += g_snprintf(&text[cnt], 1024, "<A HREF=\"%s\">%s</A>", 227 g_string_append_printf(ret, "<A HREF=\"%s\">%s</A>",
229 url_buf, url_buf); 228 url_buf, url_buf);
230 c = t; 229 c = t;
231 break; 230 break;
232 } 231 }
233 if (!t) 232 if (!t)
234 break; 233 break;
245 } 244 }
246 if (*(t - 1) == '.') 245 if (*(t - 1) == '.')
247 t--; 246 t--;
248 strncpy(url_buf, c, t - c); 247 strncpy(url_buf, c, t - c);
249 url_buf[t - c] = 0; 248 url_buf[t - c] = 0;
250 cnt += g_snprintf(&text[cnt], 1024, 249 g_string_append_printf(ret,
251 "<A HREF=\"ftp://%s\">%s</A>", url_buf, 250 "<A HREF=\"ftp://%s\">%s</A>", url_buf,
252 url_buf); 251 url_buf);
253 c = t; 252 c = t;
254 break; 253 break;
255 } 254 }
256 if (!t) 255 if (!t)
257 break; 256 break;
264 if (badchar(*t)) { 263 if (badchar(*t)) {
265 if (*(t - 1) == '.') 264 if (*(t - 1) == '.')
266 t--; 265 t--;
267 strncpy(url_buf, c, t - c); 266 strncpy(url_buf, c, t - c);
268 url_buf[t - c] = 0; 267 url_buf[t - c] = 0;
269 cnt += g_snprintf(&text[cnt], 1024, "<A HREF=\"%s\">%s</A>", 268 g_string_append_printf(ret, "<A HREF=\"%s\">%s</A>",
270 url_buf, url_buf); 269 url_buf, url_buf);
271 c = t; 270 c = t;
272 break; 271 break;
273 } 272 }
274 if (!t) 273 if (!t)
275 break; 274 break;
276 t++; 275 t++;
277 276
278 } 277 }
279 } else if (c != cpy && (*c == '@')) { 278 } else if (c != text && (*c == '@')) {
280 char *tmp; 279 char *tmp;
281 int flag; 280 int flag;
282 int len = 0; 281 int len = 0;
283 const char illegal_chars[] = "!@#$%^&*()[]{}/|\\<>\":;\r\n \0"; 282 const char illegal_chars[] = "!@#$%^&*()[]{}/|\\<>\":;\r\n \0";
284 url_buf[0] = 0; 283 url_buf[0] = 0;
289 flag = 1; 288 flag = 1;
290 289
291 t = c; 290 t = c;
292 while (flag) { 291 while (flag) {
293 if (badchar(*t)) { 292 if (badchar(*t)) {
294 cnt -= (len - 1); 293 ret = g_string_truncate(ret, ret->len - (len - 1));
295 break; 294 break;
296 } else { 295 } else {
297 len++; 296 len++;
298 tmp = g_malloc(len + 1); 297 tmp = g_malloc(len + 1);
299 tmp[len] = 0; 298 tmp[len] = 0;
301 strncpy(tmp + 1, url_buf, len - 1); 300 strncpy(tmp + 1, url_buf, len - 1);
302 strcpy(url_buf, tmp); 301 strcpy(url_buf, tmp);
303 url_buf[len] = 0; 302 url_buf[len] = 0;
304 g_free(tmp); 303 g_free(tmp);
305 t--; 304 t--;
306 if (t < cpy) { 305 if (t < text) {
307 cnt = 0; 306 ret = g_string_assign(ret, "");
308 break; 307 break;
309 } 308 }
310 } 309 }
311 } 310 }
312 311
317 char *d; 316 char *d;
318 317
319 for (d = url_buf + strlen(url_buf) - 1; *d == '.'; d--, t--) 318 for (d = url_buf + strlen(url_buf) - 1; *d == '.'; d--, t--)
320 *d = '\0'; 319 *d = '\0';
321 320
322 cnt += g_snprintf(&text[cnt], 1024, 321 g_string_append_printf(ret, "<A HREF=\"mailto:%s\">%s</A>",
323 "<A HREF=\"mailto:%s\">%s</A>", url_buf, 322 url_buf, url_buf);
324 url_buf);
325 text[cnt] = 0;
326
327
328 c = t; 323 c = t;
329 324
330 break; 325 break;
331 } else { 326 } else {
332 strncat(url_buf, t, 1); 327 strncat(url_buf, t, 1);
339 } 334 }
340 335
341 if (*c == 0) 336 if (*c == 0)
342 break; 337 break;
343 338
344 text[cnt++] = *c; 339 ret = g_string_append_c(ret, *c);
345 c++; 340 c++;
346 341
347 } 342 }
348 text[cnt] = 0; 343 tmp = ret->str;
349 g_free(cpy); 344 g_string_free(ret, FALSE);
350 return cnt; 345 return tmp;
351 } 346 }
352 347
353 348
354 static const char alphabet[] = 349 static const char alphabet[] =
355 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" 350 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
890 885
891 strcpy(text, text2); 886 strcpy(text, text2);
892 g_free(text2); 887 g_free(text2);
893 } 888 }
894 889
895 char *add_cr(char *text) 890 char *add_cr(const char *text)
896 { 891 {
897 char *ret = NULL; 892 char *ret = NULL;
898 int count = 0, i, j; 893 int count = 0, i, j;
899 894
900 if (text[0] == '\n') 895 if (text[0] == '\n')