tdeioslave/imap4

imapcommand.cpp
1 /**********************************************************************
2  *
3  * imapcommand.cpp - IMAP4rev1 command 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 s.carstens@gmx.de
21  *
22  *********************************************************************/
23 
24 #include "imapcommand.h"
25 #include "rfcdecoder.h"
26 
27 /*#include <stdlib.h>
28 
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <sys/wait.h>
32 #include <sys/stat.h>
33 
34 #include <fcntl.h>
35 
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
38 
39 #include <errno.h>
40 #include <signal.h>
41 #include <stdio.h>
42 #include <netdb.h>
43 #include <unistd.h>
44 #include <stdlib.h>
45 
46 #include <tqregexp.h>
47 #include <tqbuffer.h>
48 
49 #include <tdeprotocolmanager.h>
50 #include <ksock.h>
51 #include <kdebug.h>
52 #include <kinstance.h>
53 #include <tdeio/connection.h>
54 #include <tdeio/slaveinterface.h>
55 #include <tdeio/passdlg.h>
56 #include <tdelocale.h> */
57 
59 {
60  mComplete = false;
61  mId = TQString();
62 }
63 
64 imapCommand::imapCommand (const TQString & command, const TQString & parameter)
65 // aCommand(NULL),
66 // mResult(NULL),
67 // mParameter(NULL)
68 {
69  mComplete = false;
70  aCommand = command;
71  aParameter = parameter;
72  mId = TQString();
73 }
74 
75 bool
77 {
78  return mComplete;
79 }
80 
81 const TQString &
83 {
84  return mResult;
85 }
86 
87 const TQString &
89 {
90  return mResultInfo;
91 }
92 
93 const TQString &
95 {
96  return mId;
97 }
98 
99 const TQString &
101 {
102  return aParameter;
103 }
104 
105 const TQString &
107 {
108  return aCommand;
109 }
110 
111 void
112 imapCommand::setId (const TQString & id)
113 {
114  if (mId.isEmpty ())
115  mId = id;
116 }
117 
118 void
120 {
121  mComplete = true;
122 }
123 
124 void
125 imapCommand::setResult (const TQString & result)
126 {
127  mResult = result;
128 }
129 
130 void
131 imapCommand::setResultInfo (const TQString & result)
132 {
133  mResultInfo = result;
134 }
135 
136 void
137 imapCommand::setCommand (const TQString & command)
138 {
139  aCommand = command;
140 }
141 
142 void
143 imapCommand::setParameter (const TQString & parameter)
144 {
145  aParameter = parameter;
146 }
147 
148 const TQString
150 {
151  if (parameter().isEmpty())
152  return id() + " " + command() + "\r\n";
153  else
154  return id() + " " + command() + " " + parameter() + "\r\n";
155 }
156 
157 imapCommand *
159 {
160  return new imapCommand ("NOOP", "");
161 }
162 
163 imapCommand *
164 imapCommand::clientFetch (ulong uid, const TQString & fields, bool nouid)
165 {
166  return clientFetch (uid, uid, fields, nouid);
167 }
168 
169 imapCommand *
170 imapCommand::clientFetch (ulong fromUid, ulong toUid, const TQString & fields,
171  bool nouid)
172 {
173  TQString uid = TQString::number(fromUid);
174 
175  if (fromUid != toUid)
176  {
177  uid += ":";
178  if (toUid < fromUid)
179  uid += "*";
180  else
181  uid += TQString::number(toUid);
182  }
183  return clientFetch (uid, fields, nouid);
184 }
185 
186 imapCommand *
187 imapCommand::clientFetch (const TQString & sequence, const TQString & fields,
188  bool nouid)
189 {
190  return new imapCommand (nouid ? "FETCH" : "UID FETCH",
191  sequence + " (" + fields + ")");
192 }
193 
194 imapCommand *
195 imapCommand::clientList (const TQString & reference, const TQString & path,
196  bool lsub)
197 {
198  return new imapCommand (lsub ? "LSUB" : "LIST",
199  TQString ("\"") + rfcDecoder::toIMAP (reference) +
200  "\" \"" + rfcDecoder::toIMAP (path) + "\"");
201 }
202 
203 imapCommand *
204 imapCommand::clientSelect (const TQString & path, bool examine)
205 {
206  Q_UNUSED(examine);
210  return new imapCommand ("SELECT",
211  TQString ("\"") + rfcDecoder::toIMAP (path) + "\"");
212 }
213 
214 imapCommand *
216 {
217  return new imapCommand("CLOSE", "");
218 }
219 
220 imapCommand *
221 imapCommand::clientCopy (const TQString & box, const TQString & sequence,
222  bool nouid)
223 {
224  return new imapCommand (nouid ? "COPY" : "UID COPY",
225  sequence + " \"" + rfcDecoder::toIMAP (box) + "\"");
226 }
227 
228 imapCommand *
229 imapCommand::clientAppend (const TQString & box, const TQString & flags,
230  ulong size)
231 {
232  return new imapCommand ("APPEND",
233  "\"" + rfcDecoder::toIMAP (box) + "\" " +
234  ((flags.isEmpty()) ? "" : ("(" + flags + ") ")) +
235  "{" + TQString::number(size) + "}");
236 }
237 
238 imapCommand *
239 imapCommand::clienStatus (const TQString & path, const TQString & parameters)
240 {
241  return new imapCommand ("STATUS",
242  TQString ("\"") + rfcDecoder::toIMAP (path) +
243  "\" (" + parameters + ")");
244 }
245 
246 imapCommand *
247 imapCommand::clientCreate (const TQString & path)
248 {
249  return new imapCommand ("CREATE",
250  TQString ("\"") + rfcDecoder::toIMAP (path) + "\"");
251 }
252 
253 imapCommand *
254 imapCommand::clientDelete (const TQString & path)
255 {
256  return new imapCommand ("DELETE",
257  TQString ("\"") + rfcDecoder::toIMAP (path) + "\"");
258 }
259 
260 imapCommand *
261 imapCommand::clientSubscribe (const TQString & path)
262 {
263  return new imapCommand ("SUBSCRIBE",
264  TQString ("\"") + rfcDecoder::toIMAP (path) + "\"");
265 }
266 
267 imapCommand *
268 imapCommand::clientUnsubscribe (const TQString & path)
269 {
270  return new imapCommand ("UNSUBSCRIBE",
271  TQString ("\"") + rfcDecoder::toIMAP (path) + "\"");
272 }
273 
274 imapCommand *
276 {
277  return new imapCommand ("EXPUNGE", TQString (""));
278 }
279 
280 imapCommand *
281 imapCommand::clientRename (const TQString & src, const TQString & dest)
282 {
283  return new imapCommand ("RENAME",
284  TQString ("\"") + rfcDecoder::toIMAP (src) +
285  "\" \"" + rfcDecoder::toIMAP (dest) + "\"");
286 }
287 
288 imapCommand *
289 imapCommand::clientSearch (const TQString & search, bool nouid)
290 {
291  return new imapCommand (nouid ? "SEARCH" : "UID SEARCH", search);
292 }
293 
294 imapCommand *
295 imapCommand::clientStore (const TQString & set, const TQString & item,
296  const TQString & data, bool nouid)
297 {
298  return new imapCommand (nouid ? "STORE" : "UID STORE",
299  set + " " + item + " (" + data + ")");
300 }
301 
302 imapCommand *
304 {
305  return new imapCommand ("LOGOUT", "");
306 }
307 
308 imapCommand *
310 {
311  return new imapCommand ("STARTTLS", "");
312 }
313 
314 imapCommand *
315 imapCommand::clientSetACL( const TQString& box, const TQString& user, const TQString& acl )
316 {
317  return new imapCommand ("SETACL", TQString("\"") + rfcDecoder::toIMAP (box)
318  + "\" \"" + rfcDecoder::toIMAP (user)
319  + "\" \"" + rfcDecoder::toIMAP (acl) + "\"");
320 }
321 
322 imapCommand *
323 imapCommand::clientDeleteACL( const TQString& box, const TQString& user )
324 {
325  return new imapCommand ("DELETEACL", TQString("\"") + rfcDecoder::toIMAP (box)
326  + "\" \"" + rfcDecoder::toIMAP (user)
327  + "\"");
328 }
329 
330 imapCommand *
331 imapCommand::clientGetACL( const TQString& box )
332 {
333  return new imapCommand ("GETACL", TQString("\"") + rfcDecoder::toIMAP (box)
334  + "\"");
335 }
336 
337 imapCommand *
338 imapCommand::clientListRights( const TQString& box, const TQString& user )
339 {
340  return new imapCommand ("LISTRIGHTS", TQString("\"") + rfcDecoder::toIMAP (box)
341  + "\" \"" + rfcDecoder::toIMAP (user)
342  + "\"");
343 }
344 
345 imapCommand *
346 imapCommand::clientMyRights( const TQString& box )
347 {
348  return new imapCommand ("MYRIGHTS", TQString("\"") + rfcDecoder::toIMAP (box)
349  + "\"");
350 }
351 
352 imapCommand *
353 imapCommand::clientSetAnnotation( const TQString& box, const TQString& entry, const TQMap<TQString, TQString>& attributes )
354 {
355  TQString parameter = TQString("\"") + rfcDecoder::toIMAP (box)
356  + "\" \"" + rfcDecoder::toIMAP (entry)
357  + "\" (";
358  for( TQMap<TQString,TQString>::ConstIterator it = attributes.begin(); it != attributes.end(); ++it ) {
359  parameter += "\"";
360  parameter += rfcDecoder::toIMAP (it.key());
361  parameter += "\" \"";
362  parameter += rfcDecoder::toIMAP (it.data());
363  parameter += "\" ";
364  }
365  // Turn last space into a ')'
366  parameter[parameter.length()-1] = ')';
367 
368  return new imapCommand ("SETANNOTATION", parameter);
369 }
370 
371 imapCommand *
372 imapCommand::clientGetAnnotation( const TQString& box, const TQString& entry, const TQStringList& attributeNames )
373 {
374  TQString parameter = TQString("\"") + rfcDecoder::toIMAP (box)
375  + "\" \"" + rfcDecoder::toIMAP (entry)
376  + "\" ";
377  if ( attributeNames.count() == 1 )
378  parameter += "\"" + rfcDecoder::toIMAP (attributeNames.first()) + '"';
379  else {
380  parameter += '(';
381  for( TQStringList::ConstIterator it = attributeNames.begin(); it != attributeNames.end(); ++it ) {
382  parameter += "\"" + rfcDecoder::toIMAP (*it) + "\" ";
383  }
384  // Turn last space into a ')'
385  parameter[parameter.length()-1] = ')';
386  }
387  return new imapCommand ("GETANNOTATION", parameter);
388 }
389 
390 imapCommand *
392 {
393  return new imapCommand("NAMESPACE", "");
394 }
395 
396 imapCommand *
397 imapCommand::clientGetQuotaroot( const TQString& box )
398 {
399  TQString parameter = TQString("\"") + rfcDecoder::toIMAP (box) + '"';
400  return new imapCommand ("GETQUOTAROOT", parameter);
401 }
402 
403 imapCommand *
404 imapCommand::clientCustom( const TQString& command, const TQString& arguments )
405 {
406  return new imapCommand (command, arguments);
407 }
408 
encapulate a IMAP command
Definition: imapcommand.h:38
static imapCommand * clientSubscribe(const TQString &path)
Create a SUBSCRIBE command.
static imapCommand * clienStatus(const TQString &path, const TQString &parameters)
Create a STATUS command.
void setParameter(const TQString &)
set the command parameter(s)
const TQString & id()
get the id
Definition: imapcommand.cpp:94
static imapCommand * clientCreate(const TQString &path)
Create a CREATE command.
static imapCommand * clientNoop()
Create a NOOP command.
static imapCommand * clientRename(const TQString &src, const TQString &dest)
Create a RENAME command.
static imapCommand * clientGetQuotaroot(const TQString &box)
Create a GETQUOTAROOT command.
static imapCommand * clientMyRights(const TQString &box)
Create a MYRIGHTS command.
const TQString getStr()
returns the data to send to the server The function returns the complete data to be sent to the serve...
static imapCommand * clientList(const TQString &reference, const TQString &path, bool lsub=false)
Create a LIST command.
static imapCommand * clientSetAnnotation(const TQString &box, const TQString &entry, const TQMap< TQString, TQString > &attributes)
Create a SETANNOTATION command.
void setComplete()
set the completed state
const TQString & parameter()
get the parameter
void setCommand(const TQString &)
set the command
static imapCommand * clientSetACL(const TQString &box, const TQString &user, const TQString &acl)
Create a SETACL command.
static imapCommand * clientGetACL(const TQString &box)
Create a GETACL command.
static imapCommand * clientDelete(const TQString &path)
Create a DELETE command.
bool isComplete()
is it complete?
Definition: imapcommand.cpp:76
static imapCommand * clientListRights(const TQString &box, const TQString &user)
Create a LISTRIGHTS command.
const TQString & command()
get the command
static imapCommand * clientFetch(ulong uid, const TQString &fields, bool nouid=false)
Create a FETCH command.
static imapCommand * clientDeleteACL(const TQString &box, const TQString &user)
Create a DELETEACL command.
static imapCommand * clientUnsubscribe(const TQString &path)
Create a UNSUBSCRIBE command.
static imapCommand * clientNamespace()
Create a NAMESPACE command.
void setId(const TQString &)
set the id
void setResultInfo(const TQString &)
set the completed state
static imapCommand * clientSearch(const TQString &search, bool nouid=false)
Create a SEARCH command.
static imapCommand * clientExpunge()
Create a EXPUNGE command.
const TQString & resultInfo()
get information about the result
Definition: imapcommand.cpp:88
static imapCommand * clientStartTLS()
Create a STARTTLS command.
static imapCommand * clientClose()
Create a CLOSE command.
void setResult(const TQString &)
set the completed state
static imapCommand * clientLogout()
Create a LOGOUT command.
static imapCommand * clientCopy(const TQString &box, const TQString &sequence, bool nouid=false)
Create a COPY command.
static imapCommand * clientSelect(const TQString &path, bool examine=false)
Create a SELECT command.
const TQString & result()
get the result of the command
Definition: imapcommand.cpp:82
static imapCommand * clientCustom(const TQString &command, const TQString &arguments)
Create a custom command.
static imapCommand * clientGetAnnotation(const TQString &box, const TQString &entry, const TQStringList &attributeNames)
Create a GETANNOTATION command.
static imapCommand * clientStore(const TQString &set, const TQString &item, const TQString &data, bool nouid=false)
Create a STORE command.
imapCommand()
Constructor.
Definition: imapcommand.cpp:58
static imapCommand * clientAppend(const TQString &box, const TQString &flags, ulong size)
Create a APPEND command.
static TQString toIMAP(const TQString &inSrc)
Convert Unicode path to modified UTF-7 IMAP mailbox.
Definition: rfcdecoder.cpp:175