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 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
|
/*
** Copyright 2002-2008, Double Precision Inc.
**
** See COPYING for distribution information.
*/
#include "libmail_config.h"
#include <cstring>
#include "mbox.H"
#include "mboxopen.H"
#include "mboxsighandler.H"
#include "mboxmagictag.H"
#include "file.H"
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <utime.h>
using namespace std;
mail::mbox::OpenTask::OpenTask(mail::mbox &mboxAccount,
string folderArg, mail::callback &callbackArg,
mail::callback::folder *openCallbackArg)
: TimedTask(mboxAccount, callbackArg),
folder(folderArg),
openCallback(openCallbackArg)
{
}
mail::mbox::OpenTask::~OpenTask()
{
}
bool mail::mbox::OpenTask::doit()
{
bool isInbox= folder == "INBOX";
if (folder == "")
{
fail(strerror(ENOENT));
return true;
}
mail::mbox::lock mailbox_lock(isInbox ? mboxAccount.inboxMailboxPath:folder);
mail::mbox::lock inbox_lock(mboxAccount.inboxSpoolPath); // Just in case
struct stat stat_buf;
if (isInbox)
{
// We must have a r/w lock on the mail spool file.
if (!inbox_lock(false))
{
if (errno == EAGAIN)
return false; // Deferral, try again later.
// ENOENT is ok, the spool file does not exist.
if (errno != ENOENT)
{
fail(strerror(errno));
return true;
}
isInbox=false;
// spool file not created yet, new account probably.
}
// Make sure $HOME/mboxAccount exists.
int createMbox=::open(mboxAccount.inboxMailboxPath.c_str(),
O_RDWR|O_CREAT, 0600);
if (createMbox < 0)
{
fail(strerror(errno));
return true;
}
close(createMbox);
}
bool hasExistingFolder= openCallback == NULL;
// Opening an existing folder, in read-only mode. A read-only lock
// will be fine.
bool ro=false;
if (hasExistingFolder && mboxAccount.currentFolderReadOnly)
{
if (!mailbox_lock(true))
{
if (errno != EAGAIN && errno != EEXIST)
{
fail(strerror(errno));
return true;
}
return false;
}
}
// Try for a read/write lock
else if (!mailbox_lock(false))
{
// If not permission denied, we're in trouble.
if (errno != EPERM && errno != EACCES)
{
if (errno != EAGAIN && errno != EEXIST)
{
fail(strerror(errno));
return true;
}
// EAGAIN means try again
return false;
}
if (isInbox) // Must have r/w to inbox
{
if (access(mboxAccount.inboxSpoolPath.c_str(), W_OK))
{
fail("Invalid permissions on system mailbox.");
return true;
}
return false;
}
// Try a read-only lock, then.
if (!mailbox_lock(true))
{
if (errno != EAGAIN)
{
fail(strerror(errno));
return true;
}
return false;
}
ro=true;
}
// Lock-n-load
int mboxfd=mailbox_lock.getfd();
mail::file mboxFp(mboxfd, isInbox ? "r+":"r");
if (!mboxAccount.scan(mboxFp, NULL, openCallback == NULL,
NULL,
false, &callback))
{
fail(errno == EINVAL
? "File does not appear to be a mail folder."
: strerror(errno));
return true;
}
if (isInbox)
{
// Copy new mail.
mail::mbox::sighandler updating(mboxfd); // Critical section
mail::file spoolFp(inbox_lock.getfd(), "r");
if (!mboxAccount.scan(spoolFp, &mboxFp, openCallback == NULL,
NULL,
false, &callback))
{
updating.rollback();
fail(errno == EINVAL
? "File does not appear to be a mail folder."
: strerror(errno));
return true;
}
updating.block();
if (ftruncate(inbox_lock.getfd(), 0) < 0)
; // Ignore gcc warning
struct utimbuf ut;
if (fstat(mboxfd, &stat_buf) < 0)
{
fail(strerror(errno));
return true;
}
ut.modtime=--stat_buf.st_mtime;
ut.actime= ut.modtime;
if (utime(mboxAccount.inboxMailboxPath.c_str(), &ut) < 0)
{
fail(strerror(errno));
return true;
}
}
else if (fstat(mboxfd, &stat_buf) < 0)
{
fail(strerror(errno));
return true;
}
// Save folder particulars, to detect someone else's changes.
mboxAccount.folderSavedTimestamp= stat_buf.st_mtime;
mboxAccount.folderSavedSize=stat_buf.st_size;
const char *okmsg="OK";
if (openCallback) // We're doing mail::folder::open()
{
mboxAccount.currentFolderCallback=openCallback;
mboxAccount.currentFolderReadOnly=ro;
mboxAccount.index.clear();
vector<mboxMessageIndex>::iterator b, e;
b=mboxAccount.folderMessageIndex.begin();
e=mboxAccount.folderMessageIndex.end();
while (b != e)
mboxAccount.index.push_back( (*b++).tag.getMessageInfo());
mboxAccount.currentFolder=folder;
if (ro)
okmsg="WARNING: Folder opened in read-only mode.";
}
opened(okmsg, callback);
return true;
}
void mail::mbox::OpenTask::opened(const char *okmsg, mail::callback &callback)
{
done();
callback.success("Folder opened");
}
//
// Slight variation on the above - explicit new mail check.
//
mail::mbox::CheckNewMailTask::CheckNewMailTask(mail::mbox &mboxAccount,
string folderArg,
mail::callback &callbackArg,
mail::callback::folder
*openCallbackArg)
: OpenTask(mboxAccount, folderArg, callbackArg, openCallbackArg)
{
}
mail::mbox::CheckNewMailTask::~CheckNewMailTask()
{
}
void mail::mbox::CheckNewMailTask::opened(const char *okmsg,
mail::callback &callback)
{
mboxAccount.checkNewMail();
if (mboxAccount.newMessages)
{
mboxAccount.newMessages=false;
if (mboxAccount.currentFolderCallback)
mboxAccount.currentFolderCallback->newMessages();
}
OpenTask::opened(okmsg, callback);
}
|