Replace functions which called once with their bodies
[pidgin-git.git] / libpurple / protocols / gg / libgaduw.c
blobf8caed4c9cc1e0917525d00269d8cc8f8eebe862
1 /* purple
3 * Purple 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.
7 * Rewritten from scratch during Google Summer of Code 2012
8 * by Tomek Wasilczyk (http://www.wasilczyk.pl).
10 * Previously implemented by:
11 * - Arkadiusz Miskiewicz <misiek@pld.org.pl> - first implementation (2001);
12 * - Bartosz Oler <bartosz@bzimage.us> - reimplemented during GSoC 2005;
13 * - Krzysztof Klinikowski <grommasher@gmail.com> - some parts (2009-2011).
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
30 #include "libgaduw.h"
32 #include <debug.h>
34 #include "purplew.h"
35 #include "gg.h"
37 static void ggp_libgaduw_debug_handler(int level, const char * format,
38 va_list args);
40 /*******************************************************************************
41 * Setup/cleanup.
42 ******************************************************************************/
44 void ggp_libgaduw_setup(void)
46 gg_debug_handler = ggp_libgaduw_debug_handler;
49 void ggp_libgaduw_cleanup(void)
51 gg_debug_handler = NULL;
54 /*******************************************************************************
55 * General.
56 ******************************************************************************/
58 const gchar * ggp_libgaduw_version(PurpleConnection *gc)
60 GGPInfo *accdata = purple_connection_get_protocol_data(gc);
61 const gchar *ver = accdata->session->client_version;
63 if (ver != NULL && isdigit(ver[0]))
64 return ver;
65 return GG_DEFAULT_CLIENT_VERSION;
68 static void ggp_libgaduw_debug_handler(int level, const char * format,
69 va_list args)
71 PurpleDebugLevel purple_level;
72 char msgbuff[1000];
73 int ret;
75 if ((level & GG_DEBUG_NET) || (level & GG_DEBUG_FUNCTION) ||
76 (level & GG_DEBUG_VERBOSE))
78 if (!purple_debug_is_verbose())
79 return;
82 if ((level & GG_DEBUG_DUMP) || /* GG session protocol packets */
83 (level & GG_DEBUG_TRAFFIC)) /* HTTP traffic */
85 if (!purple_debug_is_verbose() || !purple_debug_is_unsafe())
86 return;
89 /* Don't use glib's printf family, since it might not support
90 * system-specific formatting modifiers (like %Iu for size on win32). */
91 ret = vsnprintf(msgbuff, sizeof(msgbuff) / sizeof(char), format, args);
93 if (ret <= 0) {
94 purple_debug_fatal("gg",
95 "failed to printf the following message: %s",
96 format ? format : "(null)\n");
98 return;
101 if (level & GG_DEBUG_ERROR)
102 purple_level = PURPLE_DEBUG_ERROR;
103 else if (level & GG_DEBUG_WARNING)
104 purple_level = PURPLE_DEBUG_WARNING;
105 else
106 purple_level = PURPLE_DEBUG_MISC;
108 purple_debug(purple_level, "gg", "%s", msgbuff);
111 /*******************************************************************************
112 * HTTP requests.
113 ******************************************************************************/
115 static void ggp_libgaduw_http_processing_cancel(PurpleConnection *gc,
116 void *_req);
118 static void ggp_libgaduw_http_handler(gpointer _req, gint fd,
119 PurpleInputCondition cond);
121 static void ggp_libgaduw_http_finish(ggp_libgaduw_http_req *req,
122 gboolean success);
124 /******************************************************************************/
126 ggp_libgaduw_http_req * ggp_libgaduw_http_watch(PurpleConnection *gc,
127 struct gg_http *h, ggp_libgaduw_http_cb cb,
128 gpointer user_data, gboolean show_processing)
130 ggp_libgaduw_http_req *req;
131 purple_debug_misc("gg", "ggp_libgaduw_http_watch(h=%p, "
132 "show_processing=%d)\n", h, show_processing);
134 req = g_new(ggp_libgaduw_http_req, 1);
135 req->user_data = user_data;
136 req->cb = cb;
137 req->cancelled = FALSE;
138 req->h = h;
139 req->processing = NULL;
140 if (show_processing)
141 req->processing = ggp_purplew_request_processing(gc, NULL,
142 req, ggp_libgaduw_http_processing_cancel);
143 req->inpa = ggp_purplew_http_input_add(h, ggp_libgaduw_http_handler,
144 req);
146 return req;
149 static void ggp_libgaduw_http_processing_cancel(PurpleConnection *gc,
150 void *_req)
152 ggp_libgaduw_http_req *req = _req;
153 req->processing = NULL;
154 ggp_libgaduw_http_cancel(req);
157 static void ggp_libgaduw_http_handler(gpointer _req, gint fd,
158 PurpleInputCondition cond)
160 ggp_libgaduw_http_req *req = _req;
162 if (req->h->callback(req->h) == -1 || req->h->state == GG_STATE_ERROR) {
163 purple_debug_error("gg", "ggp_libgaduw_http_handler: failed to "
164 "make http request: %d\n", req->h->error);
165 ggp_libgaduw_http_finish(req, FALSE);
166 return;
169 if (purple_debug_is_verbose()) {
170 purple_debug_misc("gg", "ggp_libgaduw_http_handler: got fd "
171 "update [check=%d, state=%d]\n", req->h->check,
172 req->h->state);
175 if (req->h->state != GG_STATE_DONE) {
176 purple_input_remove(req->inpa);
177 req->inpa = ggp_purplew_http_input_add(req->h,
178 ggp_libgaduw_http_handler, req);
179 return;
182 if (!req->h->data || !req->h->body) {
183 purple_debug_error("gg", "ggp_libgaduw_http_handler: got empty "
184 "http response: %d\n", req->h->error);
185 ggp_libgaduw_http_finish(req, FALSE);
186 return;
189 ggp_libgaduw_http_finish(req, TRUE);
192 void ggp_libgaduw_http_cancel(ggp_libgaduw_http_req *req)
194 purple_debug_misc("gg", "ggp_libgaduw_http_cancel\n");
195 req->cancelled = TRUE;
196 gg_http_stop(req->h);
197 ggp_libgaduw_http_finish(req, FALSE);
200 static void ggp_libgaduw_http_finish(ggp_libgaduw_http_req *req,
201 gboolean success)
203 purple_debug_misc("gg", "ggp_libgaduw_http_finish(h=%p, processing=%p):"
204 " success=%d\n", req->h, req->processing, success);
205 if (req->processing) {
206 ggp_purplew_request_processing_done(req->processing);
207 req->processing = NULL;
209 purple_input_remove(req->inpa);
210 req->cb(req->h, success, req->cancelled, req->user_data);
211 req->h->destroy(req->h);
212 g_free(req);