5 // Copyright (C) 2015 Balthasar SzczepaĆski
\r
7 // This program is free software: you can redistribute it and/or modify
\r
8 // it under the terms of the GNU Affero General Public License as
\r
9 // published by the Free Software Foundation, either version 3 of the
\r
10 // License, or (at your option) any later version.
\r
12 // This program is distributed in the hope that it will be useful,
\r
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
15 // GNU Affero General Public License for more details.
\r
17 // You should have received a copy of the GNU Affero General Public License
\r
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
\r
25 #include <sys/wait.h>
\r
27 #define TEMP_PATH "/eizm/tmp/t1i/bot5."
\r
28 #define LOG_PATH "/eizm/log/t1i/bot5.log"
\r
29 #define WAIT_PATH "/eizm/mem/t1i/mpost/"
\r
30 #define SENT_PATH "/eizm/mem/t1i/mpost/sent/"
\r
31 #define TEMPFILE_PATH "/eizm/tmp/t1i/bot5temp"
\r
32 #define POSTDATA_PATH "/eizm/tmp/t1i/bot5post"
\r
33 #define COOKIE_PATH "/eizm/tmp/t1i/bot5cook"
\r
34 #define STOP_PATH "/eizm/tmp/t1i/bot5stop"
\r
35 #define LS_PATH "/bin/ls"
\r
36 #define MV_PATH "/bin/mv"
\r
37 #define RM_PATH "/bin/rm"
\r
38 #define MAWK_PATH "/usr/bin/mawk"
\r
39 #define WGET_PATH "/usr/bin/wget"
\r
40 #define AWK_POST "/eizm/pro/t1i/post.awk"
\r
41 #define AWK_MPOST "/eizm/pro/t1i/mpost.awk"
\r
42 #define AWK_LASTPOST "/eizm/pro/t1i/lastpost.awk"
\r
44 #define LASTPOST_ADDRESS "http://forums.xkcd.com/viewtopic.php?f=7&t=101043&sd=d"
\r
45 #define USERAGENT "SilentBot (http://1190.bicyclesonthemoon.info/silentbot/; the Silent Chronotransponder bot)"
\r
50 int main(int argc, char **argv)
\r
55 unsigned char text=0;
\r
56 unsigned char newlog=0;
\r
61 unsigned char logopen=0;
\r
72 unsigned char d,m,h;
\r
74 unsigned char another=0;
\r
78 sprintf(temppath,"%s%lu",TEMP_PATH,(unsigned long)getpid());
\r
80 while((yarg = getopt(argc,argv,"tn")) != -1)
\r
97 if(freopen(LOG_PATH,newlog?"wt":"at",stdout)!=NULL)
\r
100 setvbuf(stdout, NULL, _IONBF, 0);
\r
101 dup2(fileno(stdout),fileno(stderr));
\r
102 printf("\n%s",ctime(&t));
\r
104 stopfile=fopen(STOP_PATH,"wt");
\r
105 if (stopfile==NULL)
\r
107 printf("FULL STOPFULL STOP\n");
\r
110 if (lockf(fileno(stopfile),F_TLOCK,0)!=0)
\r
112 printf("FULL STOPFULL STOP\n");
\r
115 fprintf(stopfile,"%hu.FULL STOPFULL STOP\n",0);
\r
121 if(freopen(temppath,"wt",stdout)==NULL)
\r
123 r=execl(LS_PATH,LS_PATH,"-1","--color=never",WAIT_PATH,(char *)0);
\r
128 printf("ls fail.\n");
\r
131 list=fopen(temppath,"rt");
\r
133 printf("list fail.\n");
\r
136 for(i=0; fgets(line,255,list)!=NULL; ++i)
\r
138 sscanf(line,"%s",value);
\r
139 if(value[0]>'9'||value[0]<'0')
\r
144 printf("%s",value);
\r
145 sprintf(inpath,"%s%s",WAIT_PATH,value);
\r
146 post=fopen(inpath,"rt");
\r
149 printf(" fail.\n");
\r
152 for(h=0xff,d=0xff,m=0xff,y=0xffff;fgets(line,31,post)!=NULL;)
\r
154 if(line[strlen(line)-1]!='\n')
\r
156 if(line[0]=='h'&&line[1]=='=')
\r
158 sscanf(line+2,"%hhu",&h);
\r
159 //printf("\n h%hhu d%hhu %s",h,d,line);
\r
161 else if(line[0]=='d'&&line[1]=='=')
\r
163 sscanf(line+2,"%hhu",&d);
\r
164 //printf("\n d%hhu d%hhu %s",d,d,line);
\r
166 else if(line[0]=='m'&&line[1]=='=')
\r
168 sscanf(line+2,"%hhu",&m);
\r
169 //printf("\n m%hhu d%hhu %s",m,d,line);
\r
171 else if(line[0]=='y'&&line[1]=='=')
\r
173 sscanf(line+2,"%hu",&y);
\r
174 //printf("\n y%hu d%hhu %s",y,d,line);
\r
178 printf(" %02hhu.%02hhu.%04hu %02hhu:00",d,m,y,h);
\r
181 if((1900+nowS->tm_year-y)>=2)
\r
183 printf(" timer mustard");
\r
187 if(y>1900+nowS->tm_year)
\r
192 else if(y==1900+nowS->tm_year)
\r
194 if(m>1+nowS->tm_mon)
\r
199 else if(m==1+nowS->tm_mon)
\r
201 if(d>nowS->tm_mday)
\r
206 else if(d==nowS->tm_mday)
\r
208 if(h>nowS->tm_hour)
\r
218 printf(" deliver");
\r
221 {//test latest post
\r
226 r=execl(WGET_PATH,WGET_PATH, "-q","-t", "3","-U", USERAGENT, "-O", TEMPFILE_PATH,LASTPOST_ADDRESS, (char *)0);
\r
236 if(freopen(temppath,"wt",stdout)==NULL)
\r
238 r=execl(MAWK_PATH,MAWK_PATH, "-f",AWK_LASTPOST, TEMPFILE_PATH, (char *)0);
\r
245 post=fopen(temppath,"rt");
\r
249 if(fgets(argp1,255,post)!=NULL)
\r
251 sscanf(argp1,"%s",argp2);
\r
252 sprintf(argp4,"edit=%s",argp2);
\r
259 if(fgets(argp1,255,post)!=NULL)
\r
261 sscanf(argp1,"%s",argp2);
\r
262 sprintf(argp5,"user=%s",argp2);
\r
273 sprintf(argp1 ,"postfile=%s",POSTDATA_PATH);
\r
274 sprintf(argp2,"cookiefile=%s",COOKIE_PATH);
\r
275 sprintf(argp3,"tempfile=%s",TEMPFILE_PATH);
\r
276 // sprintf(argp4,"name=%s",name);
\r
277 // sprintf(argp5,"pass=%s",pass);
\r
279 //no longer necessary - bot can now edit
\r
280 //ETA: not true, still have to wait but only 15s - so that phpBB doesn't
\r
284 sprintf(outpath,"%s%s",SENT_PATH,value);
\r
289 r=execl(MV_PATH,MV_PATH,inpath,outpath,(char *)0);
\r
294 printf(" mv fail.\n");
\r
300 r=execl(MAWK_PATH,MAWK_PATH,"-f",AWK_POST,"-v",argp1,"-v",argp2,"-v",argp3,"-v",argp4,"-v",argp5,outpath,(char *)0);
\r
302 r=execl(MAWK_PATH,MAWK_PATH,"-f",AWK_POST,"-v",argp1,"-v",argp2,"-v",argp3,outpath,(char *)0);
\r
311 r=execl(MAWK_PATH,MAWK_PATH,"-f",AWK_MPOST,"-v",argp1,"-v",argp2,"-v",argp3,outpath,(char *)0);
\r
323 post=fopen(POSTDATA_PATH,"rt");
\r
326 if(fgets(line,255,post)!=NULL)
\r
328 sscanf(line,"%s",value);
\r
331 post=fopen(outpath,"at");
\r
334 fprintf(post,"ID=%s\n",value);
\r
349 printf("Nothing.\n");
\r
354 r=execl(RM_PATH,RM_PATH,"-f",temppath,(char *)0);
\r
359 printf("rm fail.\n");
\r
365 r=execl(RM_PATH,RM_PATH,"-f",COOKIE_PATH,(char *)0);
\r
372 r=execl(RM_PATH,RM_PATH,"-f",POSTDATA_PATH,(char *)0);
\r
379 r=execl(RM_PATH,RM_PATH,"-f",TEMPFILE_PATH,(char *)0);
\r
384 lockf(fileno(stopfile),F_ULOCK,0);
\r
390 r=execl(RM_PATH,RM_PATH,"-f",STOP_PATH,(char *)0);
\r