/*
* This program was hacked by Harald Kipp using a source
* written by Eric S. Raymond.
*
* Bug reports related to THIS modified version should be sent to
*
* harald@os2point.ping.de
* harald@haport.sesam.com
* Fido: 2:2448/434
*
* You may freely copy or redistribute this software. However,
* this may not apply to any part of it, if otherwise noted.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <version.h>
#include <lprintf.h>
#include <getopt.h>
#include <active.h>
#include <chanlib.h>
#include <readcfg.h>
#include <fcntl.h>
#include <io.h>
#include "config.h"
#include "chanco.h"
static CFGITEM cfgitm[] = {
{ "access" , 0, 4, NULL },
{ "actimes" , 0, 4, cfg.active_times },
{ "active" , 0, 4, cfg.activefile },
{ "control" , 0, 4, NULL },
{ "dupegroup" , 0, 4, NULL },
{ "gunzip" , 0, 4, NULL },
{ "history" , 0, 4, cfg.historyfile },
{ "inews" , 0, 4, NULL },
{ "junkgroup" , 0, 4, NULL },
{ "mydomain" , 0, 4, NULL },
{ "mynode" , 0, 4, NULL },
{ "newsdir" , 0, 4, cfg.newsdir },
{ "newsserver", 0, 4, NULL },
{ "rnews" , 0, 4, NULL },
{ "spooldir" , 0, 4, NULL },
{ "uncompress", 0, 4, NULL }
};
typedef struct {
char *type;
void (*exec)(int, char **);
} method_t;
static method_t methods[] = {
/* name vector to */
{"cancel", c_cancel},
{"checkgroups", c_checkgroups},
{"newgroup", c_newgroup},
{"rmgroup", c_rmgroup}
};
#define NMETHODS (sizeof(methods) / sizeof(methods[0]))
static void usage(void);
/************************************************************************/
/* */
/* CHANCO */
/* */
/* This program handles incoming control messages. */
/* */
/************************************************************************/
int main(int argc, char **argv)
{
int result = 0;
FILE *fp;
char *cp;
int option;
time_t t_proc = time(NULL);
init_cfg();
initexit();
lopen(cfg.logfile);
lprintf("Chanco %s - %s", version, __TIMESTAMP__);
/*
* Process command line options
*/
while((option = getopt(argc, argv, "?a:c:d:f:n:y:")) != EOF) {
switch(option) {
case 'a':
strcpy(cfg.activefile, optarg);
break;
case 'c':
strcpy(cfg.configfile, optarg);
break;
case 'd':
cp = optarg;
while(*cp) {
switch(*cp) {
case 'a':
cfg.logflg |= LOG_ACTIVE;
break;
case 'h':
cfg.logflg |= LOG_HISTORY;
break;
case 'f':
cfg.logflg |= LOG_FLUSH;
lflush(1);
break;
default:
lprintf("Unknown option -d%c ignored", *cp);
break;
}
cp++;
}
break;
case 'f':
strcpy(cfg.newsfile, optarg);
break;
case 'n':
strcpy(cfg.newsdir, optarg);
break;
case 'y':
strcpy(cfg.historyfile, optarg);
break;
default:
case '?':
lprintf("Unknown option -%c", option);
usage();
return(2);
}
}
/*
* All command line options are processed, check if one
* argument is left to give us an article file name.
*/
argc -= optind;
argv += optind;
if(!argc) {
lprintf("Missing command, returning error %d", 2);
lclose();
return(2);
}
if(cfg.newsfile[0]) {
if((fp = freopen(cfg.newsfile, "rb", stdin)) == NULL) {
lperror(argv[0]);
lprintf("Chanco returned error %d", 3);
lclose();
return(3);
}
}
else {
fp = stdin;
setmode(0, O_BINARY);
}
if(fp) {
ReadCfg(cfg.configfile, cfgitm, sizeof(cfgitm) / sizeof(CFGITEM));
if(validate_cfg()) {
int i;
if(load_active(cfg.activefile)) {
for (i = 0; i < NMETHODS; ++i)
if (!stricmp(methods[i].type, argv[0]))
break;
if (i < NMETHODS)
(*methods[i].exec) (argc, argv);
else {
lprintf("Command %s unrecognized.", argv[0]);
result = 3;
}
}
}
else
result = 2;
fclose(fp);
}
else
lperror("Input file");
if(result)
lprintf("Chanco returned error %d", result);
else {
t_proc = time(NULL) - t_proc;
lprintf("Chanco processed command in %lu second(s)", t_proc);
}
lclose();
return(result);
}
/************************************************************************/
/* */
/************************************************************************/
static void usage(void)
{
puts("usage: chanco [options] <command>\n\n"
"commands:\n"
" cancel <message-id>\n"
" newgroup <newsgroup>\n"
" rmgroup <newsgroup>\n\n"
"options:\n"
" -a<filename> active file\n"
" -c<filename> config file\n"
" -d<log-flags> logfile flags\n"
" -n<directory> news directory\n"
" -y<filename> history file\n\n"
"logfile flags:\n"
" a active file processing\n"
" f flush logfile after each line\n"
" h history file processing");
lprintf("Chanco usage displayed%c", '\n');
lclose();
}