]> bicyclesonthemoon.info Git - ott/enhance/blob - bluenhd.cpp
54ef52831f9548f5e28596c0874b5a7cc092774c
[ott/enhance] / bluenhd.cpp
1 //  //  //Download images and 
2    //  // ENHANCE them!
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 ENH_PATH   "./bluenh"
22 #define RM_PATH    "/bin/rm"
23 #define LOG_PATH   "/eizm/log/bluenh/bluenh.log"
24 #define USERAGENT  "Bluenhancement\x20(1190.bicyclesonthemoon.dnsd.info/bluenh/bluenh.htm)"
25 #define MSTD_PATH  "/pro/bluenh/nh.png"
26
27 int main (int argc, char *argv[]);
28 void mustard(int mustard);
29
30 FILE *pix;
31 unsigned char buf[1024];
32 char url[1024];
33 char path[256];
34 char method[256];
35 s_cgi *cgi;
36 int t;
37 pid_t sub;
38 struct stat st;
39 unsigned long p;
40 unsigned short q;
41 char **up;
42 // unsigned char a,b,c,d,e,f;
43 // bool meg=false;
44
45 int main (int argc, char *argv[])
46 {
47         cgi=cgiInit();
48         
49         up=cgiGetFiles(cgi);
50         if (up)
51         {
52                 sprintf(path,"%s",cgiGetFile(cgi,up[0])->tmpfile);
53                 sprintf(url,"%s",cgiGetFile(cgi,up[0])->filename);
54         }
55         else
56         {
57                 sprintf(url,"%s",(cgiGetValue(cgi,"inpix")!=NULL)?(cgiGetValue(cgi,"inpix")):"");
58                 sprintf(path,"/var/tmp/%lu",(unsigned long)getpid());
59                 
60                 //Why is  there no spawnl() in linux?
61                 sub=fork();
62                 if(sub==0)
63                 {
64                         t=execl(WGET_PATH,WGET_PATH,"-q","-t","2","-U",USERAGENT,"-O",path,url,(char *)0);
65                         exit(t);
66                 }
67                 waitpid(sub,&t,0);
68                 if(t)mustard(t);
69         }
70         
71         sub=fork();
72         if(sub==0)
73         {
74                 t=execl(ENH_PATH,ENH_PATH,path,path,cgiGetValue(cgi,"a")!=NULL?cgiGetValue(cgi,"a"):"0",cgiGetValue(cgi,"b")!=NULL?cgiGetValue(cgi,"b"):"127",cgiGetValue(cgi,"c")!=NULL?cgiGetValue(cgi,"c"):"255",cgiGetValue(cgi,"d")!=NULL?cgiGetValue(cgi,"d"):"0",cgiGetValue(cgi,"e")!=NULL?cgiGetValue(cgi,"e"):"127",cgiGetValue(cgi,"f")!=NULL?cgiGetValue(cgi,"f"):"255","q",(char *)0);
75                 exit(t);
76         }
77         waitpid(sub,&t,0);
78         if(t)mustard(t);
79         
80         pix=fopen(path,"rb");
81         if(pix==NULL)
82                 mustard(123);
83         fstat(fileno(pix),&st);
84         p=st.st_size/1024;
85         q=st.st_size%1024;
86         
87         printf("Content-Length: %lu\n",(unsigned long)(st.st_size));
88         printf("Content-type: image/png\n\n");
89         for(unsigned long i=0;i<p;++i)
90         {
91                 fread(buf,1,1024,pix);
92                 fwrite(buf,1,1024,stdout);
93         }
94         if(q!=0)
95         {
96                 fread(buf,1,q,pix);
97                 fwrite(buf,1,q,stdout);
98         }
99         fclose(pix);
100         fflush(stdout);
101         mustard(0);//no mustard
102 }
103
104 void mustard(int mustard)
105 {
106         if(mustard)
107         {
108                 cgiRedirect(MSTD_PATH);
109         }
110         sub=fork();
111         if(sub==0)
112         {
113                 t=execl(RM_PATH,RM_PATH,"-f",path,(char *)0);
114                 exit(t);
115         }
116         waitpid(sub,&t,0);
117         pix=fopen(LOG_PATH,"at");
118         if(pix!=NULL)
119         {
120                 fprintf(pix,"%x %s\n",mustard,url);
121                 fclose(pix);
122         }
123         exit(mustard);
124 }