imapcommand.cc
00001 /********************************************************************** 00002 * 00003 * imapcommand.cc - IMAP4rev1 command handler 00004 * Copyright (C) 2000 Sven Carstens <s.carstens@gmx.de> 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00019 * 00020 * Send comments and bug fixes to s.carstens@gmx.de 00021 * 00022 *********************************************************************/ 00023 00024 #include "imapcommand.h" 00025 #include "rfcdecoder.h" 00026 00027 /*#include <stdlib.h> 00028 00029 #include <sys/types.h> 00030 #include <sys/socket.h> 00031 #include <sys/wait.h> 00032 #include <sys/stat.h> 00033 00034 #include <fcntl.h> 00035 00036 #include <netinet/in.h> 00037 #include <arpa/inet.h> 00038 00039 #include <errno.h> 00040 #include <signal.h> 00041 #include <stdio.h> 00042 #include <netdb.h> 00043 #include <unistd.h> 00044 #include <stdlib.h> 00045 00046 #include <tqregexp.h> 00047 #include <tqbuffer.h> 00048 00049 #include <tdeprotocolmanager.h> 00050 #include <ksock.h> 00051 #include <kdebug.h> 00052 #include <kinstance.h> 00053 #include <tdeio/connection.h> 00054 #include <tdeio/slaveinterface.h> 00055 #include <tdeio/passdlg.h> 00056 #include <tdelocale.h> */ 00057 00058 imapCommand::imapCommand () 00059 { 00060 mComplete = false; 00061 mId = TQString(); 00062 } 00063 00064 imapCommand::imapCommand (const TQString & command, const TQString & parameter) 00065 // aCommand(NULL), 00066 // mResult(NULL), 00067 // mParameter(NULL) 00068 { 00069 mComplete = false; 00070 aCommand = command; 00071 aParameter = parameter; 00072 mId = TQString(); 00073 } 00074 00075 bool 00076 imapCommand::isComplete () 00077 { 00078 return mComplete; 00079 } 00080 00081 const TQString & 00082 imapCommand::result () 00083 { 00084 return mResult; 00085 } 00086 00087 const TQString & 00088 imapCommand::resultInfo () 00089 { 00090 return mResultInfo; 00091 } 00092 00093 const TQString & 00094 imapCommand::id () 00095 { 00096 return mId; 00097 } 00098 00099 const TQString & 00100 imapCommand::parameter () 00101 { 00102 return aParameter; 00103 } 00104 00105 const TQString & 00106 imapCommand::command () 00107 { 00108 return aCommand; 00109 } 00110 00111 void 00112 imapCommand::setId (const TQString & id) 00113 { 00114 if (mId.isEmpty ()) 00115 mId = id; 00116 } 00117 00118 void 00119 imapCommand::setComplete () 00120 { 00121 mComplete = true; 00122 } 00123 00124 void 00125 imapCommand::setResult (const TQString & result) 00126 { 00127 mResult = result; 00128 } 00129 00130 void 00131 imapCommand::setResultInfo (const TQString & result) 00132 { 00133 mResultInfo = result; 00134 } 00135 00136 void 00137 imapCommand::setCommand (const TQString & command) 00138 { 00139 aCommand = command; 00140 } 00141 00142 void 00143 imapCommand::setParameter (const TQString & parameter) 00144 { 00145 aParameter = parameter; 00146 } 00147 00148 const TQString 00149 imapCommand::getStr () 00150 { 00151 if (parameter().isEmpty()) 00152 return id() + " " + command() + "\r\n"; 00153 else 00154 return id() + " " + command() + " " + parameter() + "\r\n"; 00155 } 00156 00157 imapCommand * 00158 imapCommand::clientNoop () 00159 { 00160 return new imapCommand ("NOOP", ""); 00161 } 00162 00163 imapCommand * 00164 imapCommand::clientFetch (ulong uid, const TQString & fields, bool nouid) 00165 { 00166 return clientFetch (uid, uid, fields, nouid); 00167 } 00168 00169 imapCommand * 00170 imapCommand::clientFetch (ulong fromUid, ulong toUid, const TQString & fields, 00171 bool nouid) 00172 { 00173 TQString uid = TQString::number(fromUid); 00174 00175 if (fromUid != toUid) 00176 { 00177 uid += ":"; 00178 if (toUid < fromUid) 00179 uid += "*"; 00180 else 00181 uid += TQString::number(toUid); 00182 } 00183 return clientFetch (uid, fields, nouid); 00184 } 00185 00186 imapCommand * 00187 imapCommand::clientFetch (const TQString & sequence, const TQString & fields, 00188 bool nouid) 00189 { 00190 return new imapCommand (nouid ? "FETCH" : "UID FETCH", 00191 sequence + " (" + fields + ")"); 00192 } 00193 00194 imapCommand * 00195 imapCommand::clientList (const TQString & reference, const TQString & path, 00196 bool lsub) 00197 { 00198 return new imapCommand (lsub ? "LSUB" : "LIST", 00199 TQString ("\"") + rfcDecoder::toIMAP (reference) + 00200 "\" \"" + rfcDecoder::toIMAP (path) + "\""); 00201 } 00202 00203 imapCommand * 00204 imapCommand::clientSelect (const TQString & path, bool examine) 00205 { 00206 Q_UNUSED(examine); 00210 return new imapCommand ("SELECT", 00211 TQString ("\"") + rfcDecoder::toIMAP (path) + "\""); 00212 } 00213 00214 imapCommand * 00215 imapCommand::clientClose() 00216 { 00217 return new imapCommand("CLOSE", ""); 00218 } 00219 00220 imapCommand * 00221 imapCommand::clientCopy (const TQString & box, const TQString & sequence, 00222 bool nouid) 00223 { 00224 return new imapCommand (nouid ? "COPY" : "UID COPY", 00225 sequence + " \"" + rfcDecoder::toIMAP (box) + "\""); 00226 } 00227 00228 imapCommand * 00229 imapCommand::clientAppend (const TQString & box, const TQString & flags, 00230 ulong size) 00231 { 00232 return new imapCommand ("APPEND", 00233 "\"" + rfcDecoder::toIMAP (box) + "\" " + 00234 ((flags.isEmpty()) ? "" : ("(" + flags + ") ")) + 00235 "{" + TQString::number(size) + "}"); 00236 } 00237 00238 imapCommand * 00239 imapCommand::clienStatus (const TQString & path, const TQString & parameters) 00240 { 00241 return new imapCommand ("STATUS", 00242 TQString ("\"") + rfcDecoder::toIMAP (path) + 00243 "\" (" + parameters + ")"); 00244 } 00245 00246 imapCommand * 00247 imapCommand::clientCreate (const TQString & path) 00248 { 00249 return new imapCommand ("CREATE", 00250 TQString ("\"") + rfcDecoder::toIMAP (path) + "\""); 00251 } 00252 00253 imapCommand * 00254 imapCommand::clientDelete (const TQString & path) 00255 { 00256 return new imapCommand ("DELETE", 00257 TQString ("\"") + rfcDecoder::toIMAP (path) + "\""); 00258 } 00259 00260 imapCommand * 00261 imapCommand::clientSubscribe (const TQString & path) 00262 { 00263 return new imapCommand ("SUBSCRIBE", 00264 TQString ("\"") + rfcDecoder::toIMAP (path) + "\""); 00265 } 00266 00267 imapCommand * 00268 imapCommand::clientUnsubscribe (const TQString & path) 00269 { 00270 return new imapCommand ("UNSUBSCRIBE", 00271 TQString ("\"") + rfcDecoder::toIMAP (path) + "\""); 00272 } 00273 00274 imapCommand * 00275 imapCommand::clientExpunge () 00276 { 00277 return new imapCommand ("EXPUNGE", TQString ("")); 00278 } 00279 00280 imapCommand * 00281 imapCommand::clientRename (const TQString & src, const TQString & dest) 00282 { 00283 return new imapCommand ("RENAME", 00284 TQString ("\"") + rfcDecoder::toIMAP (src) + 00285 "\" \"" + rfcDecoder::toIMAP (dest) + "\""); 00286 } 00287 00288 imapCommand * 00289 imapCommand::clientSearch (const TQString & search, bool nouid) 00290 { 00291 return new imapCommand (nouid ? "SEARCH" : "UID SEARCH", search); 00292 } 00293 00294 imapCommand * 00295 imapCommand::clientStore (const TQString & set, const TQString & item, 00296 const TQString & data, bool nouid) 00297 { 00298 return new imapCommand (nouid ? "STORE" : "UID STORE", 00299 set + " " + item + " (" + data + ")"); 00300 } 00301 00302 imapCommand * 00303 imapCommand::clientLogout () 00304 { 00305 return new imapCommand ("LOGOUT", ""); 00306 } 00307 00308 imapCommand * 00309 imapCommand::clientStartTLS () 00310 { 00311 return new imapCommand ("STARTTLS", ""); 00312 } 00313 00314 imapCommand * 00315 imapCommand::clientSetACL( const TQString& box, const TQString& user, const TQString& acl ) 00316 { 00317 return new imapCommand ("SETACL", TQString("\"") + rfcDecoder::toIMAP (box) 00318 + "\" \"" + rfcDecoder::toIMAP (user) 00319 + "\" \"" + rfcDecoder::toIMAP (acl) + "\""); 00320 } 00321 00322 imapCommand * 00323 imapCommand::clientDeleteACL( const TQString& box, const TQString& user ) 00324 { 00325 return new imapCommand ("DELETEACL", TQString("\"") + rfcDecoder::toIMAP (box) 00326 + "\" \"" + rfcDecoder::toIMAP (user) 00327 + "\""); 00328 } 00329 00330 imapCommand * 00331 imapCommand::clientGetACL( const TQString& box ) 00332 { 00333 return new imapCommand ("GETACL", TQString("\"") + rfcDecoder::toIMAP (box) 00334 + "\""); 00335 } 00336 00337 imapCommand * 00338 imapCommand::clientListRights( const TQString& box, const TQString& user ) 00339 { 00340 return new imapCommand ("LISTRIGHTS", TQString("\"") + rfcDecoder::toIMAP (box) 00341 + "\" \"" + rfcDecoder::toIMAP (user) 00342 + "\""); 00343 } 00344 00345 imapCommand * 00346 imapCommand::clientMyRights( const TQString& box ) 00347 { 00348 return new imapCommand ("MYRIGHTS", TQString("\"") + rfcDecoder::toIMAP (box) 00349 + "\""); 00350 } 00351 00352 imapCommand * 00353 imapCommand::clientSetAnnotation( const TQString& box, const TQString& entry, const TQMap<TQString, TQString>& attributes ) 00354 { 00355 TQString parameter = TQString("\"") + rfcDecoder::toIMAP (box) 00356 + "\" \"" + rfcDecoder::toIMAP (entry) 00357 + "\" ("; 00358 for( TQMap<TQString,TQString>::ConstIterator it = attributes.begin(); it != attributes.end(); ++it ) { 00359 parameter += "\""; 00360 parameter += rfcDecoder::toIMAP (it.key()); 00361 parameter += "\" \""; 00362 parameter += rfcDecoder::toIMAP (it.data()); 00363 parameter += "\" "; 00364 } 00365 // Turn last space into a ')' 00366 parameter[parameter.length()-1] = ')'; 00367 00368 return new imapCommand ("SETANNOTATION", parameter); 00369 } 00370 00371 imapCommand * 00372 imapCommand::clientGetAnnotation( const TQString& box, const TQString& entry, const TQStringList& attributeNames ) 00373 { 00374 TQString parameter = TQString("\"") + rfcDecoder::toIMAP (box) 00375 + "\" \"" + rfcDecoder::toIMAP (entry) 00376 + "\" "; 00377 if ( attributeNames.count() == 1 ) 00378 parameter += "\"" + rfcDecoder::toIMAP (attributeNames.first()) + '"'; 00379 else { 00380 parameter += '('; 00381 for( TQStringList::ConstIterator it = attributeNames.begin(); it != attributeNames.end(); ++it ) { 00382 parameter += "\"" + rfcDecoder::toIMAP (*it) + "\" "; 00383 } 00384 // Turn last space into a ')' 00385 parameter[parameter.length()-1] = ')'; 00386 } 00387 return new imapCommand ("GETANNOTATION", parameter); 00388 } 00389 00390 imapCommand * 00391 imapCommand::clientNamespace() 00392 { 00393 return new imapCommand("NAMESPACE", ""); 00394 } 00395 00396 imapCommand * 00397 imapCommand::clientGetQuotaroot( const TQString& box ) 00398 { 00399 TQString parameter = TQString("\"") + rfcDecoder::toIMAP (box) + '"'; 00400 return new imapCommand ("GETQUOTAROOT", parameter); 00401 } 00402 00403 imapCommand * 00404 imapCommand::clientCustom( const TQString& command, const TQString& arguments ) 00405 { 00406 return new imapCommand (command, arguments); 00407 } 00408