]> bicyclesonthemoon.info Git - ott/enhance/blob - npb-cgi.c
nofading online
[ott/enhance] / npb-cgi.c
1 //  //  //Download images and add
2    //  // Newpixbots to them! (or Megans)
3   //  //  
4  //  //   Requires cgilib
5 //  //  //line 146 of cgi.h
6    //  // must be changed
7   //  //  from "extern }"
8  //  //   to "}"
9 //  //  //
10    //  // ~~bicyclesonthemoon
11
12 #include <cgi.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <string.h>
17 #include <sys/wait.h>
18 #include <sys/stat.h>
19
20 #define WGET_PATH  "/usr/bin/wget"
21 #define NPB_PATH   "./npb"
22 #define MEG_PATH   "./403"
23 #define RM_PATH    "/bin/rm"
24 #define LOG_PATH   "/eizm/log/npb/npb.log"
25 #define LOG_PATH2  "/eizm/log/npb/403.log"
26 #define USERAGENT  "NewpixbOTTification\x20(1190.bicyclesonthemoon.dnsd.info/npb/npb.htm)"
27 #define USERAGENT2 "403fication\x20(1190.bicyclesonthemoon.dnsd.info/npb/403.htm)"
28 #define MSTD_PATH  "/pro/npb/mpb.png"
29 #define MSTD_PATH2 "/pro/npb/503.png"
30
31 int main (int argc, char *argv[]);
32 void mustard(int mustard);
33
34 FILE *pix;
35 unsigned char buf[1024];
36 char url[1024];
37 char path[256];
38 char method[256];
39 s_cgi *cgi;
40 int t;
41 pid_t sub;
42 struct stat st;
43 unsigned long p;
44 unsigned short q;
45 char **up;
46 bool meg=false;
47
48 int main (int argc, char *argv[])
49 {
50         cgi=cgiInit();
51         
52         sprintf(path,"%s",(getenv("PATH_INFO")!=NULL)?(getenv("PATH_INFO")):"");
53         if(strcmp(path,"/403")==0)
54         {
55                 meg=true;
56                 //mustard(0);
57         }
58         
59         up=cgiGetFiles(cgi);
60         if (up)
61         {
62                 sprintf(path,"%s",cgiGetFile(cgi,up[0])->tmpfile);
63                 sprintf(url,"%s",cgiGetFile(cgi,up[0])->filename);
64         }
65         else
66         {
67                 sprintf(url,"%s",(cgiGetValue(cgi,"inpix")!=NULL)?(cgiGetValue(cgi,"inpix")):"");
68                 sprintf(path,"/var/tmp/%lu",(unsigned long)getpid());
69                 
70                 //Why is  there no spawnl() in linux?
71                 sub=fork();
72                 if(sub==0)
73                 {
74                         t=execl(WGET_PATH,WGET_PATH,"-q","-t","2","-U",meg?USERAGENT2:USERAGENT,"-O",path,url,(char *)0);
75                         exit(t);
76                 }
77                 waitpid(sub,&t,0);
78                 if(t)mustard(t);
79         }
80         
81         sub=fork();
82         if(sub==0)
83         {
84                 t=execl(meg?MEG_PATH:NPB_PATH,meg?MEG_PATH:NPB_PATH,path,path,"q",(char *)0);
85                 exit(t);
86         }
87         waitpid(sub,&t,0);
88         if(t)mustard(t);
89         
90         pix=fopen(path,"rb");
91         if(pix==NULL)
92                 mustard(123);
93         fstat(fileno(pix),&st);
94         p=st.st_size/1024;
95         q=st.st_size%1024;
96         
97         printf("Content-Length: %lu\n",(unsigned long)(st.st_size));
98         printf("Content-type: image/png\n\n");
99         for(unsigned long i=0;i<p;++i)
100         {
101                 fread(buf,1,1024,pix);
102                 fwrite(buf,1,1024,stdout);
103         }
104         if(q!=0)
105         {
106                 fread(buf,1,q,pix);
107                 fwrite(buf,1,q,stdout);
108         }
109         fclose(pix);
110         fflush(stdout);
111         mustard(0);//no mustard
112 }
113
114 void mustard(int mustard)
115 {
116         if(mustard)
117         {
118                 cgiRedirect(meg?MSTD_PATH2:MSTD_PATH);
119         }
120         sub=fork();
121         if(sub==0)
122         {
123                 t=execl(RM_PATH,RM_PATH,"-f",path,(char *)0);
124                 exit(t);
125         }
126         waitpid(sub,&t,0);
127         pix=fopen(meg?LOG_PATH2:LOG_PATH,"at");
128         if(pix!=NULL)
129         {
130                 fprintf(pix,"%x %s\n",mustard,url);
131                 fclose(pix);
132         }
133         exit(mustard);
134 }