messenger-cli

Command-line user interface for GNUnet Messenger
Log | Files | Refs | README | LICENSE

application.c (3696B)


      1 /*
      2    This file is part of GNUnet.
      3    Copyright (C) 2022--2025 GNUnet e.V.
      4 
      5    GNUnet is free software: you can redistribute it and/or modify it
      6    under the terms of the GNU Affero General Public License as published
      7    by the Free Software Foundation, either version 3 of the License,
      8    or (at your option) any later version.
      9 
     10    GNUnet is distributed in the hope that it will be useful, but
     11    WITHOUT ANY WARRANTY; without even the implied warranty of
     12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13    Affero General Public License for more details.
     14 
     15    You should have received a copy of the GNU Affero General Public License
     16    along with this program.  If not, see <http://www.gnu.org/licenses/>.
     17 
     18    SPDX-License-Identifier: AGPL3.0-or-later
     19  */
     20 /*
     21  * @author Tobias Frisch
     22  * @file application.c
     23  */
     24 
     25 #include "application.h"
     26 
     27 #include "util.h"
     28 
     29 #ifndef MESSENGER_CLI_BINARY
     30 #define MESSENGER_CLI_BINARY "messenger_cli"
     31 #endif
     32 
     33 #ifndef MESSENGER_CLI_VERSION
     34 #define MESSENGER_CLI_VERSION "unknown"
     35 #endif
     36 
     37 #ifndef MESSENGER_CLI_DESC
     38 #define MESSENGER_CLI_DESC "A CLI for the Messenger service of GNUnet."
     39 #endif
     40 
     41 void
     42 application_clear(MESSENGER_Application *app)
     43 {
     44   app->accounts.window = NULL;
     45   app->chats.window = NULL;
     46   app->current.members.window = NULL;
     47   app->current.messages.window = NULL;
     48 }
     49 
     50 void
     51 application_init(MESSENGER_Application *app,
     52                  int argc,
     53                  char **argv)
     54 {
     55   const struct GNUNET_OS_ProjectData *data =
     56     GNUNET_OS_project_data_gnunet();
     57 
     58   const struct GNUNET_GETOPT_CommandLineOption options [] = {
     59     GNUNET_GETOPT_option_version(MESSENGER_CLI_VERSION),
     60     GNUNET_GETOPT_option_help(data, MESSENGER_CLI_DESC),
     61     GNUNET_GETOPT_OPTION_END
     62   };
     63 
     64   memset(app, 0, sizeof(*app));
     65 
     66   app->argc = argc;
     67   app->argv = argv;
     68 
     69   const int parsing = GNUNET_GETOPT_run(
     70     MESSENGER_CLI_BINARY,
     71     options,
     72     app->argc,
     73     app->argv
     74   );
     75 
     76   if (parsing <= 0)
     77   {
     78     app->window = NULL;
     79     app->status = GNUNET_SYSERR == parsing? GNUNET_SYSERR : GNUNET_OK;
     80     return;
     81   }
     82 
     83   app->window = initscr();
     84 
     85   if (!(app->window))
     86   {
     87     app->status = GNUNET_SYSERR;
     88     return;
     89   }
     90 
     91   if (has_colors())
     92     util_init_unique_colors();
     93 
     94   application_refresh(app);
     95 
     96   noecho();
     97 
     98   keypad(app->window, TRUE);
     99   wtimeout(app->window, 10);
    100 }
    101 
    102 void
    103 application_refresh(MESSENGER_Application *app)
    104 {
    105   if (app->ui.logo) delwin(app->ui.logo);
    106   if (app->ui.main) delwin(app->ui.main);
    107   if (app->ui.left) delwin(app->ui.left);
    108   if (app->ui.right) delwin(app->ui.right);
    109   if (app->ui.input) delwin(app->ui.input);
    110 
    111   memset(&(app->ui), 0, sizeof(app->ui));
    112 
    113   curs_set(0);
    114 }
    115 
    116 static void
    117 run (void *cls,
    118      UNUSED char* const* args,
    119      UNUSED const char *cfgfile,
    120      const struct GNUNET_CONFIGURATION_Handle *cfg)
    121 {
    122   MESSENGER_Application *app = cls;
    123 
    124   if (!(app->window))
    125     return;
    126 
    127   chat_start(&(app->chat), app, cfg);
    128 }
    129 
    130 void
    131 application_run(MESSENGER_Application *app)
    132 {
    133   const struct GNUNET_OS_ProjectData *data =
    134     GNUNET_OS_project_data_gnunet();
    135 
    136   const struct GNUNET_GETOPT_CommandLineOption options [] = {
    137     GNUNET_GETOPT_OPTION_END
    138   };
    139 
    140   app->status = GNUNET_PROGRAM_run(
    141     data,
    142     1,
    143     app->argv,
    144     MESSENGER_CLI_BINARY,
    145     gettext_noop(MESSENGER_CLI_DESC),
    146     options,
    147     &run,
    148     app
    149   );
    150 
    151   members_clear(&(app->current.members));
    152   messages_clear(&(app->current.messages));
    153 
    154   application_clear(app);
    155 
    156   if (app->window)
    157     delwin(app->window);
    158 
    159   if (ERR == endwin())
    160   {
    161     app->status = GNUNET_SYSERR;
    162     return;
    163   }
    164 }
    165 
    166 int
    167 application_status(MESSENGER_Application *app)
    168 {
    169   if (app->status != GNUNET_OK)
    170     return EXIT_FAILURE;
    171   else
    172     return EXIT_SUCCESS;
    173 }