Commit 4f3ca814 4f3ca8147d96eac6fac979844f8ef94b4c16d394 by Alain Magloire

commit simple url imap parsing.

1 parent 61ec5e08
...@@ -49,6 +49,7 @@ tcp.c \ ...@@ -49,6 +49,7 @@ tcp.c \
49 trans_stream.c \ 49 trans_stream.c \
50 url.c \ 50 url.c \
51 url_file.c \ 51 url_file.c \
52 url_imap.c \
52 url_mbox.c \ 53 url_mbox.c \
53 url_path.c \ 54 url_path.c \
54 url_pop.c \ 55 url_pop.c \
......
1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <errno.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26
27 #include <registrar0.h>
28 #include <url0.h>
29
30 static void url_imap_destroy (url_t url);
31
32 static void
33 url_imap_destroy (url_t url)
34 {
35 (void)url;
36 }
37
38 /*
39 IMAP URL
40 imap://[<user>;AUTH=<auth>@]<host>/
41 */
42 int
43 _url_imap_init (url_t url)
44 {
45 const char *host, *indexe;
46 char *name = url->name;
47
48 /* reject the obvious */
49 if (name == NULL || strncmp (MU_IMAP_SCHEME, name, MU_IMAP_SCHEME_LEN) != 0)
50 return EINVAL;
51
52 /* do I need to decode url encoding '% hex hex' ? */
53
54 /* TYPE */
55 url->_destroy = url_imap_destroy;
56
57 /* SCHEME */
58 url->scheme = strdup (MU_IMAP_SCHEME);
59 if (url->scheme == NULL)
60 {
61 url_imap_destroy (url);
62 return ENOMEM;
63 }
64
65 name += MU_IMAP_SCHEME_LEN; /* pass the scheme */
66
67 host = strchr (name, '@');
68 if (host == NULL)
69 host= name;
70
71 /* looking for ";auth=auth-enc" */
72 for (indexe = name; indexe != host; indexe++)
73 {
74 /* Auth ? */
75 if (*indexe == ';')
76 {
77 /* make sure it's the token */
78 if (strncasecmp(indexe + 1, "auth=", 5) == 0)
79 break;
80 }
81 }
82
83 /* USER */
84 url->user = malloc(indexe - name + 1);
85 if (url->user == NULL)
86 {
87 url_imap_destroy (url);
88 return -1;
89 }
90 ((char *)memcpy(url->user, name, indexe - name))[indexe - name] = '\0';
91
92 /* AUTH */
93 if (indexe == host)
94 {
95 /* Use default AUTH '*' */
96 url->auth = malloc (1 + 1);
97 if (url->auth)
98 {
99 url->auth[0] = '*';
100 url->auth[1] = '\0';
101 }
102 }
103 else
104 {
105 /* move pass AUTH= */
106 indexe += 6;
107 url->auth = malloc (host - indexe + 1);
108 if (url->auth)
109 {
110 ((char *)memcpy (url->auth, indexe, host - indexe))
111 [host - indexe] = '\0';
112 }
113 }
114
115 if (url->auth == NULL)
116 {
117 url_imap_destroy (url);
118 return -1;
119 }
120
121 /* HOST*/
122 if (*host == '@')
123 host++;
124
125 indexe = strchr (host, '/');
126 if (indexe == NULL)
127 url->host = strdup (host);
128 else
129 {
130 url->host = malloc (indexe - host + 1);
131 if (url->host)
132 ((char *)memcpy (url->host, host, indexe - host))[indexe - host] = '\0';
133 url->query = strdup (indexe + 1);
134 }
135
136 if (url->host == NULL)
137 {
138 url_imap_destroy (url);
139 return ENOMEM;
140 }
141
142 url->port = MU_IMAP_PORT;
143 return 0;
144 }