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.
505 lines
14 KiB
505 lines
14 KiB
15 years ago
|
#ifndef _IMAPPARSER_H
|
||
|
#define _IMAPPARSER_H
|
||
|
/**********************************************************************
|
||
|
*
|
||
|
* imapparser.h - IMAP4rev1 Parser
|
||
|
* 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
|
||
|
*
|
||
|
*********************************************************************/
|
||
|
|
||
15 years ago
|
#include <tqstringlist.h>
|
||
|
#include <tqvaluelist.h>
|
||
|
#include <tqptrlist.h>
|
||
|
#include <tqasciidict.h>
|
||
15 years ago
|
|
||
12 years ago
|
#include <tdeio/authinfo.h>
|
||
|
#include <tdeio/slavebase.h>
|
||
15 years ago
|
|
||
|
#include "imaplist.h"
|
||
|
#include "imapcommand.h"
|
||
|
#include "imapinfo.h"
|
||
|
|
||
|
#include "mailheader.h"
|
||
|
|
||
|
class KURL;
|
||
15 years ago
|
class TQString;
|
||
15 years ago
|
class mailAddress;
|
||
|
class mimeHeader;
|
||
|
|
||
|
|
||
|
/** @brief a string used during parsing
|
||
|
* the string allows you to move the effective start of the string using
|
||
|
* str.pos++ and str.pos--.
|
||
|
* @bug it is possible to move past the beginning and end of the string
|
||
|
*/
|
||
|
class parseString
|
||
|
{
|
||
|
public:
|
||
|
parseString() { pos = 0; }
|
||
|
char operator[](uint i) const { return data[i + pos]; }
|
||
|
bool isEmpty() const { return pos >= data.size(); }
|
||
15 years ago
|
TQCString cstr() const
|
||
15 years ago
|
{
|
||
15 years ago
|
if (pos >= data.size()) return TQCString();
|
||
|
return TQCString(data.data() + pos, data.size() - pos + 1);
|
||
15 years ago
|
}
|
||
13 years ago
|
int find(char c, int index = 0)
|
||
15 years ago
|
{
|
||
13 years ago
|
int res = data.find(c, index + pos);
|
||
15 years ago
|
return (res == -1) ? res : (res - pos);
|
||
|
}
|
||
|
// Warning: does not check for going past end of "data"
|
||
15 years ago
|
void takeLeft(TQCString& dest, uint len) const
|
||
15 years ago
|
{
|
||
|
dest.resize(len + 1);
|
||
14 years ago
|
tqmemmove(dest.data(), data.data() + pos, len);
|
||
15 years ago
|
}
|
||
|
// Warning: does not check for going past end of "data"
|
||
15 years ago
|
void takeLeftNoResize(TQCString& dest, uint len) const
|
||
15 years ago
|
{
|
||
14 years ago
|
tqmemmove(dest.data(), data.data() + pos, len);
|
||
15 years ago
|
}
|
||
|
// Warning: does not check for going past end of "data"
|
||
15 years ago
|
void takeMid(TQCString& dest, uint start, uint len) const
|
||
15 years ago
|
{
|
||
|
dest.resize(len + 1);
|
||
14 years ago
|
tqmemmove(dest.data(), data.data() + pos + start, len);
|
||
15 years ago
|
}
|
||
|
// Warning: does not check for going past end of "data"
|
||
15 years ago
|
void takeMidNoResize(TQCString& dest, uint start, uint len) const
|
||
15 years ago
|
{
|
||
14 years ago
|
tqmemmove(dest.data(), data.data() + pos + start, len);
|
||
15 years ago
|
}
|
||
|
void clear()
|
||
|
{
|
||
|
data.resize(0);
|
||
|
pos = 0;
|
||
|
}
|
||
|
uint length()
|
||
|
{
|
||
12 years ago
|
if( pos < data.size() ) {
|
||
|
return data.size() - pos;
|
||
|
} else {
|
||
|
return 0;
|
||
|
}
|
||
15 years ago
|
}
|
||
15 years ago
|
void fromString(const TQString &s)
|
||
15 years ago
|
{
|
||
|
clear();
|
||
|
data.duplicate(s.latin1(), s.length());
|
||
|
}
|
||
15 years ago
|
TQByteArray data;
|
||
15 years ago
|
uint pos;
|
||
|
};
|
||
|
|
||
|
class imapCache
|
||
|
{
|
||
|
public:
|
||
|
imapCache ()
|
||
|
{
|
||
|
myHeader = NULL;
|
||
|
mySize = 0;
|
||
|
myFlags = 0;
|
||
|
myUid = 0;
|
||
|
}
|
||
|
|
||
|
~imapCache ()
|
||
|
{
|
||
|
if (myHeader) delete myHeader;
|
||
|
}
|
||
|
|
||
|
mailHeader *getHeader ()
|
||
|
{
|
||
|
return myHeader;
|
||
|
}
|
||
|
void setHeader (mailHeader * inHeader)
|
||
|
{
|
||
|
myHeader = inHeader;
|
||
|
}
|
||
|
|
||
|
ulong getSize ()
|
||
|
{
|
||
|
return mySize;
|
||
|
}
|
||
|
void setSize (ulong inSize)
|
||
|
{
|
||
|
mySize = inSize;
|
||
|
}
|
||
|
|
||
|
ulong getUid ()
|
||
|
{
|
||
|
return myUid;
|
||
|
}
|
||
|
void setUid (ulong inUid)
|
||
|
{
|
||
|
myUid = inUid;
|
||
|
}
|
||
|
|
||
|
ulong getFlags ()
|
||
|
{
|
||
|
return myFlags;
|
||
|
}
|
||
|
void setFlags (ulong inFlags)
|
||
|
{
|
||
|
myFlags = inFlags;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQCString getDate ()
|
||
15 years ago
|
{
|
||
|
return myDate;
|
||
|
}
|
||
15 years ago
|
void setDate (const TQCString & _str)
|
||
15 years ago
|
{
|
||
|
myDate = _str;
|
||
|
}
|
||
|
void clear()
|
||
|
{
|
||
|
if (myHeader) delete myHeader;
|
||
|
myHeader = NULL;
|
||
|
mySize = 0;
|
||
|
myFlags = 0;
|
||
15 years ago
|
myDate = TQCString();
|
||
15 years ago
|
myUid = 0;
|
||
|
}
|
||
|
|
||
|
protected:
|
||
|
mailHeader * myHeader;
|
||
|
ulong mySize;
|
||
|
ulong myFlags;
|
||
|
ulong myUid;
|
||
15 years ago
|
TQCString myDate;
|
||
15 years ago
|
};
|
||
|
|
||
|
|
||
|
class imapParser
|
||
|
{
|
||
|
|
||
|
public:
|
||
|
|
||
|
/** the different states the client can be in */
|
||
|
enum IMAP_STATE
|
||
|
{
|
||
|
ISTATE_NO, /**< Not connected */
|
||
|
ISTATE_CONNECT, /**< Connected but not logged in */
|
||
|
ISTATE_LOGIN, /**< Logged in */
|
||
|
ISTATE_SELECT /**< A folder is currently selected */
|
||
|
};
|
||
|
|
||
|
public:
|
||
|
imapParser ();
|
||
|
virtual ~ imapParser ();
|
||
|
|
||
|
/** @brief Get the current state */
|
||
|
enum IMAP_STATE getState () { return currentState; }
|
||
|
/** @brief Set the current state */
|
||
|
void setState(enum IMAP_STATE state) { currentState = state; }
|
||
|
|
||
|
/* @brief return the currently selected mailbox */
|
||
15 years ago
|
const TQString getCurrentBox ()
|
||
15 years ago
|
{
|
||
|
return rfcDecoder::fromIMAP(currentBox);
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* @brief do setup and send the command to parseWriteLine
|
||
|
* @param aCmd The command to perform
|
||
|
* @return The completed command
|
||
|
*/
|
||
|
imapCommand *sendCommand (imapCommand * aCmd);
|
||
|
/**
|
||
|
* @brief perform a command and wait to parse the result
|
||
|
* @param aCmd The command to perform
|
||
|
* @return The completed command
|
||
|
*/
|
||
|
imapCommand *doCommand (imapCommand * aCmd);
|
||
|
|
||
|
|
||
|
/**
|
||
|
* @brief plaintext login
|
||
|
* @param aUser Username
|
||
|
* @param aPass Password
|
||
|
* @param resultInfo The resultinfo from the command
|
||
|
* @return success or failure
|
||
|
*/
|
||
15 years ago
|
bool clientLogin (const TQString & aUser, const TQString & aPass, TQString & resultInfo);
|
||
15 years ago
|
/**
|
||
|
* @brief non-plaintext login
|
||
|
* @param aUser Username
|
||
|
* @param aPass Password
|
||
|
* @param aAuth authentication method
|
||
|
* @param isSSL are we using SSL
|
||
|
* @param resultInfo The resultinfo from the command
|
||
|
* @return success or failure
|
||
|
*/
|
||
12 years ago
|
bool clientAuthenticate (TDEIO::SlaveBase *slave, TDEIO::AuthInfo &ai, const TQString & aFTQDN,
|
||
15 years ago
|
const TQString & aAuth, bool isSSL, TQString & resultInfo);
|
||
15 years ago
|
|
||
|
/**
|
||
|
* main loop for the parser
|
||
|
* reads one line and dispatches it to the appropriate sub parser
|
||
|
*/
|
||
|
int parseLoop ();
|
||
|
|
||
|
/**
|
||
|
* @brief parses all untagged responses and passes them on to the
|
||
|
* following parsers
|
||
|
*/
|
||
|
void parseUntagged (parseString & result);
|
||
|
|
||
|
/** @brief parse a RECENT line */
|
||
|
void parseRecent (ulong value, parseString & result);
|
||
|
/** @brief parse a RESULT line */
|
||
15 years ago
|
void parseResult (TQByteArray & result, parseString & rest,
|
||
14 years ago
|
const TQString & command = TQString());
|
||
15 years ago
|
/** @brief parse a CAPABILITY line */
|
||
|
void parseCapability (parseString & result);
|
||
|
/** @brief parse a FLAGS line */
|
||
|
void parseFlags (parseString & result);
|
||
|
/** @brief parse a LIST line */
|
||
|
void parseList (parseString & result);
|
||
|
/** @brief parse a LSUB line */
|
||
|
void parseLsub (parseString & result);
|
||
|
/** @brief parse a LISTRIGHTS line */
|
||
|
void parseListRights (parseString & result);
|
||
|
/** @brief parse a MYRIGHTS line */
|
||
|
void parseMyRights (parseString & result);
|
||
|
/** @brief parse a SEARCH line */
|
||
|
void parseSearch (parseString & result);
|
||
|
/** @brief parse a STATUS line */
|
||
13 years ago
|
void parsetStatus (parseString & result);
|
||
15 years ago
|
/** @brief parse a EXISTS line */
|
||
|
void parseExists (ulong value, parseString & result);
|
||
|
/** @brief parse a EXPUNGE line */
|
||
|
void parseExpunge (ulong value, parseString & result);
|
||
|
/** @brief parse a ACL line */
|
||
|
void parseAcl (parseString & result);
|
||
|
/** @brief parse a ANNOTATION line */
|
||
|
void parseAnnotation (parseString & result);
|
||
|
/** @brief parse a NAMESPACE line */
|
||
|
void parseNamespace (parseString & result);
|
||
13 years ago
|
/** @brief parse a QUOTAROOT line */
|
||
15 years ago
|
void parseQuotaRoot (parseString & result);
|
||
13 years ago
|
/** @brief parse a QUOTA line */
|
||
15 years ago
|
void parseQuota (parseString & result);
|
||
|
/** @brief parse a custom command line */
|
||
|
void parseCustom (parseString & result);
|
||
|
/** @brief parse a OTHER-USER line */
|
||
|
void parseOtherUser (parseString & result);
|
||
|
/** @brief parse a DELEGATE line */
|
||
|
void parseDelegate (parseString & result);
|
||
|
/** @brief parse a OUT-OF-OFFICE line */
|
||
|
void parseOutOfOffice (parseString & result);
|
||
|
|
||
|
/**
|
||
|
* parses the results of a fetch command
|
||
|
* processes it with the following sub parsers
|
||
|
*/
|
||
|
void parseFetch (ulong value, parseString & inWords);
|
||
|
|
||
|
/** read a envelope from imap and parse the addresses */
|
||
|
mailHeader *parseEnvelope (parseString & inWords);
|
||
|
/** @brief parse an address list and return a list of addresses */
|
||
15 years ago
|
void parseAddressList (parseString & inWords, TQPtrList<mailAddress>& list);
|
||
15 years ago
|
/** @brief parse an address and return the ref again */
|
||
|
const mailAddress& parseAddress (parseString & inWords, mailAddress& buffer);
|
||
|
|
||
|
/** parse the result of the body command */
|
||
|
void parseBody (parseString & inWords);
|
||
|
|
||
|
/** parse the body structure recursively */
|
||
|
mimeHeader *parseBodyStructure (parseString & inWords,
|
||
15 years ago
|
TQString & section, mimeHeader * inHeader = 0);
|
||
15 years ago
|
|
||
|
/** parse only one not nested part */
|
||
15 years ago
|
mimeHeader *parseSimplePart (parseString & inWords, TQString & section,
|
||
15 years ago
|
mimeHeader * localPart = 0);
|
||
|
|
||
|
/** parse a parameter list (name value pairs) */
|
||
15 years ago
|
TQAsciiDict < TQString > parseParameters (parseString & inWords);
|
||
15 years ago
|
|
||
|
/**
|
||
|
* parse the disposition list (disposition (name value pairs))
|
||
|
* the disposition has the key 'content-disposition'
|
||
|
*/
|
||
15 years ago
|
TQAsciiDict < TQString > parseDisposition (parseString & inWords);
|
||
15 years ago
|
|
||
|
// reimplement these
|
||
|
|
||
|
/** relay hook to send the fetched data directly to an upper level */
|
||
15 years ago
|
virtual void parseRelay (const TQByteArray & buffer);
|
||
15 years ago
|
|
||
|
/** relay hook to announce the fetched data directly to an upper level
|
||
|
*/
|
||
|
virtual void parseRelay (ulong);
|
||
|
|
||
|
/** read at least len bytes */
|
||
15 years ago
|
virtual bool parseRead (TQByteArray & buffer, ulong len, ulong relay = 0);
|
||
15 years ago
|
|
||
|
/** read at least a line (up to CRLF) */
|
||
15 years ago
|
virtual bool parseReadLine (TQByteArray & buffer, ulong relay = 0);
|
||
15 years ago
|
|
||
|
/** write argument to server */
|
||
15 years ago
|
virtual void parseWriteLine (const TQString &);
|
||
15 years ago
|
|
||
|
// generic parser routines
|
||
|
|
||
14 years ago
|
/** parse a parenthesized list */
|
||
15 years ago
|
void parseSentence (parseString & inWords);
|
||
|
|
||
|
/** parse a literal or word, may require more data */
|
||
15 years ago
|
TQCString parseLiteralC(parseString & inWords, bool relay = false,
|
||
15 years ago
|
bool stopAtBracket = false, int *outlen = 0);
|
||
15 years ago
|
inline TQByteArray parseLiteral (parseString & inWords, bool relay = false,
|
||
15 years ago
|
bool stopAtBracket = false) {
|
||
|
int len = 0; // string size
|
||
15 years ago
|
// Choice: we can create an extra TQCString, or we can get the buffer in
|
||
15 years ago
|
// the wrong size to start. Let's try option b.
|
||
15 years ago
|
TQCString tmp = parseLiteralC(inWords, relay, stopAtBracket, &len);
|
||
|
return TQByteArray().duplicate(tmp.data(), len);
|
||
15 years ago
|
}
|
||
|
|
||
|
// static parser routines, can be used elsewhere
|
||
|
|
||
15 years ago
|
static TQCString b2c(const TQByteArray &ba)
|
||
|
{ return TQCString(ba.data(), ba.size() + 1); }
|
||
15 years ago
|
|
||
|
/** parse one word (maybe quoted) upto next space " ) ] } */
|
||
15 years ago
|
static TQCString parseOneWordC (parseString & inWords,
|
||
15 years ago
|
bool stopAtBracket = FALSE, int *len = 0);
|
||
|
|
||
|
/** parse one number using parseOneWord */
|
||
|
static bool parseOneNumber (parseString & inWords, ulong & num);
|
||
|
|
||
|
/** extract the box,section,list type, uid, uidvalidity,info from an url */
|
||
15 years ago
|
static void parseURL (const KURL & _url, TQString & _box, TQString & _section,
|
||
|
TQString & _type, TQString & _uid, TQString & _validity,
|
||
|
TQString & _info);
|
||
15 years ago
|
|
||
|
|
||
|
/** @brief return the last handled foo
|
||
|
* @todo work out what a foo is
|
||
|
*/
|
||
|
imapCache *getLastHandled ()
|
||
|
{
|
||
|
return lastHandled;
|
||
|
};
|
||
|
|
||
|
/** @brief return the last results */
|
||
15 years ago
|
const TQStringList & getResults ()
|
||
15 years ago
|
{
|
||
|
return lastResults;
|
||
|
};
|
||
|
|
||
|
/** @brief return the last status code */
|
||
13 years ago
|
const imapInfo & geStatus ()
|
||
15 years ago
|
{
|
||
13 years ago
|
return lasStatus;
|
||
15 years ago
|
};
|
||
|
/** return the select info */
|
||
|
const imapInfo & getSelected ()
|
||
|
{
|
||
|
return selectInfo;
|
||
|
};
|
||
|
|
||
15 years ago
|
const TQByteArray & getContinuation ()
|
||
15 years ago
|
{
|
||
|
return continuation;
|
||
|
};
|
||
|
|
||
|
/** @brief see if server has a capability */
|
||
15 years ago
|
bool hasCapability (const TQString &);
|
||
15 years ago
|
|
||
15 years ago
|
void removeCapability (const TQString & cap);
|
||
15 years ago
|
|
||
|
static inline void skipWS (parseString & inWords)
|
||
|
{
|
||
|
char c;
|
||
|
while (!inWords.isEmpty() &&
|
||
|
((c = inWords[0]) == ' ' || c == '\t' || c == '\r' || c == '\n'))
|
||
|
{
|
||
|
inWords.pos++;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/** @brief find the namespace for the given box */
|
||
15 years ago
|
TQString namespaceForBox( const TQString & box );
|
||
15 years ago
|
|
||
|
|
||
|
protected:
|
||
|
|
||
|
/** the current state we're in */
|
||
|
enum IMAP_STATE currentState;
|
||
|
|
||
|
/** the box selected */
|
||
15 years ago
|
TQString currentBox;
|
||
15 years ago
|
|
||
|
/** @brief here we store the result from select/examine and unsolicited updates */
|
||
|
imapInfo selectInfo;
|
||
|
|
||
|
/** @brief the results from the last status command */
|
||
13 years ago
|
imapInfo lasStatus;
|
||
15 years ago
|
|
||
|
/** @brief the results from the capabilities, split at ' ' */
|
||
15 years ago
|
TQStringList imapCapabilities;
|
||
15 years ago
|
|
||
|
/** @brief the results from list/lsub/listrights commands */
|
||
15 years ago
|
TQValueList < imapList > listResponses;
|
||
15 years ago
|
|
||
|
/** @brief queues handling the running commands */
|
||
15 years ago
|
TQPtrList < imapCommand > sentQueue; // no autodelete
|
||
|
TQPtrList < imapCommand > completeQueue; // autodelete !!
|
||
15 years ago
|
|
||
|
/**
|
||
|
* everything we didn't handle, everything but the greeting is bogus
|
||
|
*/
|
||
15 years ago
|
TQStringList unhandled;
|
||
15 years ago
|
|
||
|
/** the last continuation request (there MUST not be more than one pending) */
|
||
15 years ago
|
TQByteArray continuation;
|
||
15 years ago
|
|
||
|
/** the last uid seen while a fetch */
|
||
15 years ago
|
TQString seenUid;
|
||
15 years ago
|
imapCache *lastHandled;
|
||
|
|
||
|
ulong commandCounter;
|
||
|
|
||
|
/** @brief the results from search/acl commands */
|
||
15 years ago
|
TQStringList lastResults;
|
||
15 years ago
|
|
||
|
/**
|
||
|
* @brief namespace prefix - delimiter association
|
||
|
* The namespace is cleaned before so that it does not contain the delimiter
|
||
|
*/
|
||
15 years ago
|
TQMap<TQString, TQString> namespaceToDelimiter;
|
||
15 years ago
|
|
||
|
/**
|
||
|
* @brief list of namespaces in the form: section=namespace=delimiter
|
||
|
* section is 0 (personal), 1 (other users) or 2 (shared)
|
||
|
*/
|
||
15 years ago
|
TQStringList imapNamespaces;
|
||
15 years ago
|
|
||
|
private:
|
||
|
|
||
|
/** we don't want to be able to copy this object */
|
||
|
imapParser & operator = (const imapParser &); // hide the copy ctor
|
||
|
|
||
|
};
|
||
|
#endif
|