on *:text:!poll *:#:{
if ($nick !isop #) { msg # $nick $+ , you need to be a channel moderator! | return }
var %regex = /(?:(.+?)\s*[:-]+\s*|())(.+?)$/iS
if ($regex($strip($2-),%regex)) {
var %title = $iif($regml(1),$v1,Poll)
var %options = $regsubex($regml(2),/\s* $+ $chr(44) $+ \s*/g,$chr(44))
}
else return
noop $strawpoll.create(#,%title,%options)
}
alias strawpoll.create {
var %chan = $1, %title = $urlencode($2), %options
var %multi = true, %permissive = false
var %i = 1, %n = $numtok($3,44)
while (%i <= %n) {
%options = %options $+ &options[]= $+ $urlencode($gettok($3,%i,44))
inc %i
}
var %sockname = strawpoll. $+ $ticks
hfree -w %sockname | hmake %sockname
hadd %sockname headerfile %sockname $+ .header.txt
hadd %sockname datafile %sockname $+ .data.txt
hadd %sockname host strawpoll.me
hadd %sockname request /ajax/new-poll
hadd %sockname method POST
hadd %sockname data $+(title=,%title,%options,&multi=,%multi,&permissive=,%permissive)
hadd %sockname signal strawpoll.create
hadd %sockname params %chan
sockopen %sockname $hget(%sockname,host) 80
}
on *:signal:strawpoll.create:{
var %err = $1, %sockname = $2, %header = $3, %data = $4, %chan = $5
if (* 200 OK iswm $read(%header,1)) && ($regex($read(%data,n,1),(\d+))) {
msg %chan http://strawpoll.me/ $+ $regml(1)
}
else msg %chan Could not create poll.
hfree -w %sockname
.remove %header | .remove %data
}
on *:sockopen:strawpoll.*:{
var %a = sockwrite -n $sockname
%a $hget($sockname,method) $hget($sockname,request) HTTP/1.0
%a Host: $hget($sockname,host)
%a Connection: close
if ($hget($sockname,data) != $null) {
%a Content-Type: application/x-www-form-urlencoded
%a Content-Length: $len($v1)
}
%a $+($crlf,$hget($sockname,data))
}
on *:sockread:strawpoll.*:{
var %header
var %headerfile = $hget($sockname,headerfile)
var %datafile = $hget($sockname,datafile)
if (!$hget($sockname,header.complete)) {
sockread %header
while (%header != $null) {
write %headerfile %header
sockread %header
}
if ($sockbr) hadd $sockname header.complete $true
}
if ($hget($sockname,header.complete)) {
sockread &read
while ($sockbr) {
bwrite %datafile -1 -1 &read
sockread &read
}
}
}
on *:sockclose:strawpoll.*:{
var %header = $hget($sockname,headerfile)
var %data = $hget($sockname,datafile)
var %signal = $hget($sockname,signal)
var %params = $hget($sockname,params)
if (%signal) .signal %signal 0 $sockname %header %data %params
else {
hfree -w $sockname
.remove %header | .remove %data
}
}
alias urlencode return $regsubex($1-,/([^A-Z0-9])/gi,$+(%,$base($asc(\1),10,16)))