#// post.awk #// The tool to post to the OTT #// 23.03.2015 #// #// Copyright (C) 2015 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 . BEGIN{ FS="=" fora="http://forums.xkcd.com" useragent="\"bothasar_p (http://1190.bicyclesonthemoon.info/ongoing.htm; BFTF post bot)\"" timecmd="date -u +\"%d %b %Y, %k:%M UTC\"" for(i=0;i<256;++i) { ch=sprintf("%c",i) hex=sprintf("%02X",i); ch2hex[ch]=hex hex2ch[hex]=ch } }; { eq=index($0,"=") argtab[substr($0,1,eq-1)]=substr($0,eq+1) }; END{ if(entitydecode(user)!=urldecode(argtab["username"])) edit="" if(system("wget -q -t 3 --connect-timeout=60 --save-cookies=" cookiefile " -U " useragent " -O " tempfile " " fora "/ucp.php?mode=login")) { print "Pre-login fail." exit 1 } while((getline cookie < cookiefile)>0) { if(cookie ~ /_sid/) { split(cookie,arr,"_sid[ \t]*") SID=arr[2] } } close(cookiefile) print "username=" argtab["username"] "&password=" argtab["password"] "&sid=" SID "&login=Login" > postfile close(postfile) if(system("wget -q -t 3 --connect-timeout=60 --save-cookies=" cookiefile " --post-file=" postfile " -U " useragent " -O " tempfile " \"" fora "/ucp.php?mode=login\"")) { print "Login fail." exit 1 } while((getline temp < tempfile)>0) { if (temp ~ /
0) { if (tolower(line) ~ /

information/) { print "Unexpected h2: information\n" getline line < tempfile print line exit 1 } while (line!="") { i=match(line, //) if(i!=0) { tag=substr(line,1,i) line=substr(line,i+1) } else #oh no tag doesn't end on this line - whatever, I don't care. { tag=line line="" } if(tag ~ /^postfile if("&addbbcode20" in argtab) printf ("&addbbcode20=%s",argtab["addbbcode20"])>>postfile if(edit!="") printf ("&message=%s",argtab["old_message"] "%0A%5Bsize%3D110%5D%5Bb%5D" argtab["subject"] "%5B%2Fb%5D%5B%2Fsize%5D%0A%5Bsize%3D80%5D" urlencode(posttime) "%5B%2Fsize%5D%0A" argtab["message"])>>postfile else printf ("&message=%s",argtab["message"])>>postfile printf ("&post=%s","Submit")>>postfile if("disable_bbcode" in argtab) printf ("&disable_bbcode=%s",argtab["disable_bbcode"])>>postfile if("disable_smilies" in argtab) printf ("&disable_smilies=%s",argtab["disable_smilies"])>>postfile if("disable_magic_url" in argtab) printf ("&disable_magic_url=%s",argtab["disable_magic_url"])>>postfile if("attach_sig" in argtab) printf ("&attach_sig=%s",argtab["attach_sig"])>>postfile if("notify" in argtab) printf ("¬ify=%s",argtab["notify"])>>postfile printf ("&creation_time=%s",argtab["creation_time"])>>postfile printf ("&form_token=%s",argtab["form_token"])>>postfile for(i=0; i>postfile close(postfile) system("sleep 1") if(system("wget -q -t 3 --connect-timeout=60 --load-cookies=" cookiefile " --save-cookies=" cookiefile " --post-file=" postfile " -U " useragent " -O " tempfile " \"" fora "/posting.php?mode="((edit!="")?"edit&f=7&p="edit:"reply&f=7&t=101043")"\"")) { print "Submit fail." exit 1 } while((getline temp < tempfile)>0) { if (temp ~ /

information/) { success=1 } } close(tempfile) if (success!=1) { print "Sent back to preview!" exit INDELIVERABLE } while((getline cookie < cookiefile)>0) { if(cookie ~ /_sid/) { split(cookie,arr,"_sid[ \t]*") SID=arr[2] break } } close(cookiefile) if(system("wget -q -t 3 --connect-timeout=60 -U " useragent " -O " tempfile " \"" fora "/ucp.php?mode=logout&sid=" SID"\"")) { print "Logout fail." } } function urlencode(name,all, len,iii,escaped,ch) { len=length(name) escaped="" for(iii=1;iii<=len;++iii) { ch=substr(name,iii,1); if ((ch ~ /[a-zA-Z0-9\.\-_~]/)&&(all=="")) escaped = escaped ch else escaped = escaped "%" ch2hex[ch] } return escaped } function entitydecode(ht ,i,j,un,num) # quot, amp, lt, gt, nbsp, and decimal numbered. { while ((i=match(ht, /&((#[0-9]+)|([a-zA-Z]+));/))!=0)# { un=un substr(ht, 1, i-1) ht=substr(ht, i) j=index(ht, ";") if(ht~/^&#/) { num=int(substr(ht, 3, j-3)) un=un sprintf("%c",num) } else { num=tolower(substr(ht, 2, j-2)) if(num == "quot") un=un "\"" else if(num == "amp") un=un "&" else if(num == "lt") un=un "<" else if(num == "gt") un=un ">" else if(num == "nbsp") un=un "\xA0" else un = un ht } ht=substr(ht, j+1) } un=un ht return un } function urldecode(ht ,i,j,un,num) # quot, amp, lt, gt, nbsp, and decimal numbered. { while ((i=match(ht, /%[0-9a-fA-F][0-9a-fA-F]/))!=0)# { un=un substr(ht, 1, i-1) ht=substr(ht, i) num=toupper(substr(ht, 2, 2)) un=un hex2ch[num] ht=substr(ht, 4) } un=un ht return un }