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