[5ebff60] | 1 | /********************************************************************\ |
---|
[b7d3cc34] | 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; |
---|
[6f10697] | 22 | if not, write to the Free Software Foundation, Inc., 51 Franklin St., |
---|
| 23 | Fifth Floor, Boston, MA 02110-1301 USA |
---|
[b7d3cc34] | 24 | */ |
---|
| 25 | |
---|
| 26 | #include "url.h" |
---|
| 27 | |
---|
[9ad86bb] | 28 | /* Convert an URL to a url_t structure */ |
---|
[5ebff60] | 29 | int url_set(url_t *url, const char *set_url) |
---|
[b7d3cc34] | 30 | { |
---|
[5ebff60] | 31 | char s[MAX_STRING + 1]; |
---|
[52b3a99] | 32 | char *i; |
---|
[5ebff60] | 33 | |
---|
| 34 | memset(url, 0, sizeof(url_t)); |
---|
| 35 | memset(s, 0, sizeof(s)); |
---|
| 36 | |
---|
[9ad86bb] | 37 | /* protocol:// */ |
---|
[5ebff60] | 38 | if ((i = strstr(set_url, "://")) == NULL) { |
---|
[b7d3cc34] | 39 | url->proto = PROTO_DEFAULT; |
---|
[5ebff60] | 40 | strncpy(s, set_url, MAX_STRING); |
---|
| 41 | } else { |
---|
| 42 | if (g_strncasecmp(set_url, "http", i - set_url) == 0) { |
---|
[b7d3cc34] | 43 | url->proto = PROTO_HTTP; |
---|
[5ebff60] | 44 | } else if (g_strncasecmp(set_url, "https", i - set_url) == 0) { |
---|
[6fda350] | 45 | url->proto = PROTO_HTTPS; |
---|
[5ebff60] | 46 | } else if (g_strncasecmp(set_url, "socks4", i - set_url) == 0) { |
---|
[b7d3cc34] | 47 | url->proto = PROTO_SOCKS4; |
---|
[5ebff60] | 48 | } else if (g_strncasecmp(set_url, "socks5", i - set_url) == 0) { |
---|
[b7d3cc34] | 49 | url->proto = PROTO_SOCKS5; |
---|
[5ebff60] | 50 | } else { |
---|
[9ad86bb] | 51 | return 0; |
---|
[5ebff60] | 52 | } |
---|
| 53 | |
---|
| 54 | strncpy(s, i + 3, MAX_STRING); |
---|
[b7d3cc34] | 55 | } |
---|
[5ebff60] | 56 | |
---|
[9ad86bb] | 57 | /* Split */ |
---|
[5ebff60] | 58 | if ((i = strchr(s, '/')) == NULL) { |
---|
| 59 | strcpy(url->file, "/"); |
---|
| 60 | } else { |
---|
| 61 | strncpy(url->file, i, MAX_STRING); |
---|
[b7d3cc34] | 62 | *i = 0; |
---|
| 63 | } |
---|
[5ebff60] | 64 | strncpy(url->host, s, MAX_STRING); |
---|
| 65 | |
---|
[9ad86bb] | 66 | /* Check for username in host field */ |
---|
[5ebff60] | 67 | if (strrchr(url->host, '@') != NULL) { |
---|
| 68 | strncpy(url->user, url->host, MAX_STRING); |
---|
| 69 | i = strrchr(url->user, '@'); |
---|
[b7d3cc34] | 70 | *i = 0; |
---|
[5ebff60] | 71 | strcpy(url->host, i + 1); |
---|
[b7d3cc34] | 72 | *url->pass = 0; |
---|
| 73 | } |
---|
[9ad86bb] | 74 | /* If not: Fill in defaults */ |
---|
[5ebff60] | 75 | else { |
---|
[52b3a99] | 76 | *url->user = *url->pass = 0; |
---|
[b7d3cc34] | 77 | } |
---|
[5ebff60] | 78 | |
---|
[9ad86bb] | 79 | /* Password? */ |
---|
[5ebff60] | 80 | if ((i = strchr(url->user, ':')) != NULL) { |
---|
[b7d3cc34] | 81 | *i = 0; |
---|
[5ebff60] | 82 | strcpy(url->pass, i + 1); |
---|
[b7d3cc34] | 83 | } |
---|
[9ad86bb] | 84 | /* Port number? */ |
---|
[5ebff60] | 85 | if ((i = strchr(url->host, ':')) != NULL) { |
---|
[b7d3cc34] | 86 | *i = 0; |
---|
[5ebff60] | 87 | sscanf(i + 1, "%d", &url->port); |
---|
| 88 | } else { |
---|
| 89 | if (url->proto == PROTO_HTTP) { |
---|
[52b3a99] | 90 | url->port = 80; |
---|
[5ebff60] | 91 | } else if (url->proto == PROTO_HTTPS) { |
---|
[52b3a99] | 92 | url->port = 443; |
---|
[5ebff60] | 93 | } else if (url->proto == PROTO_SOCKS4 || url->proto == PROTO_SOCKS5) { |
---|
[b7d3cc34] | 94 | url->port = 1080; |
---|
[5ebff60] | 95 | } |
---|
[b7d3cc34] | 96 | } |
---|
[5ebff60] | 97 | |
---|
| 98 | return(url->port > 0); |
---|
[b7d3cc34] | 99 | } |
---|