tdeioslave/imap4

imapinfo.cpp
1 /**********************************************************************
2  *
3  * imapinfo.cpp - IMAP4rev1 SELECT / EXAMINE handler
4  * Copyright (C) 2000 Sven Carstens <s.carstens@gmx.de>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * Send comments and bug fixes to
21  *
22  *********************************************************************/
23 
24 /*
25  References:
26  RFC 2060 - Internet Message Access Protocol - Version 4rev1 - December 1996
27  RFC 2192 - IMAP URL Scheme - September 1997
28  RFC 1731 - IMAP Authentication Mechanisms - December 1994
29  (Discusses KERBEROSv4, GSSAPI, and S/Key)
30  RFC 2195 - IMAP/POP AUTHorize Extension for Simple Challenge/Response
31  - September 1997 (CRAM-MD5 authentication method)
32  RFC 2104 - HMAC: Keyed-Hashing for Message Authentication - February 1997
33 
34  Supported URLs:
35  imap://server/ - Prompt for user/pass, list all folders in home directory
36  imap://user:pass@server/ - Uses LOGIN to log in
37  imap://user;AUTH=method:pass@server/ - Uses AUTHENTICATE to log in
38 
39  imap://server/folder/ - List messages in folder
40  */
41 
42 #include "imapinfo.h"
43 #include "imapparser.h"
44 
45 #include <kdebug.h>
46 
47 imapInfo::imapInfo ():count_ (0),
48 recent_ (0),
49 unseen_ (0),
50 uidValidity_ (0),
51 uidNext_ (0),
52 flags_ (0),
53 permanentFlags_ (0),
54 readWrite_ (false),
55 countAvailable_ (false),
56 recentAvailable_ (false),
57 unseenAvailable_ (false),
58 uidValidityAvailable_ (false),
59 uidNextAvailable_ (false),
60 flagsAvailable_ (false),
61 permanentFlagsAvailable_ (false), readWriteAvailable_ (false)
62 {
63 }
64 
65 imapInfo::imapInfo (const imapInfo & mi):count_ (mi.count_),
66 recent_ (mi.recent_),
67 unseen_ (mi.unseen_),
68 uidValidity_ (mi.uidValidity_),
69 uidNext_ (mi.uidNext_),
70 flags_ (mi.flags_),
71 permanentFlags_ (mi.permanentFlags_),
72 readWrite_ (mi.readWrite_),
73 countAvailable_ (mi.countAvailable_),
74 recentAvailable_ (mi.recentAvailable_),
75 unseenAvailable_ (mi.unseenAvailable_),
76 uidValidityAvailable_ (mi.uidValidityAvailable_),
77 uidNextAvailable_ (mi.uidNextAvailable_),
78 flagsAvailable_ (mi.flagsAvailable_),
79 permanentFlagsAvailable_ (mi.permanentFlagsAvailable_),
80 readWriteAvailable_ (mi.readWriteAvailable_)
81 {
82 }
83 
84 imapInfo & imapInfo::operator = (const imapInfo & mi)
85 {
86  // Avoid a = a.
87  if (this == &mi)
88  return *this;
89 
90  count_ = mi.count_;
91  recent_ = mi.recent_;
92  unseen_ = mi.unseen_;
93  uidValidity_ = mi.uidValidity_;
94  uidNext_ = mi.uidNext_;
95  flags_ = mi.flags_;
96  permanentFlags_ = mi.permanentFlags_;
97  readWrite_ = mi.readWrite_;
98  countAvailable_ = mi.countAvailable_;
99  recentAvailable_ = mi.recentAvailable_;
100  unseenAvailable_ = mi.unseenAvailable_;
101  uidValidityAvailable_ = mi.uidValidityAvailable_;
102  uidNextAvailable_ = mi.uidNextAvailable_;
103  flagsAvailable_ = mi.flagsAvailable_;
104  permanentFlagsAvailable_ = mi.permanentFlagsAvailable_;
105  readWriteAvailable_ = mi.readWriteAvailable_;
106 
107  return *this;
108 }
109 
110 imapInfo::imapInfo (const TQStringList & list):count_ (0),
111 recent_ (0),
112 unseen_ (0),
113 uidValidity_ (0),
114 uidNext_ (0),
115 flags_ (0),
116 permanentFlags_ (0),
117 readWrite_ (false),
118 countAvailable_ (false),
119 recentAvailable_ (false),
120 unseenAvailable_ (false),
121 uidValidityAvailable_ (false),
122 uidNextAvailable_ (false),
123 flagsAvailable_ (false),
124 permanentFlagsAvailable_ (false), readWriteAvailable_ (false)
125 {
126  for (TQStringList::ConstIterator it (list.begin ()); it != list.end (); ++it)
127  {
128  TQString line (*it);
129 
130  line.truncate(line.length() - 2);
131  TQStringList tokens(TQStringList::split (' ', line));
132 
133  kdDebug(7116) << "Processing: " << line << endl;
134  if (tokens[0] != "*")
135  continue;
136 
137  if (tokens[1] == "OK")
138  {
139  if (tokens[2] == "[UNSEEN")
140  setUnseen (tokens[3].left (tokens[3].length () - 1).toULong ());
141 
142  else if (tokens[2] == "[UIDVALIDITY")
143  setUidValidity (tokens[3].left (tokens[3].length () - 1).toULong ());
144 
145  else if (tokens[2] == "[UIDNEXT")
146  setUidNext (tokens[3].left (tokens[3].length () - 1).toULong ());
147 
148  else if (tokens[2] == "[PERMANENTFLAGS")
149  {
150  int flagsStart = line.find('(');
151  int flagsEnd = line.find(')');
152 
153  kdDebug(7116) << "Checking permFlags from " << flagsStart << " to " << flagsEnd << endl;
154  if ((-1 != flagsStart) && (-1 != flagsEnd) && flagsStart < flagsEnd)
155  setPermanentFlags (_flags (line.mid (flagsStart, flagsEnd).latin1()));
156 
157  }
158  else if (tokens[2] == "[READ-WRITE")
159  {
160  setReadWrite (true);
161  }
162  else if (tokens[2] == "[READ-ONLY")
163  {
164  setReadWrite (false);
165  }
166  else
167  {
168  kdDebug(7116) << "unknown token2: " << tokens[2] << endl;
169  }
170  }
171  else if (tokens[1] == "FLAGS")
172  {
173  int flagsStart = line.find ('(');
174  int flagsEnd = line.find (')');
175 
176  if ((-1 != flagsStart) && (-1 != flagsEnd) && flagsStart < flagsEnd)
177  setFlags (_flags (line.mid (flagsStart, flagsEnd).latin1() ));
178  }
179  else
180  {
181  if (tokens[2] == "EXISTS")
182  setCount (tokens[1].toULong ());
183 
184  else if (tokens[2] == "RECENT")
185  setRecent (tokens[1].toULong ());
186 
187  else
188  kdDebug(7116) << "unknown token1/2: " << tokens[1] << " " << tokens[2] << endl;
189  }
190  }
191 
192 }
193 
194 ulong imapInfo::_flags (const TQCString & inFlags)
195 {
196  ulong flags = 0;
197  parseString flagsString;
198  flagsString.data.duplicate(inFlags.data(), inFlags.length());
199  if ( flagsString.isEmpty() ) {
200  return flags;
201  }
202 
203  if (flagsString[0] == '(')
204  flagsString.pos++;
205 
206  while (!flagsString.isEmpty () && flagsString[0] != ')')
207  {
208  TQCString entry = imapParser::parseOneWordC(flagsString).upper();
209 
210  if (entry.isEmpty ())
211  flagsString.clear();
212  else if (0 != entry.contains ("\\SEEN"))
213  flags ^= Seen;
214  else if (0 != entry.contains ("\\ANSWERED"))
215  flags ^= Answered;
216  else if (0 != entry.contains ("\\FLAGGED"))
217  flags ^= Flagged;
218  else if (0 != entry.contains ("\\DELETED"))
219  flags ^= Deleted;
220  else if (0 != entry.contains ("\\DRAFT"))
221  flags ^= Draft;
222  else if (0 != entry.contains ("\\RECENT"))
223  flags ^= Recent;
224  else if (0 != entry.contains ("\\*"))
225  flags ^= User;
226 
227  // non standard kmail falgs
228  else if ( entry.contains( "KMAILFORWARDED" ) || entry.contains( "$FORWARDED" ) )
229  flags = flags | Forwarded;
230  else if ( entry.contains( "KMAILTODO" ) || entry.contains( "$TODO" ) )
231  flags = flags | Todo;
232  else if ( entry.contains( "KMAILWATCHED" ) || entry.contains( "$WATCHED" ) )
233  flags = flags | Watched;
234  else if ( entry.contains( "KMAILIGNORED" ) || entry.contains( "$IGNORED" ) )
235  flags = flags | Ignored;
236  }
237 
238  return flags;
239 }
a string used during parsing the string allows you to move the effective start of the string using st...
Definition: imapparser.h:53