test2.cxx
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
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
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
00108
00109
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
00163
00164
00165
00166 free(popgetmsg(session, message_number));
00167 }
00168
00169 }
00170
00171 }
00172
00173 }
00174
00175 popend(session);
00176
00177 exit(0);
00178
00179 }