forked from aglasgall/barnowl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buddylist.c
137 lines (115 loc) · 3.36 KB
/
buddylist.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "owl.h"
void owl_buddylist_init(owl_buddylist *bl)
{
bl->buddies = g_ptr_array_new();
}
/* add a (logged-in) AIM buddy to the buddy list
*/
void owl_buddylist_add_aim_buddy(owl_buddylist *bl, const char *screenname)
{
owl_buddy *b;
b=g_slice_new(owl_buddy);
owl_buddy_create(b, OWL_PROTOCOL_AIM, screenname);
g_ptr_array_add(bl->buddies, b);
}
/* remove an AIM buddy from the buddy list
*/
int owl_buddylist_remove_aim_buddy(owl_buddylist *bl, const char *name)
{
int i;
owl_buddy *b;
for (i = 0; i < bl->buddies->len; i++) {
b = bl->buddies->pdata[i];
if (!strcasecmp(name, owl_buddy_get_name(b)) && owl_buddy_is_proto_aim(b)) {
owl_buddy_delete(g_ptr_array_remove_index(bl->buddies, i));
return(0);
}
}
return(1);
}
/* Deal with an "oncoming" message. This means recognizing the user
* has logged in, and displaying a message if they were not already
* logged in.
*/
void owl_buddylist_oncoming(owl_buddylist *bl, const char *screenname)
{
owl_message *m;
if (!owl_buddylist_is_aim_buddy_loggedin(bl, screenname)) {
owl_buddylist_add_aim_buddy(bl, screenname);
/* are we ingoring login messages for a while? */
if (owl_global_is_ignore_aimlogin(&g)) return;
/* if not, create the login message */
m=g_slice_new(owl_message);
owl_message_create_aim(m,
screenname,
owl_global_get_aim_screenname(&g),
"",
OWL_MESSAGE_DIRECTION_IN,
1);
owl_global_messagequeue_addmsg(&g, m);
}
}
/* Deal with an "offgoing" message. This means recognizing the user
* has logged out, and sending a message if they were logged in.
*/
void owl_buddylist_offgoing(owl_buddylist *bl, const char *screenname)
{
owl_message *m;
if (owl_buddylist_is_aim_buddy_loggedin(bl, screenname)) {
m=g_slice_new(owl_message);
owl_message_create_aim(m,
screenname,
owl_global_get_aim_screenname(&g),
"",
OWL_MESSAGE_DIRECTION_IN,
-1);
owl_global_messagequeue_addmsg(&g, m);
}
owl_buddylist_remove_aim_buddy(bl, screenname);
}
/* return the number of logged in buddies */
int owl_buddylist_get_size(const owl_buddylist *bl)
{
return bl->buddies->len;
}
/* return the buddy with index N. If out of range, return NULL
*/
owl_buddy *owl_buddylist_get_buddy_n(const owl_buddylist *bl, int index)
{
if (index<0) return(NULL);
if (index>(owl_buddylist_get_size(bl)-1)) return(NULL);
return bl->buddies->pdata[index];
}
/* return the AIM buddy with screenname 'name'. If
* no such buddy is logged in, return NULL.
*/
owl_buddy *owl_buddylist_get_aim_buddy(const owl_buddylist *bl, const char *name)
{
int i;
owl_buddy *b;
for (i = 0; i < bl->buddies->len; i++) {
b = bl->buddies->pdata[i];
if (!strcasecmp(name, owl_buddy_get_name(b))) return(b);
}
return(NULL);
}
/* return 1 if the buddy 'screenname' is logged in,
* otherwise return 0
*/
int owl_buddylist_is_aim_buddy_loggedin(const owl_buddylist *bl, const char *screenname)
{
const owl_buddy *b;
b=owl_buddylist_get_aim_buddy(bl, screenname);
if (b==NULL) return(0);
return(1);
}
/* remove all buddies from the list */
void owl_buddylist_clear(owl_buddylist *bl)
{
g_ptr_array_foreach(bl->buddies, (GFunc)owl_buddy_delete, NULL);
g_ptr_array_set_size(bl->buddies, 0);
}
void owl_buddylist_cleanup(owl_buddylist *bl)
{
owl_ptr_array_free(bl->buddies, (GDestroyNotify)owl_buddy_delete);
}