kioslaves

imapcommand.cc

Go to the documentation of this file.
00001 /**********************************************************************
00002  *
00003  *   imapcommand.cc  - IMAP4rev1 command handler
00004  *   Copyright (C) 2000 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 <qregexp.h>
00047 #include <qbuffer.h>
00048 
00049 #include <kprotocolmanager.h>
00050 #include <ksock.h>
00051 #include <kdebug.h>
00052 #include <kinstance.h>
00053 #include <kio/connection.h>
00054 #include <kio/slaveinterface.h>
00055 #include <kio/passdlg.h>
00056 #include <klocale.h> */
00057 
00058 imapCommand::imapCommand ()
00059 {
00060   mComplete = false;
00061   mId = QString::null;
00062 }
00063 
00064 imapCommand::imapCommand (const QString & command, const QString & parameter)
00065 //  aCommand(NULL),
00066 //  mResult(NULL),
00067 //  mParameter(NULL)
00068 {
00069   mComplete = false;
00070   aCommand = command;
00071   aParameter = parameter;
00072   mId = QString::null;
00073 }
00074 
00075 bool
00076 imapCommand::isComplete ()
00077 {
00078   return mComplete;
00079 }
00080 
00081 const QString &
00082 imapCommand::result ()
00083 {
00084   return mResult;
00085 }
00086 
00087 const QString &
00088 imapCommand::resultInfo ()
00089 {
00090   return mResultInfo;
00091 }
00092 
00093 const QString &
00094 imapCommand::id ()
00095 {
00096   return mId;
00097 }
00098 
00099 const QString &
00100 imapCommand::parameter ()
00101 {
00102   return aParameter;
00103 }
00104 
00105 const QString &
00106 imapCommand::command ()
00107 {
00108   return aCommand;
00109 }
00110 
00111 void
00112 imapCommand::setId (const QString & 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 QString & result)
00126 {
00127   mResult = result;
00128 }
00129 
00130 void
00131 imapCommand::setResultInfo (const QString & result)
00132 {
00133   mResultInfo = result;
00134 }
00135 
00136 void
00137 imapCommand::setCommand (const QString & command)
00138 {
00139   aCommand = command;
00140 }
00141 
00142 void
00143 imapCommand::setParameter (const QString & parameter)
00144 {
00145   aParameter = parameter;
00146 }
00147 
00148 const QString
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 QString & fields, bool nouid)
00165 {
00166   return clientFetch (uid, uid, fields, nouid);
00167 }
00168 
00169 imapCommand *
00170 imapCommand::clientFetch (ulong fromUid, ulong toUid, const QString & fields,
00171                           bool nouid)
00172 {
00173   QString uid = QString::number(fromUid);
00174 
00175   if (fromUid != toUid)
00176   {
00177     uid += ":";
00178     if (toUid < fromUid)
00179       uid += "*";
00180     else
00181       uid += QString::number(toUid);
00182   }
00183   return clientFetch (uid, fields, nouid);
00184 }
00185 
00186 imapCommand *
00187 imapCommand::clientFetch (const QString & sequence, const QString & fields,
00188                           bool nouid)
00189 {
00190   return new imapCommand (nouid ? "FETCH" : "UID FETCH",
00191                           sequence + " (" + fields + ")");
00192 }
00193 
00194 imapCommand *
00195 imapCommand::clientList (const QString & reference, const QString & path,
00196                          bool lsub)
00197 {
00198   return new imapCommand (lsub ? "LSUB" : "LIST",
00199                           QString ("\"") + rfcDecoder::toIMAP (reference) +
00200                           "\" \"" + rfcDecoder::toIMAP (path) + "\"");
00201 }
00202 
00203 imapCommand *
00204 imapCommand::clientSelect (const QString & path, bool examine)
00205 {
00206   Q_UNUSED(examine);
00210   return new imapCommand ("SELECT",
00211                           QString ("\"") + rfcDecoder::toIMAP (path) + "\"");
00212 }
00213 
00214 imapCommand *
00215 imapCommand::clientClose()
00216 {
00217   return new imapCommand("CLOSE", "");
00218 }
00219 
00220 imapCommand *
00221 imapCommand::clientCopy (const QString & box, const QString & 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 QString & box, const QString & flags,
00230                            ulong size)
00231 {
00232   return new imapCommand ("APPEND",
00233                           "\"" + rfcDecoder::toIMAP (box) + "\" " +
00234                           ((flags.isEmpty()) ? "" : ("(" + flags + ") ")) +
00235                           "{" + QString::number(size) + "}");
00236 }
00237 
00238 imapCommand *
00239 imapCommand::clientStatus (const QString & path, const QString & parameters)
00240 {
00241   return new imapCommand ("STATUS",
00242                           QString ("\"") + rfcDecoder::toIMAP (path) +
00243                           "\" (" + parameters + ")");
00244 }
00245 
00246 imapCommand *
00247 imapCommand::clientCreate (const QString & path)
00248 {
00249   return new imapCommand ("CREATE",
00250                           QString ("\"") + rfcDecoder::toIMAP (path) + "\"");
00251 }
00252 
00253 imapCommand *
00254 imapCommand::clientDelete (const QString & path)
00255 {
00256   return new imapCommand ("DELETE",
00257                           QString ("\"") + rfcDecoder::toIMAP (path) + "\"");
00258 }
00259 
00260 imapCommand *
00261 imapCommand::clientSubscribe (const QString & path)
00262 {
00263   return new imapCommand ("SUBSCRIBE",
00264                           QString ("\"") + rfcDecoder::toIMAP (path) + "\"");
00265 }
00266 
00267 imapCommand *
00268 imapCommand::clientUnsubscribe (const QString & path)
00269 {
00270   return new imapCommand ("UNSUBSCRIBE",
00271                           QString ("\"") + rfcDecoder::toIMAP (path) + "\"");
00272 }
00273 
00274 imapCommand *
00275 imapCommand::clientExpunge ()
00276 {
00277   return new imapCommand ("EXPUNGE", QString (""));
00278 }
00279 
00280 imapCommand *
00281 imapCommand::clientRename (const QString & src, const QString & dest)
00282 {
00283   return new imapCommand ("RENAME",
00284                           QString ("\"") + rfcDecoder::toIMAP (src) +
00285                           "\" \"" + rfcDecoder::toIMAP (dest) + "\"");
00286 }
00287 
00288 imapCommand *
00289 imapCommand::clientSearch (const QString & search, bool nouid)
00290 {
00291   return new imapCommand (nouid ? "SEARCH" : "UID SEARCH", search);
00292 }
00293 
00294 imapCommand *
00295 imapCommand::clientStore (const QString & set, const QString & item,
00296                           const QString & 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 QString& box, const QString& user, const QString& acl )
00316 {
00317   return new imapCommand ("SETACL", QString("\"") + rfcDecoder::toIMAP (box)
00318                           + "\" \"" + rfcDecoder::toIMAP (user)
00319                           + "\" \"" + rfcDecoder::toIMAP (acl) + "\"");
00320 }
00321 
00322 imapCommand *
00323 imapCommand::clientDeleteACL( const QString& box, const QString& user )
00324 {
00325   return new imapCommand ("DELETEACL", QString("\"") + rfcDecoder::toIMAP (box)
00326                           + "\" \"" + rfcDecoder::toIMAP (user)
00327                           + "\"");
00328 }
00329 
00330 imapCommand *
00331 imapCommand::clientGetACL( const QString& box )
00332 {
00333   return new imapCommand ("GETACL", QString("\"") + rfcDecoder::toIMAP (box)
00334                           + "\"");
00335 }
00336 
00337 imapCommand *
00338 imapCommand::clientListRights( const QString& box, const QString& user )
00339 {
00340   return new imapCommand ("LISTRIGHTS", QString("\"") + rfcDecoder::toIMAP (box)
00341                           + "\" \"" + rfcDecoder::toIMAP (user)
00342                           + "\"");
00343 }
00344 
00345 imapCommand *
00346 imapCommand::clientMyRights( const QString& box )
00347 {
00348   return new imapCommand ("MYRIGHTS", QString("\"") + rfcDecoder::toIMAP (box)
00349                           + "\"");
00350 }
00351 
00352 imapCommand *
00353 imapCommand::clientSetAnnotation( const QString& box, const QString& entry, const QMap<QString, QString>& attributes )
00354 {
00355   QString parameter = QString("\"") + rfcDecoder::toIMAP (box)
00356                       + "\" \"" + rfcDecoder::toIMAP (entry)
00357                       + "\" (";
00358   for( QMap<QString,QString>::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 QString& box, const QString& entry, const QStringList& attributeNames )
00373 {
00374   QString parameter = QString("\"") + 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( QStringList::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 QString& box )
00398 {
00399   QString parameter = QString("\"") + rfcDecoder::toIMAP (box) + '"';
00400   return new imapCommand ("GETQUOTAROOT", parameter);
00401 }
00402 
00403 imapCommand *
00404 imapCommand::clientCustom( const QString& command, const QString& arguments )
00405 {
00406   return new imapCommand (command, arguments);
00407 }
00408