/* Copyright (C) 1995 NetWin World, New Zealand, All rights reserved. */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "dnews.h"
#include "conf.h"
#include "lib.h"
#include "emsg.h"
#include "nntp.h"
#ifndef TRUE
#define FALSE 0
#define TRUE (!FALSE)
#endif
char *reg_address(void)   ;
char *reg_address2(void) ;
char *reg_address3(void);
char *hide_data(char *a, char *b, char *c);
char *unhide_data(char *s);
int debug_lvl;
char dnews_conf[BFSZ];
int do_register(void);
int print_got(char *s, int n);
static char *cmds[100];
static int ncmds;
int main(int argc, char *argv[])
{
        char bf[300];
        static char bf2[300];
        char out[10001];
        char myname[300];
        int c;
        int i,n;
	char *s;

        for (i=2; i<argc; i++) {
                if (strcmp(argv[i],"register")==0) {do_register(); return 1;}
                if (strcmp(argv[i],"-register")==0) {do_register(); return 1;}
                if (strcmp(argv[i],"REGISTER")==0) {do_register(); return 1;}
                if (strncmp(argv[i],"-c",2)==0) {
                        strcpy(dnews_conf,argv[i+1]);
                        i++;
                } else {
                        strcat(bf2,argv[i]);
                        strcat(bf2," ");
                }
        }
        nntp_startup();

 	nntp_alwaysblock();

        c = nntp_open(argv[1]);
        if (c<0) {
                goto the_end;
        }
        nntp_setblock(c,TRUE); /* Set to blocking IO */

        n = nntp_read(c,out,10000);
	print_got(out,n);

	for (i=0; i<20; i++) cmds[i] = mymalloc(200);
	for (;TRUE;) {
		printf("NNTP> "); gets(cmds[ncmds++]);
		if (strlen(cmds[ncmds-1])==0) break;
	}
	s = bf;
	for (i=0; i<ncmds; i++) {
		sprintf(s,"%s\015\012",cmds[i]);
		s += strlen(s);
	}
	nntp_write_s(c,bf,strlen(bf));

        n = nntp_read(c,out,10000);
        for (;n>=0;) {
                out[n] = 0;
		print_got(out,n);
                n = nntp_read(c,out,10000);
        }
the_end:
        return 1;
}
int print_got(char *s, int n)
{
	FILE *f;
 	char *ss=s;
	s[n] = 0;
	for (;*s!=0;) {
		if (*s=='\015') *s = '^';
		if (*s=='\012') *s = '~';
		if (*s=='\t') *s = '|';
		s++;
	}		      
	printf("%d{%s}\n",n,ss);
	f = fopen("xtelnet.log","a");  
	if (f!=NULL) {
		fprintf(f,"%d{%s}\n",n,ss);
		fclose(f);
	}

}
char *group_name(int i)
{
        return "(none)";
}


void dmsg(char * arg_list, ...)
{
        return;
}
void filemsg(char * arg_list, ...)
{
        va_list arg_ptr;
        char *format, output[1000];
        va_start(arg_ptr, arg_list);
        format = arg_list;
        vsprintf(output, format, arg_ptr);
        perror("File error:");
        printf("%s",output);

}
void imsg(char * arg_list, ...)
{
        va_list arg_ptr;
        char *format, output[1000];
        va_start(arg_ptr, arg_list);
        format = arg_list;
        vsprintf(output, format, arg_ptr);
/* printf("%s",output); */
}
void wmsg(char * arg_list, ...)
{
        va_list arg_ptr;
        char *format, output[1000];
        va_start(arg_ptr, arg_list);
        format = arg_list;
        vsprintf(output, format, arg_ptr);
        printf("%s",output);
}
void emsg(char * arg_list, ...)
{
        va_list arg_ptr;
        char *format, output[1000];
        va_start(arg_ptr, arg_list);
        format = arg_list;
        vsprintf(output, format, arg_ptr);
        printf("%s",output);
}
#ifndef unix
int lib_mail_file(char *to, char *subj, char *fname)
{
        return TRUE;
}
int lib_freespace(char *s)
{
        return 100000;
}
#endif
int diskuse_get(void)
{
        return 0;
}
char *nt_pcname(void);

int do_register(void)
{
        FILE *f;
        char *tname[]={"1 485 Full License"
                , "3 222 Limited License", "4 free license for school", "5 320 Support for school"
                , "5 320 Support", "6 2950 Priority"};
        char name[200],org[200],email[200],rtype[200],phone[200],fax[200];
        char address1[200];
        char address2[200];
        char address3[200];
        char comments[200];
        char where[200];
        char visa_number[200];
        char visa_name[200];
        char visa_expirey[200];
        char visa_type[200];
        char bf[500];
        char *s;
	int rrtype;

        printf("\n\nDNEWS Registration \n");
        printf("Your Name ? "); gets(name);
        printf("Company or Organization Name ? "); gets(org);
        printf("Phone Number (include area code) ? "); gets(phone);
        printf("Fax Number (include area code) ? "); gets(fax);
        printf("Email address ? "); gets(email);
        printf("Address line 1/3 ? "); gets(address1);
        printf("Address line 2/3 ? "); gets(address2);
        printf("Address line 3/3 ? "); gets(address3);
        printf("Registration type: (we mail invoices except for type 3) \n");
        printf(" 1) Full version with unlimited user access and 1 years email help US$485 \n");
        printf(" 2) Lite version with limited user access and 1 years email help US$222\n");
        printf(" 3) School or University using DNEWS for non profit use with no support\n");
        printf(" 4) School or University using DNEWS with email support and updates US$350\n");
        printf(" 5) Extending support and updates for 12 months US$350 \n");
        printf(" 6) Priority support with remote assistance for 12 months US$2950 \n");

again:  printf("Registration type 1-5 ? "); gets(rtype);
        if (atoi(rtype)<=0) goto again;
        if (atoi(rtype)>6) goto again;
	rrtype = atoi(rtype);
        printf("NOTE: Payment is required before a permanent license key will be issued\n");
        printf("Fill in these details if you wish to pay by VISA/Master/AMEX card\n");
        printf("Credit card type (Visa/Master/AMEX) ? "); gets(visa_type);
        printf("Name of card holder ? "); gets(visa_name);
        printf("Visa Number ? "); gets(visa_number);
        printf("Visa Expirey date ? "); gets(visa_expirey);
        
        printf("Comments ? "); gets(comments);
        printf("Where did you find DNEWS ? "); gets(where);

        f = fopen("register.txt","w");
        if (f==NULL) {perror("Cannot open register.txt"); return TRUE;}
        fprintf(f,"Email this file to  %s \n\n",reg_address()); 
        fprintf(f,"Please register my copy of DNEWS, my details are:\n\n");     
        fprintf(f,"Name: %s\n",name);
#ifdef WIN32
        fprintf(f,"ComputerName: %s\n",nt_pcname());
#endif
        fprintf(f,"ComputerType: %s\n",SYSTEM_TYPE);
        fprintf(f,"Organization: %s\n",org);
        fprintf(f,"Email: %s\n",email); 
        fprintf(f,"FAX: %s\n",fax);     
        fprintf(f,"Phone: %s\n",phone); 
        fprintf(f,"Address1: %s\n",address1);
        fprintf(f,"Address2: %s\n",address2);
        fprintf(f,"Address3: %s\n",address3);
        fprintf(f,"Type: %s\n",tname[rrtype-1]);

        sprintf(bf,"%s:%s",visa_type,visa_name);
        s = hide_data(bf,visa_number,visa_expirey);
        strcpy(bf,s); bf[60] = 0;
        fprintf(f,"Data1: %s\n",bf);
        strcpy(bf,s+60); bf[60] = 0;
        fprintf(f,"Data2: %s\n",bf);
        strcpy(bf,s+120); bf[60] = 0;
        fprintf(f,"Data3: %s\n",bf);

        fprintf(f,"Comments: %s\n",comments);
        fprintf(f,"Where: %s\n",where);
        fclose(f);
        printf("Note: If you plan to install dnews on a machine with a different\n");
        printf("      computer name then you should modify register.txt before sending it\n");
        printf("\n Orders can also be sent to:\n");
        printf("        %s\n",reg_address2());
        printf("        %s\n",reg_address3());
        printf("\nNow email register.txt to %s (or your local reseller)\n",reg_address());
        return TRUE;
}
char *reg_address(void)
{
        static char bf[512];
        strcpy(bf,config_get(C_RESELL_MAIL));
        if (strlen(bf)==0) strcpy(bf,"netwin@world.std.com");
        return bf;
}
char *reg_address2(void)
{
        static char bf[512];
        strcpy(bf,config_get(C_RESELL_ADDRESS1));
        if (strlen(bf)==0) strcpy(bf,"NetWin, PO Box 27-574, Mt Roskill, Auckland, New Zealand.");
        return bf;
}
char *reg_address3(void)
{
        static char bf[512];
        strcpy(bf,config_get(C_RESELL_ADDRESS2));
        if (strlen(bf)==0) strcpy(bf,"FAX: +64-9630-0689 ");
        return bf;
}
char *lib_findfile(char *x)
{
        return NULL;
}
void dnews_manager_report(char *s)
{
        
}
int post_local(String *xitem, char *addr, int ismod)
{
        return TRUE;
}
void dnews_exit(char *reason, char *file, int line)
{
        printf("Crashing\n");
        exit(1);
}



#ifdef WIN32
#define KEY_COMPUTER_NAME "SYSTEM\\CurrentControlSet\\Control\\ComputerName\\ComputerName"
#include <windows.h>
#include <winnt.h>
char *nt_pcname(void)
{
    HKEY MyKey;
        static    char Line[64];
    int  linelen = 64;
    DWORD type_of_entry, result;

        RegCreateKey(HKEY_LOCAL_MACHINE, KEY_COMPUTER_NAME, &MyKey);
        result = RegQueryValueEx(MyKey, "ComputerName", 0, &type_of_entry, 
                (unsigned char *)Line, &linelen);
        RegCloseKey(MyKey);
        if (result == ERROR_SUCCESS && type_of_entry == REG_SZ) {
                return Line;
        }
}

#endif


int xtable[281];
#define xswitch(aa,bb) {cc=aa; aa=bb; bb=cc;}
char *hide_data(char *a, char *b, char *c)
{
        static unsigned char bf[BFSZ];
        static char out[BFSZ];
        unsigned char *s;
        int i,x,j,cc;
        sprintf((char *) bf,"%s|%s|%s|||",a,b,c);

        for (i=0; i<180; i++) xtable[i] = i;
        for (i=0,x=0,j=0; i<180; i++,x+=5,j+=11) {
                xswitch(xtable[i],xtable[(i*i+x+j) % 180]);
        }
        for (i=0,x=0; i<180; i++,x+=19) out[i] = ((x+i*i*123) % 26) + 'a';
        
        for (s=bf; *s!=0; s++) {
                if (*s==' ') *s = '~';
                if (*s=='\t') *s = '^';
                if (*s>='0' && *s<='9') *s = '0' + 9 - (*s - '0');
                if (*s>='a' && *s<='z') *s = 'a' + 25 - (*s - 'a');
                if (*s>='A' && *s<='Z') *s = 'A' + 25 - (*s - 'A');
        }
        for (i=0; (i<80) && (bf[i]!=0); i++) {
                out[xtable[i]] = bf[i];
        }               
        out[180] = 0;
        return out;
}
