/* Callbook server */
/* Mods by KO4KS */
#include <stdio.h>
#include <string.h>
#include "global.h"
#include "files.h"
#include "mbuf.h"
#include "socket.h"
#include "session.h"
#include "proc.h"
#include "dirutil.h"
#include "commands.h"
#include "mailbox.h"
#include "config.h"

#if defined(CALLBOOK) || defined(CALLSERVER) || defined(SAMCALLB)

extern char *Callserver;  /* buckbook.c */

static int Scallbook = -1;	/* Prototype socket for service */

static void calldbd __ARGS((int s,void *unused,void *p));

#ifdef SAMCALLB
int cb_lookup __ARGS((int s,char *call,FILE *fp));
#else
void cb_lookup __ARGS((int s,char *call));  /* in buckbook.c  */
#endif

/* Start up callsign database service */
int
cdbstart(argc,argv,p)
int argc;
char *argv[];
void *p;
{
	return (installserver (argc, argv, &Scallbook, "Callbook listener", IPPORT_CALLDB,
		"calldbd", calldbd, 1024, NULL));
}
static void
calldbd(s,unused,p)
int s;
void *unused;
void *p;
{
char user[80];
int err;

	sockmode(s,SOCK_ASCII);
	sockowner(s,Curproc);
	recvline(s,user,80);
	rip(user);
	log(s,"Callbook lookup: %s",user);
	if(strlen(user) == 0)
		usprintf(s,"No Callbook information available\n");
	else 	{
#ifdef SAMCALLB
		if ((err = cb_lookup(s,user,(FILE *) 0)) != 0)
			usprintf(s,(err == 2) ? "No Callbook information available\n" : "Callbook not active\n");
#else
		cb_lookup(s,user);
#endif
	}
	close_s(s);
/*   log(s,"close Callbook"); */
}

int
cdb0(argc,argv,p)
int argc;
char *argv[];
void *p;
{
	return (deleteserver (&Scallbook));
}

#endif


/* This routine has been expanded upon, and is now located in buckbook.c
  - kb7yw Mon  01-27-1992   - except if using the SAM database - ko4ks */

#ifdef SAMCALLB
char *Callserver;

int
docallserver(argc,argv,p)
int argc;
char *argv[];
void *p;
{
	if(argc < 2)
	{
		if(Callserver != NULLCHAR)
			tprintf("The callserver's host name is: %s\n",Callserver);
		else
		{
			tprintf("Callserver not configured!\n");
			tprintf("Usage: callserver hostname OR callserver ip_address\n");
		}
	}
	else {
		if(Callserver != NULLCHAR)
			free(Callserver);
		Callserver = strdup(argv[1]);
	}
	return 0;
}

#endif

