/*
  Copyright (C) 1991, Marcus J. Ranum. All rights reserved.
*/
/*
code to interface client MUDs with rwho server.  this is a standalone library.
*/
#include        <stdio.h>
#include        <ctype.h>
#include        <fcntl.h>
#include        <sys/file.h>
#include        <sys/time.h>
#include        <sys/types.h>
#include        <sys/socket.h>
#include        <netinet/in.h>
#include        <netdb.h>
#include        "interface.h"
#include        "params.h"
#include        "externs.h"
#define DGRAMPORT               6888
#ifndef NO_HUGE_RESOLVER_CODE
extern struct hostent *gethostbyname();
#endif
static int dgramfd = -1;
static char server[BUFSIZ];	
static char password[BUFSIZ];
char localnam[BUFSIZ];
static char lcomment[BUFSIZ];
static struct sockaddr_in addr;
time_t senttime;
/* enable RWHO and send the server a "we are up" message */
int rwhocli_setup()
{
  char pbuf[512], *p;
  struct hostent *hp;
  unsigned long f;
  strcpy(localnam, "");
  if (!(find_property((dbref) 0, RWHO_SERVER, ACCESS_WI) &&
    find_property((dbref) 0, RWHO_PASS, ACCESS_WI) &&
    find_property((dbref) 0, RWHO_NAME, ACCESS_WI) &&
    find_property((dbref) 0, RWHO_COMMENT, ACCESS_WI)))
    return 1;
  strcpy(server, get_property_data((dbref) 0, RWHO_SERVER, ACCESS_WI));
  strcpy(password, get_property_data((dbref) 0, RWHO_PASS, ACCESS_WI));
  strcpy(localnam, get_property_data((dbref) 0, RWHO_NAME, ACCESS_WI));
  strcpy(lcomment, get_property_data((dbref) 0, RWHO_COMMENT, ACCESS_WI));
  if(dgramfd != -1) return(1); /* already configured...you goofed! */
  p = server;
  while(*p != '\0' && (*p == '.' || isdigit(*p))) p++;
  if(*p != '\0')
  {
#ifndef NO_HUGE_RESOLVER_CODE
    if((hp = gethostbyname(server)) == (struct hostent *)NULL)return(1);
    strncpy((char *) &addr.sin_addr, (char *) hp->h_addr, hp->h_length);
#else
    return(1);
#endif
  }
  else
  {
    if((f = inet_addr(server)) == -1L) return(1);
    strncpy((char *) &addr.sin_addr, (char *) &f, sizeof(f));
  }
  addr.sin_port = htons(DGRAMPORT);
  addr.sin_family = AF_INET;
  if((dgramfd = socket(AF_INET,SOCK_DGRAM,0)) < 0) return(1);
  time(&senttime);
  sprintf(pbuf, "U\t%.20s\t%.20s\t%.20s\t%.10d\t0\t%.25s",
    localnam, password, localnam, senttime, lcomment);
  sendto(dgramfd, pbuf, strlen(pbuf), 0, &addr, sizeof(addr));
  return(0);
}
int rwhocli_shutdown()        /* disable RWHO */
{
        char    pbuf[512];
        if(dgramfd != -1) {
                sprintf(pbuf,"D\t%.20s\t%.20s\t%.20s", localnam, password,
		localnam);
                sendto(dgramfd,pbuf,strlen(pbuf),0,&addr,sizeof(addr));
                close(dgramfd);
                dgramfd = -1;
        }
        return(0);
}
int rwhocli_pingalive()     /* send an update ping that we're alive */
{
        char    pbuf[512];
        if(dgramfd != -1) {
                sprintf(pbuf,"M\t%.20s\t%.20s\t%.20s\t%.10d\t0\t%.25s",
                        localnam,password,localnam,senttime,lcomment);
                sendto(dgramfd,pbuf,strlen(pbuf),0,&addr,sizeof(addr));
        }
        return(0);
}
/* send a "so-and-so-logged in" message */
int rwhocli_userlogin(char *uid, char *name, time_t tim)
{
        char    pbuf[512];
        if(dgramfd != -1) {
                sprintf(pbuf,"A\t%.20s\t%.20s\t%.20s\t%.20s@%.20s\t%.10d\t0\t%.20s",
                        localnam, password, localnam, uid, localnam, tim, name);
                sendto(dgramfd,pbuf,strlen(pbuf),0, &addr, sizeof(addr));
        }
        return(0);
}
int rwhocli_userlogout(uid)    /* send a "so-and-so-logged out" message */
char    *uid;
{
        char    pbuf[512];
        if(dgramfd != -1) {
                sprintf(pbuf,"Z\t%.20s\t%.20s\t%.20s\t%.20s@%.20s",
                        localnam,password,localnam,uid,localnam);
                sendto(dgramfd,pbuf,strlen(pbuf),0,&addr,sizeof(addr));
        }
        return(0);
}