mIRC Home    About    Download    Register    News    Help

Topic Options
#10403 - 10/02/03 01:06 AM Random Quote
phrozenfire Offline
Babel fish

Registered: 30/12/02
Posts: 68
I know sockets sort of but not sockreads or HTTP sockets. If anybody has some free time can they help me. I have a page (http://www.bash.org/?top2). I want it so if people type !rand in a channel it will notice them a random full quote from that page. Sorry I do not have any script but I have no idea on how I would do this, but I have seen it done. So if you have some spare time, please help me. Thank you.

Top
#10404 - 10/02/03 04:37 AM Re: Random Quote
laz Offline
Babel fish

Registered: 16/12/02
Posts: 54
Sorry, I wont be able to help you cause I suck with sockets frown

But one thing, I think it will be hard to pick a random quote from that page because some are a good number of lines lone (5+).

Anyway, what do I know smile

Top
#10405 - 11/02/03 06:19 AM Re: Random Quote
Hammer Offline
Hoopy frood

Registered: 07/12/02
Posts: 1321
Loc: Georgia, USA
This version of your script updates the top.txt file in two ways. If the file doesn't exist, it will initialize it (through the socket code). It also sets up a daily timer to get the file again at midnight whenever you start up mIRC. If you wish to manually update the file, just type /www.bash.org and it will update.

You definitely do not want to get the file everytime someone asks for a quote. It needs to be written to a file anyway, just to get the random [section]. The page doesn't change all that often anyway. A daily update should suffice.
Code:
 
on *:TEXT:!rand:#:{
  if ($isfile(top.txt)) .play $+(-nq10t,$ini(top.txt,$rand(1,$ini(top.txt,0)))) $nick top.txt 2000
  else {
    set %_top.txt $chan $nick
    msg $chan Initializing quote file...
    www.bash.org
  }
}
on *:START: .timer 00:00 0 86400 www.bash.org
 
alias www.bash.org sockopen www.bash.org www.bash.org 80
 
on *:SOCKOPEN:www.bash.org:{
  sockwrite -n $sockname GET ?top HTTP/1.1
  sockwrite -n $sockname Host: www.bash.org
  sockwrite $sockname $crlf
  write -c top.html
}
 
on *:SOCKREAD:www.bash.org:{
  var %string
  if ($sockerr) return
 
  while 1 {
    sockread %string
    if (!$sockbr) return
    if ($numtok(%string,32) == 1) && ($base(%string,16,10) isnum) continue
    if %string != $null write top.html $replace(%string, ,$chr(160),",")
  }
 
}
 
on *:SOCKCLOSE:www.bash.org:{
  var %line                   | ; Holds the working line
  var %item = 0               | ; Ignore everything before the first quote
  var %section = 1            | ; Used to create [sections]
  var %i = 1                  | ; Current line
 
  window -nh @ 1
  window -nh @ 2
  loadbuf -r @ 1 top.html
  
  while ($line(@ 1,%i)) {
    %line = $ifmatch
 
    if (!%item) && (*p class="quote"* iswm %line) %item = 1
    elseif (</td></tr> == %line) break
 
    if %item {
 
      if %section {
        if $regex(v1,%line,/(quote\.\"\>\<b\>\#[0-9]{1,}\<\/b\>)/) {
          %section = 0
          aline @ 2 $+($crlf,[,$gettok($gettok($regml(v1,1),2,35),1,60),])
        }
        elseif ($remove($replace(%line,<,<,>,>),<br />,</p>)) aline @ 2 $ifmatch
      }
      if (!%section) && $regex(v2,%line,/(\<p class=\"qt\"\>)/) {
        %section = 1
        %line = $remove($replace($mid(%line,$calc($regml(v2,1).pos + 14)),<,<,>,>),<br />,</p>)
        if (%line) aline @ 2 %line
      }
 
    }
    inc %i
  }
 
  savebuf @ 2 top.txt
  write -dl1 top.txt
 
  .remove top.html
  close -@ @ *
 
  if (%_top.txt) {
    msg $gettok(%_top.txt,1,32) Quote file initialized...
    .play $+(-nq10t,$ini(top.txt,$rand(1,$ini(top.txt,0)))) $gettok(%_top.txt,2,32) top.txt 2000
    unset %_top.txt
  }
}

Since the code is so long, I'll leave it here for a while. cool
_________________________
DALnet: #HelpDesk and #m[color:#FF0000]IR[color:#EEEE00]C

Top