File: Maildir.cpp

package info (click to toggle)
xmail 1.22-5
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,840 kB
  • ctags: 3,857
  • sloc: cpp: 33,585; sh: 625; makefile: 102; perl: 56; sql: 31
file content (163 lines) | stat: -rw-r--r-- 5,655 bytes parent folder | download | duplicates (2)
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
/*
 *  XMail by Davide Libenzi ( Intranet and Internet mail server )
 *  Copyright (C) 1999,..,2004  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 <davidel@xmailserver.org>
 *
 */

#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"

int MdirCreateStructure(char const *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(char const *pszMaildirPath, char *pszFilePath)
{

	char szTmpPath[SYS_MAX_PATH] = "";

	sprintf(szTmpPath, "%s" SYS_SLASH_STR "tmp", pszMaildirPath);

	return (MscUniqueFile(szTmpPath, pszFilePath));

}

int MdirMoveTmpEntryInNew(char const *pszTmpEntryPath)
{
///////////////////////////////////////////////////////////////////////////////
//  Lookup Maildir/tmp/ subpath
///////////////////////////////////////////////////////////////////////////////
	char const *pszTmpDir = MAILDIR_DIRECTORY SYS_SLASH_STR "tmp" SYS_SLASH_STR;
	char const *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);
	char const *pszNewDir = MAILDIR_DIRECTORY SYS_SLASH_STR "new" SYS_SLASH_STR;
	char const *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);

}

int MdirMoveMessage(char const *pszMaildirPath, const char *pszFileName, char const *pszMessageID)
{
///////////////////////////////////////////////////////////////////////////////
//  Allocate a Maildir/tmp entry
///////////////////////////////////////////////////////////////////////////////
	char szTmpEntryPath[SYS_MAX_PATH] = "";

	if (pszMessageID == NULL) {
		if (MdirGetTmpMaildirEntry(pszMaildirPath, szTmpEntryPath) < 0)
			return (ErrGetErrorCode());
	} else
		sprintf(szTmpEntryPath, "%s" SYS_SLASH_STR "tmp" SYS_SLASH_STR "%s",
			pszMaildirPath, pszMessageID);

///////////////////////////////////////////////////////////////////////////////
//  This perform a copy&delete to Maildir/tmp
///////////////////////////////////////////////////////////////////////////////
	if (MscMoveFile(pszFileName, szTmpEntryPath) < 0) {
		ErrorPush();
		CheckRemoveFile(szTmpEntryPath);
		return (ErrorPop());
	}
///////////////////////////////////////////////////////////////////////////////
//  This perform a fast system move from Maildir/tmp to Maildir/new
///////////////////////////////////////////////////////////////////////////////
	if (MdirMoveTmpEntryInNew(szTmpEntryPath) < 0) {
		ErrorPush();
		SysRemove(szTmpEntryPath);
		return (ErrorPop());
	}

	return (0);

}