src/nexus.c
changeset 92 99661e5aac91
parent 83 c8e2dac08207
child 93 42ade8285570
equal deleted inserted replaced
91:bca23cbe1dce 92:99661e5aac91
    24     OPT_CONFIG          = 'C',
    24     OPT_CONFIG          = 'C',
    25     OPT_DEBUG           = 'd',
    25     OPT_DEBUG           = 'd',
    26     
    26     
    27     /** Options without short names */
    27     /** Options without short names */
    28     _OPT_EXT_BEGIN      = 0x00ff,
    28     _OPT_EXT_BEGIN      = 0x00ff,
    29 
    29     
       
    30     OPT_CONSOLE,
    30 };
    31 };
    31 
    32 
    32 /**
    33 /**
    33  * Command-line option definitions
    34  * Command-line option definitions
    34  */
    35  */
    37     {"network",         1,  NULL,   OPT_NETWORK     },
    38     {"network",         1,  NULL,   OPT_NETWORK     },
    38     {"channel",         1,  NULL,   OPT_CHANNEL     },
    39     {"channel",         1,  NULL,   OPT_CHANNEL     },
    39     {"module",          1,  NULL,   OPT_MODULE      },
    40     {"module",          1,  NULL,   OPT_MODULE      },
    40     {"config",          1,  NULL,   OPT_CONFIG      },
    41     {"config",          1,  NULL,   OPT_CONFIG      },
    41     {"debug",           0,  NULL,   OPT_DEBUG       },
    42     {"debug",           0,  NULL,   OPT_DEBUG       },
       
    43     {"console",         0,  NULL,   OPT_CONSOLE     },
    42     {0,                 0,  0,      0               },
    44     {0,                 0,  0,      0               },
    43 };
    45 };
    44 
    46 
    45 /**
    47 /**
    46  * Display --help output on stdout.
    48  * Display --help output on stdout.
    55     printf(" --network / -n         add an IRC network using '<name>:<hostname>[:<port>[:ssl]]' format\n");
    57     printf(" --network / -n         add an IRC network using '<name>:<hostname>[:<port>[:ssl]]' format\n");
    56     printf(" --channel / -c         add an IRC channel using '<network>:<channel>' format\n");
    58     printf(" --channel / -c         add an IRC channel using '<network>:<channel>' format\n");
    57     printf(" --module / -m          add a module using '<name>:<path>' format\n");
    59     printf(" --module / -m          add a module using '<name>:<path>' format\n");
    58     printf(" --config / -C          add a module configuration option using '<mod_name>:<name>[:<value>]' format\n");
    60     printf(" --config / -C          add a module configuration option using '<mod_name>:<name>[:<value>]' format\n");
    59     printf(" --debug / -d           set logging level to DEBUG\n");
    61     printf(" --debug / -d           set logging level to DEBUG\n");
    60 
    62     printf(" --console              use the interactive console\n");
       
    63     
       
    64     // dump loaded module configs
    61     if (nexus && !TAILQ_EMPTY(&nexus->modules->list)) {
    65     if (nexus && !TAILQ_EMPTY(&nexus->modules->list)) {
    62         struct module *module;
    66         struct module *module;
    63 
    67 
    64         printf("\n");
    68         printf("\n");
    65         printf("Module configuration options\n");
    69         printf("Module configuration options\n");
   230 
   234 
   231     // ok
   235     // ok
   232     return SUCCESS;
   236     return SUCCESS;
   233 }
   237 }
   234 
   238 
       
   239 static void on_line (char *line, void *arg)
       
   240 {
       
   241     struct nexus *nexus = arg;
       
   242 
       
   243     (void) nexus;
       
   244 
       
   245     // just dump it
       
   246     log_info("read line from console: '%s'", line);
       
   247 }
       
   248 
       
   249 static struct console_callbacks nexus_console_callbacks = {
       
   250     .on_line        = &on_line,
       
   251 };
       
   252 
       
   253 /**
       
   254  * Open the console
       
   255  */
       
   256 static err_t apply_console (struct nexus *nexus, struct error_info *err)
       
   257 {
       
   258     struct console_config config = {
       
   259         .prompt     = " > ",
       
   260     };
       
   261 
       
   262     log_info("initializing the console");
       
   263     
       
   264     // just init it
       
   265     return console_init(&nexus->console, nexus->ev_base, &config, &nexus_console_callbacks, nexus, err);
       
   266 }
       
   267 
   235 /**
   268 /**
   236  * Parse arguments and apply them to the given nexus
   269  * Parse arguments and apply them to the given nexus
   237  */
   270  */
   238 static err_t parse_args (struct nexus *nexus, int argc, char **argv, struct error_info *err)
   271 static err_t parse_args (struct nexus *nexus, int argc, char **argv, struct error_info *err)
   239 {
   272 {
   273                 break;
   306                 break;
   274             
   307             
   275             case OPT_DEBUG:
   308             case OPT_DEBUG:
   276                 set_log_level(LOG_DEBUG);
   309                 set_log_level(LOG_DEBUG);
   277                 break;
   310                 break;
       
   311             
       
   312             case OPT_CONSOLE:
       
   313                 if (apply_console(nexus, err))
       
   314                     return ERROR_CODE(err);
       
   315                 
       
   316                 break;
   278 
   317 
   279             case '?':
   318             case '?':
   280                 usage(nexus, argv[0]);
   319                 usage(nexus, argv[0]);
   281                 return SET_ERROR(err, ERR_CMD_OPT);
   320                 return SET_ERROR(err, ERR_CMD_OPT);
   282         }
   321         }
   293     (void) sig;
   332     (void) sig;
   294     (void) what;
   333     (void) what;
   295     
   334     
   296     log_info("Quitting...");
   335     log_info("Quitting...");
   297 
   336 
       
   337     // destroy the console
       
   338     if (ctx->console)
       
   339         console_destroy(ctx->console);
       
   340 
   298     // unload the modules
   341     // unload the modules
   299     modules_unload(ctx->modules);
   342     modules_unload(ctx->modules);
   300 
   343 
   301     // quit the irc client
   344     // quit the irc client
   302     irc_client_quit(ctx->client, "Goodbye, cruel world ;(");
   345     irc_client_quit(ctx->client, "Goodbye, cruel world ;(");
   317 
   360 
   318     // initialize libevent
   361     // initialize libevent
   319     if ((nexus->ev_base = event_base_new()) == NULL)
   362     if ((nexus->ev_base = event_base_new()) == NULL)
   320         FATAL("event_base_new");
   363         FATAL("event_base_new");
   321     
   364     
       
   365 
   322     // initialize signal handlers
   366     // initialize signal handlers
   323     if ((ERROR_CODE(&err) = signals_create(&nexus->signals, nexus->ev_base)))
   367     if ((ERROR_CODE(&err) = signals_create(&nexus->signals, nexus->ev_base)))
   324         FATAL("signals_create");
   368         FATAL("signals_create");
   325 
   369  
   326     // add our signal handlers
   370     // add our signal handlers
   327     if (signal_ignore(SIGPIPE, &err))
   371     if (signal_ignore(SIGPIPE, &err))
   328         FATAL_ERROR(&err, "signals_ignore(SIGPIPE)");
   372         FATAL_ERROR(&err, "signals_ignore(SIGPIPE)");
   329 
   373     
       
   374     // XXX: add our SIGINT handler after console_init()?
   330     if ((ERROR_CODE(&err) = signals_add(nexus->signals, SIGINT, &on_sigint, nexus)))
   375     if ((ERROR_CODE(&err) = signals_add(nexus->signals, SIGINT, &on_sigint, nexus)))
   331         FATAL_ERROR(&err, "signals_add(SIGINT)");
   376         FATAL_ERROR(&err, "signals_add(SIGINT)");
   332  
   377  
       
   378 
   333     // initialize sock module
   379     // initialize sock module
   334     if (sock_init(nexus->ev_base, &err))
   380     if (sock_init(nexus->ev_base, &err))
   335         FATAL_ERROR(&err, "sock_init");
   381         FATAL_ERROR(&err, "sock_init");
   336 
   382 
   337     // modules 
   383     // modules 
   340     
   386     
   341     // the IRC client
   387     // the IRC client
   342     if (irc_client_create(&nexus->client, &err))
   388     if (irc_client_create(&nexus->client, &err))
   343         FATAL_ERROR(&err, "irc_client_create");
   389         FATAL_ERROR(&err, "irc_client_create");
   344 
   390 
       
   391 
   345     // parse args
   392     // parse args
   346     if (parse_args(nexus, argc, argv, &err))
   393     if (parse_args(nexus, argc, argv, &err))
   347         FATAL_ERROR(&err, "parse_args");
   394         FATAL_ERROR(&err, "parse_args");
   348     
   395 
       
   396   
   349     // run event loop
   397     // run event loop
   350     log_info("entering event loop");
   398     log_info("entering event loop");
   351 
   399 
   352     if (event_base_dispatch(nexus->ev_base))
   400     if (event_base_dispatch(nexus->ev_base))
   353         FATAL("event_base_dispatch");
   401         FATAL("event_base_dispatch");