[dab82f29] | 1 | /* Copyright (c) 2002,2003,2004,2009 James M. Kretchmar |
---|
| 2 | * |
---|
| 3 | * This file is part of Owl. |
---|
| 4 | * |
---|
| 5 | * Owl is free software: you can redistribute it and/or modify |
---|
| 6 | * it under the terms of the GNU General Public License as published by |
---|
| 7 | * the Free Software Foundation, either version 3 of the License, or |
---|
| 8 | * (at your option) any later version. |
---|
| 9 | * |
---|
| 10 | * Owl is distributed in the hope that it will be useful, |
---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 13 | * GNU General Public License for more details. |
---|
| 14 | * |
---|
| 15 | * You should have received a copy of the GNU General Public License |
---|
| 16 | * along with Owl. If not, see <http://www.gnu.org/licenses/>. |
---|
| 17 | * |
---|
| 18 | * --------------------------------------------------------------- |
---|
| 19 | * |
---|
| 20 | * As of Owl version 2.1.12 there are patches contributed by |
---|
| 21 | * developers of the the branched BarnOwl project, Copyright (c) |
---|
| 22 | * 2006-2008 The BarnOwl Developers. All rights reserved. |
---|
| 23 | */ |
---|
| 24 | |
---|
[aa5f725] | 25 | #include "owl.h" |
---|
| 26 | |
---|
[bd3f232] | 27 | static const char fileIdent[] = "$Id$"; |
---|
| 28 | |
---|
[f4d0975] | 29 | void owl_buddylist_init(owl_buddylist *bl) |
---|
[aa5f725] | 30 | { |
---|
[f4d0975] | 31 | owl_list_create(&(bl->buddies)); |
---|
| 32 | } |
---|
| 33 | |
---|
| 34 | /* add a (logged-in) AIM buddy to the buddy list |
---|
| 35 | */ |
---|
| 36 | void owl_buddylist_add_aim_buddy(owl_buddylist *bl, char *screenname) |
---|
| 37 | { |
---|
| 38 | owl_buddy *b; |
---|
| 39 | b=owl_malloc(sizeof(owl_buddy)); |
---|
| 40 | |
---|
| 41 | owl_buddy_create(b, OWL_PROTOCOL_AIM, screenname); |
---|
| 42 | owl_list_append_element(&(bl->buddies), b); |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | /* remove an AIM buddy from the buddy list |
---|
| 46 | */ |
---|
| 47 | int owl_buddylist_remove_aim_buddy(owl_buddylist *bl, char *name) |
---|
| 48 | { |
---|
| 49 | int i, j; |
---|
| 50 | owl_buddy *b; |
---|
| 51 | |
---|
| 52 | j=owl_list_get_size(&(bl->buddies)); |
---|
| 53 | for (i=0; i<j; i++) { |
---|
| 54 | b=owl_list_get_element(&(bl->buddies), i); |
---|
| 55 | if (!strcasecmp(name, owl_buddy_get_name(b)) && owl_buddy_is_proto_aim(b)) { |
---|
| 56 | owl_list_remove_element(&(bl->buddies), i); |
---|
| 57 | owl_buddy_free(b); |
---|
| 58 | return(0); |
---|
| 59 | } |
---|
| 60 | } |
---|
| 61 | return(1); |
---|
[aa5f725] | 62 | } |
---|
| 63 | |
---|
| 64 | /* Deal with an "oncoming" message. This means recognizing the user |
---|
[a352335c] | 65 | * has logged in, and displaying a message if they were not already |
---|
[aa5f725] | 66 | * logged in. |
---|
| 67 | */ |
---|
[f4d0975] | 68 | void owl_buddylist_oncoming(owl_buddylist *bl, char *screenname) |
---|
[aa5f725] | 69 | { |
---|
[a352335c] | 70 | owl_message *m; |
---|
[aa5f725] | 71 | |
---|
[f4d0975] | 72 | if (!owl_buddylist_is_aim_buddy_loggedin(bl, screenname)) { |
---|
[aa5f725] | 73 | |
---|
[f4d0975] | 74 | owl_buddylist_add_aim_buddy(bl, screenname); |
---|
[aa5f725] | 75 | |
---|
[ceb8cfb] | 76 | /* are we ingoring login messages for a while? */ |
---|
| 77 | if (!owl_timer_is_expired(owl_global_get_aim_login_timer(&g))) return; |
---|
| 78 | |
---|
[a352335c] | 79 | /* if not, create the login message */ |
---|
[aa5f725] | 80 | m=owl_malloc(sizeof(owl_message)); |
---|
[d559df9] | 81 | owl_message_create_aim(m, |
---|
| 82 | screenname, |
---|
| 83 | owl_global_get_aim_screenname(&g), |
---|
| 84 | "", |
---|
| 85 | OWL_MESSAGE_DIRECTION_IN, |
---|
| 86 | 1); |
---|
[aa5f725] | 87 | owl_global_messagequeue_addmsg(&g, m); |
---|
| 88 | } |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | /* Deal with an "offgoing" message. This means recognizing the user |
---|
| 92 | * has logged out, and sending a message if they were logged in. |
---|
| 93 | */ |
---|
[f4d0975] | 94 | void owl_buddylist_offgoing(owl_buddylist *bl, char *screenname) |
---|
[aa5f725] | 95 | { |
---|
| 96 | owl_message *m; |
---|
| 97 | |
---|
[fe6f1d3] | 98 | if (owl_buddylist_is_aim_buddy_loggedin(bl, screenname)) { |
---|
| 99 | m=owl_malloc(sizeof(owl_message)); |
---|
| 100 | owl_message_create_aim(m, |
---|
| 101 | screenname, |
---|
| 102 | owl_global_get_aim_screenname(&g), |
---|
| 103 | "", |
---|
| 104 | OWL_MESSAGE_DIRECTION_IN, |
---|
| 105 | -1); |
---|
| 106 | owl_global_messagequeue_addmsg(&g, m); |
---|
| 107 | } |
---|
[257a22f] | 108 | |
---|
| 109 | owl_buddylist_remove_aim_buddy(bl, screenname); |
---|
[aa5f725] | 110 | } |
---|
| 111 | |
---|
[f4d0975] | 112 | /* return the number of logged in buddies */ |
---|
| 113 | int owl_buddylist_get_size(owl_buddylist *bl) |
---|
[de03334] | 114 | { |
---|
[f4d0975] | 115 | return(owl_list_get_size(&(bl->buddies))); |
---|
[de03334] | 116 | } |
---|
| 117 | |
---|
[f4d0975] | 118 | /* return the buddy with index N. If out of range, return NULL |
---|
[a352335c] | 119 | */ |
---|
[f4d0975] | 120 | owl_buddy *owl_buddylist_get_buddy_n(owl_buddylist *bl, int index) |
---|
[a352335c] | 121 | { |
---|
[f4d0975] | 122 | if (index<0) return(NULL); |
---|
| 123 | if (index>(owl_buddylist_get_size(bl)-1)) return(NULL); |
---|
[a352335c] | 124 | |
---|
[f4d0975] | 125 | return(owl_list_get_element(&(bl->buddies), index)); |
---|
[aa5f725] | 126 | } |
---|
| 127 | |
---|
[f4d0975] | 128 | /* return the AIM buddy with screenname 'name'. If |
---|
| 129 | * no such buddy is logged in, return NULL. |
---|
[a352335c] | 130 | */ |
---|
[f4d0975] | 131 | owl_buddy *owl_buddylist_get_aim_buddy(owl_buddylist *bl, char *name) |
---|
[a352335c] | 132 | { |
---|
| 133 | int i, j; |
---|
[f4d0975] | 134 | owl_buddy *b; |
---|
| 135 | |
---|
| 136 | j=owl_list_get_size(&(bl->buddies)); |
---|
[a352335c] | 137 | for (i=0; i<j; i++) { |
---|
[f4d0975] | 138 | b=owl_list_get_element(&(bl->buddies), i); |
---|
| 139 | if (!strcasecmp(name, owl_buddy_get_name(b))) return(b); |
---|
[a352335c] | 140 | } |
---|
[f4d0975] | 141 | return(NULL); |
---|
[a352335c] | 142 | } |
---|
| 143 | |
---|
| 144 | /* return 1 if the buddy 'screenname' is logged in, |
---|
| 145 | * otherwise return 0 |
---|
| 146 | */ |
---|
[f4d0975] | 147 | int owl_buddylist_is_aim_buddy_loggedin(owl_buddylist *bl, char *screenname) |
---|
[a352335c] | 148 | { |
---|
[f4d0975] | 149 | owl_buddy *b; |
---|
[a352335c] | 150 | |
---|
[f4d0975] | 151 | b=owl_buddylist_get_aim_buddy(bl, screenname); |
---|
| 152 | if (b==NULL) return(0); |
---|
| 153 | return(1); |
---|
[de03334] | 154 | } |
---|
| 155 | |
---|
[f4d0975] | 156 | /* remove all buddies from the list */ |
---|
| 157 | void owl_buddylist_clear(owl_buddylist *bl) |
---|
[de03334] | 158 | { |
---|
[f4d0975] | 159 | owl_list_free_all(&(bl->buddies), (void(*)(void*))owl_buddy_free); |
---|
| 160 | owl_list_create(&(bl->buddies)); |
---|
[de03334] | 161 | } |
---|
| 162 | |
---|
[f4d0975] | 163 | void owl_buddylist_free(owl_buddylist *bl) |
---|
| 164 | { |
---|
| 165 | owl_list_free_all(&(bl->buddies), (void(*)(void*))owl_buddy_free); |
---|
[aa5f725] | 166 | } |
---|