Replace functions which called once with their bodies
[pidgin-git.git] / libpurple / protocols / jabber / google / relay.c
blob01b8156c2f6c687a0550f1717e8be0d57a3979e9
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"
24 #include "relay.h"
26 typedef struct {
27 GoogleSession *session;
28 JabberGoogleRelayCallback *cb;
29 } JabberGoogleRelayCallbackData;
31 static void
32 jabber_google_relay_parse_response(const gchar *response, gchar **ip,
33 guint *udp, guint *tcp, guint *ssltcp, gchar **username, gchar **password)
35 gchar **lines = g_strsplit(response, "\n", -1);
36 int i = 0;
38 for (; lines[i] ; i++) {
39 gchar *line = lines[i];
40 gchar **parts = g_strsplit(line, "=", 2);
42 if (parts[0] && parts[1]) {
43 if (purple_strequal(parts[0], "relay.ip")) {
44 *ip = g_strdup(parts[1]);
45 } else if (purple_strequal(parts[0], "relay.udp_port")) {
46 *udp = atoi(parts[1]);
47 } else if (purple_strequal(parts[0], "relay.tcp_port")) {
48 *tcp = atoi(parts[1]);
49 } else if (purple_strequal(parts[0], "relay.ssltcp_port")) {
50 *ssltcp = atoi(parts[1]);
51 } else if (purple_strequal(parts[0], "username")) {
52 *username = g_strdup(parts[1]);
53 } else if (purple_strequal(parts[0], "password")) {
54 *password = g_strdup(parts[1]);
57 g_strfreev(parts);
60 g_strfreev(lines);
63 static void
64 jabber_google_relay_fetch_cb(G_GNUC_UNUSED SoupSession *soup, SoupMessage *msg,
65 gpointer user_data)
67 JabberGoogleRelayCallbackData *data =
68 (JabberGoogleRelayCallbackData *) user_data;
69 GoogleSession *session = data->session;
70 JabberGoogleRelayCallback *cb = data->cb;
71 gchar *relay_ip = NULL;
72 guint relay_udp = 0;
73 guint relay_tcp = 0;
74 guint relay_ssltcp = 0;
75 gchar *relay_username = NULL;
76 gchar *relay_password = NULL;
78 g_free(data);
80 purple_debug_info("jabber", "got response on HTTP request to relay server\n");
82 if (SOUP_STATUS_IS_SUCCESSFUL(msg->status_code)) {
83 const gchar *got_data = msg->response_body->data;
84 purple_debug_info("jabber", "got Google relay request response:\n%s\n",
85 got_data);
86 jabber_google_relay_parse_response(got_data, &relay_ip, &relay_udp,
87 &relay_tcp, &relay_ssltcp, &relay_username, &relay_password);
90 if (cb)
91 cb(session, relay_ip, relay_udp, relay_tcp, relay_ssltcp,
92 relay_username, relay_password);
94 g_free(relay_ip);
95 g_free(relay_username);
96 g_free(relay_password);
99 void
100 jabber_google_do_relay_request(JabberStream *js, GoogleSession *session,
101 JabberGoogleRelayCallback cb)
103 SoupURI *uri;
104 SoupMessage *msg;
105 JabberGoogleRelayCallbackData *data = g_new0(JabberGoogleRelayCallbackData, 1);
107 data->session = session;
108 data->cb = cb;
109 purple_debug_info("jabber", "sending Google relay request\n");
111 uri = soup_uri_new(NULL);
112 soup_uri_set_scheme(uri, SOUP_URI_SCHEME_HTTP);
113 soup_uri_set_host(uri, js->google_relay_host);
114 soup_uri_set_path(uri, "/create_session");
115 msg = soup_message_new_from_uri("GET", uri);
116 g_object_unref(uri);
118 /* yes, the relay token is included twice as different request headers,
119 this is apparently needed to make Google's relay servers work... */
120 soup_message_headers_replace(msg->request_headers,
121 "X-Talk-Google-Relay-Auth",
122 js->google_relay_token);
123 soup_message_headers_replace(msg->request_headers, "X-Google-Relay-Auth",
124 js->google_relay_token);
125 soup_session_queue_message(js->http_conns, msg,
126 jabber_google_relay_fetch_cb, data);