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

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

#include <sys/types.h>
#if HAVE_DIRENT_H
#include <dirent.h>
#define NAMLEN(dirent) strlen((dirent)->d_name)
#else
#define dirent direct
#define NAMLEN(dirent) (dirent)->d_namlen
#if HAVE_SYS_NDIR_H
#include <sys/ndir.h>
#endif
#if HAVE_SYS_DIR_H
#include <sys/dir.h>
#endif
#if HAVE_NDIR_H
#include <ndir.h>
#endif
#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_filename(const char *maildir, const char *folder,
**   const char *filename)
**	- find a message in a maildir
**
** Return the full path to the indicated message.  If the message flags
** in filename have changed, we search the maildir for this message.
*/

char *maildir_filename(const char *maildir,
	const char *folder, const char *filename)
{
struct stat stat_buf;
char	*p, *q;
DIR *dirp;
struct dirent *de;
char	*dir;

	if (strchr(filename, '/') || *filename == '.')
	{
		errno=ENOENT;
		return (0);
	}

	dir=maildir_folderdir(maildir, folder);

	if (!dir)	return (0);

	p=malloc(strlen(dir)+strlen(filename)+sizeof("/cur/"));

	if (!p)
	{
		free(dir);
		return (0);
	}

	strcat(strcat(strcpy(p, dir), "/cur/"), filename);

	if (stat(p, &stat_buf) == 0)
	{
		free(dir);
		return (p);
	}

	/* Oh, a wise guy... */

	q=strrchr(p, '/');
	*q=0;
	dirp=opendir(p);
	*q='/';

	if ( dirp == NULL)
	{
		free(dir);
		return p;
	}

	/* Compare filenames, ignore filename size if set by maildirquota */

	while ((de=readdir(dirp)) != NULL)
	{
	const char *a=filename;
	const char *b=de->d_name;

		for (;;)
		{
			if ( a[0] == ',' && a[1] == 'S' && a[2] == '=')
			{
				/* File size - quota shortcut - skip */
				a += 3;
				while (*a && isdigit((int)(unsigned char)*a))
					++a;
			}

			if ( b[0] == ',' && b[1] == 'S' && b[2] == '=')
			{
				/* File size - quota shortcut - skip */
				b += 3;
				while (*b && isdigit((int)(unsigned char)*b))
					++b;
			}

			if ( (*a == 0 || *a == MDIRSEP[0]) && (*b == 0 || *b == MDIRSEP[0]))
			{
				free(p);
				p=malloc(strlen(dir)+strlen(de->d_name)+
					sizeof("/cur/"));
				if (!p)
				{
					closedir(dirp);
					free(dir);
					return (0);
				}

				strcat(strcat(strcpy(p, dir), "/cur/"),
					de->d_name);
				closedir(dirp);
				free(dir);
				return (p);
			}
			if ( *a == 0 || *a == MDIRSEP[0] || *b == 0 || *b == MDIRSEP[0] ||
				*a != *b)
				break;

			++a;
			++b;
		}
	}
	closedir(dirp);
	free(dir);
	return (p);
}