source: url.c @ b7d3cc34

0.99
Last change on this file since b7d3cc34 was b7d3cc34, checked in by Wilmer van der Gaast <wilmer@…>, at 2005-11-06T18:23:18Z

Initial repository (0.99 release tree)

  • Property mode set to 100644
File size: 3.2 KB
RevLine 
[b7d3cc34]1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2001-2004 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., 59 Temple Place,
23  Suite 330, Boston, MA  02111-1307  USA
24*/
25
26#include "url.h"
27
28/* Convert an URL to a url_t structure                                  */
29int url_set( url_t *url, char *set_url )
30{
31        char s[MAX_STRING];
32        char *i, *j;
33       
34        /* protocol://                                                  */
35        if( ( i = strstr( set_url, "://" ) ) == NULL )
36        {
37                url->proto = PROTO_DEFAULT;
38                strncpy( s, set_url, MAX_STRING );
39        }
40        else
41        {
42                if( g_strncasecmp( set_url, "http", i - set_url ) == 0 )
43                        url->proto = PROTO_HTTP;
44                else if( g_strncasecmp( set_url, "socks4", i - set_url ) == 0 )
45                        url->proto = PROTO_SOCKS4;
46                else if( g_strncasecmp( set_url, "socks5", i - set_url ) == 0 )
47                        url->proto = PROTO_SOCKS5;
48                else
49                {
50                        return( 0 );
51                }
52                strncpy( s, i + 3, MAX_STRING );
53        }
54       
55        /* Split                                                        */
56        if( ( i = strchr( s, '/' ) ) == NULL )
57        {
58                strcpy( url->dir, "/" );
59        }
60        else
61        {
62                *i = 0;
63                g_snprintf( url->dir, MAX_STRING, "/%s", i + 1 );
64                if( url->proto == PROTO_HTTP )
65                        http_encode( url->dir );
66        }
67        strncpy( url->host, s, MAX_STRING );
68        j = strchr( url->dir, '?' );
69        if( j != NULL )
70                *j = 0;
71        i = strrchr( url->dir, '/' );
72        *i = 0;
73        if( j != NULL )
74                *j = '?';
75        if( i == NULL )
76        {
77                strcpy( url->file, url->dir );
78                strcpy( url->dir, "/" );
79        }
80        else
81        {
82                strcpy( url->file, i + 1 );
83                strcat( url->dir, "/" );
84        }
85       
86        /* Check for username in host field                             */
87        if( strrchr( url->host, '@' ) != NULL )
88        {
89                strncpy( url->user, url->host, MAX_STRING );
90                i = strrchr( url->user, '@' );
91                *i = 0;
92                strcpy( url->host, i + 1 );
93                *url->pass = 0;
94        }
95        /* If not: Fill in defaults                                     */
96        else
97        {
98                if( url->proto == PROTO_FTP )
99                {
100                        strcpy( url->user, "anonymous" );
101                        strcpy( url->pass, "-p.artmaps@lintux.cx" );
102                }
103                else
104                {
105                        *url->user = *url->pass = 0;
106                }
107        }
108       
109        /* Password?                                                    */
110        if( ( i = strchr( url->user, ':' ) ) != NULL )
111        {
112                *i = 0;
113                strcpy( url->pass, i + 1 );
114        }
115        /* Port number?                                                 */
116        if( ( i = strchr( url->host, ':' ) ) != NULL )
117        {
118                *i = 0;
119                sscanf( i + 1, "%i", &url->port );
120        }
121        /* Take default port numbers from /etc/services                 */
122        else
123        {
124                if( url->proto == PROTO_HTTP )
125                        url->port = 8080;
126                else if( url->proto == PROTO_SOCKS4 || url->proto == PROTO_SOCKS4 )
127                        url->port = 1080;
128        }
129       
130        return( url->port > 0 );
131}
Note: See TracBrowser for help on using the repository browser.