rename accountopt.[ch] to purpleaccountoption.[ch]
[pidgin-git.git] / libpurple / protocols / jabber / google / relay.c
blob50cfd31d675068ef7f6f41e44603e89508f82c52
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 "http.h"
25 #include "relay.h"
27 typedef struct {
28 GoogleSession *session;
29 JabberGoogleRelayCallback *cb;
30 } JabberGoogleRelayCallbackData;
32 static void
33 jabber_google_relay_parse_response(const gchar *response, gchar **ip,
34 guint *udp, guint *tcp, guint *ssltcp, gchar **username, gchar **password)
36 gchar **lines = g_strsplit(response, "\n", -1);
37 int i = 0;
39 for (; lines[i] ; i++) {
40 gchar *line = lines[i];
41 gchar **parts = g_strsplit(line, "=", 2);
43 if (parts[0] && parts[1]) {
44 if (purple_strequal(parts[0], "relay.ip")) {
45 *ip = g_strdup(parts[1]);
46 } else if (purple_strequal(parts[0], "relay.udp_port")) {
47 *udp = atoi(parts[1]);
48 } else if (purple_strequal(parts[0], "relay.tcp_port")) {
49 *tcp = atoi(parts[1]);
50 } else if (purple_strequal(parts[0], "relay.ssltcp_port")) {
51 *ssltcp = atoi(parts[1]);
52 } else if (purple_strequal(parts[0], "username")) {
53 *username = g_strdup(parts[1]);
54 } else if (purple_strequal(parts[0], "password")) {
55 *password = g_strdup(parts[1]);
58 g_strfreev(parts);
61 g_strfreev(lines);
64 static void
65 jabber_google_relay_fetch_cb(PurpleHttpConnection *http_conn,
66 PurpleHttpResponse *response, gpointer user_data)
68 JabberGoogleRelayCallbackData *data =
69 (JabberGoogleRelayCallbackData *) user_data;
70 GoogleSession *session = data->session;
71 JabberGoogleRelayCallback *cb = data->cb;
72 gchar *relay_ip = NULL;
73 guint relay_udp = 0;
74 guint relay_tcp = 0;
75 guint relay_ssltcp = 0;
76 gchar *relay_username = NULL;
77 gchar *relay_password = NULL;
79 g_free(data);
81 purple_debug_info("jabber", "got response on HTTP request to relay server\n");
83 if (purple_http_response_is_successful(response)) {
84 const gchar *got_data =
85 purple_http_response_get_data(response, NULL);
86 purple_debug_info("jabber", "got Google relay request response:\n%s\n",
87 got_data);
88 jabber_google_relay_parse_response(got_data, &relay_ip, &relay_udp,
89 &relay_tcp, &relay_ssltcp, &relay_username, &relay_password);
92 if (cb)
93 cb(session, relay_ip, relay_udp, relay_tcp, relay_ssltcp,
94 relay_username, relay_password);
96 g_free(relay_ip);
97 g_free(relay_username);
98 g_free(relay_password);
101 void
102 jabber_google_do_relay_request(JabberStream *js, GoogleSession *session,
103 JabberGoogleRelayCallback cb)
105 PurpleHttpRequest *req;
106 JabberGoogleRelayCallbackData *data = g_new0(JabberGoogleRelayCallbackData, 1);
108 data->session = session;
109 data->cb = cb;
110 purple_debug_info("jabber", "sending Google relay request\n");
112 req = purple_http_request_new(NULL);
113 purple_http_request_set_url_printf(req, "http://%s/create_session",
114 js->google_relay_host);
115 /* yes, the relay token is included twice as different request headers,
116 this is apparently needed to make Google's relay servers work... */
117 purple_http_request_header_set(req, "X-Talk-Google-Relay-Auth", js->google_relay_token);
118 purple_http_request_header_set(req, "X-Google-Relay-Auth", js->google_relay_token);
119 purple_http_request(js->gc, req, jabber_google_relay_fetch_cb, data);
120 purple_http_request_unref(req);