Use GSList functions instead of manual iterations
[pidgin-git.git] / libpurple / protocols / jabber / google / relay.c
blob682758e021ad38244a83d4d707ec1c422cc7d948
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(G_GNUC_UNUSED SoupSession *soup, SoupMessage *msg,
66 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 (SOUP_STATUS_IS_SUCCESSFUL(msg->status_code)) {
84 const gchar *got_data = msg->response_body->data;
85 purple_debug_info("jabber", "got Google relay request response:\n%s\n",
86 got_data);
87 jabber_google_relay_parse_response(got_data, &relay_ip, &relay_udp,
88 &relay_tcp, &relay_ssltcp, &relay_username, &relay_password);
91 if (cb)
92 cb(session, relay_ip, relay_udp, relay_tcp, relay_ssltcp,
93 relay_username, relay_password);
95 g_free(relay_ip);
96 g_free(relay_username);
97 g_free(relay_password);
100 void
101 jabber_google_do_relay_request(JabberStream *js, GoogleSession *session,
102 JabberGoogleRelayCallback cb)
104 SoupURI *uri;
105 SoupMessage *msg;
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 uri = soup_uri_new(NULL);
113 soup_uri_set_scheme(uri, SOUP_URI_SCHEME_HTTP);
114 soup_uri_set_host(uri, js->google_relay_host);
115 soup_uri_set_path(uri, "/create_session");
116 msg = soup_message_new_from_uri("GET", uri);
117 g_object_unref(uri);
119 /* yes, the relay token is included twice as different request headers,
120 this is apparently needed to make Google's relay servers work... */
121 soup_message_headers_replace(msg->request_headers,
122 "X-Talk-Google-Relay-Auth",
123 js->google_relay_token);
124 soup_message_headers_replace(msg->request_headers, "X-Google-Relay-Auth",
125 js->google_relay_token);
126 soup_session_queue_message(js->http_conns, msg,
127 jabber_google_relay_fetch_cb, data);