This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

help to !translate script

Old posts that have not been replied to for several years.
Locked
d
dmc

help to !translate script

Post by dmc »

Can someone fix that script.. Actually i mean, that this script must to work via www.tranexp.com

Code: Select all

###
# Translator 3 © 1999, 2000 Per Johansson (dw@eggheads.org)
#
# For eggdrop1.3/1.4/1.5
# (might work in 1.1.5 also but I dont really care that bot version suck
#  and only clueless morons use them still)
#
# v1.0 (19/12-98) -- first release
# v1.1 (21/12-98) -- added some user settings
#                    added bindings for msg and dcc
#                    made it friendly with multichanbots
# v1.2 (20/9-99)  -- fixed security bug..wonder how many tcls that has it.
#--
# v2.0 (01/12-99) -- Totally recoded it should work with older tcl versions
#                    take less resourses, be more simple to get working
#                    and be more secure.
#                    It's now async(non blocking) and requests are stacked.
# v2.1 (03/12-99) -- Added check/fix for possible infinite loop
#                    Better error handling (host is unreachable etc.)
#                    Some code optimisation
# v2.2 (06/12-99) -- Improved the url-x-encoder so it wont puke on some
#                    chars.
#--
# v3.0 (10/01-00) -- Happy new year, and welcome to the year 2000!
#                    Another lang db, so you have a choise which to use
#                    the new one supports more languages.
#                    Some new settings on how it should behave to querys
#                    and sock# aint used as ID anymore since I reuse them
#                    to damn fast :P
#                    It now uses language codes in the commandline,
#                    type !translate langs get the codes.
#
# Installation:  gunzip translator3.0.tcl.gz
#                mv translator3.0.tcl ~/botdir/scripts/
#                echo source scripts/translator3.0.tcl >> ~/botdir/<bot>.conf
#                .rehash
#
# Upgrading: Same as above but first remove the old source line for
#            translator in the conf file.
#
# Usage: pub: !translate <fromlang> <tolang> <text>
#        msg: /msg <botnick> translate <fromlang> <tolang> <text>
#        dcc: .translate <fromlang> <tolang> <text>
#        To get help use the same commands but without any args.
#        To get language codes : /msg bot tr.. langs or !tr.. langs
#        or .tra.. langs
#######
# Note: I code all my stuff from scratch, I rip no other persons code
#       If you seen some part of this somewhere else it has been copyed
#       from me. Per "dw" Johansson and to hell with then and whoo DCU!
#######
# PS: This script uses either altavista or tranexp so if they
#     change anything it might stop working.
#     When you use altavista one of the language must be english
#     ie you must always translate to or from english.
#     tranexp can translate between anylang.
#     IMHO altavista does better translations then tranexp
#     but tranexp has alot more languages to use.
#     If someone knows of a better translator database on the net
#     please let me know.
#######

##> SETTINGS <##

# Set channels that !translate should work in
set translate(chans) "#somechannel"

# Userflag needed to use pub and msg translation
#  default "o" (global or channel op)
#  type "-" if the user don't need to have a botflag
set translate(flag) "o"

# altavista = english, french, german, italian, portuguese, spanish
# tranexp   = brazilian portuguese, bulgarian, croatian, czech, danish
#             dutch, english, european spanish, finnish, french, german
#             greek, hungarian, icelandic,  italian, japanese,
#             latin american spanish, norwegian, polish, portuguese,
#             romanian, russian, serbian, slovenian, spanish,
#             swedish, welsh

# Set translate db 0 == altavista, 1 == tranexp
set translate(db) 1

# Show status/msgid/help in channel or in priv notice
#  when issuing public command. 0 == in channel, 1 == in notice
set translate(target) 0

## End of settings no need for edit below this point ##
#######################################################

if {![info exist translate(id)]} {
  set translate(id) 0
}
set translate(help) "Usage: \002MSG:\002 /msg <botnick> translate <args>\
    \002PUB:\002 !translate <args> \002DCC:\002 .translate <args>\
    \002Args is:\002 <fromlang> <tolang> <text to translate> \002:\002\
    type langs as a single arg after the command\
    to get a list of valid language codes \
    \002(\002dw@eggheads.org\002)\002"

if {$translate(db)} {
  set translate(server) "www.tranexp.com"
  set translate(script) "/InterTran"
  set translate(port) 2000
  set translate(langs) "{Valid langcodes: pob(Brazilian Pourugese)\
      bul(Bulgarian CP1251) cro(Croatian CP1250) che(Czech CP1250)\
      dan(Danish) dut(Dutch) eng(English) spe(European Spanish)\
      fin(Finnish) fre(French) ger(German) grk(Greek) hun(Hungarian CP1250)}
  {ice(Icelandic) ita(Italian) jpn(Japanese ShiftJIS)\
      spl(LatinAmericanSpanish) nor(Norwegian) pol(Polish ISO8859-2)\
      poe(Portuguese) rom(Romanian CP1250) rus(Russian CP1251)\
      sel(Serbian Latin) slo(Slovenian CP1250) spa(Spanish)\
      swe(Swedish) wel(Welsh)}"
  set translate(codes) "pob bul cro che dan dut eng spe fin fre ger grk\
      hun ice ita jpn spl nor pol poe rom rus sel slo spa swe wel"
} else {
  set translate(server) "babelfish.altavista.com"
  set translate(script) "/cgi-bin/translate"
  set translate(port) 80
  set translate(langs) "eng(English) fre(French) ger(German)\
      ita(Italian) poe(Portuguese) spa(Spanish)"
}
set translate(chans) [string tolower $translate(chans)]

proc tr_lang {froml tol} {
  if {$froml == "eng"} {
    switch $tol {
      "fre" { return "en_fr" }
      "ger" { return "en_de" }
      "ita" { return "en_it" }
      "por" { return "en_pt" }
      "spa" { return "en_es" }
      default { return "ERROR" }
    }
  } else {
    if {$tol != "eng"} { return "ERROR" }
    switch $froml {
      "fre" { return "fr_en" }
      "ger" { return "de_en" }
      "ita" { return "it_en" }
      "por" { return "pt_en" }
      "spa" { return "es_en" }
      default { return "ERROR" }
    }
  }
}

proc xploit {text} {
  return [regexp {[\[\{\|\\]} $text]
}

if {$translate(target)} {
  proc tranout {text} {
    return "puthelp \"NOTICE \$ni :$text\""
  }
} else {
  proc tranout {text} {
    return "puthelp \"PRIVMSG \$ch :$text\""
  }
}

proc urlencode {text} {
  regsub -all {\%} $text "%25" text
  set i [string length $text]
  while {([regexp -nocase {[^a-z0-9%]} $text toh])&&!($i < 0 )} {
    scan "$toh" %c dec
    regsub -all "\\${toh}" $text "%[format %X $dec]" text
    incr i -1
  }
  if {$i < 0} {putlog "Infinite loop something is very wrong: $text"}
  return $text
}

proc incoming_translation {sid query where who id} {
  global translate
  if {![info exist translate($sid)]} {
    set ind [gets $sid]
    switch $ind {
      "HTTP/1.1 100 Continue" {
	puts $sid "$query"
	lappend translate($sid) $ind
	fconfigure $sid -blocking no -buffering full -buffersize 4096
      }
      "HTTP/1.0 200 OK" {
	lappend translate($sid) $ind
	fconfigure $sid -blocking no -buffering full -buffersize 4096
      }
      default {
	putlog "Translator: Getting garbage on $sid ($ind) closing it"
	close $sid
	unset translate($sid)
      }
    }
  } else {
    lappend translate($sid) [read $sid 4096]
  }
  if {[eof $sid]} {
    close $sid
    set answer [join $translate($sid) \n]
    if {0} {
      #debug::dump returning file
      set did [open debug.tran w+];
      puts $did $answer;
      close $did;
      putlog "debug.tran written";
    }
    if {!$translate(db)} {
      set answer [string range $answer\
	  [expr [string first {<td align="left"><br>} $answer]+53]\
	  [expr [string first "<br><br></font></td></tr>" $answer]-1]]
    } else {
      set answer [string range $answer\
	  [expr [string first "<TEXTAREA NAME=none COLS=73 ROWS" $answer]+36]\
	  [expr [string first "</TEXTAREA></TD></TR></FORM>" $answer]-1]]
    }
    regsub -all "\n" $answer " " answer
    if {![string length $answer]} {
      set answer "Translation failed of unknown reason, you could try to use\
	  http://$translate(server):$translate(port)$translate(script)\
	  directly instead."
    }
    switch $where {
      "server" { puthelp "PRIVMSG $who :${id} ${answer}" }
      "dcc" { putdcc $who "${id} ${answer}" }
    }
    unset translate($sid)
  }
}

proc pub_transl {ni uh ha ch text} {
  global translate
  if {![string match "*[string tolower "$ch"]*" "$translate(chans)"]} {
    return 0
  }
  if {[xploit $text]} {
    eval [tranout "\${ni}: This request will not be processed\
	any further, The attempt has been logged as abuse."]
    putlog "$ha ${ni}!${uh}/$ch abused translate.tcl (${text})"
    return 1
  }
  if {[llength $text] < 3} {
    if {[lindex $text 0] == "langs"} {
      foreach part $translate(langs) {
	eval [tranout $part]
      }
    } else {
      eval [tranout $translate(help)]
    }
    return 1
  }
  set froml [string tolower [lindex $text 0]]
  set tol [string tolower [lindex $text 1]]
  set text [lreplace $text 0 1]
  if {!$translate(db)} {
    set lp [tr_lang $froml $tol]
    if {$lp == "ERROR"} {
      eval [tranout "Error in language codes use !translate langs for help"]
      return 1
    }
    set query "doit=done&lp=${lp}&urltext=[urlencode $text]"
  } else {
    if {([lsearch $translate(codes) $froml] == -1) ||\
	([lsearch $translate(codes) $tol] == -1)} {
      eval [tranout "Error in language codes use !translate langs for help"]
      return 1
    }
    set query "url=&topframe=yes&type=text&text=[urlencode\
	$text]&from=${froml}&to=${tol}"
  }
  if {[catch {set sid [socket -async $translate(server) $translate(port)]}\
      emsg]} {
    putlog "Translator: Error($emsg)"
    eval [tranout "\${ni}: Error($emsg)"]
    return 1
  }
  set id ${ni}([incr translate(id)])
  eval [tranout "\${id} Translating '${text}' from '${froml}'\
      to '${tol}'  wait.."]
  fconfigure $sid -blocking 0 -buffering line
  fileevent $sid readable "incoming_translation $sid $query server $ch\
      [split $id]"
  if {$translate(db)} {
    puts $sid "POST $translate(script)/ HTTP/1.0\
	\nReferer: http://www.tranexp.com:2000/InterTran\
	\nConnection: Keep-Alive\
	\nUser-Agent: Mozilla/4.61 \[en\] (X11; I; Linux 2.2.13 i586; Nav)\
	\nPragma: no-cache\
	\nHost: $translate(server)\
	\nAccept: image/gif, iimage/jpeg, image/pjpeg, image/png, */*\
	\nAccept-Encoding: gzip\
	\nAccept-Language: en\
	\nAccept-Charset: iso-8859-1,*,utf-8\
	\nContent-type: application/x-www-form-urlencoded\
	\nContent-Length: [expr [string length $query] +2]\n"
    puts $sid "\n$query"
  } else {
    puts $sid "POST $translate(script)/ HTTP/1.1\
	\nReferer: http://babelfish.altavista.com/cgi-bin/translate?\
	\nConnection: Keep-Alive\
	\nUser-Agent: Mozilla/4.61 \[en\] (X11; I; Linux 2.2.13 i586; Nav)\
	\nPragma: no-cache\
	\nHost: $translate(server)\
	\nAccept: image/gif, iimage/jpeg, image/pjpeg, image/png, */*\
	\nAccept-Encoding: gzip\
	\nAccept-Language: en\
	\nAccept-Charset: iso-8859-1,*,utf-8\
	\nContent-type: application/x-www-form-urlencoded\
	\nContent-Length: [expr [string length $query] +2]\n"
  }  
  flush $sid
  return 1
}

proc msg_transl {ni uh ha text} {
  global translate
  if {[xploit $text]} {
    puthelp "PRIVMSG $ni :This request will not be processed any further,\
	The attempt has been logged as abuse."
    putlog "$ha ${ni}!${uh}/MSG abused translate.tcl (${text})"
    return 1
  }
  if {[llength $text] < 3} {
    if {[lindex $text 0] == "langs"} {
      foreach part $translate(langs) {
        puthelp "PRIVMSG $ni :$part"
      }
    } else {
      puthelp "PRIVMSG $ni :$translate(help)"
    }
    return 1
  }
  set froml [string tolower [lindex $text 0]]
  set tol [string tolower [lindex $text 1]]
  set text [lreplace $text 0 1]
  if {!$translate(db)} {
    set lp [tr_lang $froml $tol]
    if {$lp == "ERROR"} {
      puthelp  "PRIVMSG $ni :Error in language codes use\
	  !translate langs for help"
      return 1
    }
    set query "doit=done&lp=${lp}&urltext=[urlencode $text]"
  } else {
    if {([lsearch $translate(codes) $froml] == -1) ||\
        ([lsearch $translate(codes) $tol] == -1)} {
      puthelp  "PRIVMSG $ni :Error in language codes use\
	  !translate langs for help"
      return 1
    }
    set query "url=&topframe=yes&type=text&text=[urlencode\
        $text]&from=${froml}&to=${tol}"
  }
  if {[catch {set sid [socket -async $translate(server) $translate(port)]}\
      emsg]} {
    putlog "Translator: Error($emsg)"
    puthelp "PRIVMSG $ni :Error($emsg)"
    return 1
  }
  set id ${ni}([incr translate(id)])
  puthelp "PRIVMSG $ni :${id} Translating '${text}' from '${froml}'\
      to '${tol}'  wait.."
  fconfigure $sid -blocking 0 -buffering line
  fileevent $sid readable "incoming_translation $sid $query server $ni\
      [split $id]"
  if {$translate(db)} {
    puts $sid "POST $translate(script)/ HTTP/1.0\
        \nReferer: http://www.tranexp.com:2000/InterTran\
        \nConnection: Keep-Alive\
        \nUser-Agent: Mozilla/4.61 \[en\] (X11; I; Linux 2.2.13 i586; Nav)\
        \nPragma: no-cache\
        \nHost: $translate(server)\
        \nAccept: image/gif, iimage/jpeg, image/pjpeg, image/png, */*\
        \nAccept-Encoding: gzip\
        \nAccept-Language: en\
        \nAccept-Charset: iso-8859-1,*,utf-8\
        \nContent-type: application/x-www-form-urlencoded\
        \nContent-Length: [expr [string length $query] +2]\n"
    puts $sid "\n$query"
  } else {
    puts $sid "POST $translate(script)/ HTTP/1.1\
        \nReferer: http://babelfish.altavista.com/cgi-bin/translate?\
        \nConnection: Keep-Alive\
        \nUser-Agent: Mozilla/4.61 \[en\] (X11; I; Linux 2.2.13 i586; Nav)\
        \nPragma: no-cache\
        \nHost: $translate(server)\
        \nAccept: image/gif, iimage/jpeg, image/pjpeg, image/png, */*\
        \nAccept-Encoding: gzip\
        \nAccept-Language: en\
        \nAccept-Charset: iso-8859-1,*,utf-8\
        \nContent-type: application/x-www-form-urlencoded\
        \nContent-Length: [expr [string length $query] +2]\n"
  }
  flush $sid
  return 1
}

proc dcc_transl {ha idx text} {
  global translate
  if {[xploit $text]} {
    putdcc $idx "This request will not be processed any further,\
	The attempt has been logged as abuse."
    putlog "${ha}!DCC abused translate.tcl (${text})"
    return 1
  }
  if {[llength $text] < 3} {
    if {[lindex $text 0] == "langs"} {
      foreach part $translate(langs) {
        putdcc $idx $part
      }
    } else {
      putdcc $idx $translate(help)
    }
    return 1
  }
  set froml [string tolower [lindex $text 0]]
  set tol [string tolower [lindex $text 1]]
  set text [lreplace $text 0 1]
  if {!$translate(db)} {
    set lp [tr_lang $froml $tol]
    if {$lp == "ERROR"} {
      putdcc $idx "Error in language codes use !translate langs for help"
      return 1
    }
    set query "doit=done&lp=${lp}&urltext=[urlencode $text]"
  } else {
    if {([lsearch $translate(codes) $froml] == -1) ||\
        ([lsearch $translate(codes) $tol] == -1)} {
      putdcc $idx "Error in language codes use !translate langs for help"
      return 1
    }
    set query "url=&topframe=yes&type=text&text=[urlencode\
        $text]&from=${froml}&to=${tol}"
  }
  if {[catch {set sid [socket -async $translate(server) $translate(port)]}\
      emsg]} {
    putlog "Translator: Error($emsg)"
    putdcc $idx "Translator: Error($emsg)"
    return 1
  }
  set id ${ha}([incr translate(id)])
  putdcc $idx "${id} Translating '${text}' from '${froml}' to '${tol}'\
      wait.."
  fconfigure $sid -blocking 0 -buffering line
  fileevent $sid readable "incoming_translation $sid $query dcc $idx\
      [split $id]"
  if {$translate(db)} {
    puts $sid "POST $translate(script)/ HTTP/1.0\
        \nReferer: http://www.tranexp.com:2000/InterTran\
        \nConnection: Keep-Alive\
        \nUser-Agent: Mozilla/4.61 \[en\] (X11; I; Linux 2.2.13 i586; Nav)\
        \nPragma: no-cache\
        \nHost: $translate(server)\
        \nAccept: image/gif, iimage/jpeg, image/pjpeg, image/png, */*\
        \nAccept-Encoding: gzip\
        \nAccept-Language: en\
        \nAccept-Charset: iso-8859-1,*,utf-8\
        \nContent-type: application/x-www-form-urlencoded\
        \nContent-Length: [expr [string length $query] +2]\n"
    puts $sid "\n$query"
  } else {
    puts $sid "POST $translate(script)/ HTTP/1.1\
        \nReferer: http://babelfish.altavista.com/cgi-bin/translate?\
        \nConnection: Keep-Alive\
        \nUser-Agent: Mozilla/4.61 \[en\] (X11; I; Linux 2.2.13 i586; Nav)\
        \nPragma: no-cache\
        \nHost: $translate(server)\
        \nAccept: image/gif, iimage/jpeg, image/pjpeg, image/png, */*\
        \nAccept-Encoding: gzip\
        \nAccept-Language: en\
        \nAccept-Charset: iso-8859-1,*,utf-8\
        \nContent-type: application/x-www-form-urlencoded\
        \nContent-Length: [expr [string length $query] +2]\n"
  }
  flush $sid
  return 1
}

bind pub $translate(flag)|$translate(flag) !translate pub_transl
bind msg $translate(flag)|$translate(flag) translate msg_transl
bind dcc -|- translate dcc_transl

putlog "Translator 3.0 by dw loaded.."






User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

What's wrong in it? Any errors or something?
Once the game is over, the king and the pawn go back in the same box.
d
dmc

Post by dmc »

<asdf> translate eng fin script
<foo> asdf(6) Translating 'script' from 'eng' to 'fin' wait..
<foo> asdf(6) Translation failed of unknown reason, you could try to use http://www.tranexp.com:2000/InterTran directly instead.

There is the error.
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

its not a TCL error, the error is from the script (incase of translation failure).. which means it couldnt translate
regsub -all "\n" $answer " " answer
if {![string length $answer]} {
set answer "Translation failed of unknown reason, you could try to use\
http://$translate(server):$translate(port)$translate(script)\
directly instead."
}
Locked