summaryrefslogtreecommitdiffstats
path: root/maildir/maildirpath.c
blob: f02ede5b8dc7960020b92d59627baa00e9387a4f (plain)
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
/*
** Copyright 2000 Double Precision, Inc.
** See COPYING for distribution information.
*/

#if HAVE_CONFIG_H
#include "config.h"
#endif

#include	<sys/types.h>
#include	<sys/stat.h>
#include	<string.h>
#include	<stdlib.h>
#include	<time.h>
#if	HAVE_UNISTD_H
#include	<unistd.h>
#endif
#include	<stdio.h>
#include	<ctype.h>
#include	<errno.h>

#include	"maildirmisc.h"


char *maildir_name2dir(const char *maildir,	/* DIR location */
		       const char *foldername) /* INBOX.name */
{
	const char *inbox=INBOX;
	int l=strlen(inbox);
	char *p;

	if (!maildir)	maildir=".";

	if (foldername && strncasecmp(foldername, INBOX, l) == 0 &&
	    strchr(foldername, '/') == NULL)
	{
		if (foldername[l] == 0)
			return strdup(maildir); /* INBOX: main maildir inbox */

		if (foldername[l] == '.')
		{
			const char *r;

			for (r=foldername; *r; r++)
			{
				if (*r != '.')	continue;

				if (r[1] == 0 || r[1] == '.')
				{
					errno=EINVAL;
					return (0);
				}
			}

			r=strchr(foldername, '.');

			p=malloc(strlen(maildir)+strlen(r) + 2);

			if (!p)
				return NULL;

			return (strcat(strcat(strcpy(p, maildir), "/"),
				       r));
		}
	}

	errno=EINVAL;
	return NULL;
}

char *maildir_location(const char *homedir,
		       const char *maildir)
{
	char *p;

	if (*maildir == '/')
		return strdup(maildir);

	p=malloc(strlen(homedir)+strlen(maildir)+2);

	if (!p)
		return NULL;
	strcat(strcat(strcpy(p, homedir), "/"), maildir);
	return p;
}

char *maildir_folderdir(const char *maildir, const char *foldername)
{
char	*p;
const char *r;
size_t	l;

	if (!maildir)	maildir=".";
	l=strlen(maildir);

	if (foldername == 0 ||
		strcasecmp(foldername, INBOX) == 0)
	{
		if ((p=malloc(l+1)) == 0)	return (0);
		strcpy(p, maildir);
		return(p);
	}

	/* Rules: no leading/trailing periods, no /s */
	if (*foldername == '.' || strchr(foldername, '/'))
	{
		errno=EINVAL;
		return (0);
	}

	for (r=foldername; *r; r++)
	{
		if (*r != '.')	continue;
		if (r[1] == 0 || r[1] == '.')
		{
			errno=EINVAL;
			return (0);
		}
	}

	if ((p=malloc(l+strlen(foldername)+3)) == 0)	return (0);
	*p=0;
	if (strcmp(maildir, "."))
		strcat(strcpy(p, maildir), "/");
	
	return (strcat(strcat(p, "."), foldername));
}