comparison libpurple/protocols/jabber/google.c @ 15373: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 2c81b0a81790
comparison
equal deleted inserted replaced
15372:f79e0f4df793 15373:5fe8042783c1
1
2 /**
3 * Gaim is the legal property of its developers, whose names are too numerous
4 * to list here. Please refer to the COPYRIGHT file distributed with this
5 * source distribution.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 #include "internal.h"
23 #include "debug.h"
24 #include "privacy.h"
25
26 #include "buddy.h"
27 #include "google.h"
28 #include "jabber.h"
29 #include "presence.h"
30 #include "iq.h"
31
32 static void
33 jabber_gmail_parse(JabberStream *js, xmlnode *packet, gpointer nul)
34 {
35 const char *type = xmlnode_get_attrib(packet, "type");
36 xmlnode *child;
37 xmlnode *message, *sender_node, *subject_node;
38 const char *from, *to, *subject, *url, *tid;
39 const char *in_str;
40 char *to_name;
41 int i, count = 1, returned_count;
42
43 const char **tos, **froms, **subjects, **urls;
44
45 if (strcmp(type, "result"))
46 return;
47
48 child = xmlnode_get_child(packet, "mailbox");
49 if (!child)
50 return;
51
52 in_str = xmlnode_get_attrib(child, "total-matched");
53 if (in_str && *in_str)
54 count = atoi(in_str);
55
56 if (count == 0)
57 return;
58
59 message = xmlnode_get_child(child, "mail-thread-info");
60
61 /* Loop once to see how many messages were returned so we can allocate arrays
62 * accordingly */
63 if (!message)
64 return;
65 for (returned_count = 0; message; returned_count++, message=xmlnode_get_next_twin(message));
66
67 froms = g_new0(const char* , returned_count);
68 tos = g_new0(const char* , returned_count);
69 subjects = g_new0(const char* , returned_count);
70 urls = g_new0(const char* , returned_count);
71
72 to = xmlnode_get_attrib(packet, "to");
73 to_name = jabber_get_bare_jid(to);
74 url = xmlnode_get_attrib(child, "url");
75 if (!url || !*url)
76 url = "http://www.gmail.com";
77
78 message= xmlnode_get_child(child, "mail-thread-info");
79 for (i=0; message; message = xmlnode_get_next_twin(message), i++) {
80 subject_node = xmlnode_get_child(message, "subject");
81 sender_node = xmlnode_get_child(message, "senders");
82 sender_node = xmlnode_get_child(sender_node, "sender");
83
84 while (sender_node && (!xmlnode_get_attrib(sender_node, "unread") ||
85 !strcmp(xmlnode_get_attrib(sender_node, "unread"),"0")))
86 sender_node = xmlnode_get_next_twin(sender_node);
87
88 if (!sender_node) {
89 i--;
90 continue;
91 }
92
93 from = xmlnode_get_attrib(sender_node, "name");
94 if (!from || !*from)
95 from = xmlnode_get_attrib(sender_node, "address");
96 subject = xmlnode_get_data(subject_node);
97 /*
98 * url = xmlnode_get_attrib(message, "url");
99 */
100 tos[i] = (to_name != NULL ? to_name : "");
101 froms[i] = (from != NULL ? from : "");
102 subjects[i] = (subject != NULL ? subject : "");
103 urls[i] = (url != NULL ? url : "");
104
105 tid = xmlnode_get_attrib(message, "tid");
106 if (tid &&
107 (js->gmail_last_tid == NULL || strcmp(tid, js->gmail_last_tid) > 0)) {
108 g_free(js->gmail_last_tid);
109 js->gmail_last_tid = g_strdup(tid);
110 }
111 }
112
113 if (i>0)
114 gaim_notify_emails(js->gc, count, count == returned_count, subjects, froms, tos,
115 urls, NULL, NULL);
116
117 g_free(to_name);
118 g_free(tos);
119 g_free(froms);
120 g_free(subjects);
121 g_free(urls);
122
123 in_str = xmlnode_get_attrib(child, "result-time");
124 if (in_str && *in_str) {
125 g_free(js->gmail_last_time);
126 js->gmail_last_time = g_strdup(in_str);
127 }
128 }
129
130 void
131 jabber_gmail_poke(JabberStream *js, xmlnode *packet)
132 {
133 const char *type;
134 xmlnode *query;
135 JabberIq *iq;
136
137 /* bail if the user isn't interested */
138 if (!gaim_account_get_check_mail(js->gc->account))
139 return;
140
141 type = xmlnode_get_attrib(packet, "type");
142
143
144 /* Is this an initial incoming mail notification? If so, send a request for more info */
145 if (strcmp(type, "set") || !xmlnode_get_child(packet, "new-mail"))
146 return;
147
148 gaim_debug(GAIM_DEBUG_MISC, "jabber",
149 "Got new mail notification. Sending request for more info\n");
150
151 iq = jabber_iq_new_query(js, JABBER_IQ_GET, "google:mail:notify");
152 jabber_iq_set_callback(iq, jabber_gmail_parse, NULL);
153 query = xmlnode_get_child(iq->node, "query");
154
155 if (js->gmail_last_time)
156 xmlnode_set_attrib(query, "newer-than-time", js->gmail_last_time);
157 if (js->gmail_last_tid)
158 xmlnode_set_attrib(query, "newer-than-tid", js->gmail_last_tid);
159
160 jabber_iq_send(iq);
161 return;
162 }
163
164 void jabber_gmail_init(JabberStream *js) {
165 JabberIq *iq;
166
167 if (!gaim_account_get_check_mail(js->gc->account))
168 return;
169
170 iq = jabber_iq_new_query(js, JABBER_IQ_GET, "google:mail:notify");
171 jabber_iq_set_callback(iq, jabber_gmail_parse, NULL);
172 jabber_iq_send(iq);
173 }
174
175 void jabber_google_roster_init(JabberStream *js)
176 {
177 JabberIq *iq;
178 xmlnode *query;
179
180 iq = jabber_iq_new_query(js, JABBER_IQ_GET, "jabber:iq:roster");
181 query = xmlnode_get_child(iq->node, "query");
182
183 xmlnode_set_attrib(query, "xmlns:gr", "google:roster");
184 xmlnode_set_attrib(query, "gr:ext", "2");
185
186 jabber_iq_send(iq);
187 }
188
189 void jabber_google_roster_outgoing(JabberStream *js, xmlnode *query, xmlnode *item)
190 {
191 GaimAccount *account = gaim_connection_get_account(js->gc);
192 GSList *list = account->deny;
193 const char *jid = xmlnode_get_attrib(item, "jid");
194 char *jid_norm = g_strdup(jabber_normalize(account, jid));
195
196 while (list) {
197 if (!strcmp(jid_norm, (char*)list->data)) {
198 xmlnode_set_attrib(query, "xmlns:gr", "google:roster");
199 xmlnode_set_attrib(item, "gr:t", "B");
200 xmlnode_set_attrib(query, "xmlns:gr", "google:roster");
201 xmlnode_set_attrib(query, "gr:ext", "2");
202 return;
203 }
204 list = list->next;
205 }
206
207 }
208
209 void jabber_google_roster_incoming(JabberStream *js, xmlnode *item)
210 {
211 GaimAccount *account = gaim_connection_get_account(js->gc);
212 GSList *list = account->deny;
213 const char *jid = xmlnode_get_attrib(item, "jid");
214 gboolean on_block_list = FALSE;
215
216 char *jid_norm = g_strdup(jabber_normalize(account, jid));
217
218 const char *grt = xmlnode_get_attrib_with_namespace(item, "t", "google:roster");
219
220 while (list) {
221 if (!strcmp(jid_norm, (char*)list->data)) {
222 on_block_list = TRUE;
223 break;
224 }
225 list = list->next;
226 }
227
228 if (!on_block_list && (grt && (*grt == 'B' || *grt == 'b'))) {
229 gaim_debug_info("jabber", "Blocking %s\n", jid_norm);
230 gaim_privacy_deny_add(account, jid_norm, TRUE);
231 } else if (on_block_list && (!grt || (*grt != 'B' && *grt != 'b' ))){
232 gaim_debug_info("jabber", "Unblocking %s\n", jid_norm);
233 gaim_privacy_deny_remove(account, jid_norm, TRUE);
234 }
235 }
236
237 void jabber_google_roster_add_deny(GaimConnection *gc, const char *who)
238 {
239 JabberStream *js;
240 GSList *buddies;
241 JabberIq *iq;
242 xmlnode *query;
243 xmlnode *item;
244 xmlnode *group;
245 GaimBuddy *b;
246 JabberBuddy *jb;
247
248 js = (JabberStream*)(gc->proto_data);
249
250 if (!js || !js->server_caps & JABBER_CAP_GOOGLE_ROSTER)
251 return;
252
253 jb = jabber_buddy_find(js, who, TRUE);
254
255 buddies = gaim_find_buddies(js->gc->account, who);
256 if(!buddies)
257 return;
258
259 b = buddies->data;
260
261 iq = jabber_iq_new_query(js, JABBER_IQ_SET, "jabber:iq:roster");
262
263 query = xmlnode_get_child(iq->node, "query");
264 item = xmlnode_new_child(query, "item");
265
266 while(buddies) {
267 GaimGroup *g;
268
269 b = buddies->data;
270 g = gaim_buddy_get_group(b);
271
272 group = xmlnode_new_child(item, "group");
273 xmlnode_insert_data(group, g->name, -1);
274
275 buddies = buddies->next;
276 }
277
278 iq = jabber_iq_new_query(js, JABBER_IQ_SET, "jabber:iq:roster");
279
280 query = xmlnode_get_child(iq->node, "query");
281 item = xmlnode_new_child(query, "item");
282
283 xmlnode_set_attrib(item, "jid", who);
284 xmlnode_set_attrib(item, "name", b->alias ? b->alias : "");
285 xmlnode_set_attrib(item, "gr:t", "B");
286 xmlnode_set_attrib(query, "xmlns:gr", "google:roster");
287 xmlnode_set_attrib(query, "gr:ext", "2");
288
289 jabber_iq_send(iq);
290
291 /* Synthesize a sign-off */
292 if (jb) {
293 JabberBuddyResource *jbr;
294 GList *l = jb->resources;
295 while (l) {
296 jbr = l->data;
297 if (jbr && jbr->name)
298 {
299 gaim_debug(GAIM_DEBUG_MISC, "jabber", "Removing resource %s\n", jbr->name);
300 jabber_buddy_remove_resource(jb, jbr->name);
301 }
302 l = l->next;
303 }
304 }
305 gaim_prpl_got_user_status(gaim_connection_get_account(gc), who, "offline", NULL);
306 }
307
308 void jabber_google_roster_rem_deny(GaimConnection *gc, const char *who)
309 {
310 JabberStream *js;
311 GSList *buddies;
312 JabberIq *iq;
313 xmlnode *query;
314 xmlnode *item;
315 xmlnode *group;
316 GaimBuddy *b;
317
318 g_return_if_fail(gc != NULL);
319 g_return_if_fail(who != NULL);
320
321 js = (JabberStream*)(gc->proto_data);
322
323 if (!js || !js->server_caps & JABBER_CAP_GOOGLE_ROSTER)
324 return;
325
326 buddies = gaim_find_buddies(js->gc->account, who);
327 if(!buddies)
328 return;
329
330 b = buddies->data;
331
332 iq = jabber_iq_new_query(js, JABBER_IQ_SET, "jabber:iq:roster");
333
334 query = xmlnode_get_child(iq->node, "query");
335 item = xmlnode_new_child(query, "item");
336
337 while(buddies) {
338 GaimGroup *g;
339
340 b = buddies->data;
341 g = gaim_buddy_get_group(b);
342
343 group = xmlnode_new_child(item, "group");
344 xmlnode_insert_data(group, g->name, -1);
345
346 buddies = buddies->next;
347 }
348
349 iq = jabber_iq_new_query(js, JABBER_IQ_SET, "jabber:iq:roster");
350
351 query = xmlnode_get_child(iq->node, "query");
352 item = xmlnode_new_child(query, "item");
353
354 xmlnode_set_attrib(item, "jid", who);
355 xmlnode_set_attrib(item, "name", b->alias ? b->alias : "");
356 xmlnode_set_attrib(query, "xmlns:gr", "google:roster");
357 xmlnode_set_attrib(query, "gr:ext", "2");
358
359 jabber_iq_send(iq);
360
361 /* See if he's online */
362 jabber_presence_subscription_set(js, who, "probe");
363 }