// User info CGI. Once again it output's HTML. Customise it if you like

// Evilution @ efnet

#include <sys/param.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <syslog.h>
#include <sys/file.h>
#include <fcntl.h>
#include <dirent.h>
#include <stdio.h>
#include <time.h>


struct userfile {
  char username[20];
  int ratio, maxlogins;
  double weekalot, credits;
  char unfo[50];
  int allup_num, alldn_num, wkup_num, wkdn_num, dayup_num, daydn_num, monthup_num, monthdn_num;
  double allup_byte, alldn_byte, wkup_byte, wkdn_byte, dayup_byte, daydn_byte, monthup_byte, monthdn_byte;
  double allup_time, alldn_time, wkup_time, wkdn_time, dayup_time, daydn_time, monthup_time, monthdn_time;
  double nuke_time;
  int numnukes, mbnuked;
  int numlogins;
  double laston, timelim, ontoday;
  int slots;
  char groups[50];
  char ips[1024];
};

struct transtemp {
  int numup;
  double byte, time;
};

// GLOBAL!! I HATE THEM! but alas, for ease of code..

struct userfile buffer;
struct transtemp tempstruct;


// GreyLines code mod'd a *bit* by me

char * hms_format(time_t dirtime, char *dhmbuf, int x)
{
        time_t timenow = time(NULL);
        time_t difftime;
        int days = 0;
        int hours = 0;
        int minutes = 0;
        int seconds = 0;

	if (x == 0) {
	  difftime = timenow - dirtime;
	} else { difftime = dirtime; }

        while(difftime >= (time_t)86400)
        {
                difftime -= (time_t)86400;
                days++;
        }
        while (difftime >= (time_t)3600)
        {
                difftime -= (time_t)3600;
                hours++;
        }
        while(difftime >= (time_t)60)
        {
                difftime -= (time_t)60;
                minutes++;
        }
        seconds = (int)difftime;
        if (days != 0)
                sprintf(dhmbuf, "%2dd %2dh", days, hours);
        else if (hours != 0)
                sprintf(dhmbuf, "%2dh %2dm", hours, minutes);
        else
                sprintf(dhmbuf, "%2dm %2ds", minutes, seconds);
}

FILE * general ( FILE *fp ) {
  int ratio, maxlogins, lc = 0;
  double wkalot, credits;
  char temp[50];
  
  while (lc < 4) {
    switch(lc) {
    case 0: fscanf(fp, "%s", temp); ratio = atoi(temp); break;
    case 1: fscanf(fp, "%s", temp); wkalot = atoi(temp); break;
    case 2: fscanf(fp, "%s", temp); maxlogins = atoi(temp); break;
    case 3: fscanf(fp, "%s", temp); credits = atoi(temp); break;
    }
  lc++;
  }

  buffer.ratio = ratio;
  buffer.weekalot = wkalot;
  buffer.maxlogins = maxlogins;
  buffer.credits = credits;

  fflush(fp);
  return(fp);
}
  
FILE *unfo ( FILE *fp ) {
  char usernfo[50] = "", temp[50] = "";
  fflush(fp);
  
  while (!feof(fp)) {
    fscanf(fp, "%s", temp);
    if(strcmp(temp, "CREDITS") == 0)
      break;
    strcat(usernfo, temp);
    sprintf(usernfo, "%s ", usernfo);
  }
  strcpy(buffer.unfo, usernfo);
  return(fp);
}

FILE *transinfo( FILE *fp, char *use) {
  int numfile, lc = 0;
  char temp[50];
  double bytes;
  double time;

  while (lc < 3) {
    switch(lc) {
    case 0: fscanf(fp, "%s", temp); numfile = atoi(temp); break;
    case 1: fscanf(fp, "%s", temp); bytes = atoi(temp); break;
    case 2: fscanf(fp, "%s", temp); time = atoi(temp); break;
    }
    lc++;
  }
 
  tempstruct.numup = numfile;
  tempstruct.byte = bytes;
  tempstruct.time = time;
 
  return (fp);
}

FILE *nuked( FILE *fp ) {
  double time;
 
  int numnuked, mbnuked, lc = 0;
  char temp[50];

  while (lc < 3) {
    switch(lc) {
    case 0: fscanf(fp, "%s", temp); time = atoi(temp); break;
    case 1: fscanf(fp, "%s", temp); numnuked = atoi(temp); break;
    case 2: fscanf(fp, "%s", temp); mbnuked = atoi(temp); break;
    }
    lc++;
  }
 
  buffer.nuke_time = time;
  buffer.numnukes = numnuked;
  buffer.mbnuked = mbnuked;
  return(fp);
}

FILE *timeinfo( FILE *fp ) {
  double ontoday, laston;
  int numlogins, timelimit, lc = 0;
  char temp[50];

  while (lc < 4) {
    switch(lc) {
    case 0: fscanf(fp, "%s", temp); numlogins = atoi(temp); break;
    case 1: fscanf(fp, "%s", temp); laston = atoi(temp); break;
    case 2: fscanf(fp, "%s", temp); timelimit = atoi(temp); break;
    case 3: fscanf(fp, "%s", temp); ontoday = atoi(temp); break;
    }
    lc++;
  }
  buffer.numlogins = numlogins;
  buffer.laston = laston;
  buffer.timelim = timelimit;
  buffer.ontoday = ontoday;
  return(fp);
}

FILE *showGrp( FILE *fp ) {
  char groups[50] = "", temp[50] = "", ips[1024] = "";
  fflush(fp);
  
  while (!feof(fp)) {
    fscanf(fp, "%s", temp);
    if(strcmp(temp, "IP") == 0)
      break;
    strcat(groups, temp);
    sprintf(groups, "%s ", groups);
  }
  while (!feof(fp)) {
    fscanf(fp, "%s", temp);
    strcat(ips, temp);
    sprintf(ips, "%s ", ips);
  }
  strcpy(buffer.groups, groups);
  strcpy(buffer.ips, ips);
  return(fp);
}

char *trim (char *s1, int number) {
  int x = 0;
 
  while (x != number) {
    s1++; x++;
  }

  return(s1);
}

int main ( int argc, char *argv[]) {
   FILE *fp;

   char datapath[255], userfile[255], temp[255], temp2[255], temp3[255];
   char * username;
   int x = 1;
  
   username = getenv("QUERY_STRING");

   username = trim(username, 2);

   sprintf(userfile, "/home/evilution/users/%s", username);
 
   if((fp = fopen(userfile, "r")) == NULL) {
     printf("Content-type: text/html\n\n");
     printf("<title> USER NOT FOUND </title>\n");
     printf("<body>\n<blockquote>USER WAS NOT FOUND</BLOCKQUOTE>\n");
     printf("<p>click <A HREF=""/~evilution/user.html"">here</A HREF> to try again</p>\n");
     return 0;
   }

   while (fscanf(fp, "%s", temp) == 1 ) {
     if (strcmp(temp, "USER") == 0)
       fscanf(fp, "%s", buffer.username);
     if (strcmp(temp, "GENERAL") == 0)
       general(fp);
     if (strcmp(temp, "UNFO") == 0)
       unfo(fp);
     if (strcmp(temp, "ALLUP") == 0) {
       transinfo(fp, "ALLUP");
       buffer.allup_num = tempstruct.numup;
       buffer.allup_byte = tempstruct.byte; 
       buffer.allup_time = tempstruct.time; 
     }
     if (strcmp(temp, "ALLDN") == 0) {
       transinfo(fp, "ALLDN");
       buffer.alldn_num = tempstruct.numup;
       buffer.alldn_byte = tempstruct.byte; 
       buffer.alldn_time = tempstruct.time;
     }
     if (strcmp(temp, "WKUP") == 0) {
       transinfo(fp, "WKUP");
       buffer.wkup_num = tempstruct.numup;
       buffer.wkup_byte = tempstruct.byte; 
       buffer.wkup_time = tempstruct.time;
     }
     if (strcmp(temp, "WKDN") == 0) {
       transinfo(fp, "WKDN");
       buffer.wkdn_num = tempstruct.numup;
       buffer.wkdn_byte = tempstruct.byte; 
       buffer.wkdn_time = tempstruct.time;
     }
     if (strcmp(temp, "DAYUP") == 0) {
       transinfo(fp, "DAYUP");
       buffer.dayup_num = tempstruct.numup;
       buffer.dayup_byte = tempstruct.byte; 
       buffer.dayup_time = tempstruct.time;
     }
     if (strcmp(temp, "DAYDN") == 0) {
       transinfo(fp, "DAYDN");
       buffer.daydn_num = tempstruct.numup;
       buffer.daydn_byte = tempstruct.byte; 
       buffer.daydn_time = tempstruct.time;
     }
     if (strcmp(temp, "MONTHUP") == 0) {
       transinfo(fp, "MONTHUP");
       buffer.monthup_num = tempstruct.numup;
       buffer.monthup_byte = tempstruct.byte; 
       buffer.monthup_time = tempstruct.time;
     }
     if (strcmp(temp, "MONTHDN") == 0) {
       transinfo(fp, "MONTHDN");
       buffer.monthdn_num = tempstruct.numup;
       buffer.monthdn_byte = tempstruct.byte; 
       buffer.monthdn_time = tempstruct.time;
     }
     if (strcmp(temp, "NUKE") == 0)
       nuked(fp);
     if (strcmp(temp, "TIME") == 0)
       timeinfo(fp);
     if (strcmp(temp, "GROUP") == 0)
       showGrp(fp);
   }
   hms_format(buffer.nuke_time, temp, 0);
   hms_format(buffer.laston, temp2, 0);
   hms_format(buffer.ontoday, temp3, 1);

 printf("Content-type: text/html\n\n");

 printf("<html>\n<head>\n<title>Optic Nerve User Statistics</title></head>\n");

 printf("<body BGCOLOR=""#00000"" TEXT=""#FFFFFF"" ALINK=""#FFFF00"" VLINK=""#EEFF44"">\n<strong><big><big><p><center>Optic Nerve User Statistics</center></big></big></strong></p><div align=""left"">");

 printf("<table border=""2"" width=""640"" height=""300"" bordercolor=""#0000FF"" bordercolorlight=""#5B61A4"" bordercolordark=""#30377E"" bgcolor=""#00000"" cellspacing=""0"" cellpadding=""0"">\n");

 printf("<tr>\n<td width=""741"" height=""42""><font color=""#FFFF00"">User Data for: %-30s</font></td>\n</tr>\n", buffer.username);

 printf("<tr>\n<td width=""371"" height=""100"" bgcolor=""#00000"" bordercolor=""#808080"" valign=""top""align=""left""><center><b><font color=""#FFFF00"">General Data</b></center>\n");

 printf("<p><font color=""#FFFFFF"">Ratio: 1:%d &nbsp;&nbsp; &nbsp;Max Logins: %2d &nbsp;&nbsp; Credits: %8.2f MB<br>\n", buffer.ratio, buffer.maxlogins, (buffer.credits/1024));

 printf("<font color=""#FFFFFF"">UNFO: %-50s<br>\nIdents: %-100s<br>\n", buffer.unfo, buffer.ips);
 printf("<font color=""#FFFFFF"">Times Nuked: %2d Nuked: %5d MB Last Nuked: %-10s's ago<br>\n", buffer.numnukes, buffer.mbnuked, temp);
 printf("<font color=""#FFFFFF"">Total Logins: %4d Laston: %-10s ago Time On Today: %-10s <br>\n", buffer.numlogins, temp2, temp3);
 printf("</td>\n</tr>\n");
 printf("<tr>\n <td width=""371"" height=""50"" bgcolor=""#00000"" bordercolor=""#808080"" valign=""top"" align=""left""><b><center><font color=""#FFFF00"">All Time Transfer Information</center></b>\n");
 printf(" <font color=""#FFFFFF"">UP-Number Files: %-5d for %8.2f MB at an average rate of %6.2f K/s  <BR>", buffer.allup_num, (buffer.allup_byte/1024), (buffer.allup_byte/buffer.allup_time));
 printf(" <font color=""#FFFFFF"">DN-Number Files: %-5d for %8.2f MB at an average rate of %6.2f K/s  <BR>", buffer.alldn_num, (buffer.alldn_byte/1024), (buffer.alldn_byte/buffer.alldn_time));
 printf("</td>\n</tr>\n");
 printf("<tr>\n <td width=""371"" height=""50"" bgcolor=""#00000"" bordercolor=""#808080"" valign=""top"" align=""left""><b><center><font color=""#FFFF00"">Month's Transfer Information</center></b>\n");
 printf(" <font color=""#FFFFFF"">UP-Number Files: %-5d for %8.2f MB at an average rate of %6.2f K/s  <BR>", buffer.monthup_num, (buffer.monthup_byte/1024), (buffer.monthup_byte/buffer.monthup_time));
 printf(" <font color=""#EEEEEE"">DN-Number Files: %-5d for %8.2f MB at an average rate of %6.2f K/s  <BR>", buffer.monthdn_num, (buffer.monthdn_byte/1024), (buffer.monthdn_byte/buffer.monthdn_time)); 
 printf("</td>\n</tr>\n");
 printf("<tr>\n <td width=""371"" height=""50"" bgcolor=""#00000"" bordercolor=""#808080"" valign=""top"" align=""left""><b><center><font color=""#FFFF00"">This Weeks Transfer Information</center></b>\n");
 printf(" <font color=""#FFFFFF"">UP-Number Files: %-5d for %8.2f MB at an average rate of %6.2f K/s  <BR>", buffer.wkup_num, (buffer.wkup_byte/1024), (buffer.wkup_byte/buffer.wkup_time));
 printf(" <font color=""#FFFFFF"">DN-Number Files: %-5d for %8.2f MB at an average rate of %6.2f K/s  <BR>", buffer.wkdn_num, (buffer.wkdn_byte/1024), (buffer.wkdn_byte/buffer.wkdn_time)); 
 printf("</td>\n</tr>\n");
 printf("<tr>\n <td width=""371"" height=""50"" bgcolor=""#00000"" bordercolor=""#808080"" valign=""top"" align=""left""><b><center><font color=""#FFFF00"">Todays Transfer Information</center></b>\n");
 printf(" <font color=""#FFFFFF"">UP-Number Files: %-5d for %8.2f MB at an average rate of %6.2f K/s  <BR>", buffer.dayup_num, (buffer.dayup_byte/1024), (buffer.dayup_byte/buffer.dayup_time));
 printf(" <font color=""#FFFFFF"">DN-Number Files: %-5d for %8.2f MB at an average rate of %6.2f K/s  <BR>", buffer.daydn_num, (buffer.daydn_byte/1024), (buffer.daydn_byte/buffer.daydn_time));
 printf("</table>\n");
 printf("<center>Check Another? (<A HREF=""/~evilution/user.html"">Y</A HREF>/<A HREF=""http://www.yahoo.com"">N</A HREF>)<br>\n");
 printf("</div>\n</body>\n</html>\n");

 fclose(fp);
 return(0);
}








