home *** CD-ROM | disk | FTP | other *** search
/ Windows NT Super Tune-Up Kit / PIE-WindowsNTSuperTuneUpKit-1997.iso / MAIL / MUDWHONT / MUDWHO.TXT < prev    next >
Text File  |  1994-07-17  |  5KB  |  213 lines

  1. #! /bin/sh
  2. # Strip off top stuff and run this through /bin/sh to produce:
  3. # README
  4. # mudwho.c
  5. # Produced by Chris's stupid and simple sh shar.
  6. echo 'README'
  7. if [ -f README ] 
  8. then
  9.   echo README already exists; exit 1
  10. fi
  11. sed 's/^//' >'README' <<'ENDIT' 
  12. About mudwho:
  13. -------------
  14. 'mudwho' is a small client program that connects to an RWHO server and
  15. grabs WHO lists of every mud that the RWHO server knows about.
  16.  
  17. To compile, all you need to do is:
  18.     cc mudwho.c -o mudwho
  19.  
  20. You may define -DNO_HUGE_RESOLVER_CODE if you don't want to link in the
  21. resolver libraries; note that this means you'll have to specify hosts
  22. by number.
  23.  
  24. Usage:
  25. ------
  26. mudwho [-S server] [-P servport] [-u user] [-m mud]
  27.  
  28. The server can be specified also by setting the environment variable
  29. MUDWHOSERVER. The port defaults to 6889, but can be also specified by
  30. setting the environment variable MUDWHOPORT. 
  31. The options -u and -m will limit the output to whatever matches either
  32. the user (player) name given or the mud name given. (There is a timeout
  33. value of about a second, so if you're using a RWHO server that's far
  34. enough away that you have some net lag, you may get the entire WHO list
  35. output instead of just what you specified with -u or -m.)
  36.  
  37. Ask around for the nearest (net-wise) RWHO server to you. For starters,
  38. try using moebius.math.okstate.edu (139.78.10.3).
  39.  
  40. Any bug reports should go to the author, Marcus J. Ranum (mjr@decuac.dec.com).
  41.  
  42. ENDIT
  43. echo 'mudwho.c'
  44. if [ -f mudwho.c ] 
  45. then
  46.   echo mudwho.c already exists; exit 1
  47. fi
  48. sed 's/^//' >'mudwho.c' <<'ENDIT' 
  49. /*
  50.     Copyright (C) 1991, Marcus J. Ranum. All rights reserved.
  51. */
  52.  
  53. #ifndef    lint
  54. static    char    RCSid[] = "$Header: /usr/users/mjr/hacks/umud/RWHO/RCS/mudwho.c,v 1.6 91/06/16 18:33:51 mjr Exp $";
  55. #endif
  56.  
  57.  
  58. #include    <stdio.h>
  59. #include    <sys/types.h>
  60. #include    <errno.h>
  61. extern    int    errno;
  62. #include    <ctype.h>
  63. #include    <fcntl.h>
  64. #include    <sys/file.h>
  65. #include    <sys/time.h>
  66. #include    <sys/socket.h>
  67. #include    <netinet/in.h>
  68. #include    <netdb.h>
  69.  
  70.  
  71. #define    STREAMPORT        6889
  72.  
  73. #define    ENVHOST        "MUDWHOSERVER"
  74. #define    ENVPORT        "MUDWHOPORT"
  75.  
  76.  
  77. #ifndef    NO_HUGE_RESOLVER_CODE
  78. extern    struct    hostent    *gethostbyname();
  79. #endif
  80.  
  81. extern    char    *getenv();
  82. extern    char    *optarg;
  83.  
  84.  
  85. main(ac,av)
  86. int    ac;
  87. char    *av[];
  88. {
  89.     struct    sockaddr_in    addr;
  90.     struct    hostent        *hp;
  91.     int            fd;
  92.     char            *p;
  93.     int            red;
  94.     char            rbuf[1024];
  95.     char            *srv = (char *)0;
  96.     int            portnum = STREAMPORT;
  97.     char            *checkwho = (char *)0;
  98.     char            *checkmud = (char *)0;
  99.  
  100.     srv = getenv(ENVHOST);
  101.     if((p = getenv(ENVPORT)) != (char *)0)
  102.         portnum = atoi(p);
  103.  
  104.     while((red = getopt(ac,av,"S:P:u:m:")) != EOF) {
  105.         switch(red) {
  106.         case 'S':
  107.             srv = optarg;
  108.             break;
  109.  
  110.         case 'P':
  111.             portnum = atoi(optarg);
  112.             break;
  113.  
  114.         case 'u':
  115.             checkwho = optarg;
  116.             break;
  117.  
  118.         case 'm':
  119.             checkmud = optarg;
  120.             break;
  121.  
  122.         default:
  123.             exit(usage());
  124.         }
  125.     }
  126.  
  127.     if(srv == (char *)0) {
  128.         fprintf(stderr,"rwho server host must be provided [-S host]\n");
  129.         exit(1);
  130.     }
  131.  
  132.     if(checkmud != (char *)0 && checkwho != (char *)0) {
  133.         fprintf(stderr,"You can only search for one user or MUD entry\n");
  134.         exit(1);
  135.     }
  136.  
  137.  
  138.     p = srv;
  139.     while(*p != '\0' && (*p == '.' || isdigit(*p)))
  140.         p++;
  141.  
  142.     /* not all digits or dots */
  143.     if(*p != '\0') {
  144. #ifndef    NO_HUGE_RESOLVER_CODE
  145.         if((hp = gethostbyname(srv)) == (struct hostent *)0) {
  146.             fprintf(stderr,"unknown host %s\n",srv);
  147.             exit(1);
  148.         }
  149.  
  150.         (void)bcopy(hp->h_addr,(char *)&addr.sin_addr,hp->h_length);
  151. #else
  152.         fprintf(stderr,"must use numerical notation for host name\n");
  153.         exit(1);
  154. #endif
  155.     } else {
  156.         unsigned long    f;
  157.  
  158.         if((f = inet_addr(srv)) == -1L) {
  159.             fprintf(stderr,"unknown host %s\n",srv);
  160.             exit(1);
  161.         }
  162.         (void)bcopy((char *)&f,(char *)&addr.sin_addr,sizeof(f));
  163.     }
  164.  
  165.     addr.sin_port = htons(portnum);
  166.     addr.sin_family = AF_INET;
  167.  
  168.     if((fd = socket(AF_INET,SOCK_STREAM,0)) < 0) {
  169.         perror("socket");
  170.         exit(1);
  171.     }
  172.  
  173.     if(connect(fd,&addr,sizeof(addr)) < 0) {
  174.         perror("connect");
  175.         exit(1);
  176.     }
  177.  
  178.  
  179.     /* send request if one */
  180.     if(checkmud != (char *)0 || checkwho != (char *)0) {
  181.         char    xuf[512];
  182.         int    xlen;
  183.  
  184.         sprintf(xuf,"%s=%.30s",
  185.             checkmud == (char *)0 ? "who" : "mud",
  186.             checkmud == (char *)0 ? checkwho : checkmud);
  187.  
  188.         xlen = strlen(xuf) + 1;
  189.         if(write(fd,xuf,xlen) != xlen) {
  190.             perror("write to rwho server failed");
  191.             exit(1);
  192.         }
  193.     }
  194.  
  195.  
  196.     while((red = read(fd,rbuf,sizeof(rbuf))) > 0)
  197.         write(1,rbuf,red);
  198.     exit(0);
  199. }
  200.  
  201.  
  202.  
  203. usage()
  204. {
  205.     fprintf(stderr,"usage: mudwho [-S server] [-P servport] [-u user] [-m mud]\n");
  206.     fprintf(stderr,"\twhere user is someone to scan for\n");
  207.     fprintf(stderr,"\tmud is a specific MUD to scan for\n");
  208.     fprintf(stderr,"\t%s and %s can be set in the environment\n",ENVPORT,ENVHOST);
  209.     return(1);
  210. }
  211. ENDIT
  212. exit 0
  213.