2 * Dolda Connect - Modular multiuser Direct Connect-style client
3 * Copyright (C) 2007 Fredrik Tolf (fredrik@dolda2000.com)
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
39 static void settags(void);
41 static regex_t *filter = NULL;
42 static int itag = -1, otag = -1;
43 static int (*handler)(int, char *, size_t) = NULL;
45 static struct htconn *hc;
46 static bz_stream *bzs;
47 /* Only needed because of bzlib: */
49 static size_t mybufsize, mybufdata;
51 void aborthublist(void)
56 mybufsize = mybufdata = 0;
60 gdk_input_remove(itag);
62 gdk_input_remove(otag);
67 BZ2_bzDecompressEnd(bzs);
76 handler(PHO_FINI, NULL, 0);
82 int validhub(char *field, ...)
90 va_start(args, field);
92 if(!regexec(filter, field, 0, NULL, 0)) {
96 } while((field = va_arg(args, char *)) != NULL);
101 static void fdcb(gpointer data, gint source, GdkInputCondition cond)
103 int ret, bzret, hret;
106 gdk_input_remove(itag);
108 gdk_input_remove(otag);
110 ret = htprocess(hc, ((cond & GDK_INPUT_READ)?POLLIN:0) | ((cond & GDK_INPUT_WRITE)?POLLOUT:0));
112 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not read hublist from server: %s"), strerror(errno));
117 if(hc->rescode != 0) {
118 if(hc->rescode != 200) {
119 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("The hublist server returned an error: \"%i %s\""), hc->rescode, hc->resstr);
127 if(hc->databufdata > 0) {
129 bufcat(mybuf, hc->databuf, hc->databufdata);
132 bzs->next_in = hc->databuf;
133 bzs->avail_in = hc->databufdata;
135 sizebuf2(mybuf, mybufdata + 1024, 1);
136 bzs->next_out = mybuf + mybufdata;
137 bzs->avail_out = mybufsize - mybufdata;
138 bzret = BZ2_bzDecompress(bzs);
139 if((bzret != BZ_OK) && (bzret != BZ_STREAM_END)) {
140 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not decompress hublist (%i)"), hret);
144 mybufdata = mybufsize - bzs->avail_out;
145 if(bzret == BZ_STREAM_END) {
149 } while(bzs->avail_out == 0);
150 memmove(hc->databuf, bzs->next_in, hc->databufdata -= (bzs->next_in - hc->databuf));
152 if((hret = handler(PHO_DATA, mybuf, mybufdata)) < 0)
155 memmove(mybuf, mybuf + hret, mybufdata -= hret);
158 handler(PHO_EOF, NULL, 0);
166 static void settags(void)
168 if(htpollflags(hc) & POLLIN)
169 itag = gdk_input_add(hc->fd, GDK_INPUT_READ, fdcb, NULL);
170 if(htpollflags(hc) & POLLOUT)
171 otag = gdk_input_add(hc->fd, GDK_INPUT_WRITE, fdcb, NULL);
174 void fetchhublist(char *url, regex_t *flt)
190 if((len > 4) && !strncmp(p - 4, ".bz2", 4)) {
193 bzs = memset(smalloc(sizeof(*bzs)), 0, sizeof(*bzs));
194 if(BZ2_bzDecompressInit(bzs, 0, 0) != BZ_OK) {
195 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not initialize decompression library"));
202 if((len > 4) && !strncmp(p - 4, ".xml", 4)) {
205 handler = pubhubxmlhandler;
207 handler = pubhuboldhandler;
209 handler(PHO_INIT, NULL, 0);