00001 #include "ocSocket.h"
00002 #include "ocString.h"
00003
00004
00005 const int reasonableBufSize = 200;
00006
00007 struct mlistItem
00008 {
00009 string id;
00010 string to;
00011 string from;
00012 string sent;
00013 string reply_to;
00014 string cc;
00015 string in_reply_to;
00016 string subject;
00017 string received;
00018 string flags;
00019 string size;
00020
00021
00022
00023
00024
00025
00026
00027 mlistItem( string & listItem )
00028 {
00029 ocString parser = listItem;
00030 parser.parseInit();
00031 ocString worker;
00032
00033
00034 id = parser.parse(" ");
00035
00036 parser.parse(" (");
00037
00038 flags = parser.parse(") ");
00039
00040 parser.parse("RFC822.SIZE ");
00041
00042 size = parser.parse(" ENVELOPE (\"");
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092 received = parser.parse("\" ");
00093 if( parser.remainder()[0] == '\"' )
00094 {
00095 worker = parser.parse("\" ");
00096 worker = worker.remove("\"");
00097 }
00098 else
00099 {
00100 worker = parser.parse(" ");
00101 }
00102 subject = worker;
00103 setAddress(from,parser);
00104 setAddress(sent,parser);
00105 setAddress(reply_to,parser);
00106 setAddress(to,parser);
00107 setAddress(cc,parser);
00108 setAddress(in_reply_to,parser);
00109 if( subject.length() == 0 || subject == "NIL" ) subject="(None)";
00110 }
00111
00112
00113 void setAddress( string & address, ocString & data )
00114 {
00115
00116
00117
00118 ocString worker = data.parse(" ");
00119 address = "";
00120 if( worker[0] == '(' )
00121 {
00122
00123 worker = worker.remove("((");
00124 if( worker[0] == '\"' )
00125 {
00126
00127 address += worker;
00128 if( worker.length() && worker[worker.length()-1] != '\"' )
00129 {
00130
00131 address += " ";
00132 address += data.parse("\" ");
00133 address += "\"";
00134 }
00135 address += " ";
00136 }
00137
00138 worker = data.parse(" ");
00139 if( worker[0] == '\"' )
00140 {
00141 data.parse("\" ");
00142 }
00143
00144
00145 worker = data.parse(" ");
00146 if( worker[0] == '\"' )
00147 {
00148 address += worker.remove("\"").remove("\"");
00149 address += "@";
00150 }
00151
00152
00153 worker = data.parse(")) ");
00154 if( worker[0] == '\"' )
00155 {
00156 address += worker.remove("\"").remove("\"");
00157 }
00158
00159 }
00160
00161 }
00162
00163 };
00164
00165 class oc_IMap: protected ocSocket
00166 {
00167 vector <string> responseLines;
00168 vector <string> listItems;
00169 ocString sequenceBuffer;
00170 ocString currentLine;
00171 string callPrefix;
00172 string currentPrefix;
00173
00174 int callSequence;
00175 bool responseComplete;
00176
00177 bool cache;
00178
00179
00180 const char * sequence( void )
00181 {
00182 sequenceBuffer="";
00183 return sequenceBuffer.append( callSequence++ ).c_str();
00184 }
00185 bool checkCommandResponse( void )
00186 {
00187
00188 responseLines.clear();
00189 quenchQueue();
00190 return responseOK();
00191 }
00192 void quenchQueue( void )
00193 {
00194 do
00195 {
00196 Response();
00197 } while(!responseComplete);
00198 }
00199 bool responseOK( void )
00200 {
00201 bool bret = false;
00202 currentLine.parseInit();
00203 string token = currentLine.parse(" ");
00204 if( token == currentPrefix )
00205 {
00206 token = currentLine.parse(" ");
00207
00208
00209 if( token == "OK" )
00210 {
00211 bret = true;
00212 }
00213 }
00214 return bret;
00215 }
00216
00217
00218 public:
00219 oc_IMap( const char * chPrefix, const char * address, int port )
00220 :ocSocket( address, port )
00221 ,responseLines(reasonableBufSize)
00222 ,listItems(reasonableBufSize)
00223 ,callPrefix(chPrefix)
00224 ,callSequence(1)
00225 ,responseComplete(false)
00226 ,cache(false)
00227 { ; };
00228
00229 ~oc_IMap()
00230 {;}
00231
00232
00233
00234
00235
00236 void CacheData( bool bVal = true )
00237 {
00238 cache = bVal;
00239 }
00240 void ResetCache( void )
00241 {
00242 responseLines.clear();
00243 }
00244 vector <string> & GetDataCache( void )
00245 {
00246 return responseLines;
00247 }
00248 vector <string> & GetListItems( void )
00249 {
00250 return listItems;
00251 }
00252
00253 bool Command( const char * in )
00254 {
00255 bool bRet = false;
00256 if( in && strlen( in ) )
00257 {
00258 string cmdIn = in;
00259 int cmdLen = cmdIn.length();
00260
00261 if( cmdIn[cmdLen-1] == '\n' ) cmdIn.resize(cmdLen-1);
00262 if( cmdLen > 1 && cmdIn[cmdLen-2] == '\r' ) cmdIn.resize(cmdLen-2);
00263
00264
00265 currentPrefix = callPrefix;
00266 currentPrefix += sequence();
00267 string command = currentPrefix;
00268 command += " ";
00269 command += cmdIn;
00270 command += "\r\n";
00271
00272 cmdLen = command.length();
00273
00274
00275
00276 bRet = ( cmdLen == Write( command.c_str(), cmdLen ) );
00277 }
00278 return bRet;
00279 }
00280 string & Response( void )
00281 {
00282 string token;
00283 responseComplete = false;
00284 currentLine = ReadLine();
00285
00286 currentLine.parseInit();
00287 token = currentLine.parse(" ");
00288 if( token == currentPrefix )
00289 {
00290 responseComplete = true;
00291 }
00292 token = currentLine.parse(" ");
00293 if( token == "BYE" )
00294 {
00295 responseComplete = true;
00296 }
00297 if( cache )
00298 {
00299 responseLines.push_back( currentLine );
00300 }
00301 return currentLine;
00302 }
00303
00304
00305 bool GetResponseComplete( void )
00306 {
00307 return responseComplete;
00308 }
00309
00310
00311
00312
00313 bool login( const char * user, const char * password)
00314 {
00315 bool bret = false;
00316 if( user && strlen(user) &&
00317 password && strlen(password) )
00318 {
00319 string command = "LOGIN ";
00320 command += user;
00321 command += " ";
00322 command += password;
00323 if( Command(command.c_str()) ) bret = checkCommandResponse();
00324 }
00325 return bret;
00326 }
00327
00328
00329 bool select( const char * mailbox )
00330 {
00331 bool bret = false;
00332 if( mailbox && strlen(mailbox) )
00333 {
00334 string command = "SELECT ";
00335 command += mailbox;
00336 if( Command(command.c_str()) ) bret = checkCommandResponse();
00337 }
00338 return bret;
00339 }
00340 bool examine( const char * mailbox )
00341 {
00342 bool bret = false;
00343 if( mailbox && strlen(mailbox) )
00344 {
00345 string command = "EXAMINE ";
00346 command += mailbox;
00347 if( Command(command.c_str()) ) bret = checkCommandResponse();
00348 }
00349 return bret;
00350 }
00351 bool create( const char * mailbox )
00352 {
00353 bool bret = false;
00354 if( mailbox && strlen(mailbox) )
00355 {
00356 string command = "CREATE ";
00357 command += mailbox;
00358 if( Command(command.c_str()) ) bret = checkCommandResponse();
00359 }
00360 return bret;
00361 }
00362
00363
00364 bool fetchHeaders( void )
00365 {
00366 bool bret = false;
00367 bool savedCache = cache;
00368 cache = true;
00369 string command = "SEARCH ALL";
00370
00371 if( Command(command.c_str()) && checkCommandResponse() )
00372 {
00373
00374 for( size_t x=0; x<responseLines.size(); x++ )
00375 {
00376 ocString line = responseLines[x];
00377 line = line.replace("\r\n"," ");
00378 line.parseInit();
00379
00380
00381
00382
00383 string token = line.parse(" ");
00384 if( token == "*" )
00385 {
00386 token = line.parse(" ");
00387 if( token == "SEARCH" )
00388 {
00389 listItems.clear();
00390
00391 do
00392 {
00393 token = line.parse(" ");
00394 if( token.length() )
00395 {
00396 cache = false;
00397 command = "FETCH ";
00398 command += token;
00399 command += " ALL";
00400 if( Command(command.c_str()) )
00401 {
00402 ocString response = Response();
00403
00404 response.parseInit();
00405 token = response.parse( " " );
00406
00407 if( token == "*" )
00408 {
00409
00410 string item = response.parse( " " );
00411 item += " ";
00412 if( !response.endOfParse() )
00413 {
00414 token = response.parse( " (" );
00415 if( token == "FETCH" )
00416 {
00417 item += response.remainder();
00418
00419 listItems.push_back( item );
00420 }
00421 }
00422 }
00423 }
00424
00425 quenchQueue();
00426
00427 if( ! responseOK() )
00428 {
00429 non_fatal_error( "detected failure in 'fetch all' response" );
00430 return false;
00431 }
00432 }
00433 } while(!line.endOfParse());
00434
00435 break;
00436 }
00437 }
00438 }
00439 }
00440 cache = savedCache;
00441 bret = listItems.size() > 0;
00442 return bret;
00443 }
00444
00445 bool deleteItems( const char * itemList )
00446 {
00447 bool bret = false;
00448 string command = "STORE ";
00449 command += itemList;
00450 command += " FLAGS.SILENT (\\Deleted)";
00451 if( Command(command.c_str()) && checkCommandResponse() )
00452 {
00453 command = "EXPUNGE";
00454 if( Command(command.c_str()) && checkCommandResponse() )
00455 {
00456 bret = true;
00457 }
00458 }
00459 return bret;
00460 }
00461
00462 bool copyItems( const char * itemList, const char * toBox )
00463 {
00464 bool bret = false;
00465 string command = "COPY ";
00466 command += itemList;
00467 command += " ";
00468 command += toBox;
00469 if( Command(command.c_str()) && checkCommandResponse() )
00470 {
00471 bret = true;
00472 }
00473 return bret;
00474 }
00475
00476
00477 string & fetchRFC822 ( const char * item )
00478 {
00479 bool savedCache = cache;
00480 cache = true;
00481 string command = "FETCH ";
00482 command += item;
00483 command += " RFC822";
00484 if( Command(command.c_str()) && checkCommandResponse() )
00485 {
00486 currentLine = "";
00487
00488 for( size_t x=0; x<responseLines.size(); x++ )
00489 {
00490 ocString examine = responseLines[x];
00491 examine.parseInit();
00492 ocString firstToken = examine.parse(" ");
00493 firstToken = firstToken.remove("\r\n");
00494 if( firstToken == "*" &&
00495 examine.parse(" ") == item &&
00496 examine.parse(" ") == "FETCH" )
00497 {
00498 x+=4;
00499 }
00500 else if( firstToken != ")" &&
00501 firstToken != currentPrefix )
00502 {
00503 currentLine += responseLines[x];
00504 }
00505 }
00506 }
00507 cache = savedCache;
00508 return currentLine;
00509 }
00510
00511
00512
00513
00514 string & rawFetchRFC822 ( const char * item )
00515 {
00516 cache = false;
00517 currentLine = "";
00518 string command = "FETCH ";
00519 command += item;
00520 command += " RFC822";
00521
00522 if( Command(command.c_str()) )
00523 {
00524
00525 Response();
00526 }
00527
00528 return currentLine;
00529 }
00530
00531 };
00532