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
|
/* $Id: mboxadd.C,v 1.4 2006/06/04 21:36:02 mrsam Exp $
**
** Copyright 2002-2006, Double Precision Inc.
**
** See COPYING for distribution information.
*/
#include "libmail_config.h"
#include "mboxadd.H"
#include "mboxsighandler.H"
#include "file.H"
#include <errno.h>
#include <pwd.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
using namespace std;
mail::mbox::folder::add::add(mail::mbox &mboxArg, string pathArg,
mail::callback &callbackArg)
: mail::addMessage(&mboxArg), path(pathArg),
callback(callbackArg),
fp(tmpfile()),
mboxAccount(&mboxArg)
{
}
mail::mbox::folder::add::~add()
{
if (fp)
fclose(fp);
}
void mail::mbox::folder::add::saveMessageContents(string msg)
{
if (*(msg.c_str()) == 0)
return;
if (fp)
if (fwrite(&msg[0], msg.size(), 1, fp) != 1)
; // Ignore gcc warning
}
void mail::mbox::folder::add::fail(string msg)
{
callback.fail(msg);
delete this;
}
void mail::mbox::folder::add::success(string msg)
{
callback.success(msg);
delete this;
}
void mail::mbox::folder::add::reportProgress(size_t bytesCompleted,
size_t bytesEstimatedTotal,
size_t messagesCompleted,
size_t messagesEstimatedTotal)
{
callback.reportProgress(bytesCompleted, bytesEstimatedTotal,
messagesCompleted, messagesEstimatedTotal);
}
void mail::mbox::folder::add::go()
{
if (!fp)
fail(strerror(errno));
if (mboxAccount.isDestroyed())
{
fail("Server connection aborted");
return;
}
try {
if (path == mboxAccount->currentFolder)
{
mboxAccount->installTask(new LockCurrentFolder( *this ));
}
else
{
mboxAccount->installTask(new LockCurrentFolder( *this,
path));
}
} catch (...) {
fail("An exception occured while adding message.");
return;
}
}
//
// Folder's now locked.
//
void mail::mbox::folder::add::copyTo(mail::file &file)
{
struct stat st;
mail::mbox::sighandler updating(fileno(static_cast<FILE *>(file)));
try {
// Make sure the mboxAccount file ends with a trailing newline
if (fstat(fileno(static_cast<FILE *>(file)), &st) < 0)
{
fail(strerror(errno));
return;
}
int ch='\n';
if (st.st_size > 0)
{
if (fseek(file, -1, SEEK_END) < 0 ||
(ch=getc(file)) == EOF)
{
fail(strerror(errno));
return;
}
}
if (fseek(file, 0L, SEEK_END) < 0 ||
fseek(fp, 0L, SEEK_SET) < 0)
{
fail(strerror(errno));
return;
}
if (ch != '\n')
putc('\n', file);
mail::file f(fileno(fp), "w+");
messageInfo.uid= mail::mboxMagicTag()
.getMessageInfo().uid;
string hdr=mail::mboxMagicTag(messageInfo.uid,
messageInfo,
mail::keywords::Message()
// KEYWORDS
).toString();
struct passwd *pw;
pw=getpwuid(getuid());
fprintf(file, "From %s %s%s\n",
pw ? pw->pw_name:"nobody",
ctime(&messageDate),
hdr.c_str());
size_t bytesDone=0;
size_t bytesNextReport=0;
while (!feof(f))
{
string l=f.getline();
if (l.size() == 0 && feof(f))
break;
const char *p=l.c_str();
while (*p == '>')
p++;
if (strncmp(p, "From ", 5) == 0)
l=">" + l;
l += "\n";
if (fwrite(&l[0], l.size(), 1, file) != 1)
; // Ignore gcc warning
bytesDone += l.size();
if (bytesDone >= bytesNextReport)
{
bytesNextReport=bytesDone + BUFSIZ;
callback.reportProgress(bytesDone, 0, 0, 1);
}
}
if (fflush(file) < 0 || ferror(file))
{
updating.rollback();
fail(strerror(errno));
return;
}
callback.reportProgress(bytesDone, bytesDone, 1, 1);
updating.block();
} catch (...) {
updating.rollback();
fail("An exception occured while adding message.");
return;
}
try {
fclose(fp);
fp=NULL;
success("OK");
} catch (...) {
delete this;
LIBMAIL_THROW();
}
}
///////////////////////////////////////////////////////////////////////////
mail::mbox::folder::add::LockCurrentFolder
::LockCurrentFolder(add &meArg, string pathArg)
: LockTask(*meArg.mboxAccount, meArg, pathArg), me(meArg)
{
}
mail::mbox::folder::add::LockCurrentFolder::~LockCurrentFolder()
{
}
bool mail::mbox::folder::add::LockCurrentFolder::locked(mail::file
&lockedFile)
{
// If the current folder is opened in read-only mode, this is a no-go.
if (me.path == me.mboxAccount->currentFolder && mboxAccount.currentFolderReadOnly)
{
me.fail("Folder opened in read-only mode.");
done();
return true;
}
me.copyTo(lockedFile);
done();
return true;
}
|