[aa5f725] | 1 | #include "owl.h" |
---|
| 2 | |
---|
[bd3f232] | 3 | static const char fileIdent[] = "$Id$"; |
---|
| 4 | |
---|
[aa5f725] | 5 | void owl_buddylist_init(owl_buddylist *b) |
---|
| 6 | { |
---|
| 7 | owl_list_create(&(b->buddies)); |
---|
[de03334] | 8 | owl_list_create(&(b->idletimes)); |
---|
[2404c3a] | 9 | /* owl_list_create(&(g->buddymsg_queue)); */ |
---|
[aa5f725] | 10 | } |
---|
| 11 | |
---|
| 12 | /* Deal with an "oncoming" message. This means recognizing the user |
---|
[a352335c] | 13 | * has logged in, and displaying a message if they were not already |
---|
[aa5f725] | 14 | * logged in. |
---|
| 15 | */ |
---|
| 16 | void owl_buddylist_oncoming(owl_buddylist *b, char *screenname) |
---|
| 17 | { |
---|
[de03334] | 18 | int *zero; |
---|
[a352335c] | 19 | owl_message *m; |
---|
[aa5f725] | 20 | |
---|
[a352335c] | 21 | if (!owl_buddylist_is_buddy_loggedin(b, screenname)) { |
---|
[aa5f725] | 22 | |
---|
[de03334] | 23 | /* add the buddy */ |
---|
[aa5f725] | 24 | owl_list_append_element(&(b->buddies), owl_strdup(screenname)); |
---|
[de03334] | 25 | zero=owl_malloc(sizeof(int)); |
---|
| 26 | *zero=0; |
---|
| 27 | owl_list_append_element(&(b->idletimes), zero); |
---|
[aa5f725] | 28 | |
---|
[de03334] | 29 | /* do a request for idle time */ |
---|
[a352335c] | 30 | owl_buddylist_request_idletime(b, screenname); |
---|
[de03334] | 31 | |
---|
[ceb8cfb] | 32 | /* are we ingoring login messages for a while? */ |
---|
| 33 | if (!owl_timer_is_expired(owl_global_get_aim_login_timer(&g))) return; |
---|
| 34 | |
---|
[a352335c] | 35 | /* if not, create the login message */ |
---|
[aa5f725] | 36 | m=owl_malloc(sizeof(owl_message)); |
---|
[d559df9] | 37 | owl_message_create_aim(m, |
---|
| 38 | screenname, |
---|
| 39 | owl_global_get_aim_screenname(&g), |
---|
| 40 | "", |
---|
| 41 | OWL_MESSAGE_DIRECTION_IN, |
---|
| 42 | 1); |
---|
[aa5f725] | 43 | owl_global_messagequeue_addmsg(&g, m); |
---|
| 44 | } |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | /* Deal with an "offgoing" message. This means recognizing the user |
---|
| 48 | * has logged out, and sending a message if they were logged in. |
---|
| 49 | */ |
---|
| 50 | void owl_buddylist_offgoing(owl_buddylist *b, char *screenname) |
---|
| 51 | { |
---|
[a352335c] | 52 | int index; |
---|
[aa5f725] | 53 | owl_message *m; |
---|
| 54 | |
---|
[a352335c] | 55 | index=owl_buddylist_get_buddy_index(b, screenname); |
---|
| 56 | if (index==-1) return; |
---|
| 57 | |
---|
| 58 | owl_free(owl_list_get_element(&(b->buddies), index)); |
---|
| 59 | owl_free(owl_list_get_element(&(b->idletimes), index)); |
---|
| 60 | owl_list_remove_element(&(b->buddies), index); |
---|
| 61 | owl_list_remove_element(&(b->idletimes), index); |
---|
| 62 | |
---|
| 63 | m=owl_malloc(sizeof(owl_message)); |
---|
| 64 | owl_message_create_aim(m, |
---|
| 65 | screenname, |
---|
| 66 | owl_global_get_aim_screenname(&g), |
---|
| 67 | "", |
---|
| 68 | OWL_MESSAGE_DIRECTION_IN, |
---|
| 69 | -1); |
---|
| 70 | owl_global_messagequeue_addmsg(&g, m); |
---|
[aa5f725] | 71 | } |
---|
| 72 | |
---|
[de03334] | 73 | /* send requests to the AIM server to retrieve info |
---|
| 74 | * on all buddies. The AIM callback then fills in the |
---|
| 75 | * values when the responses are received |
---|
| 76 | */ |
---|
| 77 | void owl_buddylist_request_idletimes(owl_buddylist *b) |
---|
| 78 | { |
---|
| 79 | int i, j; |
---|
| 80 | |
---|
| 81 | j=owl_buddylist_get_size(b); |
---|
| 82 | for (i=0; i<j; i++) { |
---|
| 83 | owl_aim_get_idle(owl_buddylist_get_buddy(b, i)); |
---|
| 84 | } |
---|
| 85 | } |
---|
| 86 | |
---|
[a352335c] | 87 | /* send request to the AIM server to retrieve info on one buddy. The |
---|
| 88 | * AIM callback then fills in the values when the responses are |
---|
| 89 | * received. The buddy must be logged in or no request will be |
---|
| 90 | * performed. |
---|
| 91 | */ |
---|
| 92 | void owl_buddylist_request_idletime(owl_buddylist *b, char *screenname) |
---|
| 93 | { |
---|
| 94 | if (!owl_buddylist_is_buddy_loggedin(b, screenname)) return; |
---|
[2404c3a] | 95 | |
---|
[a352335c] | 96 | owl_aim_get_idle(screenname); |
---|
| 97 | } |
---|
| 98 | |
---|
[b2b0773] | 99 | /* return the number of logged in buddies */ |
---|
[aa5f725] | 100 | int owl_buddylist_get_size(owl_buddylist *b) |
---|
| 101 | { |
---|
| 102 | return(owl_list_get_size(&(b->buddies))); |
---|
| 103 | } |
---|
| 104 | |
---|
[b2b0773] | 105 | /* get buddy number 'n' */ |
---|
[aa5f725] | 106 | char *owl_buddylist_get_buddy(owl_buddylist *b, int n) |
---|
| 107 | { |
---|
[a352335c] | 108 | if (n > owl_buddylist_get_size(b)-1) return(""); |
---|
[aa5f725] | 109 | return(owl_list_get_element(&(b->buddies), n)); |
---|
| 110 | } |
---|
| 111 | |
---|
[a352335c] | 112 | /* Return the index of the buddy 'screename' or -1 |
---|
| 113 | * if the buddy is not logged in. |
---|
| 114 | */ |
---|
| 115 | int owl_buddylist_get_buddy_index(owl_buddylist *b, char *screenname) |
---|
| 116 | { |
---|
| 117 | int i, j; |
---|
| 118 | |
---|
| 119 | j=owl_list_get_size(&(b->buddies)); |
---|
| 120 | for (i=0; i<j; i++) { |
---|
| 121 | if (!strcasecmp(owl_list_get_element(&(b->buddies), i), screenname)) { |
---|
| 122 | return(i); |
---|
| 123 | } |
---|
| 124 | } |
---|
| 125 | return(-1); |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | /* return 1 if the buddy 'screenname' is logged in, |
---|
| 129 | * otherwise return 0 |
---|
| 130 | */ |
---|
| 131 | int owl_buddylist_is_buddy_loggedin(owl_buddylist *b, char *screenname) |
---|
| 132 | { |
---|
| 133 | if (owl_buddylist_get_buddy_index(b, screenname)!=-1) return(1); |
---|
| 134 | return(0); |
---|
| 135 | } |
---|
| 136 | |
---|
[de03334] | 137 | /* get the idle time for buddy number 'n' */ |
---|
| 138 | int owl_buddylist_get_idletime(owl_buddylist *b, int n) |
---|
| 139 | { |
---|
| 140 | int *foo; |
---|
| 141 | |
---|
| 142 | foo=owl_list_get_element(&(b->idletimes), n); |
---|
| 143 | return(*foo); |
---|
| 144 | } |
---|
| 145 | |
---|
[2404c3a] | 146 | /* Set the idle time for user 'screenname'. If the given screenname |
---|
| 147 | * is not on the buddy list do nothing. If there is a queued request |
---|
| 148 | * for this screename, remove it from the queue. |
---|
[de03334] | 149 | */ |
---|
[5b85d19] | 150 | void owl_buddylist_set_idletime(owl_buddylist *b, char *screenname, int minutes) |
---|
[de03334] | 151 | { |
---|
[a352335c] | 152 | int index, *idle; |
---|
[de03334] | 153 | |
---|
[a352335c] | 154 | index=owl_buddylist_get_buddy_index(b, screenname); |
---|
| 155 | if (index==-1) return; |
---|
| 156 | |
---|
| 157 | owl_free(owl_list_get_element(&(b->idletimes), index)); |
---|
| 158 | idle=owl_malloc(sizeof(int)); |
---|
| 159 | *idle=minutes; |
---|
| 160 | owl_list_replace_element(&(b->idletimes), index, idle); |
---|
[de03334] | 161 | } |
---|
| 162 | |
---|
[b2b0773] | 163 | /* remove all buddies from the list */ |
---|
[aa5f725] | 164 | void owl_buddylist_clear(owl_buddylist *b) { |
---|
| 165 | owl_list_free_all(&(b->buddies), owl_free); |
---|
[de03334] | 166 | owl_list_free_all(&(b->idletimes), owl_free); |
---|
[aa5f725] | 167 | owl_list_create(&(b->buddies)); |
---|
[de03334] | 168 | owl_list_create(&(b->idletimes)); |
---|
[aa5f725] | 169 | } |
---|