]> bicyclesonthemoon.info Git - ott/enhance/blobdiff - npb-cgi.c
newpixbot online
[ott/enhance] / npb-cgi.c
index d155c7a3cf864ac024d5bbe3ba41d56c36dc9761..27049dd425df6d29e236372b58cd3b16e7317b24 100644 (file)
--- a/npb-cgi.c
+++ b/npb-cgi.c
-//  //  //Download images and add
-   //  // Newpixbots to them! (or Megans)
-  //  //  
- //  //   Requires cgilib
-//  //  //line 146 of cgi.h
-   //  // must be changed
-  //  //  from "extern }"
- //  //   to "}"
-//  //  //
-   //  // ~~bicyclesonthemoon
+/*
+npb-cgi.c
+Download images and add Newpixbots to them! (or Megans)
+05.12.2022
+
+Copyright (C) 2013, 2014, 2022  Balthasar SzczepaƄski
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU Affero General Public License as
+published by the Free Software Foundation, either version 3 of the
+License, or (at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU Affero General Public License for more details.
+
+You should have received a copy of the GNU Affero General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+Requires cgilib (http://www.infodrom.org/projects/cgilib/)
+*/
 
-#include <cgi.h>
 #include <stdio.h>
-#include <stdlib.h>
+#include <stdint.h>
 #include <unistd.h>
-#include <string.h>
+#include <stdlib.h>
 #include <sys/wait.h>
-#include <sys/stat.h>
+#include <stdint.h>
+#include <inttypes.h>
 
-#define WGET_PATH  "/usr/bin/wget"
-#define NPB_PATH   "./npb"
-#define MEG_PATH   "./403"
-#define RM_PATH    "/bin/rm"
-#define LOG_PATH   "/eizm/log/npb/npb.log"
-#define LOG_PATH2  "/eizm/log/npb/403.log"
-#define USERAGENT  "NewpixbOTTification\x20(1190.bicyclesonthemoon.dnsd.info/npb/npb.htm)"
-#define USERAGENT2 "403fication\x20(1190.bicyclesonthemoon.dnsd.info/npb/403.htm)"
-#define MSTD_PATH  "/pro/npb/mpb.png"
-#define MSTD_PATH2 "/pro/npb/503.png"
+#include <cgi.h>
 
-int main (int argc, char *argv[]);
-void mustard(int mustard);
+#include "online-core.h"
+#include "mpb.h"
+#include "mpb_503.h"
 
-FILE *pix;
-unsigned char buf[1024];
-char url[1024];
-char path[256];
-char method[256];
-s_cgi *cgi;
-int t;
-pid_t sub;
-struct stat st;
-unsigned long p;
-unsigned short q;
-char **up;
-bool meg=false;
+
+int npb (
+       char *inpix, char *outpix,
+       uint_fast8_t remove_border, uint_fast32_t remove_border_width,
+       uint_fast8_t new_border, uint_fast32_t new_border_width,
+       uint_fast8_t external_border, uint_fast32_t external_border_width,
+       uint_fast8_t corners, uint_fast8_t ong1, uint8_t t403
+);
 
 int main (int argc, char *argv[])
 {
-       cgi=cgiInit();
+       s_cgi *cgi;
+       char in_tmp[256];
+       char out_tmp[256];
+       char *in_path;
+       char *t;
+       uint_fast8_t remove_border, new_border, external_border, corners, ong1;
+       uint_fast32_t remove_border_width, new_border_width, external_border_width;
+       uint_fast8_t t403 = 0;
        
-       sprintf(path,"%s",(getenv("PATH_INFO")!=NULL)?(getenv("PATH_INFO")):"");
-       if(strcmp(path,"/403")==0)
-       {
-               meg=true;
-               //mustard(0);
-       }
+       int r=0;
+       int r1=0;
+       int r2=0;
+       int r3=0;
        
-       up=cgiGetFiles(cgi);
-       if (up)
-       {
-               sprintf(path,"%s",cgiGetFile(cgi,up[0])->tmpfile);
-               sprintf(url,"%s",cgiGetFile(cgi,up[0])->filename);
-       }
-       else
-       {
-               sprintf(url,"%s",(cgiGetValue(cgi,"inpix")!=NULL)?(cgiGetValue(cgi,"inpix")):"");
-               sprintf(path,"/var/tmp/%lu",(unsigned long)getpid());
+       do {
+               make_tmp_path(in_tmp, 256, 0, "");
+               make_tmp_path(out_tmp, 256, 1, ".png");
+               
+               cgi=cgiInit();
                
-               //Why is  there no spawnl() in linux?
-               sub=fork();
-               if(sub==0)
+               r = get_file(cgi,"inpix2", in_tmp, &in_path);
+               if (r)
+                       r = get_file(cgi,"inpix", in_tmp, &in_path);
+               if (r)
+                       break;
+               
+               remove_border   = (cgiGetValue(cgi, "r")!=NULL) ? 1 : 0;
+               external_border = (cgiGetValue(cgi, "e")!=NULL) ? 1 : 0;
+               new_border      = (cgiGetValue(cgi, "b")!=NULL) ? 1 : 0;
+               corners         = (cgiGetValue(cgi, "c")!=NULL) ? 1 : 0;
+               ong1            = (cgiGetValue(cgi, "o")!=NULL) ? 1 : 0;
+               t403            = (cgiGetValue(cgi, "4")!=NULL) ? 1 : 0;
+               
+               if (remove_border)
                {
-                       t=execl(WGET_PATH,WGET_PATH,"-q","-t","2","-U",meg?USERAGENT2:USERAGENT,"-O",path,url,(char *)0);
-                       exit(t);
+                       t = cgiGetValue(cgi, "rw");
+                       if (t != NULL)
+                               sscanf(t,"%"SCNuFAST32, &remove_border_width);
+                       else
+                               remove_border_width = 0;
                }
-               waitpid(sub,&t,0);
-               if(t)mustard(t);
-       }
-       
-       sub=fork();
-       if(sub==0)
-       {
-               t=execl(meg?MEG_PATH:NPB_PATH,meg?MEG_PATH:NPB_PATH,path,path,"q",(char *)0);
-               exit(t);
-       }
-       waitpid(sub,&t,0);
-       if(t)mustard(t);
-       
-       pix=fopen(path,"rb");
-       if(pix==NULL)
-               mustard(123);
-       fstat(fileno(pix),&st);
-       p=st.st_size/1024;
-       q=st.st_size%1024;
-       
-       printf("Content-Length: %lu\n",(unsigned long)(st.st_size));
-       printf("Content-type: image/png\n\n");
-       for(unsigned long i=0;i<p;++i)
-       {
-               fread(buf,1,1024,pix);
-               fwrite(buf,1,1024,stdout);
-       }
-       if(q!=0)
+               
+               if (external_border)
+               {
+                       t = cgiGetValue(cgi, "ew");
+                       if (t != NULL)
+                               sscanf(t,"%"SCNuFAST32, &external_border_width);
+                       else
+                               external_border_width = 0;
+               }
+               
+               if (new_border)
+               {
+                       t = cgiGetValue(cgi, "bw");
+                       if (t != NULL)
+                               sscanf(t,"%"SCNuFAST32, &new_border_width);
+                       else
+                               new_border_width = 0;
+               }
+               
+               r = npb(
+                       in_path, out_tmp,
+                       remove_border, remove_border_width,
+                       new_border, new_border_width,
+                       external_border, external_border_width,
+                       corners, ong1, t403
+               );
+               if (r)
+                       break;
+               
+               r1 = send_file(out_tmp, "image/png", 0);
+       } while (0);
+       if (r)
        {
-               fread(buf,1,q,pix);
-               fwrite(buf,1,q,stdout);
+               r1 = send_data(
+                       t403 ? mpb_503 : mpb,
+                       t403 ? mpb_503_size : mpb_size,
+                       "image/png",
+                       500
+               );
        }
-       fclose(pix);
-       fflush(stdout);
-       mustard(0);//no mustard
+       r2 = rm(in_tmp);
+       r3 = rm(out_tmp);
+       if (r)
+               return r;
+       if (r1)
+               return r1;
+       if (r2)
+               return r2;
+       if (r3)
+               return r3;
+       return 0;
 }
 
-void mustard(int mustard)
+int npb (
+       char *inpix, char *outpix,
+       uint_fast8_t remove_border, uint_fast32_t remove_border_width,
+       uint_fast8_t new_border, uint_fast32_t new_border_width,
+       uint_fast8_t external_border, uint_fast32_t external_border_width,
+       uint_fast8_t corners, uint_fast8_t ong1, uint8_t t403
+)
 {
-       if(mustard)
-       {
-               cgiRedirect(meg?MSTD_PATH2:MSTD_PATH);
-       }
-       sub=fork();
-       if(sub==0)
-       {
-               t=execl(RM_PATH,RM_PATH,"-f",path,(char *)0);
-               exit(t);
-       }
-       waitpid(sub,&t,0);
-       pix=fopen(meg?LOG_PATH2:LOG_PATH,"at");
-       if(pix!=NULL)
+       char s_rw[13] = "-0";
+       char s_bw[13] = "-0";
+       char s_ew[13] = "-0";
+       
+       pid_t sub;
+       int r;
+       
+       if (remove_border)
+               snprintf(s_rw, 13, "-r%"PRIuFAST32, remove_border_width);
+       if (new_border)
+               snprintf(s_bw, 13, "-b%"PRIuFAST32, new_border_width);
+       if (external_border)
+               snprintf(s_ew, 13, "-e%"PRIuFAST32, external_border_width);
+       
+       sub = fork();
+       if (sub == 0)
        {
-               fprintf(pix,"%x %s\n",mustard,url);
-               fclose(pix);
+               r = execl(
+                       NPB_PATH, NPB_PATH,
+                       s_rw, s_ew, s_bw,
+                       corners ? "-c" : "-0",
+                       ong1 ? "-o" : "-0",
+                       t403 ? "-4" : "-0",
+                       inpix, outpix,
+                       (char *)0
+               );
+               exit(r);
        }
-       exit(mustard);
-}
\ No newline at end of file
+       waitpid(sub, &r, 0);
+       return r;
+}