[aa5f725] | 1 | #include "owl.h" |
---|
| 2 | |
---|
[bd3f232] | 3 | static const char fileIdent[] = "$Id$"; |
---|
| 4 | |
---|
[f4d0975] | 5 | void owl_buddylist_init(owl_buddylist *bl) |
---|
[aa5f725] | 6 | { |
---|
[f4d0975] | 7 | owl_list_create(&(bl->buddies)); |
---|
| 8 | } |
---|
| 9 | |
---|
| 10 | /* add a (logged-in) AIM buddy to the buddy list |
---|
| 11 | */ |
---|
| 12 | void owl_buddylist_add_aim_buddy(owl_buddylist *bl, char *screenname) |
---|
| 13 | { |
---|
| 14 | owl_buddy *b; |
---|
| 15 | b=owl_malloc(sizeof(owl_buddy)); |
---|
| 16 | |
---|
| 17 | owl_buddy_create(b, OWL_PROTOCOL_AIM, screenname); |
---|
| 18 | owl_list_append_element(&(bl->buddies), b); |
---|
| 19 | } |
---|
| 20 | |
---|
| 21 | /* remove an AIM buddy from the buddy list |
---|
| 22 | */ |
---|
| 23 | int owl_buddylist_remove_aim_buddy(owl_buddylist *bl, char *name) |
---|
| 24 | { |
---|
| 25 | int i, j; |
---|
| 26 | owl_buddy *b; |
---|
| 27 | |
---|
| 28 | j=owl_list_get_size(&(bl->buddies)); |
---|
| 29 | for (i=0; i<j; i++) { |
---|
| 30 | b=owl_list_get_element(&(bl->buddies), i); |
---|
| 31 | if (!strcasecmp(name, owl_buddy_get_name(b)) && owl_buddy_is_proto_aim(b)) { |
---|
| 32 | owl_list_remove_element(&(bl->buddies), i); |
---|
| 33 | owl_buddy_free(b); |
---|
| 34 | return(0); |
---|
| 35 | } |
---|
| 36 | } |
---|
| 37 | return(1); |
---|
[aa5f725] | 38 | } |
---|
| 39 | |
---|
| 40 | /* Deal with an "oncoming" message. This means recognizing the user |
---|
[a352335c] | 41 | * has logged in, and displaying a message if they were not already |
---|
[aa5f725] | 42 | * logged in. |
---|
| 43 | */ |
---|
[f4d0975] | 44 | void owl_buddylist_oncoming(owl_buddylist *bl, char *screenname) |
---|
[aa5f725] | 45 | { |
---|
[a352335c] | 46 | owl_message *m; |
---|
[aa5f725] | 47 | |
---|
[f4d0975] | 48 | if (!owl_buddylist_is_aim_buddy_loggedin(bl, screenname)) { |
---|
[aa5f725] | 49 | |
---|
[f4d0975] | 50 | owl_buddylist_add_aim_buddy(bl, screenname); |
---|
[aa5f725] | 51 | |
---|
[ceb8cfb] | 52 | /* are we ingoring login messages for a while? */ |
---|
| 53 | if (!owl_timer_is_expired(owl_global_get_aim_login_timer(&g))) return; |
---|
| 54 | |
---|
[a352335c] | 55 | /* if not, create the login message */ |
---|
[aa5f725] | 56 | m=owl_malloc(sizeof(owl_message)); |
---|
[d559df9] | 57 | owl_message_create_aim(m, |
---|
| 58 | screenname, |
---|
| 59 | owl_global_get_aim_screenname(&g), |
---|
| 60 | "", |
---|
| 61 | OWL_MESSAGE_DIRECTION_IN, |
---|
| 62 | 1); |
---|
[aa5f725] | 63 | owl_global_messagequeue_addmsg(&g, m); |
---|
| 64 | } |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | /* Deal with an "offgoing" message. This means recognizing the user |
---|
| 68 | * has logged out, and sending a message if they were logged in. |
---|
| 69 | */ |
---|
[f4d0975] | 70 | void owl_buddylist_offgoing(owl_buddylist *bl, char *screenname) |
---|
[aa5f725] | 71 | { |
---|
| 72 | owl_message *m; |
---|
| 73 | |
---|
[f4d0975] | 74 | owl_buddylist_remove_aim_buddy(bl, screenname); |
---|
| 75 | |
---|
[a352335c] | 76 | m=owl_malloc(sizeof(owl_message)); |
---|
| 77 | owl_message_create_aim(m, |
---|
| 78 | screenname, |
---|
| 79 | owl_global_get_aim_screenname(&g), |
---|
| 80 | "", |
---|
| 81 | OWL_MESSAGE_DIRECTION_IN, |
---|
| 82 | -1); |
---|
| 83 | owl_global_messagequeue_addmsg(&g, m); |
---|
[aa5f725] | 84 | } |
---|
| 85 | |
---|
[f4d0975] | 86 | /* return the number of logged in buddies */ |
---|
| 87 | int owl_buddylist_get_size(owl_buddylist *bl) |
---|
[de03334] | 88 | { |
---|
[f4d0975] | 89 | return(owl_list_get_size(&(bl->buddies))); |
---|
[de03334] | 90 | } |
---|
| 91 | |
---|
[f4d0975] | 92 | /* return the buddy with index N. If out of range, return NULL |
---|
[a352335c] | 93 | */ |
---|
[f4d0975] | 94 | owl_buddy *owl_buddylist_get_buddy_n(owl_buddylist *bl, int index) |
---|
[a352335c] | 95 | { |
---|
[f4d0975] | 96 | if (index<0) return(NULL); |
---|
| 97 | if (index>(owl_buddylist_get_size(bl)-1)) return(NULL); |
---|
[a352335c] | 98 | |
---|
[f4d0975] | 99 | return(owl_list_get_element(&(bl->buddies), index)); |
---|
[aa5f725] | 100 | } |
---|
| 101 | |
---|
[f4d0975] | 102 | /* return the AIM buddy with screenname 'name'. If |
---|
| 103 | * no such buddy is logged in, return NULL. |
---|
[a352335c] | 104 | */ |
---|
[f4d0975] | 105 | owl_buddy *owl_buddylist_get_aim_buddy(owl_buddylist *bl, char *name) |
---|
[a352335c] | 106 | { |
---|
| 107 | int i, j; |
---|
[f4d0975] | 108 | owl_buddy *b; |
---|
| 109 | |
---|
| 110 | j=owl_list_get_size(&(bl->buddies)); |
---|
[a352335c] | 111 | for (i=0; i<j; i++) { |
---|
[f4d0975] | 112 | b=owl_list_get_element(&(bl->buddies), i); |
---|
| 113 | if (!strcasecmp(name, owl_buddy_get_name(b))) return(b); |
---|
[a352335c] | 114 | } |
---|
[f4d0975] | 115 | return(NULL); |
---|
[a352335c] | 116 | } |
---|
| 117 | |
---|
| 118 | /* return 1 if the buddy 'screenname' is logged in, |
---|
| 119 | * otherwise return 0 |
---|
| 120 | */ |
---|
[f4d0975] | 121 | int owl_buddylist_is_aim_buddy_loggedin(owl_buddylist *bl, char *screenname) |
---|
[a352335c] | 122 | { |
---|
[f4d0975] | 123 | owl_buddy *b; |
---|
[a352335c] | 124 | |
---|
[f4d0975] | 125 | b=owl_buddylist_get_aim_buddy(bl, screenname); |
---|
| 126 | if (b==NULL) return(0); |
---|
| 127 | return(1); |
---|
[de03334] | 128 | } |
---|
| 129 | |
---|
[f4d0975] | 130 | /* remove all buddies from the list */ |
---|
| 131 | void owl_buddylist_clear(owl_buddylist *bl) |
---|
[de03334] | 132 | { |
---|
[f4d0975] | 133 | owl_list_free_all(&(bl->buddies), (void(*)(void*))owl_buddy_free); |
---|
| 134 | owl_list_create(&(bl->buddies)); |
---|
[de03334] | 135 | } |
---|
| 136 | |
---|
[f4d0975] | 137 | void owl_buddylist_free(owl_buddylist *bl) |
---|
| 138 | { |
---|
| 139 | owl_list_free_all(&(bl->buddies), (void(*)(void*))owl_buddy_free); |
---|
[aa5f725] | 140 | } |
---|