source: buddylist.c @ 8ba9313

release-1.10release-1.8release-1.9
Last change on this file since 8ba9313 was 96828e4, checked in by Anders Kaseorg <andersk@mit.edu>, 14 years ago
Replace owl_malloc with g_new and g_new0. Signed-off-by: Anders Kaseorg <andersk@mit.edu> Reviewed-by: Karl Ramm <kcr@mit.edu>
  • Property mode set to 100644
File size: 3.5 KB
Line 
1#include "owl.h"
2
3void owl_buddylist_init(owl_buddylist *bl)
4{
5  owl_list_create(&(bl->buddies));
6}
7
8/* add a (logged-in) AIM buddy to the buddy list
9 */
10void owl_buddylist_add_aim_buddy(owl_buddylist *bl, const char *screenname)
11{
12  owl_buddy *b;
13  b=g_new(owl_buddy, 1);
14 
15  owl_buddy_create(b, OWL_PROTOCOL_AIM, screenname);
16  owl_list_append_element(&(bl->buddies), b);
17}
18
19/* remove an AIM buddy from the buddy list
20 */
21int owl_buddylist_remove_aim_buddy(owl_buddylist *bl, const char *name)
22{
23  int i, j;
24  owl_buddy *b;
25
26  j=owl_list_get_size(&(bl->buddies));
27  for (i=0; i<j; i++) {
28    b=owl_list_get_element(&(bl->buddies), i);
29    if (!strcasecmp(name, owl_buddy_get_name(b)) && owl_buddy_is_proto_aim(b)) {
30      owl_list_remove_element(&(bl->buddies), i);
31      owl_buddy_delete(b);
32      return(0);
33    }
34  }
35  return(1);
36}
37
38/* Deal with an "oncoming" message.  This means recognizing the user
39 * has logged in, and displaying a message if they were not already
40 * logged in.
41 */
42void owl_buddylist_oncoming(owl_buddylist *bl, const char *screenname)
43{
44  owl_message *m;
45
46  if (!owl_buddylist_is_aim_buddy_loggedin(bl, screenname)) {
47
48    owl_buddylist_add_aim_buddy(bl, screenname);
49
50    /* are we ingoring login messages for a while? */
51    if (owl_global_is_ignore_aimlogin(&g)) return;
52
53    /* if not, create the login message */
54    m=g_new(owl_message, 1);
55    owl_message_create_aim(m,
56                           screenname,
57                           owl_global_get_aim_screenname(&g),
58                           "",
59                           OWL_MESSAGE_DIRECTION_IN,
60                           1);
61    owl_global_messagequeue_addmsg(&g, m);
62  }
63}
64
65/* Deal with an "offgoing" message.  This means recognizing the user
66 * has logged out, and sending a message if they were logged in.
67 */
68void owl_buddylist_offgoing(owl_buddylist *bl, const char *screenname)
69{
70  owl_message *m;
71
72  if (owl_buddylist_is_aim_buddy_loggedin(bl, screenname)) {
73    m=g_new(owl_message, 1);
74    owl_message_create_aim(m,
75                           screenname,
76                           owl_global_get_aim_screenname(&g),
77                           "",
78                           OWL_MESSAGE_DIRECTION_IN,
79                           -1);
80    owl_global_messagequeue_addmsg(&g, m);
81  }
82
83  owl_buddylist_remove_aim_buddy(bl, screenname);
84}
85
86/* return the number of logged in buddies */
87int owl_buddylist_get_size(const owl_buddylist *bl)
88{
89  return(owl_list_get_size(&(bl->buddies)));
90}
91
92/* return the buddy with index N.  If out of range, return NULL
93 */
94owl_buddy *owl_buddylist_get_buddy_n(const owl_buddylist *bl, int index)
95{
96  if (index<0) return(NULL);
97  if (index>(owl_buddylist_get_size(bl)-1)) return(NULL);
98
99  return(owl_list_get_element(&(bl->buddies), index));
100}
101
102/* return the AIM buddy with screenname 'name'.  If
103 * no such buddy is logged in, return NULL.
104 */
105owl_buddy *owl_buddylist_get_aim_buddy(const owl_buddylist *bl, const char *name)
106{
107  int i, j;
108  owl_buddy *b;
109
110  j=owl_list_get_size(&(bl->buddies));
111  for (i=0; i<j; i++) {
112    b=owl_list_get_element(&(bl->buddies), i);
113    if (!strcasecmp(name, owl_buddy_get_name(b))) return(b);
114  }
115  return(NULL);
116}
117
118/* return 1 if the buddy 'screenname' is logged in,
119 * otherwise return 0
120 */
121int owl_buddylist_is_aim_buddy_loggedin(const owl_buddylist *bl, const char *screenname)
122{
123  const owl_buddy *b;
124
125  b=owl_buddylist_get_aim_buddy(bl, screenname);
126  if (b==NULL) return(0);
127  return(1);
128}
129
130/* remove all buddies from the list */
131void owl_buddylist_clear(owl_buddylist *bl)
132{
133  owl_list_cleanup(&(bl->buddies), (void (*)(void *))owl_buddy_delete);
134  owl_list_create(&(bl->buddies));
135}
136
137void owl_buddylist_cleanup(owl_buddylist *bl)
138{
139  owl_list_cleanup(&(bl->buddies), (void (*)(void *))owl_buddy_delete);
140}
Note: See TracBrowser for help on using the repository browser.