LibOFX: ofxconnect.cpp Source File

Main Page | Class Hierarchy | Data Structures | File List | Data Fields | Globals

# ofxconnect.cpp

Go to the documentation of this file.

    00001 /***************************************************************************
    00002          ofx_connect.cpp
    00003                              -------------------
    00004     copyright            : (C) 2005 by Ace Jones
    00005     email                : acejones@users.sourceforge.net
    00006 ***************************************************************************/
    00023 /***************************************************************************
    00024  *                                                                         *
    00025  *   This program is free software; you can redistribute it and/or modify  *
    00026  *   it under the terms of the GNU General Public License as published by  *
    00027  *   the Free Software Foundation; either version 2 of the License, or     *
    00028  *   (at your option) any later version.                                   *
    00029  *                                                                         *
    00030  ***************************************************************************/
    00031 #include <iostream>
    00032 #include <string>
    00033 #include "libofx.h"
    00034 #include <config.h>             /* Include config constants, e.g., VERSION TF */
    00035 #include <curl/curl.h>
    00036 #include <stdio.h>
    00037 #include <stdlib.h>
    00038 #include <unistd.h>
    00039
    00040 #include "cmdline.h" /* Gengetopt generated parser */
    00041
    00042 using namespace std;
    00043
    00044 bool post(const char* request, const char* url, const char* filename)
    00045 {
    00046   CURL *curl = curl_easy_init();
    00047   if(! curl)
    00048     return false;
    00049
    00050   unlink("tmpout");
    00051   FILE* file = fopen(filename,"wb");
    00052   if (! file )
    00053   {
    00054     curl_easy_cleanup(curl);
    00055     return false;
    00056   }
    00057
    00058   curl_easy_setopt(curl, CURLOPT_URL, url);
    00059   curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request);
    00060
    00061   struct curl_slist *headerlist=NULL;
    00062   headerlist=curl_slist_append(headerlist, "Content-type: application/x-ofx");
    00063   headerlist=curl_slist_append(headerlist, "Accept: */*, application/x-ofx");
    00064
    00065   curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
    00066   curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
    00067   curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)file);
    00068
    00069   CURLcode res = curl_easy_perform(curl);
    00070
    00071   curl_easy_cleanup(curl);
    00072   curl_slist_free_all (headerlist);
    00073
    00074   fclose(file);
    00075
    00076   return true;
    00077 }
    00078
    00079 int main (int argc, char *argv[])
    00080 {
    00081   gengetopt_args_info args_info;
    00082
    00083   if (cmdline_parser (argc, argv, &args_info) != 0)
    00084     exit(1) ;
    00085
    00086   if ( argc == 1 )
    00087   {
    00088     cmdline_parser_print_help();
    00089     exit(1);
    00090   }
    00091
    00092   if ( args_info.inputs_num > 0 )
    00093   {
    00094     cout << "file " << args_info.inputs[0] << endl;
    00095   }
    00096   else
    00097   {
    00098     cerr << "ERROR: You must specify an output file" << endl;
    00099   }
    00100
    00101   // Get the FI Login information
    00102   //
    00103   OfxFiLogin fi;
    00104   memset(&fi,0,sizeof(OfxFiLogin));
    00105   bool ok = true;
    00106   if ( args_info.fid_given )
    00107   {
    00108     cerr << "fid " <<  args_info.fid_arg << endl;
    00109     strncpy(fi.fid,args_info.fid_arg,OFX_FID_LENGTH-1);
    00110   }
    00111   else
    00112   {
    00113     cerr << "ERROR: --fid is required" << endl;
    00114     ok = false;
    00115   }
    00116
    00117   if ( args_info.org_given )
    00118   {
    00119     cerr << "org " << args_info.org_arg << endl;
    00120     strncpy(fi.org,args_info.org_arg,OFX_ORG_LENGTH-1);
    00121   }
    00122   else
    00123   {
    00124     cerr << "ERROR: --org is required" << endl;
    00125     ok = false;
    00126   }
    00127
    00128   if ( args_info.user_given )
    00129   {
    00130     cerr << "user " << args_info.user_arg << endl;
    00131     strncpy(fi.userid,args_info.user_arg,OFX_USERID_LENGTH-1);
    00132   }
    00133   else
    00134   {
    00135     cerr << "ERROR: --user is required" << endl;
    00136     ok = false;
    00137   }
    00138
    00139   if ( args_info.pass_given )
    00140   {
    00141     cerr << "pass " << args_info.pass_arg << endl;
    00142     strncpy(fi.userpass,args_info.pass_arg,OFX_USERPASS_LENGTH-1);
    00143   }
    00144   else
    00145   {
    00146     cerr << "ERROR: --pass is required" << endl;
    00147     ok = false;
    00148   }
    00149
    00150   if ( args_info.statement_req_flag )
    00151   {
    00152     cerr << "Statement request" << endl;
    00153
    00154     OfxAccountInfo account;
    00155     memset(&account,0,sizeof(OfxAccountInfo));
    00156
    00157     if ( args_info.bank_given )
    00158     {
    00159       cerr << "bank " << args_info.bank_arg << endl;
    00160       strncpy(account.bankid,args_info.bank_arg,OFX_BANKID_LENGTH-1);
    00161     }
    00162     else
    00163     {
    00164       if ( args_info.type_given && args_info.type_arg == 1 )
    00165       {
    00166         cerr << "ERROR: --bank is required for a bank request" << endl;
    00167         ok = false;
    00168       }
    00169     }
    00170
    00171     if ( args_info.broker_given )
    00172     {
    00173       cerr << "broker " << args_info.broker_arg << endl;
    00174       strncpy(account.brokerid,args_info.broker_arg,OFX_BROKERID_LENGTH-1);
    00175     }
    00176     else
    00177     {
    00178       if ( args_info.type_given && args_info.type_arg == 2 )
    00179       {
    00180         cerr << "ERROR: --broker is required for an investment statement request" << endl;
    00181         ok = false;
    00182       }
    00183     }
    00184
    00185     if ( args_info.acct_given )
    00186     {
    00187       cerr << "acct " << args_info.acct_arg << endl;
    00188       strncpy(account.accountid,args_info.acct_arg,OFX_ACCOUNT_ID_LENGTH-1);
    00189     }
    00190     else
    00191     {
    00192       cerr << "ERROR: --acct is required for a statement request" << endl;
    00193       ok = false;
    00194     }
    00195
    00196     if ( args_info.type_given )
    00197     {
    00198       cerr << "type " << args_info.type_arg << endl;
    00199       account.type = static_cast<AccountType>(args_info.type_arg);
    00200     }
    00201     else
    00202     {
    00203       cerr << "ERROR: --type is required for a statement request" << endl;
    00204       ok = false;
    00205     }
    00206
    00207     if ( args_info.past_given )
    00208     {
    00209       cerr << "past " << args_info.past_arg << endl;
    00210     }
    00211     else
    00212     {
    00213       cerr << "ERROR: --past is required for a statement request" << endl;
    00214       ok = false;
    00215     }
    00216
    00217     if ( ok )
    00218     {
    00219       char* request = libofx_request_statement( &fi, &account, time(NULL) - args_info.past_arg * 86400L );
    00220
    00221       if ( args_info.url_given )
    00222         post(request,args_info.url_arg,args_info.inputs[0]);
    00223       else
    00224         cout << request;
    00225
    00226       free(request);
    00227     }
    00228   }
    00229
    00230   if ( args_info.accountinfo_req_flag )
    00231   {
    00232     if ( ok )
    00233     {
    00234       char* request = libofx_request_accountinfo( &fi );
    00235
    00236       if ( args_info.url_given )
    00237         post(request,args_info.url_arg,args_info.inputs[0]);
    00238       else
    00239         cout << request;
    00240
    00241       free(request);
    00242     }
    00243   }
    00244
    00245   return 0;
    00246 }
    00247
    00248
    00249 // vim:cin:si:ai:et:ts=2:sw=2:
    00250