main.c

  1/*!
  2 * @file main.c
  3 *
  4 * vim: expandtab:ts=2:sts=2:sw=2
  5 *
  6 * @authors
  7 * Copyright (C) 2020 Anoxinon e.V.
  8 *
  9 * @copyright
 10 * This file is part of xmppc.
 11 *
 12 * xmppc is free software: you can redistribute it and/or modify
 13 * it under the terms of the GNU General Public License as published by
 14 * the Free Software Foundation, either version 3 of the License, or
 15 * (at your option) any later version.
 16 *
 17 * xmppc is distributed in the hope that it will be useful,
 18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 20 * GNU General Public License for more details.
 21 *
 22 * You should have received a copy of the GNU General Public License
 23 * along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
 24 *
 25 * German
 26 *
 27 * Diese Datei ist Teil von xmppc.
 28 *
 29 * xmppc ist Freie Software: Sie können es unter den Bedingungen
 30 * der GNU General Public License, wie von der Free Software Foundation,
 31 * Version 3 der Lizenz oder (nach Ihrer Wahl) jeder neueren
 32 * veröffentlichten Version, weiter verteilen und/oder modifizieren.
 33 *
 34 * xmppc wird in der Hoffnung, dass es nützlich sein wird, aber
 35 * OHNE JEDE GEWÄHRLEISTUNG, bereitgestellt; sogar ohne die implizite
 36 * Gewährleistung der MARKTFÄHIGKEIT oder EIGNUNG FÜR EINEN BESTIMMTEN ZWECK.
 37 * Siehe die GNU General Public License für weitere Details.
 38 *
 39 * Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
 40 * Programm erhalten haben. Wenn nicht, siehe <https://www.gnu.org/licenses/>.
 41 */
 42
 43/*!
 44 * @mainpage xmppc - XMPP command line
 45 *
 46 * @section whatIsXmppc What is xmppc
 47 *
 48 * xmppc is a command line program for XMPP (https://xmpp.org/).
 49 * The program has been written for the Debian GNU/Linux system (https://www.debian.org/).
 50 *
 51 * @section build Build
 52 *
 53 * @subsection dependencies Dependencies
 54 *
 55 * - The GNU C Library (glibc) - 2.28-10
 56 * - GLib - 2.58.3
 57 * - XMPP library libstrophe - 0.9.2-2
 58 * - GPGME (GnuPG Made Easy) - 1.12.0-6
 59 *
 60 * @subsection compile Compile
 61 *
 62 * @code{.bash}
 63 * ./bootstrap.sh
 64 * ./configure
 65 * make
 66 * @endcode
 67 *
 68 * @subsection documentation Documentation
 69 * @code{.bash}
 70 * cd doc
 71 * make
 72 * @endcode
 73 * @section usage Usage
 74 *
 75 * @code{.bash}
 76 * xmppc --jid <jabberid> --pwd <secret> --mode <mode> <command> <command args>
 77 * @endcode
 78 *
 79 * @code{.bash}
 80 * xmppc --jid user@domain.tld --pwd "my secret" --mode pgp chat friend@domain.tld "Hello!"
 81 * xmppc --jid user@domain.tld --pwd $(pass XMPP/domain/user) --mode omemo list
 82 * @endcode
 83 *
 84 *
 85 * @section development Development
 86 *
 87 * - \subpage module
 88 *
 89 */
 90
 91/*!
 92 * \page module Modules
 93 *
 94 * \section account Account
 95 * \section roster Roster
 96 * \section message Message
 97 * \section muc Multi-User-Chat
 98 * \section omemo OMEMO
 99 * XEP-0384: OMEMO Encryption (Version 0.3.0 (2018-07-31)). xmppc can use used
100 * to request the users OMEMO Device ID List and displays the Fingerprints of
101 * the keys.
102 *
103 * @code{.bash}
104 * xmppc --jid user@domain.tld --pwd $(pass domain.tld/user) --mode omemo
105 * @endcode
106 *
107 * @code{.bash}
108 * xmppc --jid user@domain.tld --pwd $(pass domain.tld/user) --mode omemo| gpg --clear-sign --sign-with 123ABC_MY_OPENPGP_KEY_1234 > omemo.asc
109 * @endcode
110 *
111 *
112 * \section pgp PGP
113 * XEP-0027: Current Jabber OpenPGP Usage
114 * (https://xmpp.org/extensions/xep-0027.html) is obsolete, but there are still
115 * clients which supports XEP-0027 instead of XEP-0373 / XEP-0374. This module
116 * supports sending of OpenPGP encrypted messages via XMPP's XEP-0027.
117 *
118 * @code{.bash}
119 * xmppc --jid user@domain.tld --pwd "my secret" --mode pgp friend@domain.tld "Hello!"
120 * @endcode
121 *
122 * xmppc use GnuPG's GPGME API use lookup the own key and the key of the
123 * recipient. Those two keys will be used to encrypt the message.
124 *
125 * Details \subpage module-pgp
126 *
127 */
128
129#include "config.h"
130#include <assert.h>
131#include <getopt.h>
132#include <stdarg.h>
133#include <stdio.h>
134#include <stdlib.h>
135#include <string.h>
136#include <glib.h>
137#include <strophe.h>
138
139#include "xmppc.h"
140#include "mode/account.h"
141#include "mode/message.h"
142#include "mode/muc.h"
143#include "mode/omemo.h"
144#include "mode/roster.h"
145#include "mode/pgp.h"
146#include "mode/openpgp.h"
147#include "mode/monitor.h"
148
149/*!
150 * @brief The callback structure.
151 *
152 * This struct is used to call the "execution"-function of the mode module (e.g
153 * account_execute_command, roster_execute_command, ...).
154 *
155 * @authors DebxWoody
156 * @since 0.0.2
157 * @version 1
158 *
159 */
160
161typedef struct {
162  /*! Size of arguments */
163  int argc;
164  /*! Array of C-Strings (arguments) with size of argc */
165  char **argv;
166  /*! Pointer to Execute-Handler */
167  ExecuteHandler callback;
168  /*! XMPPC context object */
169  xmppc_t *xmppc;
170} callback_t;
171
172/*!
173 * @brief Mode Mapping.
174 *
175 * This struct is used to provider the mapping of the mode name (name via
176 * command line interface of the user (e.g. -m account). The technical ID of the
177 * mode which is defined via enum xmppc_mode_t and the pointer of function which
178 * should be used to handle those mode.
179 *
180 * @authors DebxWoody
181 * @since 0.0.2
182 * @version 1
183 */
184
185struct mode_mapping {
186  const char *name;
187  xmppc_mode_t mode;
188  ExecuteHandler callback;
189};
190
191/*!
192 * @brief Mode mapping table.
193 *
194 * Mapping of mode's string, enum and function.
195 *
196 * @authors DebxWoody
197 * @since 0.0.2
198 * @version 1
199 *
200 */
201
202static struct mode_mapping map[] = {
203  /*!
204  * Account Mode Mapping
205  * @since 0.0.2
206  * @version 1
207  */
208  {"account", ACCOUNT, account_execute_command},
209  /*!
210  * roster Mode Mapping
211  * @since 0.0.2
212  * @version 1
213  */
214  {"roster", ROSTER, roster_execute_command},
215  /*!
216  * Message Mode Mapping
217  * @since 0.0.2
218  * @version 1
219  */
220  {"message", MESSAGE, message_execute_command},
221  /*!
222  * MUC Mode Mapping
223  * @since 0.0.2
224  * @version 1
225  */
226  {"muc", MUC, muc_execute_command},
227  /*!
228  * OMEMO Mode Mapping
229  * @since 0.0.2
230  * @version 1
231  */
232  {"omemo", OMEMO, omemo_execute_command},
233  /*!
234  * PGP Mode Mapping
235  * @since 0.0.2
236  * @version 1
237  */
238  {"pgp", PGP, pgp_execute_command},
239  /*!
240  * XEP-0373: OpenPGP for XMPP
241  */
242  {"openpgp", OPENPGP, openpgp_execute_command},
243  /*!
244  * Monitor
245  */
246  {"monitor", MONITOR, monitor_execute_command},
247  // End of Map
248  {NULL, 0}
249};
250
251/*!
252 * \brief Connection Handler Callback of libstrophe.
253 *
254 * This function will be called by libstrophe.
255 *
256 * \param conn See libstrophe documentation
257 * \param status See libstrophe documentation
258 * \param error See libstrophe documentation
259 * \param stream_error See libstrophe documentation
260 *
261 * \param userdata callback_t object for the mode request by user.
262 *
263 * @authors DebxWoody
264 * @since 0.0.2
265 * @version 1
266 *
267 */
268
269void conn_handler(xmpp_conn_t *const conn, const xmpp_conn_event_t status,
270                  const int error, xmpp_stream_error_t *const stream_error,
271                  void *const userdata) {
272  callback_t *callback = (callback_t *)userdata;
273
274  if (status == XMPP_CONN_CONNECT) {
275    logInfo(callback->xmppc, "Connected\n");
276    callback->callback(callback->xmppc, callback->argc, callback->argv);
277  } else {
278    xmpp_stop(xmpp_conn_get_context(conn));
279  }
280}
281
282static void _show_help();
283
284/*!
285 * \brief C-Main function
286 *
287 * - Parse argv arguments of the command line.
288 * - Checks jid and password for xmpp logging
289 * - Checks mode requested by user
290 * - All other arguments will be used as mode parameters
291 *
292 * \param argc Arguments counter
293 * \param argv Arguments vector
294 * \returns Exit Code
295 *
296 * @authors DebxWoody
297 * @since 0.0.2
298 * @version 2
299 */
300
301int main(int argc, char *argv[]) {
302#if XMPPC_DEVELOPMENT
303  printf("!!! WARNING: XMPPC is running in development mode !!!\n");
304#endif
305
306  INIT_XMPPC(xmppc);
307
308  static int verbose_flag = 0;
309  int c = 0;
310  xmppc_mode_t mode = UNKOWN;
311  char *jid = NULL;
312  char *pwd = NULL;
313
314  static struct option long_options[] = {
315      /* These options set a flag. */
316      {"verbose", no_argument, &verbose_flag, 1},
317      {"help", no_argument, 0, 'h'},
318      {"config", required_argument, 0, 'c'},
319      {"jid", required_argument, 0, 'j'},
320      {"pwd", required_argument, 0, 'p'},
321      {"mode", required_argument, 0, 'm'},
322      {"file", required_argument, 0, 'f'},
323      {0, 0, 0, 0}};
324  while (c > -1) {
325    int option_index = 0;
326
327    c = getopt_long(argc, argv, "hvj:p:m:", long_options, &option_index);
328    if (c > -1) {
329      switch (c) {
330      case 'h':
331        _show_help();
332        return EXIT_SUCCESS;
333
334      case 'c':
335        printf("option -c with value `%s'\n", optarg);
336        break;
337
338      case 'f':
339        printf("option -f with value `%s'\n", optarg);
340        break;
341
342      case 'j':
343        jid = malloc(strlen(optarg) + 1);
344        strcpy(jid, optarg);
345        break;
346
347      case 'p':
348        pwd = malloc(strlen(optarg) + 1);
349        strcpy(pwd, optarg);
350        break;
351
352      case 'm':
353        for(int i = 0; map[i].name;i++ ) {
354          if (strcmp(optarg, map[i].name) == 0) {
355            mode = map[i].mode;
356            break;
357          }
358        }
359        break;
360
361      case 'v':
362        verbose_flag++;
363        break;
364
365      case '?':
366        break;
367
368      default:
369        abort();
370      }
371    }
372  }
373
374  // Loading config file
375  GKeyFile *config_file = g_key_file_new();
376  GError *error = NULL;
377  GString* configfile = g_string_new( g_get_home_dir());
378  g_string_append(configfile,"/.config/xmppc.conf");
379  gboolean configfilefound = g_key_file_load_from_file(
380    config_file,
381    configfile->str,
382    G_KEY_FILE_NONE,
383    &error);
384
385  if (!configfilefound) {
386   if(!g_error_matches(error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND)) {
387      logError(&xmppc, "Error loading key file: %s", error->message);
388      return -1;
389    }
390  } else {
391    if(jid == NULL && pwd == NULL) { 
392      logInfo(&xmppc,"Loading default account\n");
393      jid = g_key_file_get_value (config_file, "default", "jid" ,&error);
394      pwd = g_key_file_get_value (config_file, "default", "pwd" ,&error);
395    }
396  }
397
398  int paramc = argc- optind;
399  char* paramv[paramc];
400
401  for (int i = optind; i < argc; i++) {
402    char* x= malloc(strlen(argv[i])+1 *sizeof(char)  );
403    strcpy(x,argv[i]);
404    paramv[i-optind] = x;
405  }
406  xmppc_context(&xmppc, verbose_flag);
407
408  logInfo(&xmppc, "Connecting... \n");
409  xmppc_connect(&xmppc, jid, pwd);
410
411  ExecuteHandler handler = NULL;
412        for(int i = 0; map[i].name;i++ ) {
413          if (mode ==  map[i].mode) {
414            handler = map[i].callback;
415            break;
416          }
417        }
418
419  callback_t callback = {paramc, paramv, handler, &xmppc};
420
421  xmpp_connect_client(xmppc.conn, NULL, 0, conn_handler, &callback);
422
423  xmpp_run(xmppc.ctx);
424  xmpp_conn_release(xmppc.conn);
425  xmpp_ctx_free(xmppc.ctx);
426  xmpp_shutdown();
427
428  return EXIT_SUCCESS;
429}
430
431static void _show_help() {
432#ifdef XMPPC_DEVELOPMENT
433  printf("%s - Development\n", PACKAGE_STRING);
434#else
435  printf("%s\n", PACKAGE_STRING);
436#endif
437  printf("Usage: xmppc --jid <jid> --pwd <pwd> --mode <mode> <command> <parameters>\n");
438  printf("Options:\n");
439  printf("  -h / --help             Display this information.\n");
440  printf("  -j / --jid <jid>        Jabber ID\n");
441  printf("  -p / --pwd <password>   Passwort\n");
442  printf("  -m / --mode <mode>      xmppc mode\n");
443  printf("\n");
444  printf("Modes:\n");
445  printf("  -m --mode roster      xmppc roster mode\n");
446  printf("    list                  List all contacts\n");
447  printf("    export                Exports all contacts\n");
448  printf("\n");
449  printf("  -m --mode message     xmppc message mode\n");
450  printf("    chat <jid> <message>  Sending unencrypted message to jid\n");
451  printf("\n");
452  printf("  -m --mode pgp         xmppc pgp mode (XEP-0027) \n");
453  printf("    chat <jid> <message>  Sending pgp encrypted message to jid\n");
454  printf("\n");
455  printf("  -m --mode omemo       xmppc omemo mode\n");
456  printf("    list                  List the device IDs and fingerprints\n");
457  printf("\n");
458  printf("  -m --mode openpgp          xmppc openpgp mode (XEP-0373)\n");
459  printf("    signcrypt <jid> <message>  Sending pgp signed and encrypted message to jid\n");
460  printf("\n");
461  printf("  -m --mode monitor     Monitot mode");
462  printf("    stanza              Stanza Monitor\n");
463  printf("    monitor             microblog Monitor microblog (XEP-0277: Microblogging over XMPP)\n");
464  printf("\n");
465  printf("\n");
466  printf("Examples:\n");
467  printf("  Usage: xmppc --jid user@domain.tld --pwd \"secret\" --mode roster list\n");
468  printf("  Usage: xmppc --jid user@domain.tld --pwd \"secret\" --mode pgp chat friend@domain.tld \"Hello\"\n");
469
470}