Changeset 6a1128d for protocols/jabber/jabber_util.c
- Timestamp:
- 2006-10-09T18:19:05Z (17 years ago)
- Branches:
- master
- Children:
- a21a8ac
- Parents:
- 861c199
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
protocols/jabber/jabber_util.c
r861c199 r6a1128d 202 202 g_free( buf ); 203 203 } 204 205 /* Adds a buddy/resource to our list. Returns NULL if full_jid is not really a 206 FULL jid or if we already have this buddy/resource. */ 207 struct jabber_buddy *jabber_buddy_add( struct gaim_connection *gc, char *full_jid ) 208 { 209 struct jabber_data *jd = gc->proto_data; 210 struct jabber_buddy *bud, *new, *bi; 211 char *s; 212 213 if( !( s = strchr( full_jid, '/' ) ) ) 214 return NULL; 215 216 new = g_new0( struct jabber_buddy, 1 ); 217 218 *s = 0; 219 if( ( bud = g_hash_table_lookup( jd->buddies, full_jid ) ) ) 220 { 221 new->handle = bud->handle; 222 223 /* We already have another resource for this buddy, add the 224 new one to the list. */ 225 for( bi = bud; bi; bi = bi->next ) 226 { 227 /* Check for dupes. Resource seem to be case sensitive. */ 228 if( strcmp( bi->resource, s + 1 ) == 0 ) 229 { 230 *s = '/'; 231 g_free( new ); 232 return NULL; 233 } 234 /* Append the new item to the list. */ 235 else if( bi->next == NULL ) 236 { 237 bi->next = new; 238 break; 239 } 240 } 241 } 242 else 243 { 244 new->handle = g_strdup( full_jid ); 245 g_hash_table_insert( jd->buddies, new->handle, new ); 246 } 247 248 *s = '/'; 249 new->resource = g_strdup( s + 1 ); 250 251 return new; 252 } 253 254 struct jabber_buddy *jabber_buddy_by_jid( struct gaim_connection *gc, char *jid ) 255 { 256 struct jabber_data *jd = gc->proto_data; 257 struct jabber_buddy *bud; 258 char *s; 259 260 if( ( s = strchr( jid, '/' ) ) ) 261 { 262 *s = 0; 263 if( ( bud = g_hash_table_lookup( jd->buddies, jid ) ) ) 264 for( ; bud; bud = bud->next ) 265 if( strcmp( bud->resource, s + 1 ) == 0 ) 266 break; 267 } 268 else 269 { 270 /* TODO: Add selection. */ 271 return g_hash_table_lookup( jd->buddies, jid ); 272 } 273 274 *s = '/'; 275 return bud; 276 } 277 278 int jabber_buddy_remove( struct gaim_connection *gc, char *full_jid ) 279 { 280 struct jabber_data *jd = gc->proto_data; 281 struct jabber_buddy *bud, *prev, *bi; 282 char *s; 283 284 if( !( s = strchr( full_jid, '/' ) ) ) 285 return 0; 286 287 *s = 0; 288 if( ( bud = g_hash_table_lookup( jd->buddies, full_jid ) ) ) 289 { 290 /* If there's only one item in the list (and if the resource 291 matches), removing it is simple. (And the hash reference 292 should be removed too!) */ 293 if( bud->next == NULL && strcmp( bud->resource, s + 1 ) == 0 ) 294 { 295 g_hash_table_remove( jd->buddies, bud->handle ); 296 g_free( bud->handle ); 297 g_free( bud->resource ); 298 g_free( bud->away_message ); 299 g_free( bud ); 300 } 301 else 302 { 303 for( bi = bud, prev = NULL; bi; bi = (prev=bi)->next ) 304 if( strcmp( bi->resource, s + 1 ) == 0 ) 305 break; 306 307 if( bi ) 308 { 309 if( prev ) 310 prev->next = bi->next; 311 else 312 /* The hash table should point at the second 313 item, because we're removing the first. */ 314 g_hash_table_replace( jd->buddies, bi->handle, bi->next ); 315 316 g_free( bi->resource ); 317 g_free( bi->away_message ); 318 g_free( bi ); 319 } 320 else 321 { 322 *s = '/'; 323 return 0; 324 } 325 } 326 327 *s = '/'; 328 return 1; 329 } 330 else 331 { 332 *s = '/'; 333 return 0; 334 } 335 }
Note: See TracChangeset
for help on using the changeset viewer.