1 | /********************************************************************\ |
---|
2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
3 | * * |
---|
4 | * Copyright 2001-2005 Wilmer van der Gaast and others * |
---|
5 | \********************************************************************/ |
---|
6 | |
---|
7 | /* URL/mirror stuff - Stolen from Axel */ |
---|
8 | |
---|
9 | /* |
---|
10 | This program is free software; you can redistribute it and/or modify |
---|
11 | it under the terms of the GNU General Public License as published by |
---|
12 | the Free Software Foundation; either version 2 of the License, or |
---|
13 | (at your option) any later version. |
---|
14 | |
---|
15 | This program is distributed in the hope that it will be useful, |
---|
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
18 | GNU General Public License for more details. |
---|
19 | |
---|
20 | You should have received a copy of the GNU General Public License with |
---|
21 | the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; |
---|
22 | if not, write to the Free Software Foundation, Inc., 51 Franklin St., |
---|
23 | Fifth Floor, Boston, MA 02110-1301 USA |
---|
24 | */ |
---|
25 | |
---|
26 | #include "url.h" |
---|
27 | |
---|
28 | /* Convert an URL to a url_t structure */ |
---|
29 | int url_set( url_t *url, const char *set_url ) |
---|
30 | { |
---|
31 | char s[MAX_STRING+1]; |
---|
32 | char *i; |
---|
33 | |
---|
34 | memset( url, 0, sizeof( url_t ) ); |
---|
35 | memset( s, 0, sizeof( s ) ); |
---|
36 | |
---|
37 | /* protocol:// */ |
---|
38 | if( ( i = strstr( set_url, "://" ) ) == NULL ) |
---|
39 | { |
---|
40 | url->proto = PROTO_DEFAULT; |
---|
41 | strncpy( s, set_url, MAX_STRING ); |
---|
42 | } |
---|
43 | else |
---|
44 | { |
---|
45 | if( g_strncasecmp( set_url, "http", i - set_url ) == 0 ) |
---|
46 | url->proto = PROTO_HTTP; |
---|
47 | else if( g_strncasecmp( set_url, "https", i - set_url ) == 0 ) |
---|
48 | url->proto = PROTO_HTTPS; |
---|
49 | else if( g_strncasecmp( set_url, "socks4", i - set_url ) == 0 ) |
---|
50 | url->proto = PROTO_SOCKS4; |
---|
51 | else if( g_strncasecmp( set_url, "socks5", i - set_url ) == 0 ) |
---|
52 | url->proto = PROTO_SOCKS5; |
---|
53 | else |
---|
54 | return 0; |
---|
55 | |
---|
56 | strncpy( s, i + 3, MAX_STRING ); |
---|
57 | } |
---|
58 | |
---|
59 | /* Split */ |
---|
60 | if( ( i = strchr( s, '/' ) ) == NULL ) |
---|
61 | { |
---|
62 | strcpy( url->file, "/" ); |
---|
63 | } |
---|
64 | else |
---|
65 | { |
---|
66 | strncpy( url->file, i, MAX_STRING ); |
---|
67 | *i = 0; |
---|
68 | } |
---|
69 | strncpy( url->host, s, MAX_STRING ); |
---|
70 | |
---|
71 | /* Check for username in host field */ |
---|
72 | if( strrchr( url->host, '@' ) != NULL ) |
---|
73 | { |
---|
74 | strncpy( url->user, url->host, MAX_STRING ); |
---|
75 | i = strrchr( url->user, '@' ); |
---|
76 | *i = 0; |
---|
77 | strcpy( url->host, i + 1 ); |
---|
78 | *url->pass = 0; |
---|
79 | } |
---|
80 | /* If not: Fill in defaults */ |
---|
81 | else |
---|
82 | { |
---|
83 | *url->user = *url->pass = 0; |
---|
84 | } |
---|
85 | |
---|
86 | /* Password? */ |
---|
87 | if( ( i = strchr( url->user, ':' ) ) != NULL ) |
---|
88 | { |
---|
89 | *i = 0; |
---|
90 | strcpy( url->pass, i + 1 ); |
---|
91 | } |
---|
92 | /* Port number? */ |
---|
93 | if( ( i = strchr( url->host, ':' ) ) != NULL ) |
---|
94 | { |
---|
95 | *i = 0; |
---|
96 | sscanf( i + 1, "%d", &url->port ); |
---|
97 | } |
---|
98 | else |
---|
99 | { |
---|
100 | if( url->proto == PROTO_HTTP ) |
---|
101 | url->port = 80; |
---|
102 | else if( url->proto == PROTO_HTTPS ) |
---|
103 | url->port = 443; |
---|
104 | else if( url->proto == PROTO_SOCKS4 || url->proto == PROTO_SOCKS5 ) |
---|
105 | url->port = 1080; |
---|
106 | } |
---|
107 | |
---|
108 | return( url->port > 0 ); |
---|
109 | } |
---|