Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

aqbanking / src / tools / aqbanking-cli / main.c @ 0f293628

History | View | Annotate | Download (13.1 KB)

1
/***************************************************************************
2
 begin       : Tue May 03 2005
3
 copyright   : (C) 2018 by Martin Preuss
4
 email       : martin@libchipcard.de
5

6
 ***************************************************************************
7
 *          Please see toplevel file COPYING for license details           *
8
 ***************************************************************************/
9

    
10
#ifdef HAVE_CONFIG_H
11
# include <config.h>
12
#endif
13

    
14
#include <gwenhywfar/logger.h>
15
#include <gwenhywfar/db.h>
16
#include <gwenhywfar/debug.h>
17
#include <gwenhywfar/cgui.h>
18

    
19
#include <gwenhywfar/gwenhywfar.h>
20
#include <gwenhywfar/logger.h>
21
#include <gwenhywfar/debug.h>
22
#include <gwenhywfar/directory.h>
23
#include <aqbanking/banking.h>
24
#include <aqbanking/abgui.h>
25

    
26
#include "globals.h"
27

    
28

    
29

    
30
static void cmdAddHelpStr(GWEN_BUFFER *ubuf,
31
                          const char* cmdname,
32
                          const char* cmdhelp)
33
{
34
  // Indentation of the command: one space
35
  GWEN_Buffer_AppendString(ubuf, " ");
36
  GWEN_Buffer_AppendString(ubuf, cmdname);
37
  GWEN_Buffer_AppendString(ubuf, ":\n");
38
  // Indentation of the help: three spaces
39
  GWEN_Buffer_AppendString(ubuf, "   ");
40
  GWEN_Buffer_AppendString(ubuf, cmdhelp);
41
  GWEN_Buffer_AppendString(ubuf, "\n");
42
}
43

    
44

    
45
int main(int argc, char **argv) {
46
  GWEN_DB_NODE *db;
47
  const char *cmd;
48
  int rv;
49
  AB_BANKING *ab;
50
  GWEN_GUI *gui;
51
  const char *ctrlBackend=NULL;
52
  int nonInteractive=0;
53
  int acceptValidCerts=0;
54
  const char *pinFile;
55
  const char *cfgDir;
56
  const char *s;
57
  const GWEN_ARGS args[]={
58
  {
59
    GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
60
    GWEN_ArgsType_Char,           /* type */
61
    "cfgdir",                     /* name */
62
    0,                            /* minnum */
63
    1,                            /* maxnum */
64
    "D",                          /* short option */
65
    "cfgdir",                     /* long option */
66
    I18S("Specify the configuration folder"),
67
    I18S("Specify the configuration folder")
68
  },
69
  {
70
    0,                            /* flags */
71
    GWEN_ArgsType_Int,            /* type */
72
    "nonInteractive",             /* name */
73
    0,                            /* minnum */
74
    1,                            /* maxnum */
75
    "n",                          /* short option */
76
    "noninteractive",             /* long option */
77
    "Select non-interactive mode",/* short description */
78
    "Select non-interactive mode.\n"        /* long description */
79
    "This automatically returns a confirmative answer to any non-critical\n"
80
    "message."
81
  },
82
  {
83
    0,                            /* flags */
84
    GWEN_ArgsType_Int,            /* type */
85
    "acceptValidCerts",           /* name */
86
    0,                            /* minnum */
87
    1,                            /* maxnum */
88
    "A",                          /* short option */
89
    "acceptvalidcerts",           /* long option */
90
    "Automatically accept all valid TLS certificate",
91
    "Automatically accept all valid TLS certificate"
92
  },
93
  {
94
    GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
95
    GWEN_ArgsType_Char,           /* type */
96
    "charset",                    /* name */
97
    0,                            /* minnum */
98
    1,                            /* maxnum */
99
    0,                            /* short option */
100
    "charset",                    /* long option */
101
    "Specify the output character set",       /* short description */
102
    "Specify the output character set"        /* long description */
103
  },
104
  {
105
    GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
106
    GWEN_ArgsType_Char,           /* type */
107
    "pinfile",                    /* name */
108
    0,                            /* minnum */
109
    1,                            /* maxnum */
110
    "P",                          /* short option */
111
    "pinfile",                    /* long option */
112
    "Specify the PIN file",       /* short description */
113
    "Specify the PIN file"        /* long description */
114
  },
115
  {
116
    GWEN_ARGS_FLAGS_HAS_ARGUMENT,   /* flags */
117
    GWEN_ArgsType_Char,             /* type */
118
    "control",                      /* name */
119
    0,                              /* minnum */
120
    1,                              /* maxnum */
121
    0,                              /* short option */
122
    "control",                      /* long option */
123
    "backend for control function", /* short description */
124
    "Call the CONTROL function of the given backend"          /* long description */
125
  },
126
  {
127
    GWEN_ARGS_FLAGS_HELP | GWEN_ARGS_FLAGS_LAST, /* flags */
128
    GWEN_ArgsType_Int,            /* type */
129
    "help",                       /* name */
130
    0,                            /* minnum */
131
    0,                            /* maxnum */
132
    "h",                          /* short option */
133
    "help",
134
    I18S("Show this help screen. For help on commands, "
135
         "run aqbanking-cli <COMMAND> --help."),
136
    I18S("Show this help screen. For help on commands, run aqbanking-cli <COMMAND> --help.")
137
  }
138
  };
139

    
140
  rv=GWEN_Init();
141
  if (rv) {
142
    fprintf(stderr, "ERROR: Unable to init Gwen.\n");
143
    exit(2);
144
  }
145

    
146
  GWEN_Logger_Open(0, "aqbanking-cli", 0,
147
                   GWEN_LoggerType_Console,
148
                   GWEN_LoggerFacility_User);
149
  GWEN_Logger_SetLevel(0, GWEN_LoggerLevel_Warning);
150

    
151
  rv=GWEN_I18N_BindTextDomain_Dir(PACKAGE, LOCALEDIR);
152
  if (rv) {
153
    DBG_ERROR(0, "Could not bind textdomain (%d)", rv);
154
  }
155
  else {
156
    rv=GWEN_I18N_BindTextDomain_Codeset(PACKAGE, "UTF-8");
157
    if (rv) {
158
      DBG_ERROR(0, "Could not set codeset (%d)", rv);
159
    }
160
  }
161

    
162
  db=GWEN_DB_Group_new("arguments");
163
  rv=GWEN_Args_Check(argc, argv, 1,
164
                     GWEN_ARGS_MODE_ALLOW_FREEPARAM |
165
                     GWEN_ARGS_MODE_STOP_AT_FREEPARAM,
166
                     args,
167
                     db);
168
  if (rv==GWEN_ARGS_RESULT_ERROR) {
169
    fprintf(stderr, "ERROR: Could not parse arguments main\n");
170
    GWEN_DB_Group_free(db);
171
    return 1;
172
  }
173
  else if (rv==GWEN_ARGS_RESULT_HELP) {
174
    GWEN_BUFFER *ubuf;
175

    
176
    ubuf=GWEN_Buffer_new(0, 1024, 0, 1);
177
    GWEN_Buffer_AppendString(ubuf, I18N("This is version "));
178
    GWEN_Buffer_AppendString(ubuf, AQHBCI_VERSION_STRING "\n");
179
    GWEN_Buffer_AppendString(ubuf,
180
                             I18N("Usage: "));
181
    GWEN_Buffer_AppendString(ubuf, argv[0]);
182
    GWEN_Buffer_AppendString(ubuf,
183
                             I18N(" [GLOBAL OPTIONS] COMMAND "
184
                                  "[LOCAL OPTIONS]\n"));
185
    GWEN_Buffer_AppendString(ubuf,
186
                             I18N("\nGlobal Options:\n"));
187
    if (GWEN_Args_Usage(args, ubuf, GWEN_ArgsOutType_Txt)) {
188
      fprintf(stderr, "ERROR: Could not create help string\n");
189
      GWEN_DB_Group_free(db);
190
      return 1;
191
    }
192
    GWEN_Buffer_AppendString(ubuf,
193
                             I18N("\nCommands:\n"));
194
    cmdAddHelpStr(ubuf, "listaccs",
195
                  I18N("Prints the list of accounts"));
196

    
197
    cmdAddHelpStr(ubuf, "listbal",
198
                  I18N("Export balances from a context file."));
199

    
200
    cmdAddHelpStr(ubuf, "listtrans",
201
                  I18N("List transactions or transfers from a context file."));
202

    
203
    cmdAddHelpStr(ubuf, "listtransfers",
204
                  I18N("Export transactions from a context file which match certain status."));
205

    
206
    cmdAddHelpStr(ubuf, "request",
207
                  I18N("Requests transactions, balances, standing orders etc."));
208

    
209
    cmdAddHelpStr(ubuf, "chkiban",
210
                  I18N("Check an IBAN"));
211

    
212
    cmdAddHelpStr(ubuf, "import",
213
                  I18N("Import a file into an import context file"));
214

    
215
    cmdAddHelpStr(ubuf, "export",
216
                  I18N("Export an import context file into a file (previously \"listtrans\")"));
217

    
218
    cmdAddHelpStr(ubuf, "transfer",
219
                  I18N("Issue a single transfer (data from command line)"));
220

    
221
    cmdAddHelpStr(ubuf, "transfers",
222
                  I18N("Issue a number of transfers (data from a file)"));
223

    
224
    cmdAddHelpStr(ubuf, "sepatransfer",
225
                  I18N("Issue a single SEPA transfer (data from command line)"));
226

    
227
    cmdAddHelpStr(ubuf, "sepatransfers",
228
                  I18N("Issue a number of SEPA transfers (data from a file)"));
229

    
230
    cmdAddHelpStr(ubuf, "debitnote",
231
                  I18N("Issue a single debit note (data from command line)"));
232

    
233
    cmdAddHelpStr(ubuf, "debitnotes",
234
                  I18N("Issue a number of debit notes (data from a file)"));
235

    
236
    cmdAddHelpStr(ubuf, "sepadebitnote",
237
                  I18N("Issue a single SEPA debit note (data from command line)"));
238

    
239
    cmdAddHelpStr(ubuf, "sepaflashdebitnote",
240
                  I18N("Issue a single flash SEPA debit note COR1 (data from command line)"));
241

    
242
    cmdAddHelpStr(ubuf, "sepadebitnotes",
243
                  I18N("Issue a number of SEPA debit notes (data from a file)"));
244

    
245
    cmdAddHelpStr(ubuf, "addtransfer",
246
                  I18N("Add a transfer to an existing import context file"));
247

    
248
    cmdAddHelpStr(ubuf, "addsepadebitnote",
249
                  I18N("Add a SEPA debit note to an existing import context file"));
250

    
251
    cmdAddHelpStr(ubuf, "sepasto",
252
                  I18N("Manage SEPA standing orders"));
253

    
254
    cmdAddHelpStr(ubuf, "fillgaps",
255
                  I18N("Fill gaps in an import context file from configuration settings"));
256

    
257
    cmdAddHelpStr(ubuf, "listprofiles",
258
                  I18N("Print existing profiles"));
259

    
260
    cmdAddHelpStr(ubuf, "versions",
261
                  I18N("Print the program and library versions"));
262

    
263
    GWEN_Buffer_AppendString(ubuf, "\n");
264

    
265
    fprintf(stderr, "%s\n", GWEN_Buffer_GetStart(ubuf));
266
    GWEN_Buffer_free(ubuf);
267
    GWEN_DB_Group_free(db);
268
    return 0;
269
  }
270
  if (rv) {
271
    argc-=rv-1;
272
    argv+=rv-1;
273
  }
274

    
275
  nonInteractive=GWEN_DB_GetIntValue(db, "nonInteractive", 0, 0);
276
  acceptValidCerts=GWEN_DB_GetIntValue(db, "acceptValidCerts", 0, 0);
277
  cfgDir=GWEN_DB_GetCharValue(db, "cfgdir", 0, 0);
278
  ctrlBackend=GWEN_DB_GetCharValue(db, "control", 0, 0);
279

    
280
  gui=GWEN_Gui_CGui_new();
281
  s=GWEN_DB_GetCharValue(db, "charset", 0, NULL);
282
  if (s && *s)
283
    GWEN_Gui_SetCharSet(gui, s);
284

    
285
  if (nonInteractive)
286
    GWEN_Gui_AddFlags(gui, GWEN_GUI_FLAGS_NONINTERACTIVE);
287
  else
288
    GWEN_Gui_SubFlags(gui, GWEN_GUI_FLAGS_NONINTERACTIVE);
289

    
290
  if (acceptValidCerts)
291
    GWEN_Gui_AddFlags(gui, GWEN_GUI_FLAGS_ACCEPTVALIDCERTS);
292
  else
293
    GWEN_Gui_SubFlags(gui, GWEN_GUI_FLAGS_ACCEPTVALIDCERTS);
294

    
295
  pinFile=GWEN_DB_GetCharValue(db, "pinFile", 0, NULL);
296
  if (pinFile) {
297
    GWEN_DB_NODE *dbPins;
298

    
299
    dbPins=GWEN_DB_Group_new("pins");
300
    if (GWEN_DB_ReadFile(dbPins, pinFile,
301
                         GWEN_DB_FLAGS_DEFAULT |
302
                         GWEN_PATH_FLAGS_CREATE_GROUP)) {
303
      fprintf(stderr, "Error reading pinfile \"%s\"\n", pinFile);
304
      GWEN_DB_Group_free(dbPins);
305
      GWEN_DB_Group_free(db);
306
      return 2;
307
    }
308
    GWEN_Gui_SetPasswordDb(gui, dbPins, 1);
309
  }
310

    
311
  GWEN_Gui_SetGui(gui);
312

    
313
  ab=AB_Banking_new("aqbanking-cli", cfgDir, 0);
314
  AB_Gui_Extend(gui, ab);
315

    
316
  if (ctrlBackend && *ctrlBackend) {
317
    rv=control(ab, ctrlBackend, db, argc, argv);
318
  }
319
  else {
320
    cmd=GWEN_DB_GetCharValue(db, "params", 0, 0);
321
    if (!cmd) {
322
      fprintf(stderr, "ERROR: Command needed.\n");
323
      GWEN_DB_Group_free(db);
324
      return 1;
325
    }
326

    
327
    if (strcasecmp(cmd, "listaccs")==0 ||
328
        strcasecmp(cmd, "listaccounts")==0) {
329
      rv=listAccs(ab, db, argc, argv);
330
    }
331
    else if (strcasecmp(cmd, "listbal")==0) {
332
      rv=listBal(ab, db, argc, argv);
333
    }
334
    else if (strcasecmp(cmd, "listtrans")==0) {
335
      rv=listTrans(ab, db, argc, argv);
336
    }
337
    else if (strcasecmp(cmd, "listtransfers")==0) {
338
      fprintf(stderr, "ERROR: Please use the commands \"listtrans\" or \"export\" and specify the transaction type via \"-tt TYPE\"\n");
339
      GWEN_DB_Group_free(db);
340
      return 1;
341
    }
342
    else if (strcasecmp(cmd, "request")==0) {
343
      rv=request(ab, db, argc, argv);
344
    }
345
    else if (strcasecmp(cmd, "chkiban")==0) {
346
      rv=chkIban(ab, db, argc, argv);
347
    }
348
    else if (strcasecmp(cmd, "import")==0) {
349
      rv=import(ab, db, argc, argv);
350
    }
351
    else if (strcasecmp(cmd, "export")==0) {
352
      rv=exportCtx(ab, db, argc, argv);
353
    }
354
    else if (strcasecmp(cmd, "sepatransfer")==0) {
355
      rv=sepaTransfer(ab, db, argc, argv);
356
    }
357
    else if (strcasecmp(cmd, "sepatransfers")==0) {
358
      rv=sepaMultiJobs(ab, db, argc, argv, AQBANKING_TOOL_SEPA_TRANSFERS);
359
    }
360
    else if (strcasecmp(cmd, "sepadebitnote")==0) {
361
      rv=sepaDebitNote(ab, db, argc, argv, 0);
362
    }
363
    else if (strcasecmp(cmd, "sepaFlashDebitNote")==0) {
364
      rv=sepaDebitNote(ab, db, argc, argv, 1);
365
    }
366
    else if (strcasecmp(cmd, "sepadebitnotes")==0) {
367
      rv=sepaMultiJobs(ab, db, argc, argv, AQBANKING_TOOL_SEPA_DEBITNOTES);
368
    }
369
    else if (strcasecmp(cmd, "addtrans")==0) {
370
      rv=addTransaction(ab, db, argc, argv);
371
    }
372
    else if (strcasecmp(cmd, "addsepadebitnote")==0) {
373
      rv=addSepaDebitNote(ab, db, argc, argv, 0);
374
    }
375
    else if (strcasecmp(cmd, "addFlashSepadebitnote")==0) {
376
      rv=addSepaDebitNote(ab, db, argc, argv, 1);
377
    }
378
    else if (strcasecmp(cmd, "sepasto")==0) {
379
      rv=sepaRecurTransfer(ab, db, argc, argv);
380
    }
381
    else if (strcasecmp(cmd, "fillgaps")==0) {
382
      rv=fillGaps(ab, db, argc, argv);
383
    }
384
    else if (strcasecmp(cmd, "listprofiles")==0) {
385
      rv=listProfiles(ab, db, argc, argv);
386
    }
387
    else if (strcasecmp(cmd, "versions")==0) {
388
      rv=versions(ab, db, argc, argv);
389
    }
390
    else {
391
      fprintf(stderr, "ERROR: Unknown command \"%s\".\n", cmd);
392
      rv=1;
393
    }
394
  }
395

    
396
  GWEN_DB_Group_free(db);
397
  return rv;
398
}
399

    
400

    
401