1 | #include "owl.h" |
---|
2 | |
---|
3 | static const char fileIdent[] = "$Id$"; |
---|
4 | |
---|
5 | void owl_buddylist_init(owl_buddylist *bl) |
---|
6 | { |
---|
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); |
---|
38 | } |
---|
39 | |
---|
40 | /* Deal with an "oncoming" message. This means recognizing the user |
---|
41 | * has logged in, and displaying a message if they were not already |
---|
42 | * logged in. |
---|
43 | */ |
---|
44 | void owl_buddylist_oncoming(owl_buddylist *bl, char *screenname) |
---|
45 | { |
---|
46 | owl_message *m; |
---|
47 | |
---|
48 | if (!owl_buddylist_is_aim_buddy_loggedin(bl, screenname)) { |
---|
49 | |
---|
50 | owl_buddylist_add_aim_buddy(bl, screenname); |
---|
51 | |
---|
52 | /* are we ingoring login messages for a while? */ |
---|
53 | if (!owl_timer_is_expired(owl_global_get_aim_login_timer(&g))) return; |
---|
54 | |
---|
55 | /* if not, create the login message */ |
---|
56 | m=owl_malloc(sizeof(owl_message)); |
---|
57 | owl_message_create_aim(m, |
---|
58 | screenname, |
---|
59 | owl_global_get_aim_screenname(&g), |
---|
60 | "", |
---|
61 | OWL_MESSAGE_DIRECTION_IN, |
---|
62 | 1); |
---|
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 | */ |
---|
70 | void owl_buddylist_offgoing(owl_buddylist *bl, char *screenname) |
---|
71 | { |
---|
72 | owl_message *m; |
---|
73 | |
---|
74 | owl_buddylist_remove_aim_buddy(bl, screenname); |
---|
75 | |
---|
76 | if (owl_buddylist_is_aim_buddy_loggedin(bl, screenname)) { |
---|
77 | m=owl_malloc(sizeof(owl_message)); |
---|
78 | owl_message_create_aim(m, |
---|
79 | screenname, |
---|
80 | owl_global_get_aim_screenname(&g), |
---|
81 | "", |
---|
82 | OWL_MESSAGE_DIRECTION_IN, |
---|
83 | -1); |
---|
84 | owl_global_messagequeue_addmsg(&g, m); |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | /* return the number of logged in buddies */ |
---|
89 | int owl_buddylist_get_size(owl_buddylist *bl) |
---|
90 | { |
---|
91 | return(owl_list_get_size(&(bl->buddies))); |
---|
92 | } |
---|
93 | |
---|
94 | /* return the buddy with index N. If out of range, return NULL |
---|
95 | */ |
---|
96 | owl_buddy *owl_buddylist_get_buddy_n(owl_buddylist *bl, int index) |
---|
97 | { |
---|
98 | if (index<0) return(NULL); |
---|
99 | if (index>(owl_buddylist_get_size(bl)-1)) return(NULL); |
---|
100 | |
---|
101 | return(owl_list_get_element(&(bl->buddies), index)); |
---|
102 | } |
---|
103 | |
---|
104 | /* return the AIM buddy with screenname 'name'. If |
---|
105 | * no such buddy is logged in, return NULL. |
---|
106 | */ |
---|
107 | owl_buddy *owl_buddylist_get_aim_buddy(owl_buddylist *bl, char *name) |
---|
108 | { |
---|
109 | int i, j; |
---|
110 | owl_buddy *b; |
---|
111 | |
---|
112 | j=owl_list_get_size(&(bl->buddies)); |
---|
113 | for (i=0; i<j; i++) { |
---|
114 | b=owl_list_get_element(&(bl->buddies), i); |
---|
115 | if (!strcasecmp(name, owl_buddy_get_name(b))) return(b); |
---|
116 | } |
---|
117 | return(NULL); |
---|
118 | } |
---|
119 | |
---|
120 | /* return 1 if the buddy 'screenname' is logged in, |
---|
121 | * otherwise return 0 |
---|
122 | */ |
---|
123 | int owl_buddylist_is_aim_buddy_loggedin(owl_buddylist *bl, char *screenname) |
---|
124 | { |
---|
125 | owl_buddy *b; |
---|
126 | |
---|
127 | b=owl_buddylist_get_aim_buddy(bl, screenname); |
---|
128 | if (b==NULL) return(0); |
---|
129 | return(1); |
---|
130 | } |
---|
131 | |
---|
132 | /* remove all buddies from the list */ |
---|
133 | void owl_buddylist_clear(owl_buddylist *bl) |
---|
134 | { |
---|
135 | owl_list_free_all(&(bl->buddies), (void(*)(void*))owl_buddy_free); |
---|
136 | owl_list_create(&(bl->buddies)); |
---|
137 | } |
---|
138 | |
---|
139 | void owl_buddylist_free(owl_buddylist *bl) |
---|
140 | { |
---|
141 | owl_list_free_all(&(bl->buddies), (void(*)(void*))owl_buddy_free); |
---|
142 | } |
---|