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
|
/*
** Copyright 2002-2004, Double Precision Inc.
**
** See COPYING for distribution information.
*/
#include "imap.H"
#include "misc.H"
#include "imapfolder.H"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <iostream>
using namespace std;
LIBMAIL_START
/////////////////////////////////////////////////////////////////////////
//
// The LOGOUT command
class imapLogoutHandler : public imapHandler {
bool eatEverything;
private:
mail::callback &callback;
bool orderlyShutdown;
void dologout(imap &imapAccount, mail::callback &callback);
const char *getName();
void timedOut(const char *errmsg);
void installed(imap &imapAccount);
public:
imapLogoutHandler(mail::callback &myCallback);
~imapLogoutHandler();
int process(imap &imapAccount, string &buffer);
} ;
LIBMAIL_END
//////////////////////////////////////////////////////////////////////////////
//
// Logging out.
void mail::imap::logout(mail::callback &callback)
{
if (!socketConnected())
{
callback.success("Already logged out.");
return;
}
orderlyShutdown=true;
if (currentFolder)
currentFolder->closeInProgress=true;
installForegroundTask(new mail::imapLogoutHandler(callback));
}
int mail::imapLogoutHandler::process(mail::imap &imapAccount, string &buffer)
{
if (eatEverything)
return buffer.size();
size_t p=buffer.find('\n');
if (p == std::string::npos)
{
if (buffer.size() > 16000)
return buffer.size() - 16000;
// SANITY CHECK - DON'T LET HOSTILE SERVER DOS us
return (0);
}
string buffer_cpy=buffer;
buffer_cpy.erase(p);
++p;
string::iterator b=buffer_cpy.begin();
string::iterator e=buffer_cpy.end();
string word=imapAccount.get_cmdreply(&b, &e);
upper(word);
if (word == (imapAccount.smap ? "+OK":"LOGOUT"))
dologout(imapAccount, callback);
return p;
}
const char *mail::imapLogoutHandler::getName()
{
return "LOGOUT";
}
void mail::imapLogoutHandler::timedOut(const char *errmsg)
{
callback.success(errmsg ? errmsg:"Timed out.");
}
void mail::imapLogoutHandler::installed(mail::imap &imapAccount)
{
if (imapAccount.smap)
imapAccount.imapcmd("", "LOGOUT\n");
else
imapAccount.imapcmd("LOGOUT", "LOGOUT\r\n");
}
mail::imapLogoutHandler::imapLogoutHandler(mail::callback &myCallback)
: eatEverything(false),
callback(myCallback), orderlyShutdown(false)
{
}
mail::imapLogoutHandler::~imapLogoutHandler()
{
if (!orderlyShutdown)
callback.success("Logged out.");
}
#include <fstream>
void mail::imapLogoutHandler::dologout(mail::imap &imapAccount,
mail::callback &c)
{
if (!imapAccount.socketEndEncryption())
{
orderlyShutdown=true;
imapAccount.uninstallHandler(this);
c.success("Logged out.");
}
else
{
eatEverything=true;
}
}
|