[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 | |
---|
[fe6f1d3] | 74 | if (owl_buddylist_is_aim_buddy_loggedin(bl, screenname)) { |
---|
| 75 | m=owl_malloc(sizeof(owl_message)); |
---|
| 76 | owl_message_create_aim(m, |
---|
| 77 | screenname, |
---|
| 78 | owl_global_get_aim_screenname(&g), |
---|
| 79 | "", |
---|
| 80 | OWL_MESSAGE_DIRECTION_IN, |
---|
| 81 | -1); |
---|
| 82 | owl_global_messagequeue_addmsg(&g, m); |
---|
| 83 | } |
---|
[257a22f] | 84 | |
---|
| 85 | owl_buddylist_remove_aim_buddy(bl, screenname); |
---|
[aa5f725] | 86 | } |
---|
| 87 | |
---|
[f4d0975] | 88 | /* return the number of logged in buddies */ |
---|
| 89 | int owl_buddylist_get_size(owl_buddylist *bl) |
---|
[de03334] | 90 | { |
---|
[f4d0975] | 91 | return(owl_list_get_size(&(bl->buddies))); |
---|
[de03334] | 92 | } |
---|
| 93 | |
---|
[f4d0975] | 94 | /* return the buddy with index N. If out of range, return NULL |
---|
[a352335c] | 95 | */ |
---|
[f4d0975] | 96 | owl_buddy *owl_buddylist_get_buddy_n(owl_buddylist *bl, int index) |
---|
[a352335c] | 97 | { |
---|
[f4d0975] | 98 | if (index<0) return(NULL); |
---|
| 99 | if (index>(owl_buddylist_get_size(bl)-1)) return(NULL); |
---|
[a352335c] | 100 | |
---|
[f4d0975] | 101 | return(owl_list_get_element(&(bl->buddies), index)); |
---|
[aa5f725] | 102 | } |
---|
| 103 | |
---|
[f4d0975] | 104 | /* return the AIM buddy with screenname 'name'. If |
---|
| 105 | * no such buddy is logged in, return NULL. |
---|
[a352335c] | 106 | */ |
---|
[f4d0975] | 107 | owl_buddy *owl_buddylist_get_aim_buddy(owl_buddylist *bl, char *name) |
---|
[a352335c] | 108 | { |
---|
| 109 | int i, j; |
---|
[f4d0975] | 110 | owl_buddy *b; |
---|
| 111 | |
---|
| 112 | j=owl_list_get_size(&(bl->buddies)); |
---|
[a352335c] | 113 | for (i=0; i<j; i++) { |
---|
[f4d0975] | 114 | b=owl_list_get_element(&(bl->buddies), i); |
---|
| 115 | if (!strcasecmp(name, owl_buddy_get_name(b))) return(b); |
---|
[a352335c] | 116 | } |
---|
[f4d0975] | 117 | return(NULL); |
---|
[a352335c] | 118 | } |
---|
| 119 | |
---|
| 120 | /* return 1 if the buddy 'screenname' is logged in, |
---|
| 121 | * otherwise return 0 |
---|
| 122 | */ |
---|
[f4d0975] | 123 | int owl_buddylist_is_aim_buddy_loggedin(owl_buddylist *bl, char *screenname) |
---|
[a352335c] | 124 | { |
---|
[f4d0975] | 125 | owl_buddy *b; |
---|
[a352335c] | 126 | |
---|
[f4d0975] | 127 | b=owl_buddylist_get_aim_buddy(bl, screenname); |
---|
| 128 | if (b==NULL) return(0); |
---|
| 129 | return(1); |
---|
[de03334] | 130 | } |
---|
| 131 | |
---|
[f4d0975] | 132 | /* remove all buddies from the list */ |
---|
| 133 | void owl_buddylist_clear(owl_buddylist *bl) |
---|
[de03334] | 134 | { |
---|
[f4d0975] | 135 | owl_list_free_all(&(bl->buddies), (void(*)(void*))owl_buddy_free); |
---|
| 136 | owl_list_create(&(bl->buddies)); |
---|
[de03334] | 137 | } |
---|
| 138 | |
---|
[f4d0975] | 139 | void owl_buddylist_free(owl_buddylist *bl) |
---|
| 140 | { |
---|
| 141 | owl_list_free_all(&(bl->buddies), (void(*)(void*))owl_buddy_free); |
---|
[aa5f725] | 142 | } |
---|