All pastes #613281 Raw Edit

Untitled

public text v1 · immutable
·published 2007-07-10 22:00 UTC
rendered paste body
; Name : Leo GER<->ENG translator script
; Ver. : v1.10 (Last edited 04.08.2006)
; Coder: Jay2k1 (Jay2k1 @ #scripting @ QuakeNet)
; mIRC : v6.17 or above
;
; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;
; Desc.: This is a translator script for german and english which works
;	 bidirectional, e.g. you can do !leo <english word> to get the
; 	 german translation and vice versa. I'ts based on the well-known
; 	 website 'http://dict.leo.org'.
;
; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;
; Syntax with default settings:
;
;	 !leo <word>
;
; Examples:
;
;	 !leo to sneeze
;	 !leo Kuh
;
; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;
; Settings:
;
;   - public trigger (default: !leo)
;
alias -l leo.trigger { return !leo }
;
;
;   - target (put either notice, channel or query there)
;
alias -l leo.target { return notice }
;
;
;   - number of max. results in output (default: 7)
;
alias -l leo.results { return 7 }
;
;
;   - linking (put on or off) adds the URL to the results
;     page to the output
;
alias -l leo.linking { return on }
;
;
;   - Channels to exclude (separate by comma)
;
alias -l leo.exclude { return #help,#help.script,#scripting }
;
; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;
; Special thanks to the guys in #scripting and #help.script
; on QuakeNet who did their part to make this script work.
;
;
; Don't edit anything below here unless you know what you're
; doing or you might screw the script up.
;
; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


; I added this to replace occuring HTML codes with their ascii
; pendants, the first is the ® (registered trademark) sign.

alias -l htmlreplace { return $replace(%leo.results,&reg;,$chr(174)) }


; Let's tell people who do not specify a search term some information.

on *:TEXT:$($leo.trigger):#:{
  if $istok($leo.exclude,$chan,44) { return }
  .notice $nick [Leo] German <-> English translator script, based on http://dict.leo.org/. Syntax: $leo.trigger <search term>
}


; The actual on text event to start the script

on *:TEXT:$($leo.trigger *):#:{
  if $istok($leo.exclude,$chan,44) { return }
  set %leo.chan $chan
  set %leo.nick $nick
  leo $2-
}


alias -l leo {

  ; For the URL format, we replace spaces with plus (+) signs.
  set %leo.term $replace($1-,$chr(32),+)

  ; If, for some reason, the leo socket is still open, close it,
  ; and delete an existing temp file.
  if $sock(leo) { sockclose leo }
  if $exists(leo.tmp) { .remove leo.tmp }

  ; Let's connect.
  .sockopen leo dict.leo.org 80
  .sockmark leo %leo.term
}

on *:sockopen:leo:{
  sockwrite -n $sockname GET /ende?lp=ende&lang=en&relink=off&search= $+ $sock($sockname).mark HTTP/1.1
  sockwrite -n $sockname Host: dict.leo.org
  sockwrite -n $sockname $crlf
  sockmark leo
}

on *:sockread:leo: {
  sockread -f &temp

  ; Skip headers
  if (!$sock($sockname).mark && $bfind(&temp,1,$crlf $+ $crlf)) {
    var %i = $calc($bfind(&temp,1,Content-Length:) + 15), %ii = $bfind(&temp,%i,$crlf) - %i
    sockmark $sockname $calc($v1 + 3 + $bvar(&temp,%i,%ii).text)
  }

  ; add results to the temp file
  .bwrite leo.tmp -1 &temp
  if ($file(leo.tmp).size >= $sock($sockname).mark) { leo.process }
}

alias -l leo.process {

  ; Now, we have to process the HTML-coded results to a
  ; suitable output format.
  ; Load the results into a binvar
  bread leo.tmp 0 $file(leo.tmp).size &html

  ; Check for zero entries
  if $bfind(&html,0,The dictionary does not contain any entries for) {
    set %leo.results Sorry, no entry found for " $+ %leo.term $+ "
  }
  else {

    ; Specify the position of the results in the binvar
    var %start = $bfind(&html,$bfind(&html,0,search results),&nbsp;&nbsp;&nbsp;) + 18
    var %end = $bfind(&html,%start,</table>)

    ; Get rid of the unnecessary code in the binvar
    bcopy -c &html 1 &html %start $calc(%end - %start)

    var %pos = 1, %nr = 1

    ; Using a while loop without an ending condition
    ; because we want to decide when it should end
    while (1) {

      ; get english term
      var %i = $bfind(&html,%pos,<tr)
      var %i = $bfind(&html,%i,<td) + 3
      var %i = $bfind(&html,%i,<td)
      var %m = $bfind(&html,%i,</td>) - %i
      var %english = $regsubex($bvar(&html,%i,%m).text,/<[^>]+>/g,$null)

      if %english isnum { break }

      ; get german term
      var %i = $bfind(&html,$calc(%i +3),<td) + 3
      var %i = $bfind(&html,%i,<td)
      var %m = $bfind(&html,%i,</td>) - %i
      var %german = $regsubex($bvar(&html,%i,%m).text,/<[^>]+>/g,$null)

      if &nbsp; !isin %english && &nbsp; !isin %german {

        ; "add" the result to one variable
        if %nr <= $leo.results { set %leo.results $iif(%leo.results,%leo.results $+($chr(47),$chr(47)),) %nr $+ . %english - %german }
        inc %nr
        elseif %nr <= $leo.results {
          set %leo.results %leo.results $chr(47) $+ $chr(47) %nr $+ . %english - %german
          inc %nr
        }
      }

      ; jump to next row of the HTML table
      var %pos = %i + %m
      if !$bfind(&html,%pos,<tr) { break }
    }

    ; do the HTML replacements
    set %leo.results $htmlreplace

    ; Add the link to the results page if enabled
    if $leo.linking == on { set %leo.results %leo.results :: More: http://dict.leo.org/?lang=en&search= $+ %leo.term }
  }

  ; Message the results to the specified target and clear the vars
  $iif($leo.target == channel,msg %leo.chan,$iif($leo.target == notice,notice %leo.nick,msg %leo.nick)) [Leo] %leo.results
  unset %leo.*
}