[b7d3cc34] | 1 | /********************************************************************\ |
---|
| 2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
| 3 | * * |
---|
[52b3a99] | 4 | * Copyright 2001-2005 Wilmer van der Gaast and others * |
---|
[b7d3cc34] | 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., 59 Temple Place, |
---|
| 23 | Suite 330, Boston, MA 02111-1307 USA |
---|
| 24 | */ |
---|
| 25 | |
---|
| 26 | #include "url.h" |
---|
| 27 | |
---|
[9ad86bb] | 28 | /* Convert an URL to a url_t structure */ |
---|
[e9eaee6] | 29 | int url_set( url_t *url, const char *set_url ) |
---|
[b7d3cc34] | 30 | { |
---|
[9ad86bb] | 31 | char s[MAX_STRING+1]; |
---|
[52b3a99] | 32 | char *i; |
---|
[b7d3cc34] | 33 | |
---|
[9ad86bb] | 34 | memset( url, 0, sizeof( url_t ) ); |
---|
| 35 | memset( s, 0, sizeof( s ) ); |
---|
| 36 | |
---|
| 37 | /* protocol:// */ |
---|
[b7d3cc34] | 38 | if( ( i = strstr( set_url, "://" ) ) == NULL ) |
---|
| 39 | { |
---|
| 40 | url->proto = PROTO_DEFAULT; |
---|
| 41 | strncpy( s, set_url, MAX_STRING ); |
---|
| 42 | } |
---|
| 43 | else |
---|
| 44 | { |
---|
[6fda350] | 45 | if( g_strncasecmp( set_url, "http", i - set_url ) == 0 ) |
---|
[b7d3cc34] | 46 | url->proto = PROTO_HTTP; |
---|
[6fda350] | 47 | else if( g_strncasecmp( set_url, "https", i - set_url ) == 0 ) |
---|
| 48 | url->proto = PROTO_HTTPS; |
---|
[b7d3cc34] | 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 |
---|
[9ad86bb] | 54 | return 0; |
---|
| 55 | |
---|
[b7d3cc34] | 56 | strncpy( s, i + 3, MAX_STRING ); |
---|
| 57 | } |
---|
| 58 | |
---|
[9ad86bb] | 59 | /* Split */ |
---|
[b7d3cc34] | 60 | if( ( i = strchr( s, '/' ) ) == NULL ) |
---|
| 61 | { |
---|
[52b3a99] | 62 | strcpy( url->file, "/" ); |
---|
[b7d3cc34] | 63 | } |
---|
| 64 | else |
---|
| 65 | { |
---|
[52b3a99] | 66 | strncpy( url->file, i, MAX_STRING ); |
---|
[b7d3cc34] | 67 | *i = 0; |
---|
| 68 | } |
---|
| 69 | strncpy( url->host, s, MAX_STRING ); |
---|
| 70 | |
---|
[9ad86bb] | 71 | /* Check for username in host field */ |
---|
[b7d3cc34] | 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 | } |
---|
[9ad86bb] | 80 | /* If not: Fill in defaults */ |
---|
[b7d3cc34] | 81 | else |
---|
| 82 | { |
---|
[52b3a99] | 83 | *url->user = *url->pass = 0; |
---|
[b7d3cc34] | 84 | } |
---|
| 85 | |
---|
[9ad86bb] | 86 | /* Password? */ |
---|
[b7d3cc34] | 87 | if( ( i = strchr( url->user, ':' ) ) != NULL ) |
---|
| 88 | { |
---|
| 89 | *i = 0; |
---|
| 90 | strcpy( url->pass, i + 1 ); |
---|
| 91 | } |
---|
[9ad86bb] | 92 | /* Port number? */ |
---|
[b7d3cc34] | 93 | if( ( i = strchr( url->host, ':' ) ) != NULL ) |
---|
| 94 | { |
---|
| 95 | *i = 0; |
---|
[52b3a99] | 96 | sscanf( i + 1, "%d", &url->port ); |
---|
[b7d3cc34] | 97 | } |
---|
| 98 | else |
---|
| 99 | { |
---|
| 100 | if( url->proto == PROTO_HTTP ) |
---|
[52b3a99] | 101 | url->port = 80; |
---|
| 102 | else if( url->proto == PROTO_HTTPS ) |
---|
| 103 | url->port = 443; |
---|
[b6423a09] | 104 | else if( url->proto == PROTO_SOCKS4 || url->proto == PROTO_SOCKS5 ) |
---|
[b7d3cc34] | 105 | url->port = 1080; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | return( url->port > 0 ); |
---|
| 109 | } |
---|