url.c
21.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001, 2007, 2008 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301 USA */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include <mailutils/mutil.h>
#include <mailutils/errno.h>
#include <mailutils/argcv.h>
#include <url0.h>
#define AC2(a,b) a ## b
#define AC4(a,b,c,d) a ## b ## c ## d
static int url_parse0 (mu_url_t, char *);
static int
parse_query (const char *query,
char *delim,
int *pargc, char ***pargv, const char **pend)
{
size_t count, i;
char **v;
const char *p;
for (p = query, count = 0; ; count++)
{
size_t len = strcspn (p, delim);
p += len;
if (!*p || *p == delim[1])
break;
p++;
}
if (pend)
*pend = p;
if (p == query)
return 0;
count++;
v = calloc (count + 1, sizeof (v[0]));
for (i = 0, p = query; i < count; i++)
{
size_t len = strcspn (p, delim);
v[i] = mu_url_decode_len (p, len);
if (v[i] == NULL)
{
mu_argcv_free (i, v);
return 1;
}
p += len + 1;
}
v[i] = NULL;
*pargc = count;
*pargv = v;
return 0;
}
int
mu_url_create (mu_url_t *purl, const char *name)
{
mu_url_t url = calloc (1, sizeof (*url));
if (url == NULL)
return ENOMEM;
url->name = strdup (name);
if (url->name == NULL)
{
free (url);
return ENOMEM;
}
*purl = url;
return 0;
}
static char **
argcv_copy (size_t argc, char **argv)
{
size_t i;
char **nv = calloc (argc + 1, sizeof (nv[0]));
if (!nv)
return NULL;
for (i = 0; i < argc; i++)
if ((nv[i] = strdup (argv[i])) == NULL)
{
mu_argcv_free (i, nv);
free (nv);
return NULL;
}
return nv;
}
static int
mu_url_copy0 (mu_url_t old_url, mu_url_t new_url)
{
const char *str;
size_t argc;
char **argv;
int rc;
#define URLCOPY(what) \
do \
{ \
rc = AC2(mu_url_sget_,what) (old_url, &str); \
if (rc == 0) \
{ \
if ((new_url->what = strdup (str)) == NULL) \
return ENOMEM; \
} \
else if (rc != MU_ERR_NOENT) \
return rc; \
} \
while (0);
URLCOPY (scheme);
URLCOPY (user);
URLCOPY (passwd);
URLCOPY (auth);
URLCOPY (host);
new_url->port = old_url->port;
URLCOPY (path);
rc = mu_url_sget_fvpairs (old_url, &argc, &argv);
if (rc == 0 && argc)
{
if ((new_url->fvpairs = argcv_copy (argc, argv)) == NULL)
return ENOMEM;
new_url->fvcount = argc;
}
rc = mu_url_sget_query (old_url, &argc, &argv);
if (rc == 0 && argc)
{
if ((new_url->qargv = argcv_copy (argc, argv)) == NULL)
return ENOMEM;
new_url->qargc = argc;
}
return 0;
#undef URLCOPY
}
int
mu_url_dup (mu_url_t old_url, mu_url_t *new_url)
{
mu_url_t url;
int rc = mu_url_create (&url, mu_url_to_string (old_url));
if (rc)
return rc;
rc = mu_url_copy0 (old_url, url);
if (rc == 0)
*new_url = url;
else
mu_url_destroy (&url);
return rc;
}
void
mu_url_destroy (mu_url_t * purl)
{
if (purl && *purl)
{
mu_url_t url = (*purl);
if (url->_destroy)
url->_destroy (url);
if (url->name)
free (url->name);
if (url->scheme)
free (url->scheme);
if (url->user)
free (url->user);
if (url->passwd)
free (url->passwd);
if (url->auth)
free (url->auth);
if (url->host)
free (url->host);
if (url->path)
free (url->path);
if (url->fvcount)
mu_argcv_free (url->fvcount, url->fvpairs);
mu_argcv_free (url->qargc, url->qargv);
free (url);
*purl = NULL;
}
}
int
mu_url_parse (mu_url_t url)
{
int err = 0;
char *n = NULL;
struct _mu_url u;
if (!url || !url->name)
return EINVAL;
memset (&u, 0, sizeof u);
/* can't have been parsed already */
if (url->scheme || url->user || url->passwd || url->auth ||
url->host || url->path || url->qargc)
return EINVAL;
n = strdup (url->name);
if (!n)
return ENOMEM;
err = url_parse0 (&u, n);
if (!err)
{
/* Dup the strings we found. We wouldn't have to do this
if we did a single alloc of the source url name, and
kept it around. It's also a good time to do hex decoding,
though.
*/
#define UALLOC(X) \
if (u.X && u.X[0] && (url->X = mu_url_decode(u.X)) == 0) \
{ \
err = ENOMEM; \
goto CLEANUP; \
} \
else \
{ \
/* Set zero-length strings to NULL. */ \
u.X = NULL; \
}
UALLOC (scheme);
UALLOC (user);
UALLOC (passwd);
UALLOC (auth);
UALLOC (host);
UALLOC (path);
#undef UALLOC
url->fvcount = u.fvcount;
url->fvpairs = u.fvpairs;
url->qargc = u.qargc;
url->qargv = u.qargv;
url->port = u.port;
}
CLEANUP:
free (n);
if (err)
{
#define UFREE(X) if (X) { free(X); X = 0; }
UFREE (url->scheme);
UFREE (url->user);
UFREE (url->passwd);
UFREE (url->auth);
UFREE (url->host);
UFREE (url->path);
mu_argcv_free (url->fvcount, url->fvpairs);
mu_argcv_free (url->qargc, url->qargv);
#undef UFREE
}
return err;
}
/*
Syntax, condensed from RFC 1738, and extended with the ;auth=
of RFC 2384 (for POP) and RFC 2192 (for IMAP):
url =
scheme ":" [ "//"
[ user [ ( ":" password ) | ( ";auth=" auth ) ] "@" ]
host [ ":" port ]
[ ( "/" urlpath ) | ( "?" query ) ] ]
All hell will break loose in this parser if the user/pass/auth
portion is missing, and the urlpath has any @ or : characters
in it. A imap mailbox, say, named after the email address of
the person the mail is from:
imap://imap.uniserve.com/alain@qnx.com
Is this required to be % quoted, though? I hope so!
*/
static int
url_parse0 (mu_url_t u, char *name)
{
char *p; /* pointer into name */
/* reject the obvious */
if (name == NULL)
return EINVAL;
if (name[0] == '/')
{
u->scheme = "file";
}
else if (name[0] == '|')
{
int rc;
u->scheme = "prog";
rc = mu_argcv_get (name + 1, NULL, NULL, &u->qargc, &u->qargv);
if (rc == 0)
{
u->path = strdup (u->qargv[0]);
if (!u->path)
rc = ENOMEM;
}
return rc;
}
else
{
/* Parse out the SCHEME. */
p = strchr (name, ':');
if (p == NULL)
return MU_ERR_PARSE;
*p++ = 0;
u->scheme = name;
/* RFC 1738, section 2.1, lower the scheme case */
for (; name < p; name++)
*name = tolower (*name);
name = p;
}
/* Check for nothing following the scheme. */
if (!*name)
return 0;
if (strncmp (name, "//", 2) == 0)
{
name += 2;
if (name[0] == '/')
{
u->path = name;
p = u->path + strcspn (u->path, ";?");
}
else
{
/* Split into LHS and RHS of the '@', and then parse each side. */
u->host = strchr (name, '@');
if (u->host == NULL)
u->host = name;
else
{
/* Parse the LHS into an identification/authentication pair. */
*u->host++ = 0;
u->user = name;
/* Try to split the user into a:
<user>:<password>
or
<user>:<password>;AUTH=<auth>
*/
for (; *name; name++)
{
if (*name == ':')
{
*name++ = 0;
u->passwd = name;
}
else if (*name == ';')
{
/* Make sure it's the auth token. */
if (strncasecmp (name + 1, "auth=", 5) == 0)
{
*name++ = 0;
name += 5;
u->auth = name;
break;
}
}
}
}
/* Parse the host and port from the RHS. */
p = strchr (u->host, ':');
if (p)
{
*p++ = 0;
u->port = strtol (p, &p, 10);
/* Check for garbage after the port: we should be on the start
of a path, a query, or at the end of the string. */
if (*p && strcspn (p, "/?") != 0)
return MU_ERR_PARSE;
}
else
p = u->host + strcspn (u->host, ";/?");
}
}
else
{
u->path = name;
p = u->path + strcspn (u->path, ";?");
}
/* Either way, if we're not at a nul, we're at a path or query. */
if (u->path == NULL && *p == '/')
{
/* found a path */
*p++ = 0;
u->path = p;
p = u->path + strcspn (u->path, ";?");
}
if (*p == ';')
{
*p++ = 0;
if (parse_query (p, ";?", &u->fvcount, &u->fvpairs, (const char **)&p))
return ENOMEM;
}
if (*p == '?')
{
/* found a query */
*p++ = 0;
if (parse_query (p, "&", &u->qargc, &u->qargv, NULL))
return ENOMEM;
}
return 0;
}
/* General accessors: */
#define ACCESSOR(action,field) AC4(mu_url_,action,_,field)
#define DECL_SGET(field) \
int \
ACCESSOR(sget,field) (mu_url_t url, char const **sptr) \
{ \
if (url == NULL) \
return EINVAL; \
if (!url->field) \
{ \
if (url->AC2(_get_,field)) \
{ \
size_t n; \
char *buf; \
\
int status = url->AC2(_get_,field) (url, NULL, 0, &n); \
if (status) \
return status; \
\
buf = malloc (n + 1); \
if (!buf) \
return ENOMEM; \
\
status = url->AC2(_get_,field) (url, buf, n + 1, NULL); \
if (status) \
return status; \
\
if (buf[0]) \
{ \
url->field = mu_url_decode (buf); \
free (buf); \
} \
else \
url->field = buf; \
if (!url->field) \
return ENOMEM; \
} \
else \
return MU_ERR_NOENT; \
} \
*sptr = url->field; \
return 0; \
}
#define DECL_GET(field) \
int \
ACCESSOR(get,field) (mu_url_t url, char *buf, size_t len, size_t *n) \
{ \
size_t i; \
const char *str; \
int status = ACCESSOR(sget, field) (url, &str); \
\
if (status) \
return status; \
\
i = mu_cpystr (buf, str, len); \
if (n) \
*n = i; \
return 0; \
}
#define DECL_AGET(field) \
int \
ACCESSOR(aget, field) (mu_url_t url, char **buf) \
{ \
const char *str; \
int status = ACCESSOR(sget, field) (url, &str); \
\
if (status) \
return status; \
\
if (str) \
{ \
*buf = strdup (str); \
if (!*buf) \
status = ENOMEM; \
} \
else \
*buf = NULL; \
return status; \
}
#define DECL_CMP(field) \
int \
ACCESSOR(is_same,field) (mu_url_t url1, mu_url_t url2) \
{ \
const char *s1, *s2; \
int status1, status2; \
\
status1 = ACCESSOR(sget, field) (url1, &s1); \
if (status1 && status1 != MU_ERR_NOENT) \
return 0; \
status2 = ACCESSOR(sget, field) (url2, &s2); \
if (status2 && status2 != MU_ERR_NOENT) \
return 0; \
\
if (status1 && status1 == status2) /* Both fields are missing */ \
return 1; \
return strcasecmp (s1, s2) == 0; \
}
#define DECL_ACCESSORS(field) \
DECL_SGET(field) \
DECL_GET(field) \
DECL_AGET(field) \
DECL_CMP(field)
/* Declare particular accessors */
DECL_ACCESSORS (scheme)
DECL_ACCESSORS (user)
/* FIXME: We should not store passwd in clear, but rather
have a simple encoding, and decoding mechanism */
DECL_ACCESSORS (passwd)
DECL_ACCESSORS (auth)
DECL_ACCESSORS (host)
DECL_ACCESSORS (path)
int
mu_url_sget_query (const mu_url_t url, size_t *qc, char ***qv)
{
if (url == NULL)
return EINVAL;
/* See FIXME below */
*qc = url->qargc;
*qv = url->qargv;
return 0;
}
int
mu_url_aget_query (const mu_url_t url, size_t *qc, char ***qv)
{
size_t qargc, i;
char **qargv;
char **qcopy;
int rc = mu_url_sget_fvpairs (url, &qargc, &qargv);
if (rc)
return rc;
qcopy = calloc (qargc + 1, sizeof (qcopy[0]));
if (!qcopy)
return errno;
for (i = 0; i < qargc; i++)
{
if (!(qcopy[i] = strdup (qargv[i])))
{
mu_argcv_free (i, qcopy);
return errno;
}
}
qcopy[i] = NULL;
*qc = qargc;
*qv = qcopy;
return 0;
}
/* field-value pairs accessors */
int
mu_url_sget_fvpairs (const mu_url_t url, size_t *fvc, char ***fvp)
{
if (url == NULL)
return EINVAL;
/* FIXME: no _get_fvpairs method, but the method stuff needs to be rewritten
anyway */
*fvc = url->fvcount;
*fvp = url->fvpairs;
return 0;
}
int
mu_url_aget_fvpairs (const mu_url_t url, size_t *pfvc, char ***pfvp)
{
size_t fvc, i;
char **fvp;
char **fvcopy;
int rc = mu_url_sget_fvpairs (url, &fvc, &fvp);
if (rc)
return rc;
fvcopy = calloc (fvc + 1, sizeof (fvcopy[0]));
if (!fvcopy)
return errno;
for (i = 0; i < fvc; i++)
{
if (!(fvcopy[i] = strdup (fvp[i])))
{
mu_argcv_free (i, fvcopy);
return errno;
}
}
fvcopy[i] = NULL;
*pfvc = fvc;
*pfvp = fvcopy;
return 0;
}
int
mu_url_get_port (const mu_url_t url, long *pport)
{
if (url == NULL)
return EINVAL;
if (url->_get_port)
return url->_get_port (url, pport);
*pport = url->port;
return 0;
}
const char *
mu_url_to_string (const mu_url_t url)
{
if (url == NULL || url->name == NULL)
return "";
return url->name;
}
int
mu_url_set_scheme (mu_url_t url, const char *scheme)
{
char *p;
if (!url || !scheme)
return EINVAL;
p = realloc (url->scheme, strlen (scheme) + 1);
if (!p)
return ENOMEM;
strcpy (url->scheme, scheme);
return 0;
}
int
mu_url_is_scheme (mu_url_t url, const char *scheme)
{
if (url && scheme && url->scheme && strcasecmp (url->scheme, scheme) == 0)
return 1;
return 0;
}
int
mu_url_is_same_port (mu_url_t url1, mu_url_t url2)
{
long p1 = 0, p2 = 0;
mu_url_get_port (url1, &p1);
mu_url_get_port (url2, &p2);
return (p1 == p2);
}
/* From RFC 1738, section 2.2 */
char *
mu_url_decode_len (const char *s, size_t len)
{
char *d;
const char *eos = s + len;
int i;
d = malloc (len + 1);
if (!d)
return NULL;
for (i = 0; s < eos; i++)
{
if (*s != '%')
{
d[i] = *s;
s++;
}
else
{
unsigned long ul = 0;
s++;
/* don't check return value, it's correctly coded, or it's not,
in which case we just skip the garbage, this is a decoder,
not an AI project */
mu_hexstr2ul (&ul, s, 2);
s += 2;
d[i] = (char) ul;
}
}
d[i] = 0;
return d;
}
char *
mu_url_decode (const char *s)
{
return mu_url_decode_len (s, strlen (s));
}
static int
defined (const char *s)
{
if (s && strcmp ("*", s) != 0)
return 1;
return 0;
}
int
mu_url_is_ticket (mu_url_t ticket, mu_url_t url)
{
if (!ticket || !url)
return 0;
/* If ticket has a scheme, host, port, or path, then the queries
equivalent must be defined and match. */
if (defined (ticket->scheme))
{
if (!url->scheme || strcasecmp (ticket->scheme, url->scheme) != 0)
return 0;
}
if (defined (ticket->host))
{
if (!url->host || strcasecmp (ticket->host, url->host) != 0)
return 0;
}
if (ticket->port && ticket->port != url->port)
return 0;
/* If ticket has a user or pass, but url doesn't, that's OK, we were
urling for this info. But if url does have a user/pass, it
must match the ticket. */
if (url->user)
{
if (defined (ticket->user) && strcmp (ticket->user, url->user) != 0)
return 0;
}
/* Guess it matches. */
return 1;
}
int
mu_url_init (mu_url_t url, int port, const char *scheme)
{
int status = 0;
url->_destroy = NULL;
status = mu_url_parse (url);
if (status)
return status;
if (!mu_url_is_scheme (url, scheme))
return EINVAL;
if (url->port == 0)
url->port = port;
return status;
}
/* Default mailbox path generator */
static char *
_url_path_default (const char *spooldir, const char *user, int unused)
{
char *mbox = malloc (sizeof(spooldir) + strlen(user) + 2);
if (!mbox)
errno = ENOMEM;
else
sprintf (mbox, "%s/%s", spooldir, user);
return mbox;
}
/* Hashed indexing */
static char *
_url_path_hashed (const char *spooldir, const char *user, int param)
{
int i;
int ulen = strlen (user);
char *mbox;
unsigned hash;
if (param > ulen)
param = ulen;
for (i = 0, hash = 0; i < param; i++)
hash += user[i];
mbox = malloc (ulen + strlen (spooldir) + 5);
sprintf (mbox, "%s/%02X/%s", spooldir, hash % 256, user);
return mbox;
}
static int transtab[] = {
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd',
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
'x', 'y', 'z', 'b', 'c', 'd', 'e', 'f',
'g', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
'x', 'y', 'z', 'b', 'c', 'd', 'e', 'f',
'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e',
'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
'y', 'z', 'b', 'c', 'd', 'e', 'f', 'g',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
'y', 'z', 'b', 'c', 'd', 'e', 'f', 'g'
};
/* Forward Indexing */
static char *
_url_path_index (const char *spooldir, const char *iuser, int index_depth)
{
const unsigned char* user = (const unsigned char*) iuser;
int i, ulen = strlen (iuser);
char *mbox, *p;
if (ulen == 0)
return NULL;
mbox = malloc (ulen + strlen (spooldir) + 2*index_depth + 2);
strcpy (mbox, spooldir);
p = mbox + strlen (mbox);
for (i = 0; i < index_depth && i < ulen; i++)
{
*p++ = '/';
*p++ = transtab[ user[i] ];
}
for (; i < index_depth; i++)
{
*p++ = '/';
*p++ = transtab[ user[ulen-1] ];
}
*p++ = '/';
strcpy (p, iuser);
return mbox;
}
/* Reverse Indexing */
static char *
_url_path_rev_index (const char *spooldir, const char *iuser, int index_depth)
{
const unsigned char* user = (const unsigned char*) iuser;
int i, ulen = strlen (iuser);
char *mbox, *p;
if (ulen == 0)
return NULL;
mbox = malloc (ulen + strlen (spooldir) + 2*index_depth + 1);
strcpy (mbox, spooldir);
p = mbox + strlen (mbox);
for (i = 0; i < index_depth && i < ulen; i++)
{
*p++ = '/';
*p++ = transtab[ user[ulen - i - 1] ];
}
for (; i < index_depth; i++)
{
*p++ = '/';
*p++ = transtab[ user[0] ];
}
*p++ = '/';
strcpy (p, iuser);
return mbox;
}
static int
rmselector (const char *p, void *data MU_ARG_UNUSED)
{
return strncmp (p, "type=", 5) == 0
|| strncmp (p, "user=", 5) == 0
|| strncmp (p, "param=", 6) == 0;
}
int
mu_url_expand_path (mu_url_t url)
{
size_t i;
char *user = NULL;
int param = 0;
char *p;
char *(*fun) (const char *, const char *, int) = _url_path_default;
if (url->fvcount == 0)
return 0;
for (i = 0; i < url->fvcount; i++)
{
p = url->fvpairs[i];
if (strncmp (p, "type=", 5) == 0)
{
char *type = p + 5;
if (strcmp (type, "hash") == 0)
fun = _url_path_hashed;
else if (strcmp (type, "index") == 0)
fun = _url_path_index;
else if (strcmp (type, "rev-index") == 0)
fun = _url_path_rev_index;
else
return MU_ERR_NOENT;
}
else if (strncmp (p, "user=", 5) == 0)
{
user = p + 5;
}
else if (strncmp (p, "param=", 6) == 0)
{
param = strtoul (p + 6, NULL, 0);
}
}
if (user)
{
char *p = fun (url->path, user, param);
if (p)
{
free (url->path);
url->path = p;
}
mu_argcv_remove (&url->fvcount, &url->fvpairs, rmselector, NULL);
}
else
return MU_ERR_NOENT;
return 0;
}