<action>
<!-- 
  Auto script for Reddit Robin
  Start it with
    xidel --template-file reddit-robin.xml 
  to set it to GROW. It will ask for username and password.
  
  You can also pass username and password on the command line as
    xidel -e 'user := "your name", passwd := "your password"' --template-file reddit-robin.xml 

  Or on bash with: 
     export user=name
     export passwd=password
    xidel --variable user,passwd --template-file reddit-robin.xml 
  
  
-->
    <s> if (empty(get("user", ()))) then $user := (trace((), "Username: "), read()) else () </s>
    <s> if (empty(get("passwd", ()))) then $passwd := (trace((), "Password: "), read()) else () </s>
    <s> if (empty(get("vote", ()))) then $vote := "INCREASE" else () </s> <!-- set to CONTINUE for stay -->
    <s> delay := get("delay", 300), 
        mass-pm := get("mass-pm", ()),
        looping := get("looping", empty($mass-pm))  </s>
    <s>declare function shuffle($s) { 
      if (count($s) le 1) then $s 
      else let $i := 1 + floor(random() * count($s)) 
           return ($s[$i], shuffle((subsequence($s, 1, $i - 1),  subsequence($s, $i + 1, count($s))))) }; random-seed()
    </s>
    
    <loop test="$looping or $mass-pm ">
      <try>
        <page url="https://www.reddit.com/"/>
        <s>login := form(css('form.login-form')[1], {'user': $user, 'passwd': $passwd})</s>
        <page url="{$login}"/>
        
        <page url="https://www.reddit.com/robin"/>
        
        <s>robin := jn:parse-json(join(//script/extract(., "setup[(](.*)[)]", 1)))</s>
        
        <if test="exists($mass-pm)">
          <loop var="user" list="shuffle(($robin).robin_user_list/name)">
             <try>
              <page url="https://www.reddit.com/api/compose">
                <header name="X-Modhash" value="{($robin).modhash}"/>
                <post name="to" value="{$user}"/>
                <post name="subject" value="ROBIN MERGE"/>
                <post name="text" value="{$mass-pm}"/>
                <post name="api_type" value="json"/>
              </page>    
             <catch errors="*"></catch>
            </try>
            <s>trace($raw, "response"), sleep(500 + random(700))</s>
          </loop>
        </if>
 
        
        
        <if test='($robin).robin_user_list()[.("name")=$user].vote = $vote'>
          <s>trace("Already voted", "status")</s>
        </if>
        <else>
          <page url="https://www.reddit.com/api/robin/{($robin).robin_room_id}/vote">
            <header name="X-Requested-With" value="XMLHttpRequest"/>
            <header name="X-Modhash" value="{($robin).modhash}"/>
            <post name="room_id" value="{($robin).robin_room_id}"/>
            <post name="room_name" value="{($robin).robin_room_name}"/>
            <post name="winning_vote" value="NOVOTE"/>
            <post name="api_type" value="json"/>
            <post name="vote" value="{$vote}"/>
          </page>
          <s>trace($raw, "res")</s>
        </else>
                
        <catch errors="*"><s>trace("error", "error")</s></catch>
      </try>
      
      <s>trace(x"sleeping for {$delay} seconds", "status"), sleep($delay*1000), mass-pm := ()</s>
    </loop>
</action>