comparison src/protocols/toc/toc.c @ 6059:d8cd876e613e

[gaim-migrate @ 6509] I made a lot of server.c functions use const chat * instead of const * Made the alias entry have focus in the alias buddy dialog. Fixed a typo thanks to J. Silvestri committer: Tailor Script <tailor@pidgin.im>
author Mark Doliner <mark@kingant.net>
date Tue, 08 Jul 2003 03:36:41 +0000
parents 0878caaef952
children c28dea25983c
comparison
equal deleted inserted replaced
6058:d5f4ae4f9a02 6059:d8cd876e613e
316 msg[cnt] = '\0'; 316 msg[cnt] = '\0';
317 g_free(cpy); 317 g_free(cpy);
318 return cnt; 318 return cnt;
319 } 319 }
320 320
321 static int escape_text(char *msg) 321 /*
322 { 322 * Duplicates the input string, replacing each \n with a <BR>, and
323 char *c, *cpy; 323 * escaping a few other characters.
324 int cnt = 0; 324 */
325 /* Assumes you have a buffer able to cary at least BUF_LEN * 4 bytes */ 325 char *escape_text(const char *msg)
326 if (strlen(msg) > BUF_LEN) { 326 {
327 fprintf(stderr, "Warning: truncating message to 2048 bytes\n"); 327 char *ret;
328 msg[2047] = '\0'; 328 int i, j;
329 } 329
330 330 if (!msg)
331 cpy = g_strdup(msg); 331 return NULL;
332 c = cpy; 332
333 while (*c) { 333 /* Calculate the length after escaping */
334 switch (*c) { 334 i = 0;
335 j = 0;
336 while (msg[i++])
337 switch (msg[i]) {
335 case '\n': 338 case '\n':
336 msg[cnt++] = '<'; 339 j += 4;
337 msg[cnt++] = 'B';
338 msg[cnt++] = 'R';
339 msg[cnt++] = '>';
340 break; 340 break;
341 case '{': 341 case '{':
342 case '}': 342 case '}':
343 case '\\': 343 case '\\':
344 case '"': 344 case '"':
345 msg[cnt++] = '\\'; 345 j += 1;
346 /* Fall through */
347 default: 346 default:
348 msg[cnt++] = *c; 347 j += 1;
349 } 348 }
350 c++; 349
351 } 350 /* Allocate a string */
352 msg[cnt] = '\0'; 351 ret = (char *)malloc((j+1) * sizeof(char));
353 g_free(cpy); 352
354 return cnt; 353 /* Copy the string */
354 i = 0;
355 j = 0;
356 while (msg[i++]) {
357 switch (msg[i]) {
358 case '\n':
359 ret[j++] = '<';
360 ret[j++] = 'B';
361 ret[j++] = 'R';
362 ret[j++] = '>';
363 break;
364 case '{':
365 case '}':
366 case '\\':
367 case '"':
368 ret[j++] = '\\';
369 default:
370 ret[j++] = msg[i];
371 }
372 }
373 ret[j] = '\0';
374
375 return ret;
355 } 376 }
356 377
357 static int sflap_send(GaimConnection *gc, char *buf, int olen, int type) 378 static int sflap_send(GaimConnection *gc, char *buf, int olen, int type)
358 { 379 {
359 int len; 380 int len;
1003 } 1024 }
1004 1025
1005 static int toc_send_im(GaimConnection *gc, const char *name, const char *message, int len, int flags) 1026 static int toc_send_im(GaimConnection *gc, const char *name, const char *message, int len, int flags)
1006 { 1027 {
1007 char buf[BUF_LEN * 2]; 1028 char buf[BUF_LEN * 2];
1008 char *tmp = g_malloc(strlen(message) * 4 + 1); /* 4 because \n gets replaced with <BR> */ 1029 char *tmp;
1009 1030
1010 strcpy(tmp, message); 1031 tmp = escape_text(message);
1011 escape_text(tmp);
1012 if (strlen(tmp) + 52 > MSG_LEN) { 1032 if (strlen(tmp) + 52 > MSG_LEN) {
1013 g_free(tmp); 1033 g_free(tmp);
1014 return -E2BIG; 1034 return -E2BIG;
1015 } 1035 }
1016 g_snprintf(buf, MSG_LEN - 8, "toc_send_im %s \"%s\"%s", normalize(name), 1036 g_snprintf(buf, MSG_LEN - 8, "toc_send_im %s \"%s\"%s", normalize(name),
1045 } 1065 }
1046 1066
1047 static void toc_set_dir(GaimConnection *g, const char *first, const char *middle, const char *last, 1067 static void toc_set_dir(GaimConnection *g, const char *first, const char *middle, const char *last,
1048 const char *maiden, const char *city, const char *state, const char *country, int web) 1068 const char *maiden, const char *city, const char *state, const char *country, int web)
1049 { 1069 {
1050 char buf2[BUF_LEN * 4], buf[BUF_LEN]; 1070 char *buf3, buf2[BUF_LEN * 4], buf[BUF_LEN];
1051 g_snprintf(buf2, sizeof(buf2), "%s:%s:%s:%s:%s:%s:%s:%s", first, 1071 g_snprintf(buf2, sizeof(buf2), "%s:%s:%s:%s:%s:%s:%s:%s", first,
1052 middle, last, maiden, city, state, country, (web == 1) ? "Y" : ""); 1072 middle, last, maiden, city, state, country, (web == 1) ? "Y" : "");
1053 escape_text(buf2); 1073 buf3 = escape_text(buf2);
1054 g_snprintf(buf, sizeof(buf), "toc_set_dir %s", buf2); 1074 g_snprintf(buf, sizeof(buf), "toc_set_dir %s", buf3);
1075 g_free(buf3);
1055 sflap_send(g, buf, -1, TYPE_DATA); 1076 sflap_send(g, buf, -1, TYPE_DATA);
1056 } 1077 }
1057 1078
1058 static void toc_dir_search(GaimConnection *g, const char *first, const char *middle, const char *last, 1079 static void toc_dir_search(GaimConnection *g, const char *first, const char *middle, const char *last,
1059 const char *maiden, const char *city, const char *state, const char *country, const char *email) 1080 const char *maiden, const char *city, const char *state, const char *country, const char *email)
1064 debug_printf("Searching for: %s,%s,%s,%s,%s,%s,%s\n", first, middle, last, maiden, 1085 debug_printf("Searching for: %s,%s,%s,%s,%s,%s,%s\n", first, middle, last, maiden,
1065 city, state, country); 1086 city, state, country);
1066 sflap_send(g, buf, -1, TYPE_DATA); 1087 sflap_send(g, buf, -1, TYPE_DATA);
1067 } 1088 }
1068 1089
1069 static void toc_set_away(GaimConnection *g, char *state, char *message) 1090 static void toc_set_away(GaimConnection *g, const char *state, const char *message)
1070 { 1091 {
1071 char buf[BUF_LEN * 2]; 1092 char buf[BUF_LEN * 2];
1072 if (g->away) { 1093 if (g->away) {
1073 g_free (g->away); 1094 g_free (g->away);
1074 g->away = NULL; 1095 g->away = NULL;
1075 } 1096 }
1076 if (message) { 1097 if (message) {
1077 char *tmp = g_malloc(strlen(message) * 4 + 1); 1098 char *tmp;
1078 strcpy(tmp, message); 1099 g->away = g_strdup(message);
1079 g->away = g_strdup (message); 1100 tmp = escape_text(message);
1080 escape_text(tmp);
1081 g_snprintf(buf, MSG_LEN, "toc_set_away \"%s\"", tmp); 1101 g_snprintf(buf, MSG_LEN, "toc_set_away \"%s\"", tmp);
1082 g_free(tmp); 1102 g_free(tmp);
1083 } else 1103 } else
1084 g_snprintf(buf, MSG_LEN, "toc_set_away \"\""); 1104 g_snprintf(buf, MSG_LEN, "toc_set_away \"\"");
1085 sflap_send(g, buf, -1, TYPE_DATA); 1105 sflap_send(g, buf, -1, TYPE_DATA);
1086 } 1106 }
1087 1107
1088 static void toc_set_info(GaimConnection *g, const char *info) 1108 static void toc_set_info(GaimConnection *g, const char *info)
1089 { 1109 {
1090 char buf[BUF_LEN * 2], buf2[BUF_LEN * 2]; 1110 char buf[BUF_LEN * 2], *buf2;
1091 g_snprintf(buf2, sizeof buf2, "%s", info); 1111 buf2 = escape_text(info);
1092 escape_text(buf2);
1093 g_snprintf(buf, sizeof(buf), "toc_set_info \"%s\n\"", buf2); 1112 g_snprintf(buf, sizeof(buf), "toc_set_info \"%s\n\"", buf2);
1113 g_free(buf2);
1094 sflap_send(g, buf, -1, TYPE_DATA); 1114 sflap_send(g, buf, -1, TYPE_DATA);
1095 } 1115 }
1096 1116
1097 static void toc_change_passwd(GaimConnection *g, const char *orig, const char *new) 1117 static void toc_change_passwd(GaimConnection *g, const char *orig, const char *new)
1098 { 1118 {
1124 buddies = buddies->next; 1144 buddies = buddies->next;
1125 } 1145 }
1126 sflap_send(g, buf, -1, TYPE_DATA); 1146 sflap_send(g, buf, -1, TYPE_DATA);
1127 } 1147 }
1128 1148
1129 static void toc_remove_buddy(GaimConnection *g, char *name, char *group) 1149 static void toc_remove_buddy(GaimConnection *g, const char *name, const char *group)
1130 { 1150 {
1131 char buf[BUF_LEN * 2]; 1151 char buf[BUF_LEN * 2];
1132 g_snprintf(buf, sizeof(buf), "toc_remove_buddy %s", normalize(name)); 1152 g_snprintf(buf, sizeof(buf), "toc_remove_buddy %s", normalize(name));
1133 sflap_send(g, buf, -1, TYPE_DATA); 1153 sflap_send(g, buf, -1, TYPE_DATA);
1134 toc_set_config(g); 1154 toc_set_config(g);
1157 char buf[BUF_LEN * 2]; 1177 char buf[BUF_LEN * 2];
1158 g_snprintf(buf, sizeof(buf), "toc_set_idle %d", time); 1178 g_snprintf(buf, sizeof(buf), "toc_set_idle %d", time);
1159 sflap_send(g, buf, -1, TYPE_DATA); 1179 sflap_send(g, buf, -1, TYPE_DATA);
1160 } 1180 }
1161 1181
1162 static void toc_warn(GaimConnection *g, char *name, int anon) 1182 static void toc_warn(GaimConnection *g, const char *name, int anon)
1163 { 1183 {
1164 char send[BUF_LEN * 2]; 1184 char send[BUF_LEN * 2];
1165 g_snprintf(send, 255, "toc_evil %s %s", name, ((anon) ? "anon" : "norm")); 1185 g_snprintf(send, 255, "toc_evil %s %s", name, ((anon) ? "anon" : "norm"));
1166 sflap_send(g, send, -1, TYPE_DATA); 1186 sflap_send(g, send, -1, TYPE_DATA);
1167 } 1187 }
1239 g_snprintf(buf, 255, "toc_chat_leave %d", id); 1259 g_snprintf(buf, 255, "toc_chat_leave %d", id);
1240 sflap_send(g, buf, -1, TYPE_DATA); 1260 sflap_send(g, buf, -1, TYPE_DATA);
1241 } 1261 }
1242 } 1262 }
1243 1263
1244 static void toc_chat_whisper(GaimConnection *g, int id, char *who, char *message) 1264 static void toc_chat_whisper(GaimConnection *g, int id, const char *who, const char *message)
1245 { 1265 {
1246 char buf2[BUF_LEN * 2]; 1266 char *buf1, *buf2;
1247 escape_text(message); 1267 buf1 = escape_text(message);
1248 g_snprintf(buf2, sizeof(buf2), "toc_chat_whisper %d %s \"%s\"", id, normalize(who), message); 1268 buf2 = g_strdup_printf("toc_chat_whisper %d %s \"%s\"", id, normalize(who), buf1);
1269 g_free(buf1);
1249 sflap_send(g, buf2, -1, TYPE_DATA); 1270 sflap_send(g, buf2, -1, TYPE_DATA);
1250 } 1271 g_free(buf2);
1251 1272 }
1252 static int toc_chat_send(GaimConnection *g, int id, char *message) 1273
1253 { 1274 static int toc_chat_send(GaimConnection *g, int id, const char *message)
1254 char buf[BUF_LEN * 2]; 1275 {
1255 escape_text(message); 1276 char *buf1, *buf2;
1256 if (strlen(message) > 2000) 1277 buf1 = escape_text(message);
1278 if (strlen(buf1) > 2000) {
1279 g_free(buf1);
1257 return -E2BIG; 1280 return -E2BIG;
1258 g_snprintf(buf, sizeof(buf), "toc_chat_send %d \"%s\"", id, message); 1281 }
1259 sflap_send(g, buf, -1, TYPE_DATA); 1282 buf2 = g_strdup_printf("toc_chat_send %d \"%s\"", id, buf1);
1283 g_free(buf1);
1284 sflap_send(g, buf2, -1, TYPE_DATA);
1285 g_free(buf2);
1260 return 0; 1286 return 0;
1261 } 1287 }
1262 1288
1263 static void toc_keepalive(GaimConnection *gc) 1289 static void toc_keepalive(GaimConnection *gc)
1264 { 1290 {