+BEGIN{\r
+ FS="="\r
+ fora="http://forums.xkcd.com"\r
+ useragent="\"mr*bdexbot (http://1190.bicyclesonthemoon.info/mrobsniped/; mr*bdex bOTTeriada post bot)\""\r
+ \r
+ for(i=0;i<256;++i)\r
+ {\r
+ ch=sprintf("%c",i)\r
+ hex=sprintf("%02X",i);\r
+ ch2hex[ch]=hex\r
+ }\r
+};\r
+{\r
+ eq=index($0,"=")\r
+ argtab[substr($0,1,eq-1)]=substr($0,eq+1)\r
+};\r
+END{\r
+ if(system("wget -q -t 3 --connect-timeout=60 --save-cookies=" cookiefile " -U " useragent " -O " tempfile " " fora "/ucp.php?mode=login"))\r
+ {\r
+ print "Pre-login fail."\r
+ exit 1\r
+ }\r
+ while((getline cookie < cookiefile)>0)\r
+ {\r
+ if(cookie ~ /_sid/)\r
+ {\r
+ split(cookie,arr,"_sid[ \t]*")\r
+ SID=arr[2]\r
+ }\r
+ }\r
+ close(cookiefile)\r
+ print "username=" argtab["username"] "&password=" argtab["password"] "&sid=" SID "&login=Login" > postfile\r
+ close(postfile)\r
+ if(system("wget -q -t 3 --connect-timeout=60 --save-cookies=" cookiefile " --post-file=" postfile " -U " useragent " -O " tempfile " \"" fora "/ucp.php?mode=login\""))\r
+ {\r
+ print "Login fail."\r
+ exit 1\r
+ }\r
+ \r
+ while((getline temp < tempfile)>0)\r
+ {\r
+ if (temp ~ /<div class=.error/)\r
+ {\r
+ print temp\r
+ print "LOGIN IMPOSSIBLE!"\r
+ exit 2\r
+ }\r
+ }\r
+ close(tempfile)\r
+ \r
+ if(system("wget -q -t 3 --connect-timeout=60 --load-cookies=" cookiefile " --save-cookies=" cookiefile " -U " useragent " -O " tempfile " \"" fora "/posting.php?mode="((edit!="")?"edit&f=7&p="edit:"reply&f=7&t=101043")"\""))\r
+ {\r
+ print "Postreply fail."\r
+ exit 1\r
+ }\r
+ \r
+ while((getline temp < tempfile)>0)\r
+ {\r
+ if(((i=match(temp, /<textarea.*id=\"message\"/))!=0)) #"\r
+ {\r
+ textarea="1"\r
+ temp=substr(temp, i)\r
+ temp=substr(temp, index(temp, ">")+1)\r
+ }\r
+ if(textarea!="")\r
+ {\r
+ if((i=match(temp, /<\/textarea/))!=0)\r
+ {\r
+ temp=substr(temp,1,i-1)\r
+ textarea=""\r
+ }\r
+ argtab["old_message"] = argtab["old_message"] urlencode(entitydecode(temp)"\n")\r
+ \r
+ }\r
+ else if(temp ~ /name=\"form_token/)\r
+ #\"#"\r
+ {\r
+ temp=substr(temp, index(temp, "value=\"")+7)\r
+ temp=substr(temp, 1, index(temp, "\"")-1)\r
+ argtab["form_token"]=urlencode(entitydecode(temp))\r
+ }\r
+ else if(temp ~ /name=\"creation_time/)\r
+ #\"#"\r
+ {\r
+ temp=substr(temp, index(temp, "value=\"")+7)\r
+ temp=substr(temp, 1, index(temp, "\"")-1)\r
+ argtab["creation_time"]=urlencode(entitydecode(temp))\r
+ }\r
+ else if(temp ~ /name=\"subject/)\r
+ #\"#"\r
+ {\r
+ temp=substr(temp, index(temp, "value=\"")+7)\r
+ temp=substr(temp, 1, index(temp, "\"")-1)\r
+ argtab["old_subject"]=urlencode(entitydecode(temp))\r
+ }\r
+ if (tolower(temp) ~ /<h2>information/)\r
+ {\r
+ print "Unexpected h2: information\n"\r
+ getline temp < tempfile\r
+ print temp\r
+ exit 1\r
+ }\r
+ }\r
+ close(tempfile)\r
+ \r
+ printf ("subject=%s",argtab[(edit!="")?"old_subject":"subject"])>postfile\r
+ if("&addbbcode20" in argtab)\r
+ printf ("&addbbcode20=%s",argtab["addbbcode20"])>>postfile\r
+ if(edit!="")\r
+ printf ("&message=%s",argtab["old_message"] "%5Bsize%3D110%5D%5Bb%5D" argtab["subject"] "%5B%2Fb%5D%5B%2Fsize%5D%0A%5Bsize%3D80%5D" argtab["d"] "." argtab["m"] "." argtab["y"] "%20" argtab["h"] "%3A00%20GMT%5B%2Fsize%5D%0A" argtab["message"])>>postfile\r
+ else\r
+ printf ("&message=%s",argtab["message"])>>postfile\r
+ printf ("&post=%s","Submit")>>postfile\r
+ if("disable_bbcode" in argtab)\r
+ printf ("&disable_bbcode=%s",argtab["disable_bbcode"])>>postfile\r
+ if("disable_smilies" in argtab)\r
+ printf ("&disable_smilies=%s",argtab["disable_smilies"])>>postfile\r
+ if("disable_magic_url" in argtab)\r
+ printf ("&disable_magic_url=%s",argtab["disable_magic_url"])>>postfile\r
+ if("attach_sig" in argtab)\r
+ printf ("&attach_sig=%s",argtab["attach_sig"])>>postfile\r
+ if("notify" in argtab)\r
+ printf ("¬ify=%s",argtab["notify"])>>postfile\r
+ printf ("&creation_time=%s",argtab["creation_time"])>>postfile\r
+ printf ("&form_token=%s",argtab["form_token"])>>postfile\r
+ close(postfile)\r
+ \r
+ system("sleep 1")\r
+ 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")"\""))\r
+ {\r
+ print "Submit fail."\r
+ exit 1\r
+ }\r
+ \r
+ while((getline temp < tempfile)>0)\r
+ {\r
+ if (temp ~ /<p class=.error/)\r
+ {\r
+ print temp\r
+ print "POST IMPOSSIBLE!"\r
+ exit 2\r
+ }\r
+ if (tolower(temp) ~ /<h2>information/)\r
+ {\r
+ success=1\r
+ }\r
+ }\r
+ close(tempfile)\r
+ \r
+ if (success!=1)\r
+ {\r
+ print "Sent back to preview!"\r
+ exit INDELIVERABLE\r
+ }\r
+ \r
+ while((getline cookie < cookiefile)>0)\r
+ {\r
+ if(cookie ~ /_sid/)\r
+ {\r
+ split(cookie,arr,"_sid[ \t]*")\r
+ SID=arr[2]\r
+ break\r
+ }\r
+ }\r
+ close(cookiefile)\r
+ if(system("wget -q -t 3 --connect-timeout=60 -U " useragent " -O " tempfile " \"" fora "/ucp.php?mode=logout&sid=" SID"\""))\r
+ {\r
+ print "Logout fail."\r
+ }\r
+ \r
+ \r
+}\r
+function urlencode(name,all, len,iii,escaped,ch)\r
+{\r
+ len=length(name)\r
+ escaped=""\r
+ for(iii=1;iii<=len;++iii)\r
+ {\r
+ ch=substr(name,iii,1);\r
+ if ((ch ~ /[a-zA-Z0-9\.\-_~]/)&&(all==""))\r
+ escaped = escaped ch\r
+ else\r
+ escaped = escaped "%" ch2hex[ch]\r
+ }\r
+ return escaped\r
+}\r
+function entitydecode(ht ,i,j,un,num)\r
+# quot, amp, lt, gt, nbsp, and decimal numbered.\r
+{\r
+ while ((i=match(ht, /&((#[0-9]+)|([a-zA-Z]+));/))!=0)#\r
+ {\r
+ un=un substr(ht, 1, i-1)\r
+ ht=substr(ht, i)\r
+ j=index(ht, ";")\r
+ \r
+ if(ht~/^&#/)\r
+ {\r
+ num=int(substr(ht, 3, j-3))\r
+ un=un sprintf("%c",num)\r
+ }\r
+ else\r
+ {\r
+ num=tolower(substr(ht, 2, j-2))\r
+ if(num == "quot")\r
+ un=un "\""\r
+ else if(num == "amp")\r
+ un=un "&"\r
+ else if(num == "lt")\r
+ un=un "<"\r
+ else if(num == "gt")\r
+ un=un ">"\r
+ else if(num == "nbsp")\r
+ un=un "\xA0"\r
+ else\r
+ un = un ht\r
+ }\r
+ ht=substr(ht, j+1)\r
+ }\r
+ un=un ht\r
+ return un\r
+}\r