tdeioslave/imap4

imapparser.h
1 #ifndef _IMAPPARSER_H
2 #define _IMAPPARSER_H
3 /**********************************************************************
4  *
5  * imapparser.h - IMAP4rev1 Parser
6  * Copyright (C) 2001-2002 Michael Haeckel <haeckel@kde.org>
7  * Copyright (C) 2000 s.carstens@gmx.de
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22  *
23  * Send comments and bug fixes to s.carstens@gmx.de
24  *
25  *********************************************************************/
26 
27 #include <tqstringlist.h>
28 #include <tqvaluelist.h>
29 #include <tqptrlist.h>
30 #include <tqasciidict.h>
31 
32 #include <tdeio/authinfo.h>
33 #include <tdeio/slavebase.h>
34 
35 #include "imaplist.h"
36 #include "imapcommand.h"
37 #include "imapinfo.h"
38 
39 #include "mailheader.h"
40 
41 class KURL;
42 class TQString;
43 class mailAddress;
44 class mimeHeader;
45 
46 
53 {
54 public:
55  parseString() { pos = 0; }
56  char operator[](uint i) const { return data[i + pos]; }
57  bool isEmpty() const { return pos >= data.size(); }
58  TQCString cstr() const
59  {
60  if (pos >= data.size()) return TQCString();
61  return TQCString(data.data() + pos, data.size() - pos + 1);
62  }
63  int find(char c, int index = 0)
64  {
65  int res = data.find(c, index + pos);
66  return (res == -1) ? res : (res - pos);
67  }
68  // Warning: does not check for going past end of "data"
69  void takeLeft(TQCString& dest, uint len) const
70  {
71  dest.resize(len + 1);
72  tqmemmove(dest.data(), data.data() + pos, len);
73  }
74  // Warning: does not check for going past end of "data"
75  void takeLeftNoResize(TQCString& dest, uint len) const
76  {
77  tqmemmove(dest.data(), data.data() + pos, len);
78  }
79  // Warning: does not check for going past end of "data"
80  void takeMid(TQCString& dest, uint start, uint len) const
81  {
82  dest.resize(len + 1);
83  tqmemmove(dest.data(), data.data() + pos + start, len);
84  }
85  // Warning: does not check for going past end of "data"
86  void takeMidNoResize(TQCString& dest, uint start, uint len) const
87  {
88  tqmemmove(dest.data(), data.data() + pos + start, len);
89  }
90  void clear()
91  {
92  data.resize(0);
93  pos = 0;
94  }
95  uint length()
96  {
97  if( pos < data.size() ) {
98  return data.size() - pos;
99  } else {
100  return 0;
101  }
102  }
103  void fromString(const TQString &s)
104  {
105  clear();
106  data.duplicate(s.latin1(), s.length());
107  }
108  TQByteArray data;
109  uint pos;
110 };
111 
112 class imapCache
113 {
114 public:
115  imapCache ()
116  {
117  myHeader = NULL;
118  mySize = 0;
119  myFlags = 0;
120  myUid = 0;
121  }
122 
123  ~imapCache ()
124  {
125  if (myHeader) delete myHeader;
126  }
127 
128  mailHeader *getHeader ()
129  {
130  return myHeader;
131  }
132  void setHeader (mailHeader * inHeader)
133  {
134  myHeader = inHeader;
135  }
136 
137  ulong getSize ()
138  {
139  return mySize;
140  }
141  void setSize (ulong inSize)
142  {
143  mySize = inSize;
144  }
145 
146  ulong getUid ()
147  {
148  return myUid;
149  }
150  void setUid (ulong inUid)
151  {
152  myUid = inUid;
153  }
154 
155  ulong getFlags ()
156  {
157  return myFlags;
158  }
159  void setFlags (ulong inFlags)
160  {
161  myFlags = inFlags;
162  }
163 
164  TQCString getDate ()
165  {
166  return myDate;
167  }
168  void setDate (const TQCString & _str)
169  {
170  myDate = _str;
171  }
172  void clear()
173  {
174  if (myHeader) delete myHeader;
175  myHeader = NULL;
176  mySize = 0;
177  myFlags = 0;
178  myDate = TQCString();
179  myUid = 0;
180  }
181 
182 protected:
183  mailHeader * myHeader;
184  ulong mySize;
185  ulong myFlags;
186  ulong myUid;
187  TQCString myDate;
188 };
189 
190 
191 class imapParser
192 {
193 
194 public:
195 
197  enum IMAP_STATE
198  {
199  ISTATE_NO,
200  ISTATE_CONNECT,
201  ISTATE_LOGIN,
202  ISTATE_SELECT
203  };
204 
205 public:
206  imapParser ();
207  virtual ~ imapParser ();
208 
210  enum IMAP_STATE getState () { return currentState; }
212  void setState(enum IMAP_STATE state) { currentState = state; }
213 
214  /* @brief return the currently selected mailbox */
215  const TQString getCurrentBox ()
216  {
217  return rfcDecoder::fromIMAP(currentBox);
218  };
219 
225  imapCommand *sendCommand (imapCommand * aCmd);
231  imapCommand *doCommand (imapCommand * aCmd);
232 
233 
241  bool clientLogin (const TQString & aUser, const TQString & aPass, TQString & resultInfo);
251  bool clientAuthenticate (TDEIO::SlaveBase *slave, TDEIO::AuthInfo &ai, const TQString & aFTQDN,
252  const TQString & aAuth, bool isSSL, TQString & resultInfo);
253 
258  int parseLoop ();
259 
264  void parseUntagged (parseString & result);
265 
267  void parseRecent (ulong value, parseString & result);
269  void parseResult (TQByteArray & result, parseString & rest,
270  const TQString & command = TQString());
272  void parseCapability (parseString & result);
274  void parseFlags (parseString & result);
276  void parseList (parseString & result);
278  void parseLsub (parseString & result);
280  void parseListRights (parseString & result);
282  void parseMyRights (parseString & result);
284  void parseSearch (parseString & result);
286  void parsetStatus (parseString & result);
288  void parseExists (ulong value, parseString & result);
290  void parseExpunge (ulong value, parseString & result);
292  void parseAcl (parseString & result);
294  void parseAnnotation (parseString & result);
296  void parseNamespace (parseString & result);
298  void parseQuotaRoot (parseString & result);
300  void parseQuota (parseString & result);
302  void parseCustom (parseString & result);
304  void parseOtherUser (parseString & result);
306  void parseDelegate (parseString & result);
308  void parseOutOfOffice (parseString & result);
309 
314  void parseFetch (ulong value, parseString & inWords);
315 
317  mailHeader *parseEnvelope (parseString & inWords);
319  void parseAddressList (parseString & inWords, TQPtrList<mailAddress>& list);
321  const mailAddress& parseAddress (parseString & inWords, mailAddress& buffer);
322 
324  void parseBody (parseString & inWords);
325 
327  mimeHeader *parseBodyStructure (parseString & inWords,
328  TQString & section, mimeHeader * inHeader = 0);
329 
331  mimeHeader *parseSimplePart (parseString & inWords, TQString & section,
332  mimeHeader * localPart = 0);
333 
335  TQAsciiDict < TQString > parseParameters (parseString & inWords);
336 
341  TQAsciiDict < TQString > parseDisposition (parseString & inWords);
342 
343  // reimplement these
344 
346  virtual void parseRelay (const TQByteArray & buffer);
347 
350  virtual void parseRelay (ulong);
351 
353  virtual bool parseRead (TQByteArray & buffer, ulong len, ulong relay = 0);
354 
356  virtual bool parseReadLine (TQByteArray & buffer, ulong relay = 0);
357 
359  virtual void parseWriteLine (const TQString &);
360 
361  // generic parser routines
362 
364  void parseSentence (parseString & inWords);
365 
367  TQCString parseLiteralC(parseString & inWords, bool relay = false,
368  bool stopAtBracket = false, int *outlen = 0);
369  inline TQByteArray parseLiteral (parseString & inWords, bool relay = false,
370  bool stopAtBracket = false) {
371  int len = 0; // string size
372  // Choice: we can create an extra TQCString, or we can get the buffer in
373  // the wrong size to start. Let's try option b.
374  TQCString tmp = parseLiteralC(inWords, relay, stopAtBracket, &len);
375  return TQByteArray().duplicate(tmp.data(), len);
376  }
377 
378  // static parser routines, can be used elsewhere
379 
380  static TQCString b2c(const TQByteArray &ba)
381  { return TQCString(ba.data(), ba.size() + 1); }
382 
384  static TQCString parseOneWordC (parseString & inWords,
385  bool stopAtBracket = FALSE, int *len = 0);
386 
388  static bool parseOneNumber (parseString & inWords, ulong & num);
389 
391  static void parseURL (const KURL & _url, TQString & _box, TQString & _section,
392  TQString & _type, TQString & _uid, TQString & _validity,
393  TQString & _info);
394 
395 
399  imapCache *getLastHandled ()
400  {
401  return lastHandled;
402  };
403 
405  const TQStringList & getResults ()
406  {
407  return lastResults;
408  };
409 
411  const imapInfo & geStatus ()
412  {
413  return lasStatus;
414  };
416  const imapInfo & getSelected ()
417  {
418  return selectInfo;
419  };
420 
421  const TQByteArray & getContinuation ()
422  {
423  return continuation;
424  };
425 
427  bool hasCapability (const TQString &);
428 
429  void removeCapability (const TQString & cap);
430 
431  static inline void skipWS (parseString & inWords)
432  {
433  char c;
434  while (!inWords.isEmpty() &&
435  ((c = inWords[0]) == ' ' || c == '\t' || c == '\r' || c == '\n'))
436  {
437  inWords.pos++;
438  }
439  }
440 
442  TQString namespaceForBox( const TQString & box );
443 
444 
445 protected:
446 
448  enum IMAP_STATE currentState;
449 
451  TQString currentBox;
452 
454  imapInfo selectInfo;
455 
457  imapInfo lasStatus;
458 
460  TQStringList imapCapabilities;
461 
463  TQValueList < imapList > listResponses;
464 
466  TQPtrList < imapCommand > sentQueue; // no autodelete
467  TQPtrList < imapCommand > completeQueue; // autodelete !!
468 
472  TQStringList unhandled;
473 
475  TQByteArray continuation;
476 
478  TQString seenUid;
479  imapCache *lastHandled;
480 
481  ulong commandCounter;
482 
484  TQStringList lastResults;
485 
490  TQMap<TQString, TQString> namespaceToDelimiter;
491 
496  TQStringList imapNamespaces;
497 
498 private:
499 
501  imapParser & operator = (const imapParser &); // hide the copy ctor
502 
503 };
504 #endif
encapulate a IMAP command
Definition: imapcommand.h:38
a string used during parsing the string allows you to move the effective start of the string using st...
Definition: imapparser.h:53
static TQString fromIMAP(const TQString &src)
Convert an IMAP mailbox to a Unicode path.
Definition: rfcdecoder.cpp:55