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  char *account = NULL;
314
315  static struct option long_options[] = {
316      /* These options set a flag. */
317      {"verbose", no_argument, &verbose_flag, 1},
318      {"help", no_argument, 0, 'h'},
319      {"config", required_argument, 0, 'c'},
320      {"account", required_argument, 0, 'a'},
321      {"jid", required_argument, 0, 'j'},
322      {"pwd", required_argument, 0, 'p'},
323      {"mode", required_argument, 0, 'm'},
324      {"file", required_argument, 0, 'f'},
325      {0, 0, 0, 0}};
326  while (c > -1) {
327    int option_index = 0;
328
329    c = getopt_long(argc, argv, "hva:j:p:m:", long_options, &option_index);
330    if (c > -1) {
331      switch (c) {
332      case 'h':
333        _show_help();
334        return EXIT_SUCCESS;
335
336      case 'c':
337        printf("option -c with value `%s'\n", optarg);
338        break;
339
340      case 'f':
341        printf("option -f with value `%s'\n", optarg);
342        break;
343
344      case 'a':
345        account = malloc(strlen(optarg) + 1);
346        strcpy(account, optarg);
347        break;
348
349      case 'j':
350        jid = malloc(strlen(optarg) + 1);
351        strcpy(jid, optarg);
352        break;
353
354      case 'p':
355        pwd = malloc(strlen(optarg) + 1);
356        strcpy(pwd, optarg);
357        break;
358
359      case 'm':
360        for(int i = 0; map[i].name;i++ ) {
361          if (strcmp(optarg, map[i].name) == 0) {
362            mode = map[i].mode;
363            break;
364          }
365        }
366        break;
367
368      case 'v':
369        verbose_flag++;
370        break;
371
372      case '?':
373        break;
374
375      default:
376        abort();
377      }
378    }
379  }
380
381  // Loading config file
382  GKeyFile *config_file = g_key_file_new();
383  GError *error = NULL;
384  GString* configfile = g_string_new( g_get_home_dir());
385  g_string_append(configfile,"/.config/xmppc.conf");
386  gboolean configfilefound = g_key_file_load_from_file(
387    config_file,
388    configfile->str,
389    G_KEY_FILE_NONE,
390    &error);
391
392  if (!configfilefound) {
393   if(!g_error_matches(error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND)) {
394      logError(&xmppc, "Error loading key file: %s", error->message);
395      return -1;
396    }
397  } else {
398    if(jid == NULL && pwd == NULL) { 
399      logInfo(&xmppc,"Loading default account\n");
400      if( account == NULL ) {
401        account = "default";
402      }
403      jid = g_key_file_get_value (config_file, account, "jid" ,&error);
404      pwd = g_key_file_get_value (config_file, account, "pwd" ,&error);
405    }
406  }
407
408  int paramc = argc- optind;
409  char* paramv[paramc];
410
411  for (int i = optind; i < argc; i++) {
412    char* x= malloc(strlen(argv[i])+1 *sizeof(char)  );
413    strcpy(x,argv[i]);
414    paramv[i-optind] = x;
415  }
416  xmppc_context(&xmppc, verbose_flag);
417
418  logInfo(&xmppc, "Connecting... \n");
419  xmppc_connect(&xmppc, jid, pwd);
420
421  ExecuteHandler handler = NULL;
422        for(int i = 0; map[i].name;i++ ) {
423          if (mode ==  map[i].mode) {
424            handler = map[i].callback;
425            break;
426          }
427        }
428
429  callback_t callback = {paramc, paramv, handler, &xmppc};
430
431  xmpp_connect_client(xmppc.conn, NULL, 0, conn_handler, &callback);
432
433  xmpp_run(xmppc.ctx);
434  xmpp_conn_release(xmppc.conn);
435  xmpp_ctx_free(xmppc.ctx);
436  xmpp_shutdown();
437
438  return EXIT_SUCCESS;
439}
440
441static void _show_help() {
442#ifdef XMPPC_DEVELOPMENT
443  printf("%s - Development\n", PACKAGE_STRING);
444#else
445  printf("%s\n", PACKAGE_STRING);
446#endif
447  printf("Usage: xmppc --account <account> --jid <jid> --pwd <pwd> --mode <mode> <command> <parameters>\n");
448  printf("Options:\n");
449  printf("  -h / --help                Display this information.\n");
450  printf("  -j / --jid <jid>           Jabber ID\n");
451  printf("  -p / --pwd <password>      Passwort\n");
452  printf("  -a / --account <account>   Passwort\n");
453  printf("  -m / --mode <mode>         xmppc mode\n");
454  printf("\n");
455  printf("Modes:\n");
456  printf("  -m --mode roster      xmppc roster mode\n");
457  printf("    list                  List all contacts\n");
458  printf("    export                Exports all contacts\n");
459  printf("\n");
460  printf("  -m --mode message     xmppc message mode\n");
461  printf("    chat <jid> <message>  Sending unencrypted message to jid\n");
462  printf("\n");
463  printf("  -m --mode pgp         xmppc pgp mode (XEP-0027) \n");
464  printf("    chat <jid> <message>  Sending pgp encrypted message to jid\n");
465  printf("\n");
466  printf("  -m --mode omemo       xmppc omemo mode\n");
467  printf("    list                  List the device IDs and fingerprints\n");
468  printf("\n");
469  printf("  -m --mode openpgp          xmppc openpgp mode (XEP-0373)\n");
470  printf("    signcrypt <jid> <message>  Sending pgp signed and encrypted message to jid\n");
471  printf("\n");
472  printf("  -m --mode monitor     Monitot mode");
473  printf("    stanza              Stanza Monitor\n");
474  printf("    monitor             microblog Monitor microblog (XEP-0277: Microblogging over XMPP)\n");
475  printf("\n");
476  printf("\n");
477  printf("Examples:\n");
478  printf("  Usage: xmppc --jid user@domain.tld --pwd \"secret\" --mode roster list\n");
479  printf("  Usage: xmppc --jid user@domain.tld --pwd \"secret\" --mode pgp chat friend@domain.tld \"Hello\"\n");
480
481}