/*********************************************************
 * Gotcha library example code.
 * (c)1995 Thomas Bickel. All rights reserved.
 *
 * Show some info for an address or an addresspattern.
 *********************************************************/

#include <stdio.h>
#include <stdlib.h>

#include <clib/exec_protos.h>
#include <pragmas/exec_pragmas.h>

#include <clib/gotchalib_protos.h>
#include <pragmas/gotchalib_pragmas.h>
#include <libraries/gotchalib.h>

struct Library *GotchaLibBase=0;

struct gl_context *cont;
struct gl_address add;
struct gl_nodeinfo ninfo;
struct gl_pattern pat;

LONG	found=0;
UBYTE	adrstring[64];

void show_info(struct gl_nodeinfo *);

/************************************************************/
int main(long argc, char *argv[])
{
	if (argc<2) {
		printf("Usage: %s <address>\n",argv[0]);
		exit(10);
	}

    if (GotchaLibBase = OpenLibrary("gotcha.library",1)) {

		printf("\2330 p");
		if (cont = GL_OpenNL("nodelist:")) {

	        GL_XtractInfos(&add,argv[1]);

			if (add.anyany) {		/* pattern matching */

				GL_AddressToString(&add,adrstring);
				printf("Addresses matching %s:\n",adrstring);

				if (GL_FindNodeFirst(&add,cont,&ninfo,&pat))
				do {
					show_info(&ninfo);

				} while (GL_FindNodeNext(&add,cont,&ninfo,&pat));

				if (found) printf("\nFound %ld matches.\n",found);
				else puts("Found no matches");

			} else {			/* no pattern matching */

				if (GL_FindNode(&add,cont,&ninfo,NULL))
    	            show_info(&ninfo);
				else {
					printf("This address is not in the nodelist\n");

				/* There might be a pw even if the node is not in the nodelist!! */
					if (*ninfo.password)
						printf(" Password: %s\n",ninfo.password);
				}
			}

			GL_CloseNL(cont);
		} else
			puts("Could not open the nodelist!");

		printf("\233 p");
		CloseLibrary(GotchaLibBase);
	} else
		puts("Could not open 'gotcha.library'!");

}


void show_info(struct gl_nodeinfo *ninfo)
{
	found++;

	printf("%d:%d/%d.%d@%s\t%s / %s / %s\n",
		ninfo->zone,ninfo->net,ninfo->node,ninfo->point,ninfo->domain,
		ninfo->name,ninfo->location,ninfo->sysop);
}
