Advertisement
Ford

autotrans for eggdrop-utf-8

Mar 10th, 2013
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TCL 12.31 KB | None | 0 0
  1. ##############################################################################################
  2. ##  ##     autotrans.tcl for eggdrop by Ford_Lawnmower irc.geekshed.net #Script-Help    ##  ##
  3. ##############################################################################################
  4. ## .rss in the party line for commands and syntax. Example Feed Add Below:                  ##
  5. ##.rss add #Hawkee hawkee Hawkee http://www.hawkee.com/comment.rss.php?tool_type=snippet_id ##
  6. ##############################################################################################
  7. ##      ____                __                 ###########################################  ##
  8. ##     / __/___ _ ___ _ ___/ /____ ___   ___   ###########################################  ##
  9. ##    / _/ / _ `// _ `// _  // __// _ \ / _ \  ###########################################  ##
  10. ##   /___/ \_, / \_, / \_,_//_/   \___// .__/  ###########################################  ##
  11. ##        /___/ /___/                 /_/      ###########################################  ##
  12. ##                                             ###########################################  ##
  13. ##############################################################################################
  14. ##  ##                             Start Setup.                                         ##  ##
  15. ##############################################################################################
  16. namespace eval autotrans {
  17.   variable cmdchar "!"
  18. ## Edit textf to change the color/state of the text shown                               ##  ##
  19.   variable textf "\017\00304"
  20. ## Edit tagf to the color/state for language tags.                                      ##  ##
  21.   variable tagf "\017\002"
  22. ## Edit flooddelay to reflect the amount of seconds between querys per user.            ##  ##
  23.   variable flooddelay 2
  24. ##############################################################################################
  25. ##  ##                           End Setup.                                              ## ##
  26. ##############################################################################################
  27.   bind pubm -|- * autotrans::main
  28.   setudef flag trans
  29.   bind dcc - trans autotrans::settings
  30.   bind evnt -|- init-server autotrans::loadhash
  31.   bind evnt -|- prerehash autotrans::loadhash
  32.   proc main {nick host hand chan text} {
  33.     if {[set idx [lsearch -glob [utimers] "*autotrans::noop $nick $chan*"]] != -1} {
  34.       return
  35.     } else {
  36.       utimer $autotrans::flooddelay "autotrans::noop $nick $chan"
  37.       if {[strip [lindex $text 0]] == "${autotrans::cmdchar}trans"} {
  38.         if {[lindex $text 2] != "" && [lsearch -exact [channel info $chan] +trans] != -1} {
  39.           set lang [lindex $text 1]; set transtext [lrange $text 2 end]
  40.           if {[string match -nocase "*linux*" $::tcl_platform(os)] || $::tcl_platform(os) == "CYGWIN_NT-5.1"} {
  41.             set translation [bingtrans $lang [strip $transtext]]
  42.           } else {
  43.             set translation [encoding convertfrom utf-8 [bingtrans $lang [strip $transtext]]]
  44.           }
  45.           putserv "PRIVMSG $chan :${autotrans::tagf}[lindex $translation 0] ${autotrans::textf}[lrange $translation 1 end]"
  46.         }
  47.       } else {
  48.         set chan [string tolower $chan]; set counter [hfind "autotrans" "${chan}*" 0]; set count 1
  49.         set text [strip $text]
  50.         while {$count <= $counter} {
  51.           set current [hfind "autotrans" "${chan}*" $count]
  52.           regexp -- {^(.+)-\|\|-(.+)$} $current wholematch channel language
  53.           if {[string match -nocase "*linux*" $::tcl_platform(os)] || $::tcl_platform(os) == "CYGWIN_NT-5.1"} {
  54.             set translation [bingtrans $language $text]
  55.           } else {
  56.             set translation [encoding convertfrom utf-8 [bingtrans $language $text]]
  57.           }
  58.           if {![string match -nocase "*${text}" $translation] && [string map {" " ""} $translation] != ""} {
  59.             if {[hget "autotrans" $current] == "log"} {
  60.               putlog "${autotrans::textf}${translation}"
  61.             } else {
  62.               if {[string match "*|*" [lindex $translation 0]]} {
  63.                 putserv "PRIVMSG $chan :${autotrans::tagf}[lindex $translation 0] ${autotrans::textf}[lrange $translation 1 end]"
  64.               }
  65.             }
  66.           }
  67.           incr count
  68.         }
  69.       }
  70.     }  
  71.   }
  72.   proc settings {hand idx text} {
  73.     set choice [lindex $text 0]; set channel [lindex $text 1]
  74.     set lang [lindex $text 2]; set type [lindex $text 3]
  75.     if {[string equal -nocase "add" $choice] && $lang != ""} {
  76.       if {$type == ""} { set type msg }
  77.       hadd "autotrans" "[string tolower ${channel}-||-${lang}]" "[string tolower $type]"
  78.       savehash
  79.       putlog "Added translator ${channel}-||-${lang} $type"
  80.     } elseif {[string equal -nocase "list" $choice]} {
  81.       putlog "\[Translator list\]"
  82.       set count [hfind "autotrans" "*" 0]; set counter 1
  83.       while {$count >= $counter} {
  84.         putlog "[hfind "autotrans" "*" $counter]"
  85.         incr counter
  86.       }
  87.     } elseif {[string equal -nocase "info" $choice] && $channel != ""} {
  88.       putlog "[hget "autotrans" [string tolower ${channel}]]"
  89.     } elseif {[string equal -nocase "delete" $choice] && $channel != ""} {
  90.       hdel "autotrans" [string tolower ${channel}]
  91.       savehash
  92.     } else {
  93.       putlog "\[RSS Syntax\]"
  94.       putlog "Add Translator: .trans add <#chan> <langcode> <?type?>"
  95.       putlog "<langcode>'s  are listed here: http://msdn.microsoft.com/en-us/library/hh456380.aspx"
  96.       putlog "<type> is \"msg\" for channel message or \"log\" for partyline output.(Optional)"
  97.       putlog "Delete Translator: .trans delete <name>"
  98.       putlog "Info Translator: .trans info <name>"
  99.       putlog "List Translators: .trans list"
  100.       putlog "Help Translator: .trans"
  101.     }  
  102.   }
  103.   proc bingtrans {destinationlanguage text} {
  104.     set bingtranssite "api.microsofttranslator.com"
  105.     set bingtransappkey [hget "bingtrans" "appkey"]
  106.     set bingtransurl "/v2/ajax.svc/TranslateArray?appId=%22${bingtransappkey}%22&texts=\[%22[urlencode $text]%22\]&from=%22%22&to=%22${destinationlanguage}%22"
  107.     if {[catch {set bingtranssock [socket -async $bingtranssite 80]} sockerr]} {
  108.       return 0
  109.     } else {
  110.       puts $bingtranssock "GET $bingtransurl HTTP/1.0"
  111.       puts $bingtranssock "Host: $bingtranssite"
  112.       puts $bingtranssock "User-Agent: Opera 9.6"
  113.       puts $bingtranssock ""
  114.       flush $bingtranssock
  115.       while {![eof $bingtranssock]} {
  116.         set bingtransvar " [string map {\$ \002\$\002 \[ \( \] \)} [gets $bingtranssock]] "
  117.         if {[regexp {The\stoken\shas\sexpired.*appId} $bingtransvar]} {
  118.           putlog "token expired script needed to generate new one"
  119.           if {$bingtransappkey == ""} {
  120.             close $bingtranssock
  121.             return ""
  122.           } else {
  123.             hadd "bingtrans" "appkey" [getbingtransappkey]
  124.             close $bingtranssock
  125.             return [bingtrans $destinationlanguage $text]
  126.           }
  127.         } elseif {[regexp -- {"From":"(.*)"\,.*\,"TranslatedText":"(.*?)"\,"} $bingtransvar wholematch language translation]} {
  128.           if {[string equal -nocase $language $destinationlanguage]} {
  129.             set result $text
  130.           } else {
  131.             set result "\[[string tolower $language]|${destinationlanguage}\] $translation"
  132.           }
  133.           close $bingtranssock
  134.           return $result         
  135.         }
  136.      }
  137.       close $bingtranssock
  138.    }
  139.  }
  140.  proc getbingtransappkey {} {
  141.     set getbingtransappkeysite "www.bing.com"
  142.     set getbingtransappkeyurl "/translator"
  143.     #putlog "http://${getbingtransappkeysite}${getbingtransappkeyurl}"
  144.    if {[catch {set getbingtransappkeysock [socket -async $getbingtransappkeysite 80]} sockerr]} {
  145.      return 0
  146.    } else {
  147.      puts $getbingtransappkeysock "GET $getbingtransappkeyurl HTTP/1.0"
  148.      puts $getbingtransappkeysock "Host: $getbingtransappkeysite"
  149.      puts $getbingtransappkeysock "User-Agent: Opera 9.6"
  150.      puts $getbingtransappkeysock ""
  151.      flush $getbingtransappkeysock
  152.      while {![eof $getbingtransappkeysock]} {
  153.         set getbingtransappkeyvar " [string map {\$ \002\$\002 \[ \( \] \)} [gets $getbingtransappkeysock]] "  
  154.         #putlog $getbingtransappkeyvar
  155.         if {[regexp -- {AjaxApiAppId\s=\s\'(.*?)\';} $getbingtransappkeyvar match result]} {
  156.           close $getbingtransappkeysock
  157.           hadd "bingtrans" "appkey" $result
  158.           return $result         
  159.         }
  160.      }
  161.       close $getbingtransappkeysock
  162.       return ""
  163.    }
  164.  }
  165.  proc noop {$nick $chan} {
  166.    return ""
  167.  }
  168.  proc strip {text} {
  169.    regsub -all {\002|\031|\015|\037|\017|\003(\d{1,2})?(,\d{1,2})?} $text "" text
  170.    return $text
  171.  }
  172.  proc hex {decimal} { return [format %x $decimal] }
  173.  proc decimal {hex} { return [expr 0x$hex] }
  174.  proc dehex {string} {
  175.    regsub -all {^\{|\}$} $string "" string
  176.    set string [subst [regsub -nocase -all {\\u([a-f0-9]{4})} $string {[format %c [decimal \1]]}]]
  177.    set string [subst [regsub -nocase -all {\%([a-f0-9]{2})} $string {[format %c [decimal \1]]}]]
  178.    set string [subst [regsub -nocase -all {\&#([0-9]{2});} $string {[format %c \1]}]]
  179.    set string [subst [regsub -nocase -all {\&#x([0-9]{2});} $string {[format %c [decimal \1]]}]]
  180.    set string [string map {&quot; \" &middot; · &amp; & <b> \002 </b> \002 &ndash; – &raquo; \
  181.    » &laquo; « &Uuml; Ü &uuml; ü &Aacute; Á &aacute; á &Eacute; É &eacute; é &Iacute; Í &iacute; \
  182.    í &Oacute; Ó &oacute; ó &Ntilde; Ñ &ntilde; ñ &Uacute; Ú &uacute; ú &aelig; æ &nbsp; " " &apos; \' \
  183.     \( \002\(\002 \) \002\)\002 \{ \002\{\002 \} \002\}\002} $string]
  184.    return $string
  185.  }
  186.  proc urlencode {string} {
  187.    #regsub -all {^\{|\}$} $string "" string
  188.    return [subst [regsub -nocase -all {([^a-z0-9\+])} [encoding convertto utf-8 $string] {%[format %x [scan "\\&" %c]]}]]
  189.     #[encoding convertto utf-8 ${topic}]
  190.     #return [string map {" " +} [encoding convertto utf-8 $string]]
  191.  }
  192.  proc hadd {hashname hashitem hashdata } {
  193.    global $hashname
  194.    set ${hashname}($hashitem) $hashdata
  195.  }
  196.  proc hget {hashname hashitem} {
  197.    upvar #0 $hashname hgethashname
  198.     if {[info exists hgethashname($hashitem)]} {
  199.       return $hgethashname($hashitem)
  200.     } else {
  201.       return ""
  202.     }
  203.  }
  204.  proc hfind {hashname search matchno {type "w"}} {
  205.    upvar #0 $hashname hfindhashname
  206.     if {$type == "w"} {
  207.       if {[array exists hfindhashname]} {
  208.         if {$matchno == 0} {
  209.           return [llength [array names hfindhashname $search]]
  210.        } else {
  211.          set matchno [expr $matchno - 1]
  212.           return [lindex [array names hfindhashname $search] $matchno]
  213.         }
  214.       }
  215.     } elseif {$type == "W"} {
  216.       set count 0
  217.       foreach {item value} [array get hfindhashname] {
  218.         if {[string match $search $value]} {
  219.           incr count
  220.           if {$count == $matchno} { return $item }
  221.         }
  222.       }
  223.       if {$matchno == 0} {
  224.         return $count
  225.       } else {
  226.         return ""
  227.       }
  228.     }
  229.  }
  230.  proc hfindexact {hashname search value} {
  231.    upvar #0 $hashname hfindhashname
  232.     if {[array exists hfindhashname]} {
  233.       if {$value == 0} {
  234.         return [llength [array names hfindhashname -exact $search]]
  235.      } else {
  236.        set value [expr $value - 1]
  237.         return [lindex [array names hfindhashname -exact $search] $value]
  238.       }
  239.     }
  240.  }
  241.  proc hsave {hashname filename} {
  242.    upvar #0 $hashname hsavehashname
  243.     if {[array exists hsavehashname]} {
  244.       set hsavefile [open $filename w]
  245.       foreach {key value} [array get hsavehashname] {
  246.         puts $hsavefile "${key}=${value}"
  247.       }
  248.       close $hsavefile
  249.     }
  250.  }
  251.  proc hload {hashname filename} {
  252.    upvar #0 $hashname hloadhashname
  253.     hfree $hashname
  254.     set hloadfile [open $filename]
  255.     set linenum 0
  256.     while {[gets $hloadfile line] >= 0} {
  257.       if {[regexp -- {([^\s]+)=(.*)$} $line wholematch item data]} {
  258.         set hloadhashname($item) $data
  259.       }
  260.    }
  261.     close $hloadfile
  262.  }
  263.  proc hfree {hashname} {
  264.    upvar #0 $hashname hfreehashname
  265.     if {[array exists hfreehashname]} {
  266.      foreach key [array names hfreehashname] {
  267.         unset hfreehashname($key)
  268.       }
  269.     }
  270.  }
  271.  proc hdel {hashname hashitem} {
  272.    upvar #0 $hashname hdelhashname
  273.     if {[info exists hdelhashname($hashitem)]} {
  274.       unset hdelhashname($hashitem)
  275.     }
  276.  }
  277.  proc hcopy {hashfrom hashto} {
  278.     upvar #0 $hashfrom hashfromlocal $hashto hashtolocal
  279.     array set hashtolocal [array get hashfromlocal]
  280.  }
  281.  proc savehash {} {
  282.    hsave "autotrans" "${::network}autotrans.hsh"
  283.  }
  284.  proc loadhash {type} {
  285.    if {[file exists "${::network}autotrans.hsh"]} {
  286.       autotrans::hload "autotrans" "${::network}autotrans.hsh"
  287.     }
  288.  }  
  289. }
  290. autotrans::hadd "bingtrans" "appkey" [autotrans::getbingtransappkey]
  291. putlog "\002*Loaded* \017\00304\002Autrans\017 \002by \
  292. Ford_Lawnmower irc.GeekShed.net #Script-Help .trans for help"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement