Random Quote/Fact

By Conscious on Mar 18, 2011

Commands:
!Quote/&Quote/@Quote - posts a random quote to the channel.
!Fact/&Fact/@Fact - posts a random fact to the channel.

I made this with entire purpose to test a few things, like while ($sockbr), and my very remedial regex. I admit I did not entirely code all of the $hhfree, it is my adapted version of $htmlfree (who wrote that? I forget; if someone posts I will give credit.)

Any bugs/feedback/advice please post!

Note: 2 second spam filter per nick

on $*:TEXT:/^[!@&](quote|fact)$/Si:#:{
  if (!$($+(%,qspam,$nick),3)) {
    var %ticks = $ticks $+ $r(1,100)
    while ($sock($+(randomquotesearch,%ticks))) {
      var %ticks = $ticks $+ $r(1,100)
    }
    sockopen $+(randomquotesearch,%ticks) www.ikwote.com 80
    sockmark $+(randomquotesearch,%ticks) $+(msg # [Random $+ $chr(32),$iif($regml(1) == fact,Fact,Quote),],:,$iif($regml(1) == fact,fact,quote))
    set -u2 $+(%,qspam,$nick) $regml(1)
  }
}
on *:SOCKOPEN:randomquotesearch*:{
  sockwrite -n $sockname GET / HTTP/1.1
  sockwrite -n $sockname Host: www.ikwote.com $+ $crlf $+ $crlf
}
on *:SOCKREAD:randomquotesearch*:{
  var %a | sockread %a
  tokenize 58 $sock($sockname).mark
  while ($sockbr) {
    var %ticks = $ticks $+ $r(1,10000)
    if ($regex(%ticks,%a,/<div align="center" style="width:600px;"><p class="Quote">(.+?)<\/p>/)) {
      if (($2 == Fact) && (%randqtsrch)) || (($2 == Quote) && (!%randqtsrch)) { $1 $noqt($hhfree($regml(%ticks,1))) | sockclose $sockname | halt }
      var %randqtsrch on
    }
    sockread %a
  }
}
alias -l hhfree {
  var %x, %x = $regsubex($ticks,$1-,/(^[^<]*>|<[^>]*>|<[^>]*$)/g,$null), %x = $regsubex($+($ticks,$r(1,1000)),%x,/&#(\d+);/g,$chr(\1)), %x = $replace(%x,&nbsp;,$chr(32),&quot;,")
  return %x
} 

Comments

Sign in to comment.
Conscious   -  Mar 22, 2011

Does someone have a better site I could re-code it with? That site doesn't have very many quotes.

 Respond  
jasonh   -  Mar 19, 2011

thanks for the script. it's very fun to use

 Respond  
Jethro   -  Mar 19, 2011

I was referring to jaytea's say about "the modification you suggested may even have been necessary on an older mIRC version." So if the latest version doesn't have the issue you've demonstrated anymore (I've yet to test this because I'm lazy at the moment), I don't see you have to follow that approach to resolve it. But then again, if latest version still has the issue remains unresolved, I'll agree with you in a heart beat.

 Respond  
Conscious   -  Mar 19, 2011

Jethro_, I can't tell if you're agreeing with me or not xD

 Respond  
Jethro   -  Mar 19, 2011

I really believe people should upgrade to avoid many buggy issues that have been fixed in the latest version. Thus they won't have to come up with a workaround or go the extra mile to bypass the so-called bugs... I don't understand why some are saying they're waiting for a "stable" version to release before they can take action. Until then, good luck. :p

 Respond  
jaytea   -  Mar 19, 2011

oh, for some reason i failed to make that connection. you're absolutely right, general purpose and utility functions such as $htmlfree() should always operate in a way that impacts the scripting environment as minimally as possible. there was, up until fairly recently, a bug with nested unnamed $regsubex() calls; i can't remember the exact nature of the bug, but the modification you suggested may even have been necessary on an older mIRC version.

 Respond  
Conscious   -  Mar 19, 2011

Example of confilcting Regular Expressions with aliases:

alias regextest {
  noop $regex(Here is my regex,/(.+)/)
  echo -a $!regml(1): $regml(1)
  var %a = $regedit($regml(1))
  var %b = $regml(1)
  echo -a % $+ a - %a
  echo -a % $+ b - %b
}
alias regedit return $regsubex(abc,/D/,lol)

that kills the original $regml(1), and replaces it with nothing, as there is no $regml to be captured in that alias.

However; adding a regex name to the identifier solves this problem.

alias regextest2 {
  noop $regex(Here is my regex,/(.+)/)
  echo -a $!regml(1): $regml(1)
  var %a = $regedit2($regml(1))
  var %b = $regml(1)
  echo -a % $+ a - %a
  echo -a % $+ b - %b
}
alias regedit2 return $regsubex($ticks,abc,/D/,lol)
 Respond  
jaytea   -  Mar 18, 2011

what do you mean by 'accidentally called again later'?

 Respond  
Conscious   -  Mar 18, 2011

I prefer to use names for regex and regsubex; leaves no room for error. If you do $ticks for the names up there, it can't be accidentally called again later in the script, right?

 Respond  
Jethro   -  Mar 18, 2011
alias -l hhfree {
  return $replace($regsubex($regsubex($1,/<[^>]*>/g,),/&#(\d+);/g,$chr(\1)),&nbsp;,$chr(32),&quot;,")
}
 Respond  
Conscious   -  Mar 18, 2011

Jethro_, at your first comment, I did directly get the info; the whole purpose of the $htmlfree ($hhfree) was used to alter the html entities, not to remove html tags. Of course, I could have just done it in the actual script, but I felt that wasn't needed as I already had my alias $hhfree to change the htmlentities (N; ones, not &namehere; ones).

 Respond  
Jethro   -  Mar 18, 2011

That doesn't mean the site is going to alter its source code. Some sites never changed a thing for ages. That aside, almost all socket scripts I've seen require a touch-up every once in a while to ensure its workability, regardless of a htmlfree alias is used. It's an unpredictable matter really, and that's the deal you have to keep when you make a socket.

 Respond  
sunslayer   -  Mar 18, 2011

making strict regex with web fetches is a bad idea as every little change on the site will cause your code to fail, combining simple regex with $htmlfree() is generally better for the long-term

 Respond  
Jethro   -  Mar 18, 2011

I hardly see people use htmlfree alias with socket these days anymore...but that's just me. If you already use regex, then you can directly aim for the info you're after.

 Respond  
Sorasyn   -  Mar 18, 2011

No reason to post credit to the author of $htmlfree() there's been way too many authors of it to count.

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.