test2.cxx

Go to the documentation of this file.
00001 
00002 //
00003 // Copyright 2002, Lowell Boggs Jr.
00004 //
00005 // This file or directory, containing source code for a computer program,
00006 // is Copyrighted by Lowell Boggs, Jr.  987 Regency Drive, Lewisville
00007 // TX (USA), 75067.  You may use, copy, modify, and distribute this
00008 // source file without charge or obligation so long as you agree to
00009 // the following:
00010 //
00011 //  1.  You must indemnify Lowell Boggs against any and all financial
00012 //      obligations caused by its use, misuse, function, or malfunction.
00013 //      Further, you acknowledge that there is no warranty of any kind,
00014 //      whatsoever.
00015 //
00016 //  2.  You agree not to attempt to patent any portion of this original
00017 //      work -- though you may attempt to patent your own extensions to
00018 //      it if you so choose.
00019 //
00020 //  3.  You keep this copyright notice with the file and all copies
00021 //      of the file and do not change it anyway except language translation.
00022 //
00023 // You are responsible for enforcing your own compliance with these
00024 // conditions and may not use this source file if you cannot agree to the
00025 // above terms and conditions.
00026 //
00027 // Warning:  not all files in this directory structure are covered by the
00028 // same copyright.  Some of them are part of the GNU source distribution
00029 // and you must obey the GPL copyright for those files.
00030 
00031 //@File
00032 //
00037 
00038 
00039 #include <stdio.h>
00040 #include <stdlib.h>
00041 #include <libspopc.h>
00042 #include <cxxtls/file.h>
00043 #include <cxxtls/options.h>
00044 #include <errno.h>
00045 #include <string.h>
00046 
00047 
00048 using namespace std;
00049 using namespace cxxtls;
00050 
00051 int main(int argc, char **argv, char **environ)
00052 {
00053   static char const* allowed_arguments =
00054     "-delete,"
00055   ;
00056 
00057   ProgramOptions::global_options_ = new ProgramOptions(allowed_arguments,
00058                                                        argc,
00059                                                        argv,
00060                                                        environ);
00061 
00062   ProgramOptions& options = *ProgramOptions::global_options_;
00063 
00064   if(options.argv.size() != 4)
00065   { 
00066      fprintf(stderr, "Error:  expect 4 parameters\n");
00067      fprintf(stderr, "        hostname\n");
00068      fprintf(stderr, "        username\n");
00069      fprintf(stderr, "        password\n");
00070      exit(1);
00071   }
00072 
00073   popsession *session;
00074 
00075   char const *error = popbegin(options.argv[1].c_str(),
00076                                options.argv[2].c_str(),
00077                                options.argv[3].c_str(),
00078                                &session
00079                               );
00080 
00081   if(error)
00082   {
00083     fprintf(stderr, "Error:  %s", error);
00084     exit(1);
00085   }
00086 
00087   if(options.option("-delete"))
00088     popsetdel(session);
00089 
00090 
00091   int message_count = popnum(session);
00092 
00093   printf("message count is %d\n", message_count);
00094 
00095   int i;
00096 
00097   // make sure the incoming directory exists
00098 
00099   FileName incoming_dir("incoming");
00100   incoming_dir.mkdir();
00101 
00102   for(i=0; i < message_count; ++i)
00103   {
00104     int message_number = i + 1;
00105 
00106     //
00107     //  Read the id of each message, use the id to construct a filename
00108     //  If the file does NOT exist, read the message body and write it
00109     //  to the file
00110     //
00111 
00112     char *msg_id = popmsguid(session, message_number);
00113 
00114     if(msg_id)
00115     {
00116 
00117        FileName message_file = incoming_dir + "/" + msg_id;
00118 
00119        if(!message_file.exists())
00120        {
00121          printf("reading new message %s\n", msg_id);
00122 
00123          FILE* f = fopen(message_file.c_str(), "wb");
00124 
00125          if(!f)
00126          {
00127            message_file.remove();
00128 
00129            fprintf(stderr, "Error:  can't create '%s'\n", 
00130                            message_file.c_str()
00131                   );
00132            exit(1);
00133          }
00134 
00135          char *body = popgetmsg(session, message_number);
00136 
00137          if(body)
00138          {
00139            if(1 != fwrite(body, strlen(body), 1, f) )
00140            {
00141              fclose(f);
00142              message_file.remove();
00143              
00144              fprintf(stderr, "Error:  can't writing to '%s'\n", 
00145                              message_file.c_str()
00146                     );
00147              exit(1);
00148            }
00149 
00150            fclose(f);
00151            free(body);
00152 
00153          }
00154 
00155        }
00156        else
00157        {
00158          printf("skipping old message %s\n", msg_id);
00159 
00160          if(options.option("-delete"))
00161          {
00162            // if we are deleting the messages, then read the body
00163            // just to trigger the delete mechanism.  It would be
00164            // nice if the popdelmsg function worked, but it is broken.
00165 
00166            free(popgetmsg(session, message_number));
00167          }
00168 
00169        }
00170 
00171     }
00172 
00173   }
00174 
00175   popend(session);
00176 
00177   exit(0);
00178 
00179 }
Generated on Wed Feb 29 22:50:05 2012 for CXXUtilities by  doxygen 1.6.3