Migrate certificates, icons, logs to XDG dirs
[pidgin-git.git] / libpurple / protocols / jabber / google / gmail.c
blob3d857642e4d487373299c970c30ca48fe87487d1
1 /**
2 * Purple is the legal property of its developers, whose names are too numerous
3 * to list here. Please refer to the COPYRIGHT file distributed with this
4 * source distribution.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
21 #include "internal.h"
22 #include "debug.h"
23 #include "jabber.h"
24 #include "gmail.h"
26 static void
27 jabber_gmail_parse(JabberStream *js, const char *from,
28 JabberIqType type, const char *id,
29 PurpleXmlNode *packet, gpointer nul)
31 PurpleXmlNode *child;
32 PurpleXmlNode *message;
33 const char *to, *url;
34 const char *in_str;
35 char *to_name;
37 int i, count = 1, returned_count;
39 const char **tos, **froms, **urls;
40 char **subjects;
42 if (type == JABBER_IQ_ERROR)
43 return;
45 child = purple_xmlnode_get_child(packet, "mailbox");
46 if (!child)
47 return;
49 in_str = purple_xmlnode_get_attrib(child, "total-matched");
50 if (in_str && *in_str)
51 count = atoi(in_str);
53 /* If Gmail doesn't tell us who the mail is to, let's use our JID */
54 to = purple_xmlnode_get_attrib(packet, "to");
56 message = purple_xmlnode_get_child(child, "mail-thread-info");
58 if (count == 0 || !message) {
59 if (count > 0) {
60 char *bare_jid = jabber_get_bare_jid(to);
61 const char *default_tos[2] = { bare_jid };
63 purple_notify_emails(js->gc, count, FALSE, NULL, NULL, default_tos, NULL, NULL, NULL);
64 g_free(bare_jid);
65 } else {
66 purple_notify_emails(js->gc, count, FALSE, NULL, NULL, NULL, NULL, NULL, NULL);
69 return;
72 /* Loop once to see how many messages were returned so we can allocate arrays
73 * accordingly */
74 for (returned_count = 0; message; returned_count++, message=purple_xmlnode_get_next_twin(message));
76 froms = g_new0(const char* , returned_count + 1);
77 tos = g_new0(const char* , returned_count + 1);
78 subjects = g_new0(char* , returned_count + 1);
79 urls = g_new0(const char* , returned_count + 1);
81 to = purple_xmlnode_get_attrib(packet, "to");
82 to_name = jabber_get_bare_jid(to);
83 url = purple_xmlnode_get_attrib(child, "url");
84 if (!url || !*url)
85 url = "http://www.gmail.com";
87 message= purple_xmlnode_get_child(child, "mail-thread-info");
88 for (i=0; message; message = purple_xmlnode_get_next_twin(message), i++) {
89 PurpleXmlNode *sender_node, *subject_node;
90 const char *from, *tid;
91 char *subject;
93 subject_node = purple_xmlnode_get_child(message, "subject");
94 sender_node = purple_xmlnode_get_child(message, "senders");
95 sender_node = purple_xmlnode_get_child(sender_node, "sender");
97 while (sender_node && (!purple_xmlnode_get_attrib(sender_node, "unread") ||
98 !strcmp(purple_xmlnode_get_attrib(sender_node, "unread"),"0")))
99 sender_node = purple_xmlnode_get_next_twin(sender_node);
101 if (!sender_node) {
102 i--;
103 continue;
106 from = purple_xmlnode_get_attrib(sender_node, "name");
107 if (!from || !*from)
108 from = purple_xmlnode_get_attrib(sender_node, "address");
109 subject = purple_xmlnode_get_data(subject_node);
111 * url = purple_xmlnode_get_attrib(message, "url");
113 tos[i] = (to_name != NULL ? to_name : "");
114 froms[i] = (from != NULL ? from : "");
115 subjects[i] = (subject != NULL ? subject : g_strdup(""));
116 urls[i] = url;
118 tid = purple_xmlnode_get_attrib(message, "tid");
119 if (tid &&
120 (js->gmail_last_tid == NULL || strcmp(tid, js->gmail_last_tid) > 0)) {
121 g_free(js->gmail_last_tid);
122 js->gmail_last_tid = g_strdup(tid);
126 if (i>0)
127 purple_notify_emails(js->gc, count, count == i, (const char**) subjects, froms, tos,
128 urls, NULL, NULL);
130 g_free(to_name);
131 g_free(tos);
132 g_free(froms);
133 for (i = 0; i < returned_count; i++)
134 g_free(subjects[i]);
135 g_free(subjects);
136 g_free(urls);
138 in_str = purple_xmlnode_get_attrib(child, "result-time");
139 if (in_str && *in_str) {
140 g_free(js->gmail_last_time);
141 js->gmail_last_time = g_strdup(in_str);
145 void
146 jabber_gmail_poke(JabberStream *js, const char *from, JabberIqType type,
147 const char *id, PurpleXmlNode *new_mail)
149 PurpleXmlNode *query;
150 JabberIq *iq;
152 /* bail if the user isn't interested */
153 if (!purple_account_get_check_mail(purple_connection_get_account(js->gc)))
154 return;
156 /* Is this an initial incoming mail notification? If so, send a request for more info */
157 if (type != JABBER_IQ_SET)
158 return;
160 /* Acknowledge the notification */
161 iq = jabber_iq_new(js, JABBER_IQ_RESULT);
162 if (from)
163 purple_xmlnode_set_attrib(iq->node, "to", from);
164 purple_xmlnode_set_attrib(iq->node, "id", id);
165 jabber_iq_send(iq);
167 purple_debug_misc("jabber",
168 "Got new mail notification. Sending request for more info\n");
170 iq = jabber_iq_new_query(js, JABBER_IQ_GET, NS_GOOGLE_MAIL_NOTIFY);
171 jabber_iq_set_callback(iq, jabber_gmail_parse, NULL);
172 query = purple_xmlnode_get_child(iq->node, "query");
174 if (js->gmail_last_time)
175 purple_xmlnode_set_attrib(query, "newer-than-time", js->gmail_last_time);
176 if (js->gmail_last_tid)
177 purple_xmlnode_set_attrib(query, "newer-than-tid", js->gmail_last_tid);
179 jabber_iq_send(iq);
180 return;
183 void jabber_gmail_init(JabberStream *js) {
184 JabberIq *iq;
185 PurpleXmlNode *usersetting, *mailnotifications;
187 if (!purple_account_get_check_mail(purple_connection_get_account(js->gc)))
188 return;
191 * Quoting https://developers.google.com/talk/jep_extensions/usersettings:
192 * To ensure better compatibility with other clients, rather than
193 * setting this value to "false" to turn off notifications, it is
194 * recommended that a client set this to "true" and filter incoming
195 * email notifications itself.
197 iq = jabber_iq_new(js, JABBER_IQ_SET);
198 usersetting = purple_xmlnode_new_child(iq->node, "usersetting");
199 purple_xmlnode_set_namespace(usersetting, "google:setting");
200 mailnotifications = purple_xmlnode_new_child(usersetting, "mailnotifications");
201 purple_xmlnode_set_attrib(mailnotifications, "value", "true");
202 jabber_iq_send(iq);
204 iq = jabber_iq_new_query(js, JABBER_IQ_GET, NS_GOOGLE_MAIL_NOTIFY);
205 jabber_iq_set_callback(iq, jabber_gmail_parse, NULL);
206 jabber_iq_send(iq);