/*
	Copyright (C) 1991, Marcus J. Ranum. All rights reserved.
*/

#ifndef	lint
static	char	RCSid[] = "$Header: /usr/users/mjr/hacks/umud/RCS/cron.c,v 1.6 91/05/25 22:56:04 mjr Exp $";
#endif


/*
useable by an mwhod wizard to send a command to a remote mwhod.
It is assumed that the user knows all the syntax EXACTLY for the
command to send. heh.

primarily useful for defining new peers while the server is running
without having to kill and restart the server

for example:

sample of a mwhocmd to define a mud to a running server
note that the actual tabs here must be converted when you type it.
mwhocmd -s hussar -c 'W\twizard\twizardpasswd\tC hostname MUDname MUDpw desc'
*/


#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>


#define	DGRAMPORT		6888

#ifndef	NO_HUGE_RESOLVER_CODE
extern	struct	hostent	*gethostbyname();
#endif

extern	char	*optarg;


main(ac,av)
int	ac;
char	*av[];
{
	int	dgramfd;
	struct	sockaddr_in	addr;

#ifndef	NO_HUGE_RESOLVER_CODE
	struct	hostent		*hp;
#endif
	char			*p;
	char			*mycmd;
	char			*server;


	while((dgramfd = getopt(ac,av,"c:s:")) != EOF) {
		switch(dgramfd) {
		case 's':
			server = optarg;
			break;

		case 'c':
			mycmd = optarg;
			break;

		default:
			fprintf(stderr,"usage.\n");
			exit(1);
		}
	}

	p = server;
	while(*p != '\0' && (*p == '.' || isdigit(*p)))
		p++;

	if(*p != '\0') {
#ifndef	NO_HUGE_RESOLVER_CODE
		if((hp = gethostbyname(server)) == (struct hostent *)0)
			return(1);
		(void)bcopy(hp->h_addr,(char *)&addr.sin_addr,hp->h_length);
#else
		return(1);
#endif
	} else {
		unsigned long	f;

		if((f = inet_addr(server)) == -1L)
			return(1);
		(void)bcopy((char *)&f,(char *)&addr.sin_addr,sizeof(f));
	}

	addr.sin_port = htons(DGRAMPORT);
	addr.sin_family = AF_INET;

	if((dgramfd = socket(AF_INET,SOCK_DGRAM,0)) < 0)
		return(1);

	sendto(dgramfd,mycmd,strlen(mycmd),0,&addr,sizeof(addr));
	exit(0);
}