Mercurial > pidgin
annotate src/protocols/msn/object.c @ 9776:1e5ef71c9583
[gaim-migrate @ 10644]
A patch from Daniel Atallah that should fix sf bug 1008489:
"Windows Messenger BOT Crashes Gaim"
Someone MSN-savvy should check this to make sure it's a valid fix.
committer: Tailor Script <tailor@pidgin.im>
| author | Mark Doliner <mark@kingant.net> |
|---|---|
| date | Thu, 19 Aug 2004 01:13:58 +0000 |
| parents | ab6636c5a136 |
| children | 54377b120d19 |
| rev | line source |
|---|---|
| 9193 | 1 /** |
| 2 * @file object.c MSNObject API | |
| 3 * | |
| 4 * gaim | |
| 5 * | |
|
9198
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
6 * Gaim is the legal property of its developers, whose names are too numerous |
|
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
7 * to list here. Please refer to the COPYRIGHT file distributed with this |
|
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
8 * source distribution. |
| 9193 | 9 * |
| 10 * This program is free software; you can redistribute it and/or modify | |
| 11 * it under the terms of the GNU General Public License as published by | |
| 12 * the Free Software Foundation; either version 2 of the License, or | |
| 13 * (at your option) any later version. | |
| 14 * | |
| 15 * This program is distributed in the hope that it will be useful, | |
| 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 18 * GNU General Public License for more details. | |
| 19 * | |
| 20 * You should have received a copy of the GNU General Public License | |
| 21 * along with this program; if not, write to the Free Software | |
| 22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 23 */ | |
| 24 #include "object.h" | |
| 25 | |
| 26 #define GET_STRING_TAG(field, id) \ | |
| 27 if ((tag = strstr(str, id "=\"")) != NULL) \ | |
| 28 { \ | |
| 29 tag += strlen(id "=\""); \ | |
| 30 c = strchr(tag, '"'); \ | |
| 31 obj->field = g_strndup(tag, c - tag); \ | |
| 32 } | |
| 33 | |
| 34 #define GET_INT_TAG(field, id) \ | |
| 35 if ((tag = strstr(str, id "=\"")) != NULL) \ | |
| 36 { \ | |
| 37 char buf[16]; \ | |
| 38 tag += strlen(id "=\""); \ | |
| 39 c = strchr(tag, '"'); \ | |
| 40 strncpy(buf, tag, c - tag); \ | |
| 41 buf[c - tag] = '\0'; \ | |
| 42 obj->field = atoi(buf); \ | |
| 43 } | |
| 44 | |
| 45 static GList *local_objs; | |
| 46 | |
| 47 MsnObject * | |
| 48 msn_object_new(void) | |
| 49 { | |
| 50 MsnObject *obj; | |
| 51 | |
| 52 obj = g_new0(MsnObject, 1); | |
| 53 | |
| 54 msn_object_set_type(obj, MSN_OBJECT_UNKNOWN); | |
| 55 msn_object_set_friendly(obj, "AAA="); | |
| 56 | |
| 57 return obj; | |
| 58 } | |
| 59 | |
| 60 MsnObject * | |
| 61 msn_object_new_from_string(const char *str) | |
| 62 { | |
| 63 MsnObject *obj; | |
| 64 char *tag, *c; | |
| 65 | |
| 66 g_return_val_if_fail(str != NULL, NULL); | |
| 67 g_return_val_if_fail(!strncmp(str, "<msnobj ", 8), NULL); | |
| 68 | |
| 69 obj = msn_object_new(); | |
| 70 | |
| 71 GET_STRING_TAG(creator, "Creator"); | |
| 72 GET_INT_TAG(size, "Size"); | |
| 73 GET_INT_TAG(type, "Type"); | |
| 74 GET_STRING_TAG(location, "Location"); | |
| 75 GET_STRING_TAG(friendly, "Friendly"); | |
| 76 GET_STRING_TAG(sha1d, "SHA1D"); | |
| 77 GET_STRING_TAG(sha1c, "SHA1C"); | |
| 78 | |
| 9776 | 79 /* If we are missing any of the required elements then discard the object */ |
| 80 if (obj->creator == NULL || obj->size == 0 || obj->type == 0 | |
| 81 || obj->location == NULL || obj->friendly == NULL | |
| 82 || obj->sha1d == NULL || obj->sha1c == NULL) { | |
| 83 msn_object_destroy(obj); | |
| 84 obj = NULL; | |
| 85 } | |
| 86 | |
| 9193 | 87 return obj; |
| 88 } | |
| 89 | |
| 90 void | |
| 91 msn_object_destroy(MsnObject *obj) | |
| 92 { | |
| 93 g_return_if_fail(obj != NULL); | |
| 94 | |
| 95 if (obj->creator != NULL) | |
| 96 g_free(obj->creator); | |
| 97 | |
| 98 if (obj->location != NULL) | |
| 99 g_free(obj->location); | |
| 100 | |
| 101 if (obj->friendly != NULL) | |
| 102 g_free(obj->friendly); | |
| 103 | |
| 104 if (obj->sha1d != NULL) | |
| 105 g_free(obj->sha1d); | |
| 106 | |
| 107 if (obj->sha1c != NULL) | |
| 108 g_free(obj->sha1c); | |
|
9198
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
109 |
| 9193 | 110 if (obj->local) |
| 111 local_objs = g_list_remove(local_objs, obj); | |
| 112 | |
| 113 g_free(obj); | |
| 114 } | |
| 115 | |
| 116 char * | |
| 117 msn_object_to_string(const MsnObject *obj) | |
| 118 { | |
| 119 char *str; | |
| 120 | |
| 121 g_return_val_if_fail(obj != NULL, NULL); | |
| 122 | |
| 123 str = g_strdup_printf("<msnobj Creator=\"%s\" Size=\"%d\" Type=\"%d\" " | |
| 124 "Location=\"%s\" Friendly=\"%s\" SHA1D=\"%s\" " | |
| 125 "SHA1C=\"%s\"/>", | |
| 126 msn_object_get_creator(obj), | |
| 127 msn_object_get_size(obj), | |
| 128 msn_object_get_type(obj), | |
| 129 msn_object_get_location(obj), | |
| 130 msn_object_get_friendly(obj), | |
| 131 msn_object_get_sha1d(obj), | |
| 132 msn_object_get_sha1c(obj)); | |
| 133 | |
| 134 return str; | |
| 135 } | |
| 136 | |
| 137 void | |
| 138 msn_object_set_creator(MsnObject *obj, const char *creator) | |
| 139 { | |
| 140 g_return_if_fail(obj != NULL); | |
| 141 | |
| 142 if (obj->creator != NULL) | |
| 143 g_free(obj->creator); | |
| 144 | |
| 145 obj->creator = (creator == NULL ? NULL : g_strdup(creator)); | |
| 146 } | |
| 147 | |
| 148 void | |
| 149 msn_object_set_size(MsnObject *obj, int size) | |
| 150 { | |
| 151 g_return_if_fail(obj != NULL); | |
| 152 | |
| 153 obj->size = size; | |
| 154 } | |
| 155 | |
| 156 void | |
| 157 msn_object_set_type(MsnObject *obj, MsnObjectType type) | |
| 158 { | |
| 159 g_return_if_fail(obj != NULL); | |
| 160 | |
| 161 obj->type = type; | |
| 162 } | |
| 163 | |
| 164 void | |
| 165 msn_object_set_location(MsnObject *obj, const char *location) | |
| 166 { | |
| 167 g_return_if_fail(obj != NULL); | |
| 168 | |
| 169 if (obj->location != NULL) | |
| 170 g_free(obj->location); | |
| 171 | |
| 172 obj->location = (location == NULL ? NULL : g_strdup(location)); | |
| 173 } | |
| 174 | |
| 175 void | |
| 176 msn_object_set_friendly(MsnObject *obj, const char *friendly) | |
| 177 { | |
| 178 g_return_if_fail(obj != NULL); | |
| 179 | |
| 180 if (obj->friendly != NULL) | |
| 181 g_free(obj->friendly); | |
| 182 | |
| 183 obj->friendly = (friendly == NULL ? NULL : g_strdup(friendly)); | |
| 184 } | |
| 185 | |
| 186 void | |
| 187 msn_object_set_sha1d(MsnObject *obj, const char *sha1d) | |
| 188 { | |
| 189 g_return_if_fail(obj != NULL); | |
| 190 | |
| 191 if (obj->sha1d != NULL) | |
| 192 g_free(obj->sha1d); | |
| 193 | |
| 194 obj->sha1d = (sha1d == NULL ? NULL : g_strdup(sha1d)); | |
| 195 } | |
| 196 | |
| 197 void | |
| 198 msn_object_set_sha1c(MsnObject *obj, const char *sha1c) | |
| 199 { | |
| 200 g_return_if_fail(obj != NULL); | |
| 201 | |
| 202 if (obj->sha1c != NULL) | |
| 203 g_free(obj->sha1c); | |
| 204 | |
| 205 obj->sha1c = (sha1c == NULL ? NULL : g_strdup(sha1c)); | |
| 206 } | |
| 207 | |
| 208 const char * | |
| 209 msn_object_get_creator(const MsnObject *obj) | |
| 210 { | |
| 211 g_return_val_if_fail(obj != NULL, NULL); | |
| 212 | |
| 213 return obj->creator; | |
| 214 } | |
| 215 | |
| 216 int | |
| 217 msn_object_get_size(const MsnObject *obj) | |
| 218 { | |
| 219 g_return_val_if_fail(obj != NULL, 0); | |
| 220 | |
| 221 return obj->size; | |
| 222 } | |
| 223 | |
| 224 MsnObjectType | |
| 225 msn_object_get_type(const MsnObject *obj) | |
| 226 { | |
| 227 g_return_val_if_fail(obj != NULL, MSN_OBJECT_UNKNOWN); | |
| 228 | |
| 229 return obj->type; | |
| 230 } | |
| 231 | |
| 232 const char * | |
| 233 msn_object_get_location(const MsnObject *obj) | |
| 234 { | |
| 235 g_return_val_if_fail(obj != NULL, NULL); | |
| 236 | |
| 237 return obj->location; | |
| 238 } | |
| 239 | |
| 240 const char * | |
| 241 msn_object_get_friendly(const MsnObject *obj) | |
| 242 { | |
| 243 g_return_val_if_fail(obj != NULL, NULL); | |
| 244 | |
| 245 return obj->friendly; | |
| 246 } | |
| 247 | |
| 248 const char * | |
| 249 msn_object_get_sha1d(const MsnObject *obj) | |
| 250 { | |
| 251 g_return_val_if_fail(obj != NULL, NULL); | |
| 252 | |
| 253 return obj->sha1d; | |
| 254 } | |
| 255 | |
| 256 const char * | |
| 257 msn_object_get_sha1c(const MsnObject *obj) | |
| 258 { | |
| 259 g_return_val_if_fail(obj != NULL, NULL); | |
| 260 | |
| 261 return obj->sha1c; | |
| 262 } | |
| 263 | |
| 264 MsnObject * | |
| 265 msn_object_find_local(const char *sha1c) | |
| 266 { | |
| 267 GList *l; | |
|
9198
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
268 |
| 9193 | 269 g_return_val_if_fail(sha1c != NULL, NULL); |
| 270 | |
| 271 for (l = local_objs; l != NULL; l = l->next) | |
| 272 { | |
| 273 MsnObject *local_obj = l->data; | |
| 274 | |
| 275 if (!strcmp(msn_object_get_sha1c(local_obj), sha1c)) | |
| 276 return local_obj; | |
| 277 } | |
| 278 | |
| 279 return NULL; | |
| 280 | |
| 281 } | |
| 282 | |
| 283 void | |
| 284 msn_object_set_local(MsnObject *obj) | |
| 285 { | |
| 286 g_return_if_fail(obj != NULL); | |
| 287 | |
| 288 obj->local = TRUE; | |
| 289 | |
| 290 local_objs = g_list_append(local_objs, obj); | |
| 291 } | |
| 292 | |
| 293 void | |
| 294 msn_object_set_real_location(MsnObject *obj, const char *real_location) | |
| 295 { | |
| 296 g_return_if_fail(obj != NULL); | |
|
9198
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
297 |
| 9193 | 298 /* obj->local = TRUE; */ |
| 299 | |
| 300 if (obj->real_location != NULL) | |
| 301 g_free(obj->real_location); | |
| 302 | |
| 303 obj->real_location = | |
| 304 (real_location == NULL ? NULL : g_strdup(real_location)); | |
| 305 } | |
| 306 | |
| 307 const char * | |
| 308 msn_object_get_real_location(const MsnObject *obj) | |
| 309 { | |
| 310 MsnObject *local_obj; | |
|
9198
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
311 |
| 9193 | 312 g_return_val_if_fail(obj != NULL, NULL); |
| 313 | |
| 314 local_obj = msn_object_find_local(msn_object_get_sha1c(obj)); | |
| 315 | |
| 316 if (local_obj != NULL) | |
| 317 return local_obj->real_location; | |
|
9198
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
318 |
| 9193 | 319 return NULL; |
| 320 } |
