comparison libpurple/protocols/yahoo/yahoo_friend.c @ 15374:5fe8042783c1

Rename gtk/ and libgaim/ to pidgin/ and libpurple/
author Sean Egan <seanegan@gmail.com>
date Sat, 20 Jan 2007 02:32:10 +0000
parents
children 32c366eeeb99
comparison
equal deleted inserted replaced
15373:f79e0f4df793 15374:5fe8042783c1
1 /*
2 * gaim
3 *
4 * Gaim is the legal property of its developers, whose names are too numerous
5 * to list here. Please refer to the COPYRIGHT file distributed with this
6 * source distribution.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
24 #include "internal.h"
25 #include "prpl.h"
26 #include "util.h"
27 #include "debug.h"
28
29 #include "yahoo_friend.h"
30
31 static YahooFriend *yahoo_friend_new(void)
32 {
33 YahooFriend *ret;
34
35 ret = g_new0(YahooFriend, 1);
36 ret->status = YAHOO_STATUS_OFFLINE;
37 ret->presence = YAHOO_PRESENCE_DEFAULT;
38
39 return ret;
40 }
41
42 YahooFriend *yahoo_friend_find(GaimConnection *gc, const char *name)
43 {
44 struct yahoo_data *yd;
45 const char *norm;
46
47 g_return_val_if_fail(gc != NULL, NULL);
48 g_return_val_if_fail(gc->proto_data != NULL, NULL);
49
50 yd = gc->proto_data;
51 norm = gaim_normalize(gaim_connection_get_account(gc), name);
52
53 return g_hash_table_lookup(yd->friends, norm);
54 }
55
56 YahooFriend *yahoo_friend_find_or_new(GaimConnection *gc, const char *name)
57 {
58 YahooFriend *f;
59 struct yahoo_data *yd;
60 const char *norm;
61
62 g_return_val_if_fail(gc != NULL, NULL);
63 g_return_val_if_fail(gc->proto_data != NULL, NULL);
64
65 yd = gc->proto_data;
66 norm = gaim_normalize(gaim_connection_get_account(gc), name);
67
68 f = g_hash_table_lookup(yd->friends, norm);
69 if (!f) {
70 f = yahoo_friend_new();
71 g_hash_table_insert(yd->friends, g_strdup(norm), f);
72 }
73
74 return f;
75 }
76
77 void yahoo_friend_set_ip(YahooFriend *f, const char *ip)
78 {
79 if (f->ip)
80 g_free(f->ip);
81 f->ip = g_strdup(ip);
82 }
83
84 const char *yahoo_friend_get_ip(YahooFriend *f)
85 {
86 return f->ip;
87 }
88
89 void yahoo_friend_set_game(YahooFriend *f, const char *game)
90 {
91 if (f->game)
92 g_free(f->game);
93
94 if (game)
95 f->game = g_strdup(game);
96 else
97 f->game = NULL;
98 }
99
100 const char *yahoo_friend_get_game(YahooFriend *f)
101 {
102 return f->game;
103 }
104
105 void yahoo_friend_set_status_message(YahooFriend *f, char *msg)
106 {
107 if (f->msg)
108 g_free(f->msg);
109
110 f->msg = msg;
111 }
112
113 const char *yahoo_friend_get_status_message(YahooFriend *f)
114 {
115 return f->msg;
116 }
117
118 void yahoo_friend_set_buddy_icon_need_request(YahooFriend *f, gboolean needs)
119 {
120 f->bicon_sent_request = !needs;
121 }
122
123 gboolean yahoo_friend_get_buddy_icon_need_request(YahooFriend *f)
124 {
125 return !f->bicon_sent_request;
126 }
127
128 void yahoo_friend_free(gpointer p)
129 {
130 YahooFriend *f = p;
131 if (f->msg)
132 g_free(f->msg);
133 if (f->game)
134 g_free(f->game);
135 if (f->ip)
136 g_free(f->ip);
137 g_free(f);
138 }
139
140 void yahoo_process_presence(GaimConnection *gc, struct yahoo_packet *pkt)
141 {
142 GSList *l = pkt->hash;
143 YahooFriend *f;
144 char *who = NULL;
145 int value = 0;
146
147 while (l) {
148 struct yahoo_pair *pair = l->data;
149
150 switch (pair->key) {
151 case 7:
152 who = pair->value;
153 break;
154 case 31:
155 value = strtol(pair->value, NULL, 10);
156 break;
157 }
158
159 l = l->next;
160 }
161
162 if (value != 1 && value != 2) {
163 gaim_debug_error("yahoo", "Received unknown value for presence key: %d\n", value);
164 return;
165 }
166
167 g_return_if_fail(who != NULL);
168
169 f = yahoo_friend_find(gc, who);
170 if (!f)
171 return;
172
173 if (pkt->service == YAHOO_SERVICE_PRESENCE_PERM) {
174 gaim_debug_info("yahoo", "Setting permanent presence for %s to %d.\n", who, (value == 1));
175 /* If setting from perm offline to online when in invisible status,
176 * this has already been taken care of (when the temp status changed) */
177 if (value == 2 && f->presence == YAHOO_PRESENCE_ONLINE) {
178 } else {
179 if (value == 1) /* Setting Perm offline */
180 f->presence = YAHOO_PRESENCE_PERM_OFFLINE;
181 else
182 f->presence = YAHOO_PRESENCE_DEFAULT;
183 }
184 } else {
185 gaim_debug_info("yahoo", "Setting session presence for %s to %d.\n", who, (value == 1));
186 if (value == 1)
187 f->presence = YAHOO_PRESENCE_ONLINE;
188 else
189 f->presence = YAHOO_PRESENCE_DEFAULT;
190 }
191 }
192
193 void yahoo_friend_update_presence(GaimConnection *gc, const char *name,
194 YahooPresenceVisibility presence)
195 {
196 struct yahoo_data *yd = gc->proto_data;
197 struct yahoo_packet *pkt = NULL;
198 YahooFriend *f;
199
200 if (!yd->logged_in)
201 return;
202
203 f = yahoo_friend_find(gc, name);
204 if (!f)
205 return;
206
207 /* No need to change the value if it is already correct */
208 if (f->presence == presence) {
209 gaim_debug_info("yahoo", "Not setting presence because there are no changes.\n");
210 return;
211 }
212
213 if (presence == YAHOO_PRESENCE_PERM_OFFLINE) {
214 pkt = yahoo_packet_new(YAHOO_SERVICE_PRESENCE_PERM,
215 YAHOO_STATUS_AVAILABLE, yd->session_id);
216
217 yahoo_packet_hash(pkt, "ssss",
218 1, gaim_connection_get_display_name(gc),
219 31, "1", 13, "2", 7, name);
220 } else if (presence == YAHOO_PRESENCE_DEFAULT) {
221 if (f->presence == YAHOO_PRESENCE_PERM_OFFLINE) {
222 pkt = yahoo_packet_new(YAHOO_SERVICE_PRESENCE_PERM,
223 YAHOO_STATUS_AVAILABLE, yd->session_id);
224
225 yahoo_packet_hash(pkt, "ssss",
226 1, gaim_connection_get_display_name(gc),
227 31, "2", 13, "2", 7, name);
228 } else if (yd->current_status == YAHOO_STATUS_INVISIBLE) {
229 pkt = yahoo_packet_new(YAHOO_SERVICE_PRESENCE_SESSION,
230 YAHOO_STATUS_AVAILABLE, yd->session_id);
231 yahoo_packet_hash(pkt, "ssss",
232 1, gaim_connection_get_display_name(gc),
233 31, "2", 13, "1", 7, name);
234 }
235 } else if (presence == YAHOO_PRESENCE_ONLINE) {
236 if (f->presence == YAHOO_PRESENCE_PERM_OFFLINE) {
237 pkt = yahoo_packet_new(YAHOO_SERVICE_PRESENCE_PERM,
238 YAHOO_STATUS_AVAILABLE, yd->session_id);
239 yahoo_packet_hash(pkt, "ssss",
240 1, gaim_connection_get_display_name(gc),
241 31, "2", 13, "2", 7, name);
242 yahoo_packet_send_and_free(pkt, yd);
243 }
244
245 pkt = yahoo_packet_new(YAHOO_SERVICE_PRESENCE_SESSION,
246 YAHOO_STATUS_AVAILABLE, yd->session_id);
247 yahoo_packet_hash(pkt, "ssss",
248 1, gaim_connection_get_display_name(gc),
249 31, "1", 13, "1", 7, name);
250 }
251
252 if (pkt)
253 yahoo_packet_send_and_free(pkt, yd);
254 }