File: ofxpartner.cpp

package info (click to toggle)
libofx 1%3A0.10.9-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,644 kB
  • sloc: cpp: 8,101; ansic: 2,298; xml: 265; makefile: 224; sh: 43
file content (232 lines) | stat: -rw-r--r-- 7,640 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/***************************************************************************
                             ofx_partner.cpp
                             -------------------
    copyright            : (C) 2005 by Ace Jones
    email                : acejones@users.sourceforge.net
***************************************************************************/
/**@file
 * \brief Methods for connecting to the OFX partner server to retrieve
 * OFX server information
*/
/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <libofx.h>

//#ifdef HAVE_LIBCURL
#include <curl/curl.h>
//#endif

#include "ofxpartner.h"
#include "nodeparser.h"

#include <sys/stat.h>

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <string.h>

using std::string;
using std::vector;
using std::cout;
using std::endl;

namespace OfxPartner
{
bool post(const string& request, const string& url, const string& filename);

const string kBankFilename = "ofx-bank-index.xml";
const string kCcFilename = "ofx-cc-index.xml";
const string kInvFilename = "ofx-inv-index.xml";

void ValidateIndexCache(void)
{
  // TODO Check whether these files exist and are recent enough before getting them again

  struct stat filestats;
  if ( stat( kBankFilename.c_str(), &filestats ) || difftime(time(0), filestats.st_mtime) > 7.0 * 24.0 * 60.0 * 60.0 )
    post("T=1&S=*&R=1&O=0&TEST=0", "http://moneycentral.msn.com/money/2005/mnynet/service/ols/filist.aspx?SKU=3&VER=6", kBankFilename);
  if ( stat( kCcFilename.c_str(), &filestats ) || difftime(time(0), filestats.st_mtime) > 7.0 * 24.0 * 60.0 * 60.0 )
    post("T=2&S=*&R=1&O=0&TEST=0", "http://moneycentral.msn.com/money/2005/mnynet/service/ols/filist.aspx?SKU=3&VER=6", kCcFilename);
  if ( stat( kInvFilename.c_str(), &filestats ) || difftime(time(0), filestats.st_mtime) > 7.0 * 24.0 * 60.0 * 60.0 )
    post("T=3&S=*&R=1&O=0&TEST=0", "http://moneycentral.msn.com/money/2005/mnynet/service/ols/filist.aspx?SKU=3&VER=6", kInvFilename);
}

vector<string> BankNames(void)
{
  vector<string> result;

  // Make sure the index files are up to date
  ValidateIndexCache();

  xmlpp::DomParser parser;
  parser.set_substitute_entities();
  parser.parse_file(kBankFilename);
  if ( parser )
  {
    vector<string> names = NodeParser(parser).Path("fi/prov/name").Text();
    result.insert(result.end(), names.begin(), names.end());
  }
  parser.parse_file(kCcFilename);
  if ( parser )
  {
    vector<string> names = NodeParser(parser).Path("fi/prov/name").Text();
    result.insert(result.end(), names.begin(), names.end());
  }
  parser.parse_file(kInvFilename);
  if ( parser )
  {
    vector<string> names = NodeParser(parser).Path("fi/prov/name").Text();
    result.insert(result.end(), names.begin(), names.end());
  }

  // Add Innovision
  result.push_back("Innovision");

  // sort the list and remove duplicates, to return one unified list of all supported banks
  sort(result.begin(), result.end());
  result.erase(unique(result.begin(), result.end()), result.end());
  return result;
}

vector<string> FipidForBank(const string& bank)
{
  vector<string> result;

  xmlpp::DomParser parser;
  parser.set_substitute_entities();
  parser.parse_file(kBankFilename);
  if ( parser )
  {
    vector<string> fipids = NodeParser(parser).Path("fi/prov").Select("name", bank).Path("guid").Text();
    if ( ! fipids.back().empty() )
      result.insert(result.end(), fipids.begin(), fipids.end());
  }
  parser.parse_file(kCcFilename);
  if ( parser )
  {
    vector<string> fipids = NodeParser(parser).Path("fi/prov").Select("name", bank).Path("guid").Text();
    if ( ! fipids.back().empty() )
      result.insert(result.end(), fipids.begin(), fipids.end());
  }
  parser.parse_file(kInvFilename);
  if ( parser )
  {
    vector<string> fipids = NodeParser(parser).Path("fi/prov").Select("name", bank).Path("guid").Text();
    if ( ! fipids.back().empty() )
      result.insert(result.end(), fipids.begin(), fipids.end());
  }

  // the fipid for Innovision is 1.
  if ( bank == "Innovision" )
    result.push_back("1");

  sort(result.begin(), result.end());
  result.erase(unique(result.begin(), result.end()), result.end());

  return result;
}

OfxFiServiceInfo ServiceInfo(const std::string& fipid)
{
  OfxFiServiceInfo result;
  memset(&result, 0, sizeof(OfxFiServiceInfo));

  // Hard-coded values for Innovision test server
  if ( fipid == "1" )
  {
    strncpy(result.fid, "00000", OFX_FID_LENGTH - 1);
    strncpy(result.org, "ReferenceFI", OFX_ORG_LENGTH - 1);
    strncpy(result.url, "http://ofx.innovision.com", OFX_URL_LENGTH - 1);
    result.accountlist = 1;
    result.statements = 1;
    result.billpay = 1;
    result.investments = 1;

    return result;
  }

  string url = "http://moneycentral.msn.com/money/2005/mnynet/service/olsvcupd/OnlSvcBrandInfo.aspx?MSNGUID=&GUID=%1&SKU=3&VER=6";
  url.replace(url.find("%1"), 2, fipid);

  // TODO: Check whether this file exists and is recent enough before getting it again
  string guidfile = "fipid-%1.xml";
  guidfile.replace(guidfile.find("%1"), 2, fipid);

  struct stat filestats;
  if ( stat( guidfile.c_str(), &filestats ) || difftime(time(0), filestats.st_mtime) > 7.0 * 24.0 * 60.0 * 60.0 )
    post("", url.c_str(), guidfile.c_str());

  // Print the FI details
  xmlpp::DomParser parser;
  parser.set_substitute_entities();
  parser.parse_file(guidfile);
  if ( parser )
  {
    NodeParser nodes(parser);

    strncpy(result.fid, nodes.Path("ProviderSettings/FID").Text().back().c_str(), OFX_FID_LENGTH - 1);
    strncpy(result.org, nodes.Path("ProviderSettings/Org").Text().back().c_str(), OFX_ORG_LENGTH - 1);
    strncpy(result.url, nodes.Path("ProviderSettings/ProviderURL").Text().back().c_str(), OFX_URL_LENGTH - 1);
    result.accountlist = (nodes.Path("ProviderSettings/AcctListAvail").Text().back() == "1");
    result.statements = (nodes.Path("BankingCapabilities/Bank").Text().back() == "1");
    result.billpay = (nodes.Path("BillPayCapabilities/Pay").Text().back() == "1");
    result.investments = (nodes.Path("InvestmentCapabilities/BrkStmt").Text().back() == "1");
  }
  return result;
}

bool post(const string& request, const string& url, const string& filename)
{
#if 1 //#ifdef HAVE_LIBCURL
  CURL *curl = curl_easy_init();
  if (! curl)
    return false;

  remove(filename.c_str());
  FILE* file = fopen(filename.c_str(), "wb");
  if (! file )
  {
    curl_easy_cleanup(curl);
    return false;
  }

  curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
  if ( request.length() )
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request.c_str());
  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
  curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)file);

  /*CURLcode res =*/
  curl_easy_perform(curl);

  curl_easy_cleanup(curl);

  fclose(file);

  return true;
#else
  request;
  url;
  filename;
  cerr << "ERROR: libox must be configured with libcurl to post this request" << endl;
  return false;
#endif
}

} // namespace OfxPartner


// vim:cin:si:ai:et:ts=2:sw=2: