]> bicyclesonthemoon.info Git - ott/t1i/blob - bot5.c
Update from src as of 15.11.2015
[ott/t1i] / bot5.c
1 // bot5.c\r
2 // SilentBot\r
3 // 24.03.2015\r
4 // \r
5 // Copyright (C) 2015  Balthasar SzczepaƄski\r
6 // \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
11 // \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
16 // \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
19 \r
20 #include <unistd.h>\r
21 #include <stdio.h>\r
22 #include <stdlib.h>\r
23 #include <time.h>\r
24 #include <string.h>\r
25 #include <sys/wait.h>\r
26 \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
43 #define PWAIT 15\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
46 \r
47 pid_t sub;\r
48 int r;\r
49 \r
50 int main(int argc, char **argv)\r
51 {\r
52         time_t t;\r
53         struct tm *nowS;\r
54         int yarg;\r
55         unsigned char text=0;\r
56         unsigned char newlog=0;\r
57         FILE *list;\r
58         FILE *post;\r
59         FILE *stopfile;\r
60         char temppath[256];\r
61         unsigned char logopen=0;\r
62         unsigned char i;\r
63         char line[256];\r
64         char value[256];\r
65         char inpath[256];\r
66         char outpath[256];\r
67         char argp1[256];\r
68         char argp2[256];\r
69         char argp3[256];\r
70         char argp4[256];\r
71         char argp5[256];\r
72         unsigned char d,m,h;\r
73         unsigned short y;\r
74         unsigned char another=0;\r
75         unsigned char edit;\r
76         \r
77         t=time(NULL);\r
78         sprintf(temppath,"%s%lu",TEMP_PATH,(unsigned long)getpid());\r
79         \r
80         while((yarg = getopt(argc,argv,"tn")) != -1)\r
81         {\r
82                 switch(yarg)\r
83                 {\r
84                 case 't':\r
85                         text=1;\r
86                         break;\r
87                 case 'n':\r
88                         newlog=1;\r
89                         break;\r
90                 default:\r
91                         \r
92                         break;\r
93                 }\r
94         }\r
95         if(!text)\r
96         {\r
97                 if(freopen(LOG_PATH,newlog?"wt":"at",stdout)!=NULL)\r
98                         logopen=1;\r
99         }\r
100         setvbuf(stdout, NULL, _IONBF, 0);\r
101         dup2(fileno(stdout),fileno(stderr));\r
102         printf("\n%s",ctime(&t));\r
103         \r
104         stopfile=fopen(STOP_PATH,"wt");\r
105         if (stopfile==NULL)\r
106         {\r
107                 printf("FULL STOPFULL STOP\n");\r
108                 return 1;\r
109         }\r
110         if (lockf(fileno(stopfile),F_TLOCK,0)!=0)\r
111         {\r
112                 printf("FULL STOPFULL STOP\n");\r
113                 return 1;\r
114         }\r
115         fprintf(stopfile,"%hu.FULL STOPFULL STOP\n",0);\r
116         \r
117         \r
118         sub=fork();\r
119         if(sub==0)\r
120         {\r
121                 if(freopen(temppath,"wt",stdout)==NULL)\r
122                         exit(255);\r
123                 r=execl(LS_PATH,LS_PATH,"-1","--color=never",WAIT_PATH,(char *)0);\r
124                 exit(r);\r
125         }\r
126         waitpid(sub,&r,0);\r
127         if(r)\r
128                 printf("ls fail.\n");\r
129         else\r
130         {\r
131                 list=fopen(temppath,"rt");\r
132                 if(list==NULL)\r
133                         printf("list fail.\n");\r
134                 else\r
135                 {\r
136                         for(i=0; fgets(line,255,list)!=NULL; ++i)\r
137                         {\r
138                         sscanf(line,"%s",value);\r
139                                 if(value[0]>'9'||value[0]<'0')\r
140                                 {\r
141                                         --i;\r
142                                         continue;\r
143                                 }\r
144                                 printf("%s",value);\r
145                                 sprintf(inpath,"%s%s",WAIT_PATH,value);\r
146                                 post=fopen(inpath,"rt");\r
147                                 if(post==NULL)\r
148                                 {\r
149                                         printf(" fail.\n");\r
150                                         continue;\r
151                                 }\r
152                                 for(h=0xff,d=0xff,m=0xff,y=0xffff;fgets(line,31,post)!=NULL;)\r
153                                 {\r
154                                         if(line[strlen(line)-1]!='\n')\r
155                                                 continue;\r
156                                         if(line[0]=='h'&&line[1]=='=')\r
157                                         {\r
158                                                 sscanf(line+2,"%hhu",&h);\r
159                                                 //printf("\n h%hhu d%hhu %s",h,d,line);\r
160                                         }\r
161                                         else if(line[0]=='d'&&line[1]=='=')\r
162                                         {\r
163                                                 sscanf(line+2,"%hhu",&d);\r
164                                                 //printf("\n d%hhu d%hhu %s",d,d,line);\r
165                                         }\r
166                                         else if(line[0]=='m'&&line[1]=='=')\r
167                                         {\r
168                                                 sscanf(line+2,"%hhu",&m);\r
169                                                 //printf("\n m%hhu d%hhu %s",m,d,line);\r
170                                         }\r
171                                         else if(line[0]=='y'&&line[1]=='=')\r
172                                         {\r
173                                                 sscanf(line+2,"%hu",&y);\r
174                                                 //printf("\n y%hu d%hhu %s",y,d,line);\r
175                                         }\r
176                                 }\r
177                                 fclose(post);\r
178                                 printf(" %02hhu.%02hhu.%04hu %02hhu:00",d,m,y,h);\r
179                                 nowS=gmtime(&t);\r
180                                 \r
181                                 if((1900+nowS->tm_year-y)>=2)\r
182                                 {\r
183                                         printf(" timer mustard");\r
184                                         continue;\r
185                                 }\r
186                                 \r
187                                 if(y>1900+nowS->tm_year)\r
188                                 {\r
189                                         printf(" wait\n");\r
190                                         continue;\r
191                                 }\r
192                                 else if(y==1900+nowS->tm_year)\r
193                                 {\r
194                                         if(m>1+nowS->tm_mon)\r
195                                         {\r
196                                                 printf(" wait\n");\r
197                                                 continue;\r
198                                         }\r
199                                         else if(m==1+nowS->tm_mon)\r
200                                         {\r
201                                                 if(d>nowS->tm_mday)\r
202                                                 {\r
203                                                         printf(" wait\n");\r
204                                                         continue;\r
205                                                 }\r
206                                                 else if(d==nowS->tm_mday)\r
207                                                 {\r
208                                                         if(h>nowS->tm_hour)\r
209                                                         {\r
210                                                                 printf(" wait\n");\r
211                                                                 continue;\r
212                                                         }\r
213                                                 }\r
214                                                 \r
215                                         }\r
216                                 }\r
217                                 \r
218                                 printf(" deliver");\r
219                                 \r
220                                 do\r
221                                 {//test latest post\r
222                                         edit=0;\r
223                                         sub=fork();\r
224                                         if(sub==0)\r
225                                         {\r
226                                                 r=execl(WGET_PATH,WGET_PATH, "-q","-t", "3","-U", USERAGENT, "-O", TEMPFILE_PATH,LASTPOST_ADDRESS, (char *)0);\r
227                                                 exit(r);\r
228                                         }\r
229                                         waitpid(sub,&r,0);\r
230                                         if(r)\r
231                                                 break;\r
232                                         \r
233                                         sub=fork();\r
234                                         if(sub==0)\r
235                                         {\r
236                                                 if(freopen(temppath,"wt",stdout)==NULL)\r
237                                                         exit(255);\r
238                                                 r=execl(MAWK_PATH,MAWK_PATH, "-f",AWK_LASTPOST, TEMPFILE_PATH, (char *)0);\r
239                                                 exit(r);\r
240                                         }\r
241                                         waitpid(sub,&r,0);\r
242                                         if(r)\r
243                                                 break;\r
244                                         \r
245                                         post=fopen(temppath,"rt");\r
246                                         if(post==NULL)\r
247                                                 break;\r
248                                                 \r
249                                         if(fgets(argp1,255,post)!=NULL)\r
250                                         {\r
251                                                 sscanf(argp1,"%s",argp2);\r
252                                                 sprintf(argp4,"edit=%s",argp2);\r
253                                         }\r
254                                         else\r
255                                         {\r
256                                                 fclose(post);\r
257                                                 break;\r
258                                         }\r
259                                         if(fgets(argp1,255,post)!=NULL)\r
260                                         {\r
261                                                 sscanf(argp1,"%s",argp2);\r
262                                                 sprintf(argp5,"user=%s",argp2);\r
263                                         }\r
264                                         else\r
265                                         {\r
266                                                 fclose(post);\r
267                                                 break;\r
268                                         }\r
269                                         fclose(post);\r
270                                         edit=1;\r
271                                 }while(0);\r
272                                 \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
278                                 \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
281                                 if(another)\r
282                                         sleep(PWAIT);\r
283                                 \r
284                                 sprintf(outpath,"%s%s",SENT_PATH,value);\r
285                                 \r
286                                 sub=fork();\r
287                                 if(sub==0)\r
288                                 {\r
289                                         r=execl(MV_PATH,MV_PATH,inpath,outpath,(char *)0);\r
290                                         exit(r);\r
291                                 }\r
292                                 waitpid(sub,&r,0);\r
293                                 if(r)\r
294                                         printf(" mv fail.\n");\r
295                                 \r
296                                 sub=fork();\r
297                                 if(!sub)\r
298                                 {\r
299                                         if(edit)\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
301                                         else\r
302                                                 r=execl(MAWK_PATH,MAWK_PATH,"-f",AWK_POST,"-v",argp1,"-v",argp2,"-v",argp3,outpath,(char *)0);\r
303                                         exit(r);\r
304                                 }\r
305                                 waitpid(sub,&r,0);\r
306                                 if(r)\r
307                                 {\r
308                                         sub=fork();\r
309                                         if(!sub)\r
310                                         {\r
311                                                 r=execl(MAWK_PATH,MAWK_PATH,"-f",AWK_MPOST,"-v",argp1,"-v",argp2,"-v",argp3,outpath,(char *)0);\r
312                                                 exit(r);\r
313                                         }\r
314                                         waitpid(sub,&r,0);\r
315                                         if(r)\r
316                                         {\r
317                                                 another=0;\r
318                                                 continue;\r
319                                         }\r
320                                 }\r
321                                 else\r
322                                 {\r
323                                         post=fopen(POSTDATA_PATH,"rt");\r
324                                         if(post!=NULL)\r
325                                         {\r
326                                                 if(fgets(line,255,post)!=NULL)\r
327                                                 {\r
328                                                         sscanf(line,"%s",value);\r
329                                                         fclose(post);\r
330                                                         \r
331                                                         post=fopen(outpath,"at");\r
332                                                         if(post!=NULL)\r
333                                                         {\r
334                                                                 fprintf(post,"ID=%s\n",value);\r
335                                                                 fclose (post);\r
336                                                         }\r
337                                                 }\r
338                                                 else\r
339                                                         fclose(post);\r
340                                         }\r
341                                 \r
342                                 }\r
343                                 printf(" ok\n");\r
344                                 another=1;\r
345                                 \r
346                         }\r
347                         \r
348                         if(i==0)\r
349                                 printf("Nothing.\n");\r
350                         fclose(list);\r
351                         sub=fork();\r
352                         if(sub==0)\r
353                         {\r
354                                 r=execl(RM_PATH,RM_PATH,"-f",temppath,(char *)0);\r
355                                 exit(r);\r
356                         }\r
357                         waitpid(sub,&r,0);\r
358                         if(r)\r
359                                 printf("rm fail.\n");\r
360                         \r
361                         \r
362                                 sub=fork();\r
363                                 if(!sub)\r
364                                 {\r
365                                         r=execl(RM_PATH,RM_PATH,"-f",COOKIE_PATH,(char *)0);\r
366                                         exit(r);\r
367                                 }\r
368                                 waitpid(sub,&r,0);\r
369                                 sub=fork();\r
370                                 if(!sub)\r
371                                 {\r
372                                         r=execl(RM_PATH,RM_PATH,"-f",POSTDATA_PATH,(char *)0);\r
373                                         exit(r);\r
374                                 }\r
375                                 waitpid(sub,&r,0);\r
376                                 sub=fork();\r
377                                 if(!sub)\r
378                                 {\r
379                                         r=execl(RM_PATH,RM_PATH,"-f",TEMPFILE_PATH,(char *)0);\r
380                                         exit(r);\r
381                                 }\r
382                                 waitpid(sub,&r,0);\r
383                                 \r
384                                 lockf(fileno(stopfile),F_ULOCK,0);\r
385                                 fclose(stopfile);\r
386                                 \r
387                                 sub=fork();\r
388                                 if(!sub)\r
389                                 {\r
390                                         r=execl(RM_PATH,RM_PATH,"-f",STOP_PATH,(char *)0);\r
391                                         exit(r);\r
392                                 }\r
393                                 waitpid(sub,&r,0);\r
394                 }\r
395         }\r
396         if(logopen)\r
397                 fclose(stdout);\r
398         return 0;\r
399 }\r