You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
2095 lines
52 KiB
2095 lines
52 KiB
15 years ago
|
/**********************************************************************
|
||
|
*
|
||
4 years ago
|
* imapparser.cpp - IMAP4rev1 Parser
|
||
15 years ago
|
* Copyright (C) 2001-2002 Michael Haeckel <haeckel@kde.org>
|
||
|
* Copyright (C) 2000 s.carstens@gmx.de
|
||
|
*
|
||
|
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
|
*
|
||
|
* Send comments and bug fixes to s.carstens@gmx.de
|
||
|
*
|
||
|
*********************************************************************/
|
||
|
|
||
|
#ifdef HAVE_CONFIG_H
|
||
|
#include <config.h>
|
||
|
#endif
|
||
|
|
||
|
#include "rfcdecoder.h"
|
||
|
|
||
|
#include "imapparser.h"
|
||
|
|
||
|
#include "imapinfo.h"
|
||
|
|
||
|
#include "mailheader.h"
|
||
|
#include "mimeheader.h"
|
||
|
#include "mailaddress.h"
|
||
|
|
||
|
#include <sys/types.h>
|
||
|
|
||
|
#include <stdlib.h>
|
||
|
#include <unistd.h>
|
||
|
|
||
|
#ifdef HAVE_LIBSASL2
|
||
|
extern "C" {
|
||
|
#include <sasl/sasl.h>
|
||
|
}
|
||
|
#endif
|
||
|
|
||
14 years ago
|
#include <tqregexp.h>
|
||
|
#include <tqbuffer.h>
|
||
|
#include <tqstring.h>
|
||
|
#include <tqstringlist.h>
|
||
15 years ago
|
|
||
|
#include <kdebug.h>
|
||
|
#include <kmdcodec.h>
|
||
|
#include <kurl.h>
|
||
|
|
||
|
#include <kasciistricmp.h>
|
||
|
#include <kasciistringtools.h>
|
||
|
|
||
|
#ifdef HAVE_LIBSASL2
|
||
|
static sasl_callback_t callbacks[] = {
|
||
|
{ SASL_CB_ECHOPROMPT, NULL, NULL },
|
||
|
{ SASL_CB_NOECHOPROMPT, NULL, NULL },
|
||
|
{ SASL_CB_GETREALM, NULL, NULL },
|
||
|
{ SASL_CB_USER, NULL, NULL },
|
||
|
{ SASL_CB_AUTHNAME, NULL, NULL },
|
||
|
{ SASL_CB_PASS, NULL, NULL },
|
||
|
{ SASL_CB_CANON_USER, NULL, NULL },
|
||
|
{ SASL_CB_LIST_END, NULL, NULL }
|
||
|
};
|
||
|
#endif
|
||
|
|
||
|
imapParser::imapParser ()
|
||
|
{
|
||
|
sentQueue.setAutoDelete (false);
|
||
|
completeQueue.setAutoDelete (true);
|
||
|
currentState = ISTATE_NO;
|
||
|
commandCounter = 0;
|
||
|
lastHandled = 0;
|
||
|
}
|
||
|
|
||
|
imapParser::~imapParser ()
|
||
|
{
|
||
|
delete lastHandled;
|
||
|
lastHandled = 0;
|
||
|
}
|
||
|
|
||
|
imapCommand *
|
||
|
imapParser::doCommand (imapCommand * aCmd)
|
||
|
{
|
||
|
int pl = 0;
|
||
|
sendCommand (aCmd);
|
||
|
while (pl != -1 && !aCmd->isComplete ()) {
|
||
|
while ((pl = parseLoop ()) == 0)
|
||
|
;
|
||
|
}
|
||
|
|
||
|
return aCmd;
|
||
|
}
|
||
|
|
||
|
imapCommand *
|
||
|
imapParser::sendCommand (imapCommand * aCmd)
|
||
|
{
|
||
14 years ago
|
aCmd->setId (TQString::number(commandCounter++));
|
||
15 years ago
|
sentQueue.append (aCmd);
|
||
|
|
||
|
continuation.resize(0);
|
||
14 years ago
|
const TQString& command = aCmd->command();
|
||
15 years ago
|
|
||
|
if (command == "SELECT" || command == "EXAMINE")
|
||
|
{
|
||
|
// we need to know which box we are selecting
|
||
|
parseString p;
|
||
|
p.fromString(aCmd->parameter());
|
||
|
currentBox = parseOneWordC(p);
|
||
|
kdDebug(7116) << "imapParser::sendCommand - setting current box to " << currentBox << endl;
|
||
|
}
|
||
|
else if (command == "CLOSE")
|
||
|
{
|
||
|
// we no longer have a box open
|
||
14 years ago
|
currentBox = TQString();
|
||
15 years ago
|
}
|
||
13 years ago
|
else if (command.find ("SEARCH") != -1
|
||
15 years ago
|
|| command == "GETACL"
|
||
|
|| command == "LISTRIGHTS"
|
||
|
|| command == "MYRIGHTS"
|
||
|
|| command == "GETANNOTATION"
|
||
|
|| command == "NAMESPACE"
|
||
13 years ago
|
|| command == "GETQUOTAROOT"
|
||
|
|| command == "GETQUOTA"
|
||
15 years ago
|
|| command == "X-GET-OTHER-USERS"
|
||
|
|| command == "X-GET-DELEGATES"
|
||
|
|| command == "X-GET-OUT-OF-OFFICE")
|
||
|
{
|
||
|
lastResults.clear ();
|
||
|
}
|
||
|
else if (command == "LIST"
|
||
|
|| command == "LSUB")
|
||
|
{
|
||
|
listResponses.clear ();
|
||
|
}
|
||
|
parseWriteLine (aCmd->getStr ());
|
||
|
return aCmd;
|
||
|
}
|
||
|
|
||
|
bool
|
||
14 years ago
|
imapParser::clientLogin (const TQString & aUser, const TQString & aPass,
|
||
|
TQString & resultInfo)
|
||
15 years ago
|
{
|
||
|
imapCommand *cmd;
|
||
|
bool retVal = false;
|
||
|
|
||
|
cmd =
|
||
|
doCommand (new
|
||
|
imapCommand ("LOGIN", "\"" + rfcDecoder::quoteIMAP(aUser)
|
||
|
+ "\" \"" + rfcDecoder::quoteIMAP(aPass) + "\""));
|
||
|
|
||
|
if (cmd->result () == "OK")
|
||
|
{
|
||
|
currentState = ISTATE_LOGIN;
|
||
|
retVal = true;
|
||
|
}
|
||
|
resultInfo = cmd->resultInfo();
|
||
|
completeQueue.removeRef (cmd);
|
||
|
|
||
|
return retVal;
|
||
|
}
|
||
|
|
||
|
#ifdef HAVE_LIBSASL2
|
||
12 years ago
|
static bool sasl_interact( TDEIO::SlaveBase *slave, TDEIO::AuthInfo &ai, void *in )
|
||
15 years ago
|
{
|
||
|
kdDebug(7116) << "sasl_interact" << endl;
|
||
|
sasl_interact_t *interact = ( sasl_interact_t * ) in;
|
||
|
|
||
|
//some mechanisms do not require username && pass, so it doesn't need a popup
|
||
|
//window for getting this info
|
||
|
for ( ; interact->id != SASL_CB_LIST_END; interact++ ) {
|
||
|
if ( interact->id == SASL_CB_AUTHNAME ||
|
||
|
interact->id == SASL_CB_PASS ) {
|
||
|
|
||
|
if ( ai.username.isEmpty() || ai.password.isEmpty() ) {
|
||
|
if (!slave->openPassDlg(ai))
|
||
|
return false;
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
interact = ( sasl_interact_t * ) in;
|
||
|
while( interact->id != SASL_CB_LIST_END ) {
|
||
|
kdDebug(7116) << "SASL_INTERACT id: " << interact->id << endl;
|
||
|
switch( interact->id ) {
|
||
|
case SASL_CB_USER:
|
||
|
case SASL_CB_AUTHNAME:
|
||
|
kdDebug(7116) << "SASL_CB_[USER|AUTHNAME]: '" << ai.username << "'" << endl;
|
||
|
interact->result = strdup( ai.username.utf8() );
|
||
|
interact->len = strlen( (const char *) interact->result );
|
||
|
break;
|
||
|
case SASL_CB_PASS:
|
||
|
kdDebug(7116) << "SASL_CB_PASS: [hidden] " << endl;
|
||
|
interact->result = strdup( ai.password.utf8() );
|
||
|
interact->len = strlen( (const char *) interact->result );
|
||
|
break;
|
||
|
default:
|
||
|
interact->result = 0;
|
||
|
interact->len = 0;
|
||
|
break;
|
||
|
}
|
||
|
interact++;
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
#endif
|
||
|
|
||
|
bool
|
||
12 years ago
|
imapParser::clientAuthenticate ( TDEIO::SlaveBase *slave, TDEIO::AuthInfo &ai,
|
||
14 years ago
|
const TQString & aFTQDN, const TQString & aAuth, bool isSSL, TQString & resultInfo)
|
||
15 years ago
|
{
|
||
|
bool retVal = false;
|
||
|
#ifdef HAVE_LIBSASL2
|
||
|
int result;
|
||
|
sasl_conn_t *conn = 0;
|
||
|
sasl_interact_t *client_interact = 0;
|
||
|
const char *out = 0;
|
||
|
uint outlen = 0;
|
||
|
const char *mechusing = 0;
|
||
14 years ago
|
TQByteArray tmp, challenge;
|
||
15 years ago
|
|
||
14 years ago
|
kdDebug(7116) << "aAuth: " << aAuth << " FTQDN: " << aFTQDN << " isSSL: " << isSSL << endl;
|
||
15 years ago
|
|
||
|
// see if server supports this authenticator
|
||
|
if (!hasCapability ("AUTH=" + aAuth))
|
||
|
return false;
|
||
|
|
||
|
// result = sasl_client_new( isSSL ? "imaps" : "imap",
|
||
|
result = sasl_client_new( "imap", /* FIXME: with cyrus-imapd, even imaps' digest-uri
|
||
|
must be 'imap'. I don't know if it's good or bad. */
|
||
14 years ago
|
aFTQDN.latin1(),
|
||
15 years ago
|
0, 0, callbacks, 0, &conn );
|
||
|
|
||
|
if ( result != SASL_OK ) {
|
||
|
kdDebug(7116) << "sasl_client_new failed with: " << result << endl;
|
||
14 years ago
|
resultInfo = TQString::fromUtf8( sasl_errdetail( conn ) );
|
||
15 years ago
|
return false;
|
||
|
}
|
||
|
|
||
|
do {
|
||
|
result = sasl_client_start(conn, aAuth.latin1(), &client_interact,
|
||
|
hasCapability("SASL-IR") ? &out : 0, &outlen, &mechusing);
|
||
|
|
||
|
if ( result == SASL_INTERACT ) {
|
||
|
if ( !sasl_interact( slave, ai, client_interact ) ) {
|
||
|
sasl_dispose( &conn );
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
} while ( result == SASL_INTERACT );
|
||
|
|
||
|
if ( result != SASL_CONTINUE && result != SASL_OK ) {
|
||
|
kdDebug(7116) << "sasl_client_start failed with: " << result << endl;
|
||
14 years ago
|
resultInfo = TQString::fromUtf8( sasl_errdetail( conn ) );
|
||
15 years ago
|
sasl_dispose( &conn );
|
||
|
return false;
|
||
|
}
|
||
|
imapCommand *cmd;
|
||
|
|
||
|
tmp.setRawData( out, outlen );
|
||
|
KCodecs::base64Encode( tmp, challenge );
|
||
|
tmp.resetRawData( out, outlen );
|
||
|
// then lets try it
|
||
14 years ago
|
TQString firstCommand = aAuth;
|
||
15 years ago
|
if ( !challenge.isEmpty() ) {
|
||
|
firstCommand += " ";
|
||
13 years ago
|
firstCommand += TQString::fromLatin1( challenge.data(), challenge.size() );
|
||
15 years ago
|
}
|
||
|
cmd = sendCommand (new imapCommand ("AUTHENTICATE", firstCommand.latin1()));
|
||
|
|
||
13 years ago
|
int pl = 0;
|
||
|
while ( pl != -1 && !cmd->isComplete () )
|
||
15 years ago
|
{
|
||
|
//read the next line
|
||
13 years ago
|
while ((pl = parseLoop()) == 0) ;
|
||
15 years ago
|
|
||
|
if (!continuation.isEmpty())
|
||
|
{
|
||
14 years ago
|
// kdDebug(7116) << "S: " << TQCString(continuation.data(),continuation.size()+1) << endl;
|
||
15 years ago
|
if ( continuation.size() > 4 ) {
|
||
|
tmp.setRawData( continuation.data() + 2, continuation.size() - 4 );
|
||
|
KCodecs::base64Decode( tmp, challenge );
|
||
14 years ago
|
// kdDebug(7116) << "S-1: " << TQCString(challenge.data(),challenge.size()+1) << endl;
|
||
15 years ago
|
tmp.resetRawData( continuation.data() + 2, continuation.size() - 4 );
|
||
|
}
|
||
|
|
||
|
do {
|
||
|
result = sasl_client_step(conn, challenge.isEmpty() ? 0 : challenge.data(),
|
||
|
challenge.size(),
|
||
|
&client_interact,
|
||
|
&out, &outlen);
|
||
|
|
||
|
if (result == SASL_INTERACT) {
|
||
|
if ( !sasl_interact( slave, ai, client_interact ) ) {
|
||
|
sasl_dispose( &conn );
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
} while ( result == SASL_INTERACT );
|
||
|
|
||
|
if ( result != SASL_CONTINUE && result != SASL_OK ) {
|
||
|
kdDebug(7116) << "sasl_client_step failed with: " << result << endl;
|
||
14 years ago
|
resultInfo = TQString::fromUtf8( sasl_errdetail( conn ) );
|
||
15 years ago
|
sasl_dispose( &conn );
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
tmp.setRawData( out, outlen );
|
||
14 years ago
|
// kdDebug(7116) << "C-1: " << TQCString(tmp.data(),tmp.size()+1) << endl;
|
||
15 years ago
|
KCodecs::base64Encode( tmp, challenge );
|
||
|
tmp.resetRawData( out, outlen );
|
||
14 years ago
|
// kdDebug(7116) << "C: " << TQCString(challenge.data(),challenge.size()+1) << endl;
|
||
15 years ago
|
parseWriteLine (challenge);
|
||
|
continuation.resize(0);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (cmd->result () == "OK")
|
||
|
{
|
||
|
currentState = ISTATE_LOGIN;
|
||
|
retVal = true;
|
||
|
}
|
||
|
resultInfo = cmd->resultInfo();
|
||
|
completeQueue.removeRef (cmd);
|
||
|
|
||
|
sasl_dispose( &conn ); //we don't use sasl_en/decode(), so it's safe to dispose the connection.
|
||
|
#endif //HAVE_LIBSASL2
|
||
|
return retVal;
|
||
|
}
|
||
|
|
||
|
void
|
||
|
imapParser::parseUntagged (parseString & result)
|
||
|
{
|
||
|
//kdDebug(7116) << "imapParser::parseUntagged - '" << result.cstr() << "'" << endl;
|
||
|
|
||
|
parseOneWordC(result); // *
|
||
14 years ago
|
TQByteArray what = parseLiteral (result); // see whats coming next
|
||
15 years ago
|
|
||
12 years ago
|
if(!what.isEmpty ()) {
|
||
15 years ago
|
switch (what[0])
|
||
|
{
|
||
|
//the status responses
|
||
|
case 'B': // BAD or BYE
|
||
14 years ago
|
if (tqstrncmp(what, "BAD", what.size()) == 0)
|
||
15 years ago
|
{
|
||
|
parseResult (what, result);
|
||
|
}
|
||
14 years ago
|
else if (tqstrncmp(what, "BYE", what.size()) == 0)
|
||
15 years ago
|
{
|
||
|
parseResult (what, result);
|
||
|
if ( sentQueue.count() ) {
|
||
|
// BYE that interrupts a command -> copy the reason for it
|
||
|
imapCommand *current = sentQueue.at (0);
|
||
|
current->setResultInfo(result.cstr());
|
||
|
}
|
||
|
currentState = ISTATE_NO;
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case 'N': // NO
|
||
|
if (what[1] == 'O' && what.size() == 2)
|
||
|
{
|
||
|
parseResult (what, result);
|
||
|
}
|
||
14 years ago
|
else if (tqstrncmp(what, "NAMESPACE", what.size()) == 0)
|
||
15 years ago
|
{
|
||
|
parseNamespace (result);
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case 'O': // OK
|
||
|
if (what[1] == 'K' && what.size() == 2)
|
||
|
{
|
||
|
parseResult (what, result);
|
||
14 years ago
|
} else if (tqstrncmp(what, "OTHER-USER", 10) == 0) { // X-GET-OTHER-USER
|
||
15 years ago
|
parseOtherUser (result);
|
||
14 years ago
|
} else if (tqstrncmp(what, "OUT-OF-OFFICE", 13) == 0) { // X-GET-OUT-OF-OFFICE
|
||
15 years ago
|
parseOutOfOffice (result);
|
||
|
}
|
||
|
break;
|
||
|
case 'D':
|
||
14 years ago
|
if (tqstrncmp(what, "DELEGATE", 8) == 0) { // X-GET-DELEGATES
|
||
15 years ago
|
parseDelegate (result);
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case 'P': // PREAUTH
|
||
14 years ago
|
if (tqstrncmp(what, "PREAUTH", what.size()) == 0)
|
||
15 years ago
|
{
|
||
|
parseResult (what, result);
|
||
|
currentState = ISTATE_LOGIN;
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
// parse the other responses
|
||
|
case 'C': // CAPABILITY
|
||
14 years ago
|
if (tqstrncmp(what, "CAPABILITY", what.size()) == 0)
|
||
15 years ago
|
{
|
||
|
parseCapability (result);
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case 'F': // FLAGS
|
||
14 years ago
|
if (tqstrncmp(what, "FLAGS", what.size()) == 0)
|
||
15 years ago
|
{
|
||
|
parseFlags (result);
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case 'L': // LIST or LSUB or LISTRIGHTS
|
||
14 years ago
|
if (tqstrncmp(what, "LIST", what.size()) == 0)
|
||
15 years ago
|
{
|
||
|
parseList (result);
|
||
|
}
|
||
14 years ago
|
else if (tqstrncmp(what, "LSUB", what.size()) == 0)
|
||
15 years ago
|
{
|
||
|
parseLsub (result);
|
||
|
}
|
||
14 years ago
|
else if (tqstrncmp(what, "LISTRIGHTS", what.size()) == 0)
|
||
15 years ago
|
{
|
||
|
parseListRights (result);
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case 'M': // MYRIGHTS
|
||
14 years ago
|
if (tqstrncmp(what, "MYRIGHTS", what.size()) == 0)
|
||
15 years ago
|
{
|
||
|
parseMyRights (result);
|
||
|
}
|
||
|
break;
|
||
|
case 'S': // SEARCH or STATUS
|
||
14 years ago
|
if (tqstrncmp(what, "SEARCH", what.size()) == 0)
|
||
15 years ago
|
{
|
||
|
parseSearch (result);
|
||
|
}
|
||
14 years ago
|
else if (tqstrncmp(what, "STATUS", what.size()) == 0)
|
||
15 years ago
|
{
|
||
13 years ago
|
parsetStatus (result);
|
||
15 years ago
|
}
|
||
|
break;
|
||
|
|
||
|
case 'A': // ACL or ANNOTATION
|
||
14 years ago
|
if (tqstrncmp(what, "ACL", what.size()) == 0)
|
||
15 years ago
|
{
|
||
|
parseAcl (result);
|
||
|
}
|
||
14 years ago
|
else if (tqstrncmp(what, "ANNOTATION", what.size()) == 0)
|
||
15 years ago
|
{
|
||
|
parseAnnotation (result);
|
||
|
}
|
||
|
break;
|
||
13 years ago
|
case 'Q': // QUOTA or QUOTAROOT
|
||
13 years ago
|
if ( what.size() > 5 && tqstrncmp(what, "QUOTAROOT", what.size()) == 0)
|
||
15 years ago
|
{
|
||
|
parseQuotaRoot( result );
|
||
|
}
|
||
13 years ago
|
else if (tqstrncmp(what, "QUOTA", what.size()) == 0)
|
||
15 years ago
|
{
|
||
|
parseQuota( result );
|
||
|
}
|
||
|
break;
|
||
|
case 'X': // Custom command
|
||
|
{
|
||
|
parseCustom( result );
|
||
|
}
|
||
|
break;
|
||
|
default:
|
||
|
//better be a number
|
||
|
{
|
||
|
ulong number;
|
||
|
bool valid;
|
||
|
|
||
14 years ago
|
number = TQCString(what, what.size() + 1).toUInt(&valid);
|
||
15 years ago
|
if (valid)
|
||
|
{
|
||
|
what = parseLiteral (result);
|
||
12 years ago
|
if(!what.isEmpty ()) {
|
||
15 years ago
|
switch (what[0])
|
||
|
{
|
||
|
case 'E':
|
||
14 years ago
|
if (tqstrncmp(what, "EXISTS", what.size()) == 0)
|
||
15 years ago
|
{
|
||
|
parseExists (number, result);
|
||
|
}
|
||
14 years ago
|
else if (tqstrncmp(what, "EXPUNGE", what.size()) == 0)
|
||
15 years ago
|
{
|
||
|
parseExpunge (number, result);
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case 'F':
|
||
14 years ago
|
if (tqstrncmp(what, "FETCH", what.size()) == 0)
|
||
15 years ago
|
{
|
||
14 years ago
|
seenUid = TQString();
|
||
15 years ago
|
parseFetch (number, result);
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case 'S':
|
||
14 years ago
|
if (tqstrncmp(what, "STORE", what.size()) == 0) // deprecated store
|
||
15 years ago
|
{
|
||
14 years ago
|
seenUid = TQString();
|
||
15 years ago
|
parseFetch (number, result);
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case 'R':
|
||
14 years ago
|
if (tqstrncmp(what, "RECENT", what.size()) == 0)
|
||
15 years ago
|
{
|
||
|
parseRecent (number, result);
|
||
|
}
|
||
|
break;
|
||
|
default:
|
||
|
break;
|
||
|
}
|
||
12 years ago
|
}
|
||
15 years ago
|
}
|
||
|
}
|
||
|
break;
|
||
|
} //switch
|
||
12 years ago
|
}
|
||
15 years ago
|
} //func
|
||
|
|
||
|
|
||
|
void
|
||
14 years ago
|
imapParser::parseResult (TQByteArray & result, parseString & rest,
|
||
|
const TQString & command)
|
||
15 years ago
|
{
|
||
|
if (command == "SELECT")
|
||
|
selectInfo.setReadWrite(true);
|
||
|
|
||
|
if (rest[0] == '[')
|
||
|
{
|
||
|
rest.pos++;
|
||
14 years ago
|
TQCString option = parseOneWordC(rest, TRUE);
|
||
15 years ago
|
|
||
|
switch (option[0])
|
||
|
{
|
||
|
case 'A': // ALERT
|
||
|
if (option == "ALERT")
|
||
|
{
|
||
13 years ago
|
rest.pos = rest.data.find(']', rest.pos) + 1;
|
||
15 years ago
|
// The alert text is after [ALERT].
|
||
|
// Is this correct or do we need to care about litterals?
|
||
|
selectInfo.setAlert( rest.cstr() );
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case 'N': // NEWNAME
|
||
|
if (option == "NEWNAME")
|
||
|
{
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case 'P': //PARSE or PERMANENTFLAGS
|
||
|
if (option == "PARSE")
|
||
|
{
|
||
|
}
|
||
|
else if (option == "PERMANENTFLAGS")
|
||
|
{
|
||
13 years ago
|
uint end = rest.data.find(']', rest.pos);
|
||
14 years ago
|
TQCString flags(rest.data.data() + rest.pos, end - rest.pos);
|
||
15 years ago
|
selectInfo.setPermanentFlags (flags);
|
||
|
rest.pos = end;
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case 'R': //READ-ONLY or READ-WRITE
|
||
|
if (option == "READ-ONLY")
|
||
|
{
|
||
|
selectInfo.setReadWrite (false);
|
||
|
}
|
||
|
else if (option == "READ-WRITE")
|
||
|
{
|
||
|
selectInfo.setReadWrite (true);
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case 'T': //TRYCREATE
|
||
|
if (option == "TRYCREATE")
|
||
|
{
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case 'U': //UIDVALIDITY or UNSEEN
|
||
|
if (option == "UIDVALIDITY")
|
||
|
{
|
||
|
ulong value;
|
||
|
if (parseOneNumber (rest, value))
|
||
|
selectInfo.setUidValidity (value);
|
||
|
}
|
||
|
else if (option == "UNSEEN")
|
||
|
{
|
||
|
ulong value;
|
||
|
if (parseOneNumber (rest, value))
|
||
|
selectInfo.setUnseen (value);
|
||
|
}
|
||
|
else if (option == "UIDNEXT")
|
||
|
{
|
||
|
ulong value;
|
||
|
if (parseOneNumber (rest, value))
|
||
|
selectInfo.setUidNext (value);
|
||
|
}
|
||
|
else
|
||
|
break;
|
||
|
|
||
|
}
|
||
|
if (rest[0] == ']')
|
||
|
rest.pos++; //tie off ]
|
||
|
skipWS (rest);
|
||
|
}
|
||
|
|
||
|
if (command.isEmpty())
|
||
|
{
|
||
|
// This happens when parsing an intermediate result line (those that start with '*').
|
||
|
// No state change involved, so we can stop here.
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
switch (command[0].latin1 ())
|
||
|
{
|
||
|
case 'A':
|
||
|
if (command == "AUTHENTICATE")
|
||
14 years ago
|
if (tqstrncmp(result, "OK", result.size()) == 0)
|
||
15 years ago
|
currentState = ISTATE_LOGIN;
|
||
|
break;
|
||
|
|
||
|
case 'L':
|
||
|
if (command == "LOGIN")
|
||
14 years ago
|
if (tqstrncmp(result, "OK", result.size()) == 0)
|
||
15 years ago
|
currentState = ISTATE_LOGIN;
|
||
|
break;
|
||
|
|
||
|
case 'E':
|
||
|
if (command == "EXAMINE")
|
||
|
{
|
||
14 years ago
|
if (tqstrncmp(result, "OK", result.size()) == 0)
|
||
15 years ago
|
currentState = ISTATE_SELECT;
|
||
|
else
|
||
|
{
|
||
|
if (currentState == ISTATE_SELECT)
|
||
|
currentState = ISTATE_LOGIN;
|
||
14 years ago
|
currentBox = TQString();
|
||
15 years ago
|
}
|
||
|
kdDebug(7116) << "imapParser::parseResult - current box is now " << currentBox << endl;
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case 'S':
|
||
|
if (command == "SELECT")
|
||
|
{
|
||
14 years ago
|
if (tqstrncmp(result, "OK", result.size()) == 0)
|
||
15 years ago
|
currentState = ISTATE_SELECT;
|
||
|
else
|
||
|
{
|
||
|
if (currentState == ISTATE_SELECT)
|
||
|
currentState = ISTATE_LOGIN;
|
||
14 years ago
|
currentBox = TQString();
|
||
15 years ago
|
}
|
||
|
kdDebug(7116) << "imapParser::parseResult - current box is now " << currentBox << endl;
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
default:
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
void imapParser::parseCapability (parseString & result)
|
||
|
{
|
||
14 years ago
|
TQCString temp( result.cstr() );
|
||
|
imapCapabilities = TQStringList::split ( ' ', KPIM::kAsciiToLower( temp.data() ) );
|
||
15 years ago
|
}
|
||
|
|
||
|
void imapParser::parseFlags (parseString & result)
|
||
|
{
|
||
|
selectInfo.setFlags(result.cstr());
|
||
|
}
|
||
|
|
||
|
void imapParser::parseList (parseString & result)
|
||
|
{
|
||
|
imapList this_one;
|
||
|
|
||
|
if (result[0] != '(')
|
||
|
return; //not proper format for us
|
||
|
|
||
|
result.pos++; // tie off (
|
||
|
|
||
|
this_one.parseAttributes( result );
|
||
|
|
||
|
result.pos++; // tie off )
|
||
|
skipWS (result);
|
||
|
|
||
|
this_one.setHierarchyDelimiter(parseLiteralC(result));
|
||
|
this_one.setName (rfcDecoder::fromIMAP(parseLiteralC(result))); // decode modified UTF7
|
||
|
|
||
|
listResponses.append (this_one);
|
||
|
}
|
||
|
|
||
|
void imapParser::parseLsub (parseString & result)
|
||
|
{
|
||
|
imapList this_one (result.cstr(), *this);
|
||
|
listResponses.append (this_one);
|
||
|
}
|
||
|
|
||
|
void imapParser::parseListRights (parseString & result)
|
||
|
{
|
||
|
parseOneWordC (result); // skip mailbox name
|
||
|
parseOneWordC (result); // skip user id
|
||
|
int outlen = 1;
|
||
|
while ( outlen ) {
|
||
14 years ago
|
TQCString word = parseOneWordC (result, false, &outlen);
|
||
15 years ago
|
lastResults.append (word);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void imapParser::parseAcl (parseString & result)
|
||
|
{
|
||
|
parseOneWordC (result); // skip mailbox name
|
||
|
int outlen = 1;
|
||
|
// The result is user1 perm1 user2 perm2 etc. The caller will sort it out.
|
||
|
while ( outlen && !result.isEmpty() ) {
|
||
14 years ago
|
TQCString word = parseLiteralC (result, false, false, &outlen);
|
||
15 years ago
|
lastResults.append (word);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void imapParser::parseAnnotation (parseString & result)
|
||
|
{
|
||
|
parseOneWordC (result); // skip mailbox name
|
||
|
skipWS (result);
|
||
|
parseOneWordC (result); // skip entry name (we know it since we don't allow wildcards in it)
|
||
|
skipWS (result);
|
||
|
if (result.isEmpty() || result[0] != '(')
|
||
|
return;
|
||
|
result.pos++;
|
||
|
skipWS (result);
|
||
|
int outlen = 1;
|
||
|
// The result is name1 value1 name2 value2 etc. The caller will sort it out.
|
||
|
while ( outlen && !result.isEmpty() && result[0] != ')' ) {
|
||
14 years ago
|
TQCString word = parseLiteralC (result, false, false, &outlen);
|
||
15 years ago
|
lastResults.append (word);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
void imapParser::parseQuota (parseString & result)
|
||
|
{
|
||
13 years ago
|
// quota_response ::= "QUOTA" SP astring SP quota_list
|
||
15 years ago
|
// quota_list ::= "(" #quota_resource ")"
|
||
|
// quota_resource ::= atom SP number SP number
|
||
14 years ago
|
TQCString root = parseOneWordC( result );
|
||
15 years ago
|
if ( root.isEmpty() ) {
|
||
|
lastResults.append( "" );
|
||
|
} else {
|
||
|
lastResults.append( root );
|
||
|
}
|
||
|
if (result.isEmpty() || result[0] != '(')
|
||
|
return;
|
||
|
result.pos++;
|
||
|
skipWS (result);
|
||
14 years ago
|
TQStringList triplet;
|
||
15 years ago
|
int outlen = 1;
|
||
|
while ( outlen && !result.isEmpty() && result[0] != ')' ) {
|
||
14 years ago
|
TQCString word = parseLiteralC (result, false, false, &outlen);
|
||
15 years ago
|
triplet.append(word);
|
||
|
}
|
||
|
lastResults.append( triplet.join(" ") );
|
||
|
}
|
||
|
|
||
|
void imapParser::parseQuotaRoot (parseString & result)
|
||
|
{
|
||
|
// quotaroot_response
|
||
13 years ago
|
// ::= "QUOTAROOT" SP astring *(SP astring)
|
||
15 years ago
|
parseOneWordC (result); // skip mailbox name
|
||
|
skipWS (result);
|
||
|
if ( result.isEmpty() )
|
||
|
return;
|
||
14 years ago
|
TQStringList roots;
|
||
15 years ago
|
int outlen = 1;
|
||
|
while ( outlen && !result.isEmpty() ) {
|
||
14 years ago
|
TQCString word = parseLiteralC (result, false, false, &outlen);
|
||
15 years ago
|
roots.append (word);
|
||
|
}
|
||
|
lastResults.append( roots.isEmpty()? "" : roots.join(" ") );
|
||
|
}
|
||
|
|
||
|
void imapParser::parseCustom (parseString & result)
|
||
|
{
|
||
|
int outlen = 1;
|
||
14 years ago
|
TQCString word = parseLiteralC (result, false, false, &outlen);
|
||
15 years ago
|
lastResults.append( word );
|
||
|
}
|
||
|
|
||
|
void imapParser::parseOtherUser (parseString & result)
|
||
|
{
|
||
|
lastResults.append( parseOneWordC( result ) );
|
||
|
}
|
||
|
|
||
|
void imapParser::parseDelegate (parseString & result)
|
||
|
{
|
||
14 years ago
|
const TQString email = parseOneWordC( result );
|
||
15 years ago
|
|
||
14 years ago
|
TQStringList rights;
|
||
15 years ago
|
int outlen = 1;
|
||
|
while ( outlen && !result.isEmpty() ) {
|
||
14 years ago
|
TQCString word = parseLiteralC( result, false, false, &outlen );
|
||
15 years ago
|
rights.append( word );
|
||
|
}
|
||
|
|
||
17 years ago
|
lastResults.append( email + ':' + rights.join( "," ) );
|
||
15 years ago
|
}
|
||
|
|
||
|
void imapParser::parseOutOfOffice (parseString & result)
|
||
|
{
|
||
14 years ago
|
const TQString state = parseOneWordC (result);
|
||
15 years ago
|
parseOneWordC (result); // skip encoding
|
||
|
|
||
|
int outlen = 1;
|
||
14 years ago
|
TQCString msg = parseLiteralC (result, false, false, &outlen);
|
||
15 years ago
|
|
||
17 years ago
|
lastResults.append( state + '^' + TQString::fromUtf8( msg ) );
|
||
15 years ago
|
}
|
||
|
|
||
|
void imapParser::parseMyRights (parseString & result)
|
||
|
{
|
||
|
parseOneWordC (result); // skip mailbox name
|
||
|
Q_ASSERT( lastResults.isEmpty() ); // we can only be called once
|
||
|
lastResults.append (parseOneWordC (result) );
|
||
|
}
|
||
|
|
||
|
void imapParser::parseSearch (parseString & result)
|
||
|
{
|
||
|
ulong value;
|
||
|
|
||
|
while (parseOneNumber (result, value))
|
||
|
{
|
||
14 years ago
|
lastResults.append (TQString::number(value));
|
||
15 years ago
|
}
|
||
|
}
|
||
|
|
||
13 years ago
|
void imapParser::parsetStatus (parseString & inWords)
|
||
15 years ago
|
{
|
||
13 years ago
|
lasStatus = imapInfo ();
|
||
15 years ago
|
|
||
|
parseLiteralC(inWords); // swallow the box
|
||
|
if (inWords.isEmpty() || inWords[0] != '(')
|
||
|
return;
|
||
|
|
||
|
inWords.pos++;
|
||
|
skipWS (inWords);
|
||
|
|
||
|
while (!inWords.isEmpty() && inWords[0] != ')')
|
||
|
{
|
||
|
ulong value;
|
||
|
|
||
14 years ago
|
TQCString label = parseOneWordC(inWords);
|
||
15 years ago
|
if (parseOneNumber (inWords, value))
|
||
|
{
|
||
|
if (label == "MESSAGES")
|
||
13 years ago
|
lasStatus.setCount (value);
|
||
15 years ago
|
else if (label == "RECENT")
|
||
13 years ago
|
lasStatus.setRecent (value);
|
||
15 years ago
|
else if (label == "UIDVALIDITY")
|
||
13 years ago
|
lasStatus.setUidValidity (value);
|
||
15 years ago
|
else if (label == "UNSEEN")
|
||
13 years ago
|
lasStatus.setUnseen (value);
|
||
15 years ago
|
else if (label == "UIDNEXT")
|
||
13 years ago
|
lasStatus.setUidNext (value);
|
||
15 years ago
|
}
|
||
|
}
|
||
|
|
||
|
if (inWords[0] == ')')
|
||
|
inWords.pos++;
|
||
|
skipWS (inWords);
|
||
|
}
|
||
|
|
||
|
void imapParser::parseExists (ulong value, parseString & result)
|
||
|
{
|
||
|
selectInfo.setCount (value);
|
||
|
result.pos = result.data.size();
|
||
|
}
|
||
|
|
||
|
void imapParser::parseExpunge (ulong value, parseString & result)
|
||
|
{
|
||
|
Q_UNUSED(value);
|
||
|
Q_UNUSED(result);
|
||
|
}
|
||
|
|
||
14 years ago
|
void imapParser::parseAddressList (parseString & inWords, TQPtrList<mailAddress>& list)
|
||
15 years ago
|
{
|
||
|
if (inWords.isEmpty())
|
||
|
return;
|
||
|
if (inWords[0] != '(')
|
||
|
{
|
||
|
parseOneWordC (inWords); // parse NIL
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
inWords.pos++;
|
||
|
skipWS (inWords);
|
||
|
|
||
|
while (!inWords.isEmpty () && inWords[0] != ')')
|
||
|
{
|
||
|
if (inWords[0] == '(') {
|
||
|
mailAddress *addr = new mailAddress;
|
||
|
parseAddress(inWords, *addr);
|
||
|
list.append(addr);
|
||
|
} else {
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (!inWords.isEmpty() && inWords[0] == ')')
|
||
|
inWords.pos++;
|
||
|
skipWS (inWords);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
const mailAddress& imapParser::parseAddress (parseString & inWords, mailAddress& retVal)
|
||
|
{
|
||
|
inWords.pos++;
|
||
|
skipWS (inWords);
|
||
|
|
||
|
retVal.setFullName(parseLiteralC(inWords));
|
||
|
retVal.setCommentRaw(parseLiteralC(inWords));
|
||
|
retVal.setUser(parseLiteralC(inWords));
|
||
|
retVal.setHost(parseLiteralC(inWords));
|
||
|
|
||
|
if (!inWords.isEmpty() && inWords[0] == ')')
|
||
|
inWords.pos++;
|
||
|
skipWS (inWords);
|
||
|
|
||
|
return retVal;
|
||
|
}
|
||
|
|
||
|
mailHeader * imapParser::parseEnvelope (parseString & inWords)
|
||
|
{
|
||
|
mailHeader *envelope = 0;
|
||
|
|
||
|
if (inWords[0] != '(')
|
||
|
return envelope;
|
||
|
inWords.pos++;
|
||
|
skipWS (inWords);
|
||
|
|
||
|
envelope = new mailHeader;
|
||
|
|
||
|
//date
|
||
|
envelope->setDate(parseLiteralC(inWords));
|
||
|
|
||
|
//subject
|
||
|
envelope->setSubject(parseLiteralC(inWords));
|
||
|
|
||
14 years ago
|
TQPtrList<mailAddress> list;
|
||
15 years ago
|
list.setAutoDelete(true);
|
||
|
|
||
|
//from
|
||
|
parseAddressList(inWords, list);
|
||
|
if (!list.isEmpty()) {
|
||
|
envelope->setFrom(*list.last());
|
||
|
list.clear();
|
||
|
}
|
||
|
|
||
|
//sender
|
||
|
parseAddressList(inWords, list);
|
||
|
if (!list.isEmpty()) {
|
||
|
envelope->setSender(*list.last());
|
||
|
list.clear();
|
||
|
}
|
||
|
|
||
|
//reply-to
|
||
|
parseAddressList(inWords, list);
|
||
|
if (!list.isEmpty()) {
|
||
|
envelope->setReplyTo(*list.last());
|
||
|
list.clear();
|
||
|
}
|
||
|
|
||
|
//to
|
||
|
parseAddressList (inWords, envelope->to());
|
||
|
|
||
|
//cc
|
||
|
parseAddressList (inWords, envelope->cc());
|
||
|
|
||
|
//bcc
|
||
|
parseAddressList (inWords, envelope->bcc());
|
||
|
|
||
|
//in-reply-to
|
||
|
envelope->setInReplyTo(parseLiteralC(inWords));
|
||
|
|
||
|
//message-id
|
||
|
envelope->setMessageId(parseLiteralC(inWords));
|
||
|
|
||
|
// see if we have more to come
|
||
|
while (!inWords.isEmpty () && inWords[0] != ')')
|
||
|
{
|
||
|
//eat the extensions to this part
|
||
|
if (inWords[0] == '(')
|
||
|
parseSentence (inWords);
|
||
|
else
|
||
|
parseLiteralC (inWords);
|
||
|
}
|
||
|
|
||
|
if (!inWords.isEmpty() && inWords[0] == ')')
|
||
|
inWords.pos++;
|
||
|
skipWS (inWords);
|
||
|
|
||
|
return envelope;
|
||
|
}
|
||
|
|
||
|
// parse parameter pairs into a dictionary
|
||
|
// caller must clean up the dictionary items
|
||
14 years ago
|
TQAsciiDict < TQString > imapParser::parseDisposition (parseString & inWords)
|
||
15 years ago
|
{
|
||
14 years ago
|
TQCString disposition;
|
||
|
TQAsciiDict < TQString > retVal (17, false);
|
||
15 years ago
|
|
||
|
// return value is a shallow copy
|
||
|
retVal.setAutoDelete (false);
|
||
|
|
||
|
if (inWords[0] != '(')
|
||
|
{
|
||
|
//disposition only
|
||
|
disposition = parseOneWordC (inWords);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
inWords.pos++;
|
||
|
skipWS (inWords);
|
||
|
|
||
|
//disposition
|
||
|
disposition = parseOneWordC (inWords);
|
||
|
retVal = parseParameters (inWords);
|
||
|
if (inWords[0] != ')')
|
||
|
return retVal;
|
||
|
inWords.pos++;
|
||
|
skipWS (inWords);
|
||
|
}
|
||
|
|
||
|
if (!disposition.isEmpty ())
|
||
|
{
|
||
14 years ago
|
retVal.insert ("content-disposition", new TQString(disposition));
|
||
15 years ago
|
}
|
||
|
|
||
|
return retVal;
|
||
|
}
|
||
|
|
||
|
// parse parameter pairs into a dictionary
|
||
|
// caller must clean up the dictionary items
|
||
14 years ago
|
TQAsciiDict < TQString > imapParser::parseParameters (parseString & inWords)
|
||
15 years ago
|
{
|
||
14 years ago
|
TQAsciiDict < TQString > retVal (17, false);
|
||
15 years ago
|
|
||
|
// return value is a shallow copy
|
||
|
retVal.setAutoDelete (false);
|
||
|
|
||
|
if (inWords[0] != '(')
|
||
|
{
|
||
|
//better be NIL
|
||
|
parseOneWordC (inWords);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
inWords.pos++;
|
||
|
skipWS (inWords);
|
||
|
|
||
|
while (!inWords.isEmpty () && inWords[0] != ')')
|
||
|
{
|
||
14 years ago
|
TQCString l1 = parseLiteralC(inWords);
|
||
|
TQCString l2 = parseLiteralC(inWords);
|
||
|
retVal.insert (l1, new TQString(l2));
|
||
15 years ago
|
}
|
||
|
|
||
|
if (inWords[0] != ')')
|
||
|
return retVal;
|
||
|
inWords.pos++;
|
||
|
skipWS (inWords);
|
||
|
}
|
||
|
|
||
|
return retVal;
|
||
|
}
|
||
|
|
||
|
mimeHeader * imapParser::parseSimplePart (parseString & inWords,
|
||
14 years ago
|
TQString & inSection, mimeHeader * localPart)
|
||
15 years ago
|
{
|
||
14 years ago
|
TQCString subtype;
|
||
|
TQCString typeStr;
|
||
|
TQAsciiDict < TQString > parameters (17, false);
|
||
15 years ago
|
ulong size;
|
||
|
|
||
|
parameters.setAutoDelete (true);
|
||
|
|
||
|
if (inWords[0] != '(')
|
||
|
return 0;
|
||
|
|
||
|
if (!localPart)
|
||
|
localPart = new mimeHeader;
|
||
|
|
||
|
localPart->setPartSpecifier (inSection);
|
||
|
|
||
|
inWords.pos++;
|
||
|
skipWS (inWords);
|
||
|
|
||
|
//body type
|
||
|
typeStr = parseLiteralC(inWords);
|
||
|
|
||
|
//body subtype
|
||
|
subtype = parseLiteralC(inWords);
|
||
|
|
||
|
localPart->setType (typeStr + "/" + subtype);
|
||
|
|
||
14 years ago
|
//body parameter parenthesized list
|
||
15 years ago
|
parameters = parseParameters (inWords);
|
||
|
{
|
||
14 years ago
|
TQAsciiDictIterator < TQString > it (parameters);
|
||
15 years ago
|
|
||
|
while (it.current ())
|
||
|
{
|
||
|
localPart->setTypeParm (it.currentKey (), *(it.current ()));
|
||
|
++it;
|
||
|
}
|
||
|
parameters.clear ();
|
||
|
}
|
||
|
|
||
|
//body id
|
||
|
localPart->setID (parseLiteralC(inWords));
|
||
|
|
||
|
//body description
|
||
|
localPart->setDescription (parseLiteralC(inWords));
|
||
|
|
||
|
//body encoding
|
||
|
localPart->setEncoding (parseLiteralC(inWords));
|
||
|
|
||
|
//body size
|
||
|
if (parseOneNumber (inWords, size))
|
||
|
localPart->setLength (size);
|
||
|
|
||
|
// type specific extensions
|
||
|
if (localPart->getType().upper() == "MESSAGE/RFC822")
|
||
|
{
|
||
|
//envelope structure
|
||
|
mailHeader *envelope = parseEnvelope (inWords);
|
||
|
|
||
|
//body structure
|
||
|
parseBodyStructure (inWords, inSection, envelope);
|
||
|
|
||
|
localPart->setNestedMessage (envelope);
|
||
|
|
||
|
//text lines
|
||
|
ulong lines;
|
||
|
parseOneNumber (inWords, lines);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
if (typeStr == "TEXT")
|
||
|
{
|
||
|
//text lines
|
||
|
ulong lines;
|
||
|
parseOneNumber (inWords, lines);
|
||
|
}
|
||
|
|
||
|
// md5
|
||
|
parseLiteralC(inWords);
|
||
|
|
||
|
// body disposition
|
||
|
parameters = parseDisposition (inWords);
|
||
|
{
|
||
14 years ago
|
TQString *disposition = parameters["content-disposition"];
|
||
15 years ago
|
|
||
|
if (disposition)
|
||
|
localPart->setDisposition (disposition->ascii ());
|
||
|
parameters.remove ("content-disposition");
|
||
14 years ago
|
TQAsciiDictIterator < TQString > it (parameters);
|
||
15 years ago
|
while (it.current ())
|
||
|
{
|
||
|
localPart->setDispositionParm (it.currentKey (),
|
||
|
*(it.current ()));
|
||
|
++it;
|
||
|
}
|
||
|
|
||
|
parameters.clear ();
|
||
|
}
|
||
|
|
||
|
// body language
|
||
|
parseSentence (inWords);
|
||
|
}
|
||
|
|
||
|
// see if we have more to come
|
||
|
while (!inWords.isEmpty () && inWords[0] != ')')
|
||
|
{
|
||
|
//eat the extensions to this part
|
||
|
if (inWords[0] == '(')
|
||
|
parseSentence (inWords);
|
||
|
else
|
||
|
parseLiteralC(inWords);
|
||
|
}
|
||
|
if (inWords[0] == ')')
|
||
|
inWords.pos++;
|
||
|
skipWS (inWords);
|
||
|
|
||
|
return localPart;
|
||
|
}
|
||
|
|
||
|
mimeHeader * imapParser::parseBodyStructure (parseString & inWords,
|
||
14 years ago
|
TQString & inSection, mimeHeader * localPart)
|
||
15 years ago
|
{
|
||
|
bool init = false;
|
||
|
if (inSection.isEmpty())
|
||
|
{
|
||
|
// first run
|
||
|
init = true;
|
||
|
// assume one part
|
||
|
inSection = "1";
|
||
|
}
|
||
|
int section = 0;
|
||
|
|
||
|
if (inWords[0] != '(')
|
||
|
{
|
||
|
// skip ""
|
||
|
parseOneWordC (inWords);
|
||
|
return 0;
|
||
|
}
|
||
|
inWords.pos++;
|
||
|
skipWS (inWords);
|
||
|
|
||
|
if (inWords[0] == '(')
|
||
|
{
|
||
14 years ago
|
TQByteArray subtype;
|
||
|
TQAsciiDict < TQString > parameters (17, false);
|
||
|
TQString outSection;
|
||
15 years ago
|
parameters.setAutoDelete (true);
|
||
|
if (!localPart)
|
||
|
localPart = new mimeHeader;
|
||
|
else
|
||
|
{
|
||
|
// might be filled from an earlier run
|
||
|
localPart->clearNestedParts ();
|
||
|
localPart->clearTypeParameters ();
|
||
|
localPart->clearDispositionParameters ();
|
||
|
// an envelope was passed in so this is the multipart header
|
||
|
outSection = inSection + ".HEADER";
|
||
|
}
|
||
|
if (inWords[0] == '(' && init)
|
||
|
inSection = "0";
|
||
|
|
||
|
// set the section
|
||
|
if ( !outSection.isEmpty() ) {
|
||
|
localPart->setPartSpecifier(outSection);
|
||
|
} else {
|
||
|
localPart->setPartSpecifier(inSection);
|
||
|
}
|
||
|
|
||
16 years ago
|
// is multipart (otherwise it is a simplepart and handled later)
|
||
15 years ago
|
while (inWords[0] == '(')
|
||
|
{
|
||
14 years ago
|
outSection = TQString::number(++section);
|
||
15 years ago
|
if (!init)
|
||
|
outSection = inSection + "." + outSection;
|
||
|
mimeHeader *subpart = parseBodyStructure (inWords, outSection, 0);
|
||
|
localPart->addNestedPart (subpart);
|
||
|
}
|
||
|
|
||
|
// fetch subtype
|
||
|
subtype = parseOneWordC (inWords);
|
||
|
|
||
|
localPart->setType ("MULTIPART/" + b2c(subtype));
|
||
|
|
||
|
// fetch parameters
|
||
|
parameters = parseParameters (inWords);
|
||
|
{
|
||
14 years ago
|
TQAsciiDictIterator < TQString > it (parameters);
|
||
15 years ago
|
|
||
|
while (it.current ())
|
||
|
{
|
||
|
localPart->setTypeParm (it.currentKey (), *(it.current ()));
|
||
|
++it;
|
||
|
}
|
||
|
parameters.clear ();
|
||
|
}
|
||
|
|
||
|
// body disposition
|
||
|
parameters = parseDisposition (inWords);
|
||
|
{
|
||
14 years ago
|
TQString *disposition = parameters["content-disposition"];
|
||
15 years ago
|
|
||
|
if (disposition)
|
||
|
localPart->setDisposition (disposition->ascii ());
|
||
|
parameters.remove ("content-disposition");
|
||
14 years ago
|
TQAsciiDictIterator < TQString > it (parameters);
|
||
15 years ago
|
while (it.current ())
|
||
|
{
|
||
|
localPart->setDispositionParm (it.currentKey (),
|
||
|
*(it.current ()));
|
||
|
++it;
|
||
|
}
|
||
|
parameters.clear ();
|
||
|
}
|
||
|
|
||
|
// body language
|
||
|
parseSentence (inWords);
|
||
|
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
// is simple part
|
||
|
inWords.pos--;
|
||
|
inWords.data[inWords.pos] = '('; //fake a sentence
|
||
|
if ( localPart )
|
||
|
inSection = inSection + ".1";
|
||
|
localPart = parseSimplePart (inWords, inSection, localPart);
|
||
|
inWords.pos--;
|
||
|
inWords.data[inWords.pos] = ')'; //remove fake
|
||
|
}
|
||
|
|
||
|
// see if we have more to come
|
||
|
while (!inWords.isEmpty () && inWords[0] != ')')
|
||
|
{
|
||
|
//eat the extensions to this part
|
||
|
if (inWords[0] == '(')
|
||
|
parseSentence (inWords);
|
||
|
else
|
||
|
parseLiteralC(inWords);
|
||
|
}
|
||
|
|
||
|
if (inWords[0] == ')')
|
||
|
inWords.pos++;
|
||
|
skipWS (inWords);
|
||
|
|
||
|
return localPart;
|
||
|
}
|
||
|
|
||
|
void imapParser::parseBody (parseString & inWords)
|
||
|
{
|
||
|
// see if we got a part specifier
|
||
|
if (inWords[0] == '[')
|
||
|
{
|
||
14 years ago
|
TQCString specifier;
|
||
|
TQCString label;
|
||
15 years ago
|
inWords.pos++;
|
||
|
|
||
|
specifier = parseOneWordC (inWords, TRUE);
|
||
|
|
||
|
if (inWords[0] == '(')
|
||
|
{
|
||
|
inWords.pos++;
|
||
|
|
||
|
while (!inWords.isEmpty () && inWords[0] != ')')
|
||
|
{
|
||
|
label = parseOneWordC (inWords);
|
||
|
}
|
||
|
|
||
13 years ago
|
if (!inWords.isEmpty () && inWords[0] == ')')
|
||
15 years ago
|
inWords.pos++;
|
||
|
}
|
||
13 years ago
|
if (!inWords.isEmpty () && inWords[0] == ']')
|
||
15 years ago
|
inWords.pos++;
|
||
|
skipWS (inWords);
|
||
|
|
||
|
// parse the header
|
||
|
if (specifier == "0")
|
||
|
{
|
||
|
mailHeader *envelope = 0;
|
||
|
if (lastHandled)
|
||
|
envelope = lastHandled->getHeader ();
|
||
|
|
||
|
if (!envelope || seenUid.isEmpty ())
|
||
|
{
|
||
|
kdDebug(7116) << "imapParser::parseBody - discarding " << envelope << " " << seenUid.ascii () << endl;
|
||
|
// don't know where to put it, throw it away
|
||
|
parseLiteralC(inWords, true);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
kdDebug(7116) << "imapParser::parseBody - reading " << envelope << " " << seenUid.ascii () << endl;
|
||
|
// fill it up with data
|
||
14 years ago
|
TQString theHeader = parseLiteralC(inWords, true);
|
||
14 years ago
|
mimeIOTQString myIO;
|
||
15 years ago
|
|
||
|
myIO.setString (theHeader);
|
||
|
envelope->parseHeader (myIO);
|
||
|
|
||
|
}
|
||
|
}
|
||
|
else if (specifier == "HEADER.FIELDS")
|
||
|
{
|
||
|
// BODY[HEADER.FIELDS (References)] {n}
|
||
|
//kdDebug(7116) << "imapParser::parseBody - HEADER.FIELDS: "
|
||
14 years ago
|
// << TQCString(label.data(), label.size()+1) << endl;
|
||
15 years ago
|
if (label == "REFERENCES")
|
||
|
{
|
||
|
mailHeader *envelope = 0;
|
||
|
if (lastHandled)
|
||
|
envelope = lastHandled->getHeader ();
|
||
|
|
||
|
if (!envelope || seenUid.isEmpty ())
|
||
|
{
|
||
|
kdDebug(7116) << "imapParser::parseBody - discarding " << envelope << " " << seenUid.ascii () << endl;
|
||
|
// don't know where to put it, throw it away
|
||
|
parseLiteralC (inWords, true);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
14 years ago
|
TQCString references = parseLiteralC(inWords, true);
|
||
13 years ago
|
int start = references.find ('<');
|
||
|
int end = references.findRev ('>');
|
||
15 years ago
|
if (start < end)
|
||
|
references = references.mid (start, end - start + 1);
|
||
|
envelope->setReferences(references.simplifyWhiteSpace());
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{ // not a header we care about throw it away
|
||
|
parseLiteralC(inWords, true);
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
13 years ago
|
if (specifier.find(".MIME") != -1)
|
||
15 years ago
|
{
|
||
|
mailHeader *envelope = new mailHeader;
|
||
14 years ago
|
TQString theHeader = parseLiteralC(inWords, false);
|
||
14 years ago
|
mimeIOTQString myIO;
|
||
15 years ago
|
myIO.setString (theHeader);
|
||
|
envelope->parseHeader (myIO);
|
||
|
if (lastHandled)
|
||
|
lastHandled->setHeader (envelope);
|
||
|
return;
|
||
|
}
|
||
|
// throw it away
|
||
|
kdDebug(7116) << "imapParser::parseBody - discarding " << seenUid.ascii () << endl;
|
||
|
parseLiteralC(inWords, true);
|
||
|
}
|
||
|
|
||
|
}
|
||
|
else // no part specifier
|
||
|
{
|
||
|
mailHeader *envelope = 0;
|
||
|
if (lastHandled)
|
||
|
envelope = lastHandled->getHeader ();
|
||
|
|
||
|
if (!envelope || seenUid.isEmpty ())
|
||
|
{
|
||
|
kdDebug(7116) << "imapParser::parseBody - discarding " << envelope << " " << seenUid.ascii () << endl;
|
||
|
// don't know where to put it, throw it away
|
||
|
parseSentence (inWords);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
kdDebug(7116) << "imapParser::parseBody - reading " << envelope << " " << seenUid.ascii () << endl;
|
||
|
// fill it up with data
|
||
14 years ago
|
TQString section;
|
||
15 years ago
|
mimeHeader *body = parseBodyStructure (inWords, section, envelope);
|
||
|
if (body != envelope)
|
||
|
delete body;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void imapParser::parseFetch (ulong /* value */, parseString & inWords)
|
||
|
{
|
||
|
if (inWords[0] != '(')
|
||
|
return;
|
||
|
inWords.pos++;
|
||
|
skipWS (inWords);
|
||
|
|
||
|
delete lastHandled;
|
||
|
lastHandled = 0;
|
||
|
|
||
|
while (!inWords.isEmpty () && inWords[0] != ')')
|
||
|
{
|
||
|
if (inWords[0] == '(')
|
||
|
parseSentence (inWords);
|
||
|
else
|
||
|
{
|
||
14 years ago
|
TQCString word = parseLiteralC(inWords, false, true);
|
||
15 years ago
|
|
||
13 years ago
|
if(!word.isEmpty()) {
|
||
15 years ago
|
switch (word[0])
|
||
|
{
|
||
|
case 'E':
|
||
|
if (word == "ENVELOPE")
|
||
|
{
|
||
|
mailHeader *envelope = 0;
|
||
|
|
||
|
if (lastHandled)
|
||
|
envelope = lastHandled->getHeader ();
|
||
|
else
|
||
|
lastHandled = new imapCache();
|
||
|
|
||
|
if (envelope && !envelope->getMessageId ().isEmpty ())
|
||
|
{
|
||
|
// we have seen this one already
|
||
|
// or don't know where to put it
|
||
|
parseSentence (inWords);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
envelope = parseEnvelope (inWords);
|
||
|
if (envelope)
|
||
|
{
|
||
|
envelope->setPartSpecifier (seenUid + ".0");
|
||
|
lastHandled->setHeader (envelope);
|
||
|
lastHandled->setUid (seenUid.toULong ());
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case 'B':
|
||
|
if (word == "BODY")
|
||
|
{
|
||
|
parseBody (inWords);
|
||
|
}
|
||
|
else if (word == "BODY[]" )
|
||
|
{
|
||
|
// Do the same as with "RFC822"
|
||
|
parseLiteralC(inWords, true);
|
||
|
}
|
||
|
else if (word == "BODYSTRUCTURE")
|
||
|
{
|
||
|
mailHeader *envelope = 0;
|
||
|
|
||
|
if (lastHandled)
|
||
|
envelope = lastHandled->getHeader ();
|
||
|
|
||
|
// fill it up with data
|
||
14 years ago
|
TQString section;
|
||
15 years ago
|
mimeHeader *body =
|
||
|
parseBodyStructure (inWords, section, envelope);
|
||
14 years ago
|
TQByteArray data;
|
||
|
TQDataStream stream( data, IO_WriteOnly );
|
||
15 years ago
|
if (body) body->serialize(stream);
|
||
|
parseRelay(data);
|
||
|
|
||
|
delete body;
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case 'U':
|
||
|
if (word == "UID")
|
||
|
{
|
||
|
seenUid = parseOneWordC(inWords);
|
||
|
mailHeader *envelope = 0;
|
||
|
if (lastHandled)
|
||
|
envelope = lastHandled->getHeader ();
|
||
|
else
|
||
|
lastHandled = new imapCache();
|
||
|
|
||
|
if (seenUid.isEmpty ())
|
||
|
{
|
||
|
// unknown what to do
|
||
|
kdDebug(7116) << "imapParser::parseFetch - UID empty" << endl;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
lastHandled->setUid (seenUid.toULong ());
|
||
|
}
|
||
|
if (envelope)
|
||
|
envelope->setPartSpecifier (seenUid);
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case 'R':
|
||
|
if (word == "RFC822.SIZE")
|
||
|
{
|
||
|
ulong size;
|
||
|
parseOneNumber (inWords, size);
|
||
|
|
||
|
if (!lastHandled) lastHandled = new imapCache();
|
||
|
lastHandled->setSize (size);
|
||
|
}
|
||
13 years ago
|
else if (word.find ("RFC822") == 0)
|
||
15 years ago
|
{
|
||
|
// might be RFC822 RFC822.TEXT RFC822.HEADER
|
||
|
parseLiteralC(inWords, true);
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case 'I':
|
||
|
if (word == "INTERNALDATE")
|
||
|
{
|
||
14 years ago
|
TQCString date = parseOneWordC(inWords);
|
||
15 years ago
|
if (!lastHandled) lastHandled = new imapCache();
|
||
|
lastHandled->setDate(date);
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case 'F':
|
||
|
if (word == "FLAGS")
|
||
|
{
|
||
|
//kdDebug(7116) << "GOT FLAGS " << inWords.cstr() << endl;
|
||
|
if (!lastHandled) lastHandled = new imapCache();
|
||
|
lastHandled->setFlags (imapInfo::_flags (inWords.cstr()));
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
default:
|
||
|
parseLiteralC(inWords);
|
||
|
break;
|
||
|
}
|
||
13 years ago
|
} else {
|
||
|
parseLiteralC(inWords);
|
||
|
}
|
||
15 years ago
|
}
|
||
|
}
|
||
|
|
||
|
// see if we have more to come
|
||
|
while (!inWords.isEmpty () && inWords[0] != ')')
|
||
|
{
|
||
|
//eat the extensions to this part
|
||
|
if (inWords[0] == '(')
|
||
|
parseSentence (inWords);
|
||
|
else
|
||
|
parseLiteralC(inWords);
|
||
|
}
|
||
|
|
||
|
if (inWords.isEmpty() || inWords[0] != ')')
|
||
|
return;
|
||
|
inWords.pos++;
|
||
|
skipWS (inWords);
|
||
|
}
|
||
|
|
||
|
|
||
|
// default parser
|
||
|
void imapParser::parseSentence (parseString & inWords)
|
||
|
{
|
||
|
bool first = true;
|
||
|
int stack = 0;
|
||
|
|
||
14 years ago
|
//find the first nesting parentheses
|
||
15 years ago
|
|
||
|
while (!inWords.isEmpty () && (stack != 0 || first))
|
||
|
{
|
||
|
first = false;
|
||
|
skipWS (inWords);
|
||
|
|
||
|
unsigned char ch = inWords[0];
|
||
|
switch (ch)
|
||
|
{
|
||
|
case '(':
|
||
|
inWords.pos++;
|
||
|
++stack;
|
||
|
break;
|
||
|
case ')':
|
||
|
inWords.pos++;
|
||
|
--stack;
|
||
|
break;
|
||
|
case '[':
|
||
|
inWords.pos++;
|
||
|
++stack;
|
||
|
break;
|
||
|
case ']':
|
||
|
inWords.pos++;
|
||
|
--stack;
|
||
|
break;
|
||
|
default:
|
||
|
parseLiteralC(inWords);
|
||
|
skipWS (inWords);
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
skipWS (inWords);
|
||
|
}
|
||
|
|
||
|
void imapParser::parseRecent (ulong value, parseString & result)
|
||
|
{
|
||
|
selectInfo.setRecent (value);
|
||
|
result.pos = result.data.size();
|
||
|
}
|
||
|
|
||
|
void imapParser::parseNamespace (parseString & result)
|
||
|
{
|
||
|
if ( result[0] != '(' )
|
||
|
return;
|
||
|
|
||
14 years ago
|
TQString delimEmpty;
|
||
13 years ago
|
if ( namespaceToDelimiter.contains( TQString() ) )
|
||
14 years ago
|
delimEmpty = namespaceToDelimiter[TQString()];
|
||
15 years ago
|
|
||
|
namespaceToDelimiter.clear();
|
||
|
imapNamespaces.clear();
|
||
|
|
||
|
// remember what section we're in (user, other users, shared)
|
||
|
int ns = -1;
|
||
|
bool personalAvailable = false;
|
||
|
while ( !result.isEmpty() )
|
||
|
{
|
||
|
if ( result[0] == '(' )
|
||
|
{
|
||
|
result.pos++; // tie off (
|
||
|
if ( result[0] == '(' )
|
||
|
{
|
||
|
// new namespace section
|
||
|
result.pos++; // tie off (
|
||
|
++ns;
|
||
|
}
|
||
|
// namespace prefix
|
||
14 years ago
|
TQCString prefix = parseOneWordC( result );
|
||
15 years ago
|
// delimiter
|
||
14 years ago
|
TQCString delim = parseOneWordC( result );
|
||
15 years ago
|
kdDebug(7116) << "imapParser::parseNamespace ns='" << prefix <<
|
||
|
"',delim='" << delim << "'" << endl;
|
||
|
if ( ns == 0 )
|
||
|
{
|
||
|
// at least one personal ns
|
||
|
personalAvailable = true;
|
||
|
}
|
||
14 years ago
|
TQString nsentry = TQString::number( ns ) + "=" + TQString(prefix) +
|
||
|
"=" + TQString(delim);
|
||
15 years ago
|
imapNamespaces.append( nsentry );
|
||
|
if ( prefix.right( 1 ) == delim ) {
|
||
|
// strip delimiter to get a correct entry for comparisons
|
||
|
prefix.resize( prefix.length() );
|
||
|
}
|
||
|
namespaceToDelimiter[prefix] = delim;
|
||
|
|
||
|
result.pos++; // tie off )
|
||
|
skipWS( result );
|
||
|
} else if ( result[0] == ')' )
|
||
|
{
|
||
|
result.pos++; // tie off )
|
||
|
skipWS( result );
|
||
|
} else if ( result[0] == 'N' )
|
||
|
{
|
||
|
// drop NIL
|
||
|
++ns;
|
||
|
parseOneWordC( result );
|
||
|
} else {
|
||
|
// drop whatever it is
|
||
|
parseOneWordC( result );
|
||
|
}
|
||
|
}
|
||
|
if ( !delimEmpty.isEmpty() ) {
|
||
|
// remember default delimiter
|
||
14 years ago
|
namespaceToDelimiter[TQString()] = delimEmpty;
|
||
15 years ago
|
if ( !personalAvailable )
|
||
|
{
|
||
|
// at least one personal ns would be nice
|
||
|
kdDebug(7116) << "imapParser::parseNamespace - registering own personal ns" << endl;
|
||
14 years ago
|
TQString nsentry = "0==" + delimEmpty;
|
||
15 years ago
|
imapNamespaces.append( nsentry );
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int imapParser::parseLoop ()
|
||
|
{
|
||
|
parseString result;
|
||
|
|
||
|
if (!parseReadLine(result.data)) return -1;
|
||
|
|
||
|
//kdDebug(7116) << result.cstr(); // includes \n
|
||
|
|
||
|
if (result.data.isEmpty())
|
||
|
return 0;
|
||
|
if (!sentQueue.count ())
|
||
|
{
|
||
|
// maybe greeting or BYE everything else SHOULD not happen, use NOOP or IDLE
|
||
|
kdDebug(7116) << "imapParser::parseLoop - unhandledResponse: \n" << result.cstr() << endl;
|
||
|
unhandled << result.cstr();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
imapCommand *current = sentQueue.at (0);
|
||
|
switch (result[0])
|
||
|
{
|
||
|
case '*':
|
||
|
result.data.resize(result.data.size() - 2); // tie off CRLF
|
||
|
parseUntagged (result);
|
||
|
break;
|
||
|
case '+':
|
||
|
continuation.duplicate(result.data);
|
||
|
break;
|
||
|
default:
|
||
|
{
|
||
14 years ago
|
TQCString tag = parseLiteralC(result);
|
||
15 years ago
|
if (current->id() == tag.data())
|
||
|
{
|
||
|
result.data.resize(result.data.size() - 2); // tie off CRLF
|
||
14 years ago
|
TQByteArray resultCode = parseLiteral (result); //the result
|
||
15 years ago
|
current->setResult (resultCode);
|
||
|
current->setResultInfo(result.cstr());
|
||
|
current->setComplete ();
|
||
|
|
||
|
sentQueue.removeRef (current);
|
||
|
completeQueue.append (current);
|
||
|
if (result.length())
|
||
|
parseResult (resultCode, result, current->command());
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
kdDebug(7116) << "imapParser::parseLoop - unknown tag '" << tag << "'" << endl;
|
||
14 years ago
|
TQCString cstr = tag + " " + result.cstr();
|
||
15 years ago
|
result.data = cstr;
|
||
|
result.pos = 0;
|
||
|
result.data.resize(cstr.length());
|
||
|
}
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
void
|
||
14 years ago
|
imapParser::parseRelay (const TQByteArray & buffer)
|
||
15 years ago
|
{
|
||
|
Q_UNUSED(buffer);
|
||
13 years ago
|
tqWarning
|
||
15 years ago
|
("imapParser::parseRelay - virtual function not reimplemented - data lost");
|
||
|
}
|
||
|
|
||
|
void
|
||
|
imapParser::parseRelay (ulong len)
|
||
|
{
|
||
|
Q_UNUSED(len);
|
||
13 years ago
|
tqWarning
|
||
15 years ago
|
("imapParser::parseRelay - virtual function not reimplemented - announcement lost");
|
||
|
}
|
||
|
|
||
14 years ago
|
bool imapParser::parseRead (TQByteArray & buffer, ulong len, ulong relay)
|
||
15 years ago
|
{
|
||
|
Q_UNUSED(buffer);
|
||
|
Q_UNUSED(len);
|
||
|
Q_UNUSED(relay);
|
||
13 years ago
|
tqWarning
|
||
15 years ago
|
("imapParser::parseRead - virtual function not reimplemented - no data read");
|
||
|
return FALSE;
|
||
|
}
|
||
|
|
||
14 years ago
|
bool imapParser::parseReadLine (TQByteArray & buffer, ulong relay)
|
||
15 years ago
|
{
|
||
|
Q_UNUSED(buffer);
|
||
|
Q_UNUSED(relay);
|
||
13 years ago
|
tqWarning
|
||
15 years ago
|
("imapParser::parseReadLine - virtual function not reimplemented - no data read");
|
||
|
return FALSE;
|
||
|
}
|
||
|
|
||
|
void
|
||
14 years ago
|
imapParser::parseWriteLine (const TQString & str)
|
||
15 years ago
|
{
|
||
|
Q_UNUSED(str);
|
||
13 years ago
|
tqWarning
|
||
15 years ago
|
("imapParser::parseWriteLine - virtual function not reimplemented - no data written");
|
||
|
}
|
||
|
|
||
|
void
|
||
14 years ago
|
imapParser::parseURL (const KURL & _url, TQString & _box, TQString & _section,
|
||
|
TQString & _type, TQString & _uid, TQString & _validity, TQString & _info)
|
||
15 years ago
|
{
|
||
14 years ago
|
TQStringList parameters;
|
||
15 years ago
|
|
||
|
_box = _url.path ();
|
||
|
kdDebug(7116) << "imapParser::parseURL " << _box << endl;
|
||
13 years ago
|
int paramStart = _box.find("/;");
|
||
15 years ago
|
if ( paramStart > -1 )
|
||
|
{
|
||
14 years ago
|
TQString paramString = _box.right( _box.length() - paramStart-2 );
|
||
|
parameters = TQStringList::split (';', paramString); //split parameters
|
||
15 years ago
|
_box.truncate( paramStart ); // strip parameters
|
||
|
}
|
||
|
// extract parameters
|
||
14 years ago
|
for (TQStringList::ConstIterator it (parameters.begin ());
|
||
15 years ago
|
it != parameters.end (); ++it)
|
||
|
{
|
||
14 years ago
|
TQString temp = (*it);
|
||
15 years ago
|
|
||
13 years ago
|
int pt = temp.find ('/');
|
||
15 years ago
|
if (pt > 0)
|
||
|
{
|
||
13 years ago
|
if (temp.findRev ('"', pt) == -1 || temp.find('"', pt) == -1)
|
||
15 years ago
|
{
|
||
|
// if we have non-quoted '/' separator we'll just nuke it
|
||
|
temp.truncate(pt);
|
||
|
}
|
||
|
}
|
||
13 years ago
|
if (temp.find ("section=", 0, false) == 0)
|
||
15 years ago
|
_section = temp.right (temp.length () - 8);
|
||
13 years ago
|
else if (temp.find ("type=", 0, false) == 0)
|
||
15 years ago
|
_type = temp.right (temp.length () - 5);
|
||
13 years ago
|
else if (temp.find ("uid=", 0, false) == 0)
|
||
15 years ago
|
_uid = temp.right (temp.length () - 4);
|
||
13 years ago
|
else if (temp.find ("uidvalidity=", 0, false) == 0)
|
||
15 years ago
|
_validity = temp.right (temp.length () - 12);
|
||
13 years ago
|
else if (temp.find ("info=", 0, false) == 0)
|
||
15 years ago
|
_info = temp.right (temp.length () - 5);
|
||
|
}
|
||
|
// kdDebug(7116) << "URL: section= " << _section << ", type= " << _type << ", uid= " << _uid << endl;
|
||
|
// kdDebug(7116) << "URL: user() " << _url.user() << endl;
|
||
|
// kdDebug(7116) << "URL: path() " << _url.path() << endl;
|
||
|
// kdDebug(7116) << "URL: encodedPathAndQuery() " << _url.encodedPathAndQuery() << endl;
|
||
|
|
||
|
if (!_box.isEmpty ())
|
||
|
{
|
||
|
// strip /
|
||
|
if (_box[0] == '/')
|
||
|
_box = _box.right (_box.length () - 1);
|
||
|
if (!_box.isEmpty () && _box[_box.length () - 1] == '/')
|
||
|
_box.truncate(_box.length() - 1);
|
||
|
}
|
||
|
kdDebug(7116) << "URL: box= " << _box << ", section= " << _section << ", type= "
|
||
|
<< _type << ", uid= " << _uid << ", validity= " << _validity << ", info= " << _info << endl;
|
||
|
}
|
||
|
|
||
|
|
||
14 years ago
|
TQCString imapParser::parseLiteralC(parseString & inWords, bool relay, bool stopAtBracket, int *outlen) {
|
||
15 years ago
|
|
||
|
if (!inWords.isEmpty() && inWords[0] == '{')
|
||
|
{
|
||
14 years ago
|
TQCString retVal;
|
||
12 years ago
|
long srunLen = inWords.find ('}', 1); // Can return -1, so use a signed long
|
||
|
if (srunLen > 0)
|
||
15 years ago
|
{
|
||
12 years ago
|
ulong runLen = (ulong)srunLen;
|
||
15 years ago
|
bool proper;
|
||
|
ulong runLenSave = runLen + 1;
|
||
14 years ago
|
TQCString tmpstr(runLen);
|
||
15 years ago
|
inWords.takeMidNoResize(tmpstr, 1, runLen - 1);
|
||
|
runLen = tmpstr.toULong (&proper);
|
||
|
inWords.pos += runLenSave;
|
||
|
if (proper)
|
||
|
{
|
||
|
//now get the literal from the server
|
||
|
if (relay)
|
||
|
parseRelay (runLen);
|
||
14 years ago
|
TQByteArray rv;
|
||
15 years ago
|
parseRead (rv, runLen, relay ? runLen : 0);
|
||
14 years ago
|
rv.resize(TQMAX(runLen, rv.size())); // what's the point?
|
||
15 years ago
|
retVal = b2c(rv);
|
||
|
inWords.clear();
|
||
|
parseReadLine (inWords.data); // must get more
|
||
|
|
||
|
// no duplicate data transfers
|
||
|
relay = false;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
kdDebug(7116) << "imapParser::parseLiteral - error parsing {} - " /*<< strLen*/ << endl;
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
inWords.clear();
|
||
|
kdDebug(7116) << "imapParser::parseLiteral - error parsing unmatched {" << endl;
|
||
|
}
|
||
|
if (outlen) {
|
||
|
*outlen = retVal.length(); // optimize me
|
||
|
}
|
||
|
skipWS (inWords);
|
||
|
return retVal;
|
||
|
}
|
||
|
|
||
|
return parseOneWordC(inWords, stopAtBracket, outlen);
|
||
|
}
|
||
|
|
||
|
// does not know about literals ( {7} literal )
|
||
14 years ago
|
TQCString imapParser::parseOneWordC (parseString & inWords, bool stopAtBracket, int *outLen)
|
||
15 years ago
|
{
|
||
|
uint retValSize = 0;
|
||
|
uint len = inWords.length();
|
||
|
if (len == 0) {
|
||
14 years ago
|
return TQCString();
|
||
15 years ago
|
}
|
||
|
|
||
|
if (len > 0 && inWords[0] == '"')
|
||
|
{
|
||
|
unsigned int i = 1;
|
||
|
bool quote = FALSE;
|
||
|
while (i < len && (inWords[i] != '"' || quote))
|
||
|
{
|
||
|
if (inWords[i] == '\\') quote = !quote;
|
||
|
else quote = FALSE;
|
||
|
i++;
|
||
|
}
|
||
|
if (i < len)
|
||
|
{
|
||
14 years ago
|
TQCString retVal(i);
|
||
15 years ago
|
inWords.pos++;
|
||
|
inWords.takeLeftNoResize(retVal, i - 1);
|
||
|
len = i - 1;
|
||
|
int offset = 0;
|
||
|
for (unsigned int j = 0; j <= len; j++) {
|
||
|
if (retVal[j] == '\\') {
|
||
|
offset++;
|
||
|
j++;
|
||
|
}
|
||
|
retVal[j - offset] = retVal[j];
|
||
|
}
|
||
|
retVal[len - offset] = 0;
|
||
|
retValSize = len - offset;
|
||
|
inWords.pos += i;
|
||
|
skipWS (inWords);
|
||
|
if (outLen) {
|
||
|
*outLen = retValSize;
|
||
|
}
|
||
|
return retVal;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
kdDebug(7116) << "imapParser::parseOneWord - error parsing unmatched \"" << endl;
|
||
14 years ago
|
TQCString retVal = inWords.cstr();
|
||
15 years ago
|
retValSize = len;
|
||
|
inWords.clear();
|
||
|
if (outLen) {
|
||
|
*outLen = retValSize;
|
||
|
}
|
||
|
return retVal;
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
// not quoted
|
||
|
unsigned int i;
|
||
|
// search for end
|
||
|
for (i = 0; i < len; ++i) {
|
||
|
char ch = inWords[i];
|
||
|
if (ch <= ' ' || ch == '(' || ch == ')' ||
|
||
|
(stopAtBracket && (ch == '[' || ch == ']')))
|
||
|
break;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQCString retVal(i+1);
|
||
15 years ago
|
inWords.takeLeftNoResize(retVal, i);
|
||
|
retValSize = i;
|
||
|
inWords.pos += i;
|
||
|
|
||
|
if (retVal == "NIL") {
|
||
|
retVal.truncate(0);
|
||
|
retValSize = 0;
|
||
|
}
|
||
|
skipWS (inWords);
|
||
|
if (outLen) {
|
||
|
*outLen = retValSize;
|
||
|
}
|
||
|
return retVal;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
bool imapParser::parseOneNumber (parseString & inWords, ulong & num)
|
||
|
{
|
||
|
bool valid;
|
||
|
num = parseOneWordC(inWords, TRUE).toULong(&valid);
|
||
|
return valid;
|
||
|
}
|
||
|
|
||
14 years ago
|
bool imapParser::hasCapability (const TQString & cap)
|
||
15 years ago
|
{
|
||
14 years ago
|
TQString c = cap.lower();
|
||
15 years ago
|
// kdDebug(7116) << "imapParser::hasCapability - Looking for '" << cap << "'" << endl;
|
||
14 years ago
|
for (TQStringList::ConstIterator it = imapCapabilities.begin ();
|
||
15 years ago
|
it != imapCapabilities.end (); ++it)
|
||
|
{
|
||
|
// kdDebug(7116) << "imapParser::hasCapability - Examining '" << (*it) << "'" << endl;
|
||
|
if ( !(kasciistricmp(c.ascii(), (*it).ascii())) )
|
||
|
{
|
||
|
return true;
|
||
|
}
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
14 years ago
|
void imapParser::removeCapability (const TQString & cap)
|
||
15 years ago
|
{
|
||
|
imapCapabilities.remove(cap.lower());
|
||
|
}
|
||
|
|
||
14 years ago
|
TQString imapParser::namespaceForBox( const TQString & box )
|
||
15 years ago
|
{
|
||
|
kdDebug(7116) << "imapParse::namespaceForBox " << box << endl;
|
||
14 years ago
|
TQString myNamespace;
|
||
15 years ago
|
if ( !box.isEmpty() )
|
||
|
{
|
||
14 years ago
|
TQValueList<TQString> list = namespaceToDelimiter.keys();
|
||
|
TQString cleanPrefix;
|
||
|
for ( TQValueList<TQString>::Iterator it = list.begin(); it != list.end(); ++it )
|
||
15 years ago
|
{
|
||
13 years ago
|
if ( !(*it).isEmpty() && box.find( *it ) != -1 )
|
||
15 years ago
|
return (*it);
|
||
|
}
|
||
|
}
|
||
|
return myNamespace;
|
||
|
}
|
||
|
|