Mon, 24 Apr 2000 01:47:24 +0000
[gaim-migrate @ 163]
This should be interesting. We'll see how well this works. I can't explain
why, but I have a bad feeling about this one.
| 1 | 1 | /* |
| 2 | * gaim | |
| 3 | * | |
| 4 | * Copyright (C) 1998-1999, Mark Spencer <markster@marko.net> | |
| 5 | * | |
| 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. | |
| 10 | * | |
| 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. | |
| 15 | * | |
| 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 19 | * | |
| 20 | */ | |
| 21 | ||
| 22 | #include <string.h> | |
| 23 | #include <stdio.h> | |
| 24 | #include <stdlib.h> | |
| 25 | #include <sys/time.h> | |
| 26 | #include <unistd.h> | |
| 27 | #include <gtk/gtk.h> | |
| 28 | #include <gdk/gdkprivate.h> | |
| 29 | #include <gdk/gdkx.h> | |
| 30 | #include "gaim.h" | |
| 31 | #include <sys/types.h> | |
| 32 | #include <sys/socket.h> | |
| 33 | #include <netdb.h> | |
| 34 | #include <netinet/in.h> | |
| 35 | ||
| 36 | gchar * strip_html(gchar * text) | |
| 37 | { | |
| 38 | int i, j; | |
| 39 | int visible = 1; | |
| 40 | gchar *text2 = g_malloc(strlen(text) + 1); | |
| 41 | ||
| 42 | strcpy(text2, text); | |
| 43 | for (i = 0, j = 0;text2[i]; i++) | |
| 44 | { | |
| 45 | if(text2[i]=='<') | |
| 46 | { | |
| 47 | visible = 0; | |
| 48 | continue; | |
| 49 | } | |
| 50 | else if(text2[i]=='>') | |
| 51 | { | |
| 52 | visible = 1; | |
| 53 | continue; | |
| 54 | } | |
| 55 | if(visible) | |
| 56 | { | |
| 57 | text2[j++] = text2[i]; | |
| 58 | } | |
| 59 | } | |
| 60 | text2[j] = '\0'; | |
| 61 | return text2; | |
| 62 | } | |
| 63 | ||
| 64 | struct g_url parse_url(char *url) | |
| 65 | { | |
| 66 | struct g_url test; | |
| 67 | char scan_info[255]; | |
| 68 | char port[5]; | |
| 69 | int f; | |
| 70 | ||
| 71 | if (strstr(url, "http://")) | |
| 72 | g_snprintf(scan_info, sizeof(scan_info), "http://%%[A-Za-z0-9.]:%%[0-9]/%%[A-Za-z0-9.~_-/&%%?]"); | |
| 73 | else | |
| 74 | g_snprintf(scan_info, sizeof(scan_info), "%%[A-Za-z0-9.]:%%[0-9]/%%[A-Za-z0-9.~_-/&%%?]"); | |
| 75 | f = sscanf(url, scan_info, test.address, port, test.page); | |
| 76 | if (f == 1) { | |
| 77 | if (strstr(url, "http://")) | |
| 78 | g_snprintf(scan_info, sizeof(scan_info), "http://%%[A-Za-z0-9.]/%%[A-Za-z0-9.~_-/&%%?]"); | |
| 79 | else | |
| 80 | g_snprintf(scan_info, sizeof(scan_info), "%%[A-Za-z0-9.]/%%[A-Za-z0-9.~_-/&%%?]"); | |
| 81 | f = sscanf(url, scan_info, test.address, test.page); | |
| 82 | g_snprintf(port, sizeof(test.port), "80"); | |
| 83 | port[2] = 0; | |
| 84 | } | |
| 85 | if (f == 1) { | |
| 86 | if (strstr(url, "http://")) | |
| 87 | g_snprintf(scan_info, sizeof(scan_info), "http://%%[A-Za-z0-9.]"); | |
| 88 | else | |
| 89 | g_snprintf(scan_info, sizeof(scan_info), "%%[A-Za-z0-9.]"); | |
| 90 | f = sscanf(url, scan_info, test.address); | |
| 91 | g_snprintf(test.page, sizeof(test.page), "%c", '\0'); | |
| 92 | } | |
| 93 | ||
| 94 | sscanf(port, "%d", &test.port); | |
| 95 | return test; | |
| 96 | } | |
| 97 | ||
| 98 | char *grab_url(char *url) | |
| 99 | { | |
| 100 | struct g_url website; | |
| 101 | char *webdata = NULL; | |
| 102 | int sock; | |
| 103 | int len; | |
| 104 | int datalen = 0; | |
| 105 | struct hostent *host; | |
| 106 | struct sockaddr_in site; | |
| 107 | char buf[256]; | |
| 108 | char data; | |
| 109 | FILE *sockfile; | |
| 110 | int startsaving = 0; | |
| 111 | GtkWidget *pw = NULL, *pbar = NULL, *label; | |
| 112 | ||
| 113 | website = parse_url(url); | |
| 114 | ||
| 115 | host = gethostbyname(website.address); | |
| 116 | if (!host) { return g_strdup("g001: Error resolving host\n"); } | |
| 117 | ||
| 118 | site.sin_family = AF_INET; | |
| 119 | site.sin_addr.s_addr = *(long *)(host->h_addr); | |
| 120 | site.sin_port = htons(website.port); | |
| 121 | ||
| 122 | sock = socket(AF_INET, SOCK_STREAM, 0); | |
| 123 | if (sock < 0) { return g_strdup("g002: Socket Error\n"); } | |
| 124 | ||
| 125 | if (connect(sock, (struct sockaddr *)&site, sizeof(site))) | |
| 126 | return g_strdup("g003: Error opening connection.\n"); | |
| 127 | ||
| 128 | sockfile = fdopen(sock, "r+"); | |
| 129 | ||
| 130 | g_snprintf(buf, sizeof(buf), "GET /%s HTTP/1.0\n\n", website.page); | |
| 131 | g_snprintf(debug_buff, sizeof(debug_buff), "Request: %s\n", buf); | |
| 132 | debug_print(debug_buff); | |
| 133 | fputs(buf, sockfile); | |
| 134 | ||
| 135 | webdata = NULL; | |
| 136 | len = 0; | |
| 137 | ||
| 35 | 138 | while ((data = fgetc(sockfile)) != EOF) { |
| 1 | 139 | if (!data) |
| 140 | continue; | |
| 141 | ||
| 142 | if (!startsaving && data == '<') { | |
| 143 | #ifdef HAVE_STRSTR | |
| 144 | char *cs = strstr(webdata, "Content-Length"); | |
| 145 | if (cs) { | |
| 146 | char tmpbuf[1024]; | |
| 147 | sscanf(cs, "Content-Length: %d", &datalen); | |
| 148 | ||
| 149 | g_snprintf(tmpbuf, 1024, "Getting %d bytes from %s", datalen, url); | |
| 150 | pw = gtk_dialog_new(); | |
| 151 | ||
| 152 | label = gtk_label_new(tmpbuf); | |
| 153 | gtk_widget_show(label); | |
| 154 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(pw)->vbox), | |
| 155 | label, FALSE, FALSE, 5); | |
| 156 | ||
| 157 | pbar = gtk_progress_bar_new(); | |
| 158 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(pw)->action_area), | |
| 159 | pbar, FALSE, FALSE, 5); | |
| 160 | gtk_widget_show(pbar); | |
| 161 | ||
| 162 | gtk_window_set_title(GTK_WINDOW(pw), "Getting Data"); | |
| 163 | ||
| 164 | gtk_widget_realize(pw); | |
| 165 | aol_icon(pw->window); | |
| 166 | ||
| 167 | gtk_widget_show(pw); | |
| 168 | } else | |
| 169 | datalen = 0; | |
| 170 | #else | |
| 171 | datalen = 0; | |
| 172 | #endif | |
| 173 | g_free(webdata); | |
| 174 | webdata = NULL; | |
| 175 | len = 0; | |
| 176 | startsaving = 1; | |
| 177 | } | |
| 178 | ||
| 179 | len++; | |
| 180 | webdata = g_realloc(webdata, len); | |
| 181 | webdata[len - 1] = data; | |
| 182 | ||
| 183 | if (pbar) | |
| 184 | gtk_progress_bar_update(GTK_PROGRESS_BAR(pbar), | |
| 185 | ((100 * len) / datalen) / 100.0); | |
| 186 | ||
| 187 | while (gtk_events_pending()) | |
| 188 | gtk_main_iteration(); | |
| 189 | } | |
| 190 | ||
| 191 | webdata = g_realloc(webdata, len+1); | |
| 192 | webdata[len] = 0; | |
| 193 | ||
| 194 | ||
| 195 | g_snprintf(debug_buff, sizeof(debug_buff), "Receieved: '%s'\n", webdata); | |
| 196 | debug_print(debug_buff); | |
| 197 | ||
| 198 | if (pw) | |
| 199 | gtk_widget_destroy(pw); | |
| 200 | ||
| 201 | close(sock); | |
| 202 | return webdata; | |
| 203 | } | |
| 204 | ||
| 205 | char *fix_url(gchar *buf) | |
| 206 | { | |
| 207 | char *new,*tmp; | |
| 208 | int size; | |
| 209 | ||
| 210 | size=8; | |
| 211 | size+=strlen(quad_addr); | |
| 212 | tmp=strchr(strchr(buf,':')+1,':'); | |
| 213 | size+=strlen(tmp); | |
| 214 | new=g_malloc(size); | |
| 215 | strcpy(new,"http://"); | |
| 216 | strcat(new,quad_addr); | |
| 217 | strcat(new,tmp); | |
| 218 | return(new); | |
| 219 | } | |
| 220 | ||
| 221 |