on Sockread example

By DarthReven on Jun 10, 2005

this is an example of how you use the on sockread event with a socket connected to an IRC server.
Funtions:
Returns a version reply
Returns a time reply
Returns a ping reply
Returns whois
Returns bot's status
Returns message when someone pvts or sends a notice to the bot

on *:sockread:bot: {
  var %tmp
  sockread %tmp
  if ($gettok(%tmp,1,32) == PING) { 
    sockwrite -n $sockname PONG $gettok(%tmp,2,32)
    %c.ping = $ctime
    halt 
  }
  else {
    if ($gettok(%tmp,2,32) isnum) {
      if ($gettok(%tmp,2,32) == 376) || ($gettok(%tmp,2,32) == 422) { echo 12 -ate $sockname has connected to the server }
      else {
        tokenize 32 %tmp
        if ($2 == 311) { sockwrite -n $sockname NOTICE %b.whois [Whois- $+ $4 $+ ] | sockwrite -n $sockname NOTICE %b.whois [Address] $+([,$+($5,@,$6),]) | sockwrite -n $sockname NOTICE %b.whois [Real Name] $mid($8-,2) }
        elseif ($2 == 319) { sockwrite -n $sockname NOTICE %b.whois [Channels] $mid($5-,2) }
        elseif ($2 == 301) { sockwrite -n $sockname NOTICE %b.whois [Away] $mid($5-,2) }
        elseif ($2 == 317) { sockwrite -n $sockname NOTICE %b.whois [Idle] $duration($3) }
        elseif ($2 == 318) { sockwrite -n $sockname NOTICE %b.whois [Whois End- $+ $4 $+ ] | unset %b.* }
        elseif ($2 == 335) { sockwrite -n $sockname NOTICE %b.whois [Bot] $mid($5-,2) }
        elseif ($2 == 401) { sockwrite -n $sockname NOTICE %b.whois [Error] - $4 $+ , $mid($5-,2) [ $+ $mid($1,2) $+ ] }
      }
      }
    }
    elseif ($gettok(%tmp,2,32) == PRIVMSG) {
      tokenize 32 %tmp
      var %txt $strip($mid($4-,2)),%c $3,%n $mid($gettok($gettok(%tmp,1,32),1,33),2),%h $gettok($gettok($1,2,33),1,64),%a $gettok(%tmp,1,32)
      if (%txt == $+($chr(1),VERSION,$chr(1))) { sockwrite -n $sockname NOTICE %n $+($chr(1),VERSION mIRC $upper($sock($sockname).type) Socket using,$chr(32),v,$version,$chr(32),by Khaled Mardam-Bey owned by $me,$chr(1)) | halt }
      elseif (%txt == $+($chr(1),TIME,$chr(1))) { sockwrite -n $sockname NOTICE %n $+($chr(1),TIME $asctime(ddd mmm dd hh:nn:sstt yyyy),$chr(1)) }
      elseif ($+($chr(1),PING) ==  $gettok(%txt,1,32)) { sockwrite -n $sockname NOTICE %n %txt }
      else {
        if ($+(*,$chr(35),*) iswm $gettok(%tmp,3,32)) {
          if ($gettok(%txt,1,32) == -stat)  {
            var %lr $duration($sock($sockname).lr),%ls $duration($sock($sockname).ls)
            sockwrite -n $sockname PRIVMSG %c Stats: [Connection time: $duration($sock($sockname).to) $+ ] [Packets Sent: $bytes($sock($sockname).sent).suf $+ ] [Packets Recived: $bytes($sock($sockname).rcvd).suf $+ ] [Last Sent: %ls $+ ] [Last Server Ping: $duration($calc($ctime - %c.ping)) $+ ] [Last Error Message: $sock($sockname).wsmsg $+ ] [Type: $upper($sock($sockname).type) $+ ]
          }
          elseif ($gettok(%txt,1,32) == -whois) {
            if ($gettok(%txt,2,32)) {
              %b.whois = %n
              sockwrite -n $sockname NOTICE %n Getting Whois on: $gettok(%txt,2,32)
              sockwrite -n $sockname WHOIS $gettok(%txt,2,32)
            }
          }
          elseif ($gettok(%txt,1,32) == -mode) {
            if ($gettok(%txt,2,32)) {
              tokenize 32 %txt 
              sockwrite -n $sockname MODE %c $2-
            }
          }
        }
        else {
          if ($gettok(%tmp,3,32) == $sockname) {
            echo 4 -ate $+([,%n,]) %txt
            sockwrite -n $sockname NOTICE %n Please don't send PVT Messages to bots you won't get much of a responce.
            halt
          }
        }
      }
      elseif ($gettok(%tmp,2,32) == NOTICE) { echo 12 -at $+([,Notice-,%n,],$chr(32),%txt) }
    }
  }
}

Comments

Sign in to comment.
mischieviousboy   -  Mar 19, 2012

dartreven .. can socket connect with bnc ?

 Respond  
DarthReven   -  Jun 10, 2005

there are many other things you can do with the on sockread command and using raw irc

 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.