-
Notifications
You must be signed in to change notification settings - Fork 0
/
Maildir.cpp
159 lines (132 loc) · 4.57 KB
/
Maildir.cpp
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
/*
* XMail by Davide Libenzi (Intranet and Internet mail server)
* Copyright (C) 1999,..,2010 Davide Libenzi
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Davide Libenzi <[email protected]>
*
*/
#include "SysInclude.h"
#include "SysDep.h"
#include "SvrDefines.h"
#include "ShBlocks.h"
#include "StrUtils.h"
#include "SList.h"
#include "MD5.h"
#include "Base64Enc.h"
#include "BuffSock.h"
#include "MailConfig.h"
#include "UsrUtils.h"
#include "SvrUtils.h"
#include "MessQueue.h"
#include "MailSvr.h"
#include "MiscUtils.h"
#include "Maildir.h"
static int MdirMessageID(char *pszMessageID, size_t sMaxMessageID)
{
/*
* Get thread ID and host name. We do not use atomic inc on ulUniqSeq, since
* collision is prevented by the thread ID
*/
static unsigned long ulUniqSeq = 0;
unsigned long ulThreadID = SysGetCurrentThreadId();
SYS_INT64 iMsTime = SysMsTime();
char szHostName[MAX_HOST_NAME] = "";
gethostname(szHostName, sizeof(szHostName) - 1);
SysSNPrintf(pszMessageID, sMaxMessageID,
SYS_LLU_FMT ".%lu.%lx.%s",
iMsTime, ulThreadID, ulUniqSeq++, szHostName);
return 0;
}
int MdirCreateStructure(const char *pszBasePath)
{
/* Create Maildir directory */
char szMaildirPath[SYS_MAX_PATH] = "";
StrSNCpy(szMaildirPath, pszBasePath);
AppendSlash(szMaildirPath);
StrSNCat(szMaildirPath, MAILDIR_DIRECTORY);
if (SysMakeDir(szMaildirPath) < 0)
return ErrGetErrorCode();
/* Create Maildir/tmp directory */
char szSubPath[SYS_MAX_PATH] = "";
sprintf(szSubPath, "%s" SYS_SLASH_STR "tmp", szMaildirPath);
if (SysMakeDir(szSubPath) < 0)
return ErrGetErrorCode();
/* Create Maildir/new directory */
sprintf(szSubPath, "%s" SYS_SLASH_STR "new", szMaildirPath);
if (SysMakeDir(szSubPath) < 0)
return ErrGetErrorCode();
/* Create Maildir/cur directory */
sprintf(szSubPath, "%s" SYS_SLASH_STR "cur", szMaildirPath);
if (SysMakeDir(szSubPath) < 0)
return ErrGetErrorCode();
return 0;
}
int MdirGetTmpMaildirEntry(const char *pszMaildirPath, char *pszFilePath,
size_t sMaxPath)
{
char szMessageID[SYS_MAX_PATH];
if (MdirMessageID(szMessageID, sizeof(szMessageID)) < 0)
return ErrGetErrorCode();
SysSNPrintf(pszFilePath, sMaxPath,
"%s" SYS_SLASH_STR "tmp" SYS_SLASH_STR "%s",
pszMaildirPath, szMessageID);
return 0;
}
int MdirMoveTmpEntryInNew(const char *pszTmpEntryPath)
{
/* Lookup Maildir/tmp/ subpath */
const char *pszTmpDir = MAILDIR_DIRECTORY SYS_SLASH_STR "tmp" SYS_SLASH_STR;
const char *pszLookup = strstr(pszTmpEntryPath, pszTmpDir);
if (pszLookup == NULL) {
ErrSetErrorCode(ERR_INVALID_MAILDIR_SUBPATH);
return ERR_INVALID_MAILDIR_SUBPATH;
}
/* Build Maildir/new file path */
int iBaseLength = (int) (pszLookup - pszTmpEntryPath);
const char *pszNewDir = MAILDIR_DIRECTORY SYS_SLASH_STR "new" SYS_SLASH_STR;
const char *pszSlash = strrchr(pszTmpEntryPath, SYS_SLASH_CHAR);
char szNewEntryPath[SYS_MAX_PATH] = "";
StrSNCpy(szNewEntryPath, pszTmpEntryPath);
StrNCpy(szNewEntryPath + iBaseLength, pszNewDir, sizeof(szNewEntryPath) - iBaseLength);
StrNCat(szNewEntryPath, pszSlash + 1, sizeof(szNewEntryPath));
/* Move to Maildir/new */
if (SysMoveFile(pszTmpEntryPath, szNewEntryPath) < 0)
return ErrGetErrorCode();
return 0;
}
/*
* This function must be called with file names located onto the same
* mount/drive: of the destination Maildir. This is accomplished by
* the function UsrGetTmpFile().
*/
int MdirMoveMessage(const char *pszMaildirPath, const char *pszFileName,
const char *pszMessageID)
{
char szMessageID[SYS_MAX_PATH];
char szNewEntryPath[SYS_MAX_PATH];
if (pszMessageID == NULL) {
if (MdirMessageID(szMessageID, sizeof(szMessageID)) < 0)
return ErrGetErrorCode();
pszMessageID = szMessageID;
}
SysSNPrintf(szNewEntryPath, sizeof(szNewEntryPath),
"%s" SYS_SLASH_STR "new" SYS_SLASH_STR "%s",
pszMaildirPath, pszMessageID);
if (SysMoveFile(pszFileName, szNewEntryPath) < 0)
return ErrGetErrorCode();
return 0;
}