mIRC Homepage
Posted By: drowsy Found a !poll script but it doesn't work - 24/04/15 08:15 PM
Could I get someone to take a look at this snippet and tell me why it doesn't work, I have tried everything but I am new to scripting! The error I keep getting is "Could not create poll." Thank you.

Code:
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)))
Strawpoll now uses a ddos protection scheme that makes this script not function, bypassing it wouldn't be worth casual effort.
Posted By: drowsy Re: Found a !poll script but it doesn't work - 24/04/15 10:44 PM
Ahhh, Thank you so much smile
Posted By: Majeye Re: Found a !poll script but it doesn't work - 27/04/15 06:55 PM
Strawpoll broke this ability last summer/fall.

Been trying to find a work-around, but have not been successful yet.
I've managed to get a proof of concept together that works with strawpoll.

The methodology to do so is as such:

Get / from http://strawpoll.me
Wait for all headers to be recieved
Digest headers, looking for the set cookie headers
Store the cookies

Once you have the cookies
Make a post to /api/v2/polls on strawpoll.me
Send Cookie header with request
Wait for all headers to be recieved
If you recieve HTTP/1.1 201 Created response
parse body to get link id

if not, you did something wrong


And the proof of concept: /strawpoll
Code:
alias strawpoll {
  sockopen strawpoll-start strawpoll.me 80
}
alias -l err {
  echo -a 04[StrawPoll]: $1-
}
alias -l out {
  echo -a 03[StrawPoll]: $1-
}

on *:SOCKOPEN:strawpoll-start:{
  if ($sockerr) {
    err SockOpen Error
  }
  else {
    out SockOpen
    var %s = sockwrite $sockname
    %s GET / HTTP/1.1 $+ $crlf
    %s Host: strawpoll.me $+ $crlf
    %s $crlf
  }
}

on *:SOCKWRITE:strawpoll-start:{
  if ($sockerr) {
    err SockWrite Error
  }
  else {
    out SockWrite
  }
}
on *:SOCKREAD:strawpoll-start:{
  if ($sockerr) {
    err SockRead Error
  }
  else {
    var %tmp
    sockread %tmp
    if ($regex(%tmp, /^Set-Cookie: ([^ ;]+)/i)) {
      strawpoll2 $regml(1)
      sockclose $sockname
    }
  }
}

on *:SOCKCLOSE:strawpoll-start:{
  if ($sockerr) {
    err SockClose Error
  }
  else {
    out SockClose
  }
}



;---------------------
alias strawpoll2 {
  sockopen strawpoll-post strawpoll.me 80
  sockmark strawpoll-post $1-
}

on *:SOCKOPEN:strawpoll-post:{
  if ($sockerr) {
    err POST -> SockOpen Error
  }
  else {
    out POST -> SockOpen

    var %p {"options": ["aa1", "bb2", "cc3", "dd4"], "title": "mytest from mIRC", "permissive": true, "multi": true}

    var %s = sockwrite $sockname
    %s POST /api/v2/polls HTTP/1.1 $+ $crlf
    %s Host: strawpoll.me $+ $crlf
    %s User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36 $+ $crlf
    %s Accept: application/json, text/plain, */* $+ $crlf
    %s Accept-Language: en-US,en;q=0.8 $+ $crlf
    %s Connection: keep-alive $+ $crlf

    %s Cookie: $sock($sockname).mark $+ $crlf

    %s Origin: http://strawpoll.me $+ $crlf
    %s Referer: http://strawpoll.me/ $+ $crlf
    %s Content-Type: application/json; charset=UTF-8 $+ $crlf
    %s Content-Length: $len(%p) $+ $crlf
    %s $crlf
    %s {"options": ["aa1", "bb2", "cc3", "dd4"], "title": "mytest from mIRC", "permissive": true, "multi": true}
  }
}

on *:SOCKWRITE:strawpoll-post:{
  if ($sockerr) {
    err POST -> SockWrite Error
  }
  else {
    out POST -> SockWrite
  }
}
on *:SOCKREAD:strawpoll-post:{
  if ($sockerr) {
    err POST -> SockOpen Error
  }
  else {
    var %tmp
    sockread %tmp
    out POST -> %tmp
  }
}

on *:SOCKCLOSE:strawpoll-post:{
  if ($sockerr) {
    err POST -> SockClose Error
  }
  else {
    out POST -> SockClose
  }
}

alias -l sockwrite {
  echo -a 13[SockWrite]: $1 $replace($2-,$cr,\r,$lf,\n)
  !sockwrite -a $1-
}



If requested enough, I could write a script to manage options, title, etc and perform the post
So I got bored and actually wrote something of some value:

Code:
/strawpoll [-mp] <title> options:<optionA>,<optionB>,...
    called as a command

$strawpoll(-mp <title> options:<optionA>,<optionB>,...)
    called as an identifer (put inputs into a variable and then pass the variable to the alias)

-m:
    multi-select mode enabled

-p:
    permissive mode enabled

<title>:
    Title of the poll
    <>'s aren't needed

<optionA>,<optionB>:
    Options to select from.
    <>'s aren't needed

If called as a command the output url is echo'ed to the user

If called as a identifier the output is returned formated as:
  0 errormsg
  1 straw_poll_url


Example Usage in on text event:
Code:
on *:TEXT:!strawpollcreate ?*:#mychannel:{
  tokenize 32 $strawpoll($2-)
  if ($1 == 1) {
    msg # Straw Poll Created: $2-
  }
  else {
    msg # Unable to create straw poll: $2-
  }
}


And now the code:
Code:
#STRAWPOLL_CREATOR on
alias strawpoll {
  var %res
  if ($strawpoll.parse($1-)) {
    %res = $strawpoll.try(strawpoll_creator, $v1)
  }
  else {
    %res = 0 Invalid Format: [-mp] <title> options:<optionA>,<optionB>,<optionC>
  }

  if ($isid) {
    return %res
  }
  elseif ($gettok(%res, 1, 32) == 1) {
    echo -a 03[StrawPoll]: Created $gettok(%res, 2-, 32)
  }
  else {
    echo -a 04[StrawPoll]: Unable to create poll: $gettok(%res, 2-, 32)
  }
}
#STRAWPOLL_CREATOR end

on *:START:{
  var %err
  var %com = strawpoll_creator
  var %cls = $true
  var %js = (function(){strawpoll=function(){throw new Error("Unable to locate a valid HTTP request object")};var x=['MSXML2.SERVERXMLHTTP.6.0','MSXML2.SERVERXMLHTTP.3.0','MSXML2.SERVERXMLHTTP'],xo,i;for(i=0;i<x.length;i+=1)try{xo=new ActiveXObject(x[i]);xo=x[i];strawpoll=function(d){var z=new ActiveXObject(xo),c,i;z.open("GET","http://strawpoll.me",false);z.send();if(z.status!==200)throw new Error("Server Error: "+z.status);c=z.getResponseHeader("Set-Cookie").split(";")[0];if(!c)throw new Error("No Cookie Recieved");z=new ActiveXObject(xo);z.open("POST","http://strawpoll.me/api/v2/polls",false);z.setRequestHeader("Cookie",c);z.setRequestHeader("Origin","http://strawpoll.me");z.setRequestHeader("Referer","http://strawpoll.me/");z.setRequestHeader("Accept","application/json,text/plain,*/*");z.setRequestHeader("Content-Type","application/json;charset=utf-8");z.setRequestHeader("Content-Length",d.length.toString(10));z.send(d);if (z.status!==201)throw new Error("Poll Not Created");i=eval('('+z.responseText+')');if(i.hasOwnProperty("id"))return"http://strawpoll.me/"+i.id;throw new Error("Poll id not returned")};break}catch(e){}}());

  if ($com(%com)) {
    %cls = $false
    %err = Com name in use, unable to continue
  }
  else {
    .comopen %com MSScriptControl.ScriptControl
    if (!$com(%com) || $comerr) {
      %err = Unable to create an instance of MSScriptControl.ScriptControl
    }
    elseif (!$com(%com, language, 4, bstr, jscript) || $comerr) {
      %err = Unable to set the MSScriptControl.ScriptControl instance's language to jscript
    }
    elseif (!$com(%com, ExecuteStatement, 1, bstr, %js) || $comerr) {
      %err = Unable to add jscript to the MSScriptControl.ScriptControl instance
    }
    else {
      .enable #STRAWPOLL_CREATOR
    }
  }
  :error
  %err = $iif($error, $v1, %err)
  reseterror
  if (%err) {
    if (%cls && $com(%com)) {
      .comclose %com
    }
    .disable #STRAWPOLL_CREATOR
    echo -s 04[STRAWPOLL CREATOR]: %err
  }
}

alias -l strawpoll.try {
  var %js = (function(){try{return"1 "+strawpoll(_data_)}catch(e){return"0 "+e.message}}())
  %js = $replace(%js, _data_, $2-)
  echo -a %js
  if (!$com($1, eval, 1, bstr, %js) || $comerr) {
    return 0 com error
  }
  else {
    return $com($1).result
  }
}

alias -l strawpoll.parse {
  var %reg = /^(?:(-[mp]*) )?(.+?) options:(.+)$/i, %n, %switches, %title, %options, %multi, %permissive, %v
  if ($regex($1-, %reg)) {
    %n = 1
    %switches = -
    if (-* iswm $regml(1)) {
      %switches = $v2
      inc %n
    }
    %title = "title":" $+ $replace($regml(%n), \,\\, ", \") $+ "
    %options = $regml($calc(%n +1))
    %multi = "multi": $+ $iif(m isincs %switches, true, false)
    %permissive = "permissive": $+ $iif(p isincs %switches, true, false)

    tokenize 44 %options
    %options = $null
    %n = 0
    while (%n < $0) {
      inc %n
      %v = $regsubex($($ $+ %n, 2), /(?:^[\s\r\n\t]+)|([\s\r\n\t]+$)/g, )
      if ($len(%v)) {
        %options = $addtok(%options, " $+ $replace(%v, \, \\, ", \") $+ ", 44)
      }
    }
    if ($numtok(%options,44) > 1) {
      return ' $+ $replace($+($chr(123),%title,$chr(44),"options":[,%options,],$chr(44),%multi,$chr(44),%permissive,$chr(125)), \, \\, ', \') $+ '
    }
  }
}

Posted By: raycomp Re: Found a !poll script but it doesn't work - 03/05/15 10:46 AM
Do you needs this just as a sort of mirc bot voting system?
© mIRC Discussion Forums