Fetches the meaning of a given acronym by using "acronyms.thefreedictionary.com".
Code: Select all
###############################################
# #
# A C R O N Y M F E T C H E R #
# v1.0 (08/03/2007) by MenzAgitat #
# #
# http://www.boulets-roxx.com #
# IRC: irc.teepi.net #boulets #
# #
###############################################
# The Tinyurl procedure has been written by
# Tomekk (tomekk@oswiecim.eu.org), thanks to him.
#
# Description en français
# Affiche la signification d'un acronyme.
#
# English description
# Display the meaning of an acronym.
#
# Changelog:
# 1.0: First version
# LICENCE:
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
###########################
# SETTINGS #
###########################
##### Public command
set acronymcmd "!acronym"
##### Channels on which the translator will be active
set acronymchans "#chan1 #chan2 #chan3"
##### Number of displayed results
set maxdef 4
################################################################
# #
# DO NOT MODIFY ANYTHING BELOW THIS BOX IF YOU DON'T KNOW TCL #
# #
################################################################
package require http
set acronymversion "1.0"
set DEBUGMODE 0
set useragent "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1"
set acronymurl "http://acronyms.thefreedictionary.com/"
bind pub -|- $acronymcmd pub:acronym
##### Main procedure
proc pub:acronym {nick host hand chan arg} {
##### Do we have permission to use this command on this chan ?
set permission_result [channel_check_acronym $chan]
if {$permission_result == 0} {return}
set arg [string trim $arg]
if {$arg == ""} {
acronym_syntax $chan
} else {
global useragent DEBUGMODE acronymurl acronymcmd maxdef
putcmdlog "$nick@$chan $acronymcmd $arg"
set res ""
##### URL construction
set arg [string map {
" " "%20" "\"" "%22" "#" "%23" "$" "%24" "%" "%25"
"&" "%26" "'" "%27" "\(" "%28" "\)" "%29" "*" "%2A"
"+" "%2B" "," "%2C" "." "%2E" "\/" "%2F" ":" "%3A"
";" "%3B" "<" "%3C" "=" "%3D" ">" "%3E" "?" "%3F"
"@" "%40" "\[" "%5B" "\\" "%5C" "\]" "%5D" "^" "%5E"
"\{" "%7B" "|" "%7C" "\}" "%7D" "~" "%7E" "¡" "%A1"
"¢" "%A2" "£" "%A3" "¤" "%A4" "¥" "%A5" "¦" "%A6"
"§" "%A7" "¨" "%A8" "©" "%A9" "ª" "%AA" "«" "%AB"
"¬" "%AC" "" "%AD" "®" "%AE" "¯" "%AF" "°" "%B0"
"±" "%B1" "²" "%B2" "³" "%B3" "´" "%B4" "µ" "%B5"
"¶" "%B6" "·" "%B7" "¸" "%B8" "¹" "%B9" "º" "%BA"
"»" "%BB" "¼" "%BC" "½" "%BD" "¾" "%BE" "¿" "%BF"
"À" "%C0" "Á" "%C1" "Â" "%C2" "Ã" "%C3" "Ä" "%C4"
"Å" "%C5" "Æ" "%C6" "Ç" "%C7" "È" "%C8" "É" "%C9"
"Ê" "%CA" "Ë" "%CB" "Ì" "%CC" "Í" "%CD" "Î" "%CE"
"Ï" "%CF" "Ð" "%D0" "Ñ" "%D1" "Ò" "%D2" "Ó" "%D3"
"Ô" "%D4" "Õ" "%D5" "Ö" "%D6" "×" "%D7" "Ø" "%D8"
"Ù" "%D9" "Ú" "%DA" "Û" "%DB" "Ü" "%DC" "Ý" "%DD"
"Þ" "%DE" "ß" "%DF" "à" "%E0" "á" "%E1" "â" "%E2"
"ã" "%E3" "ä" "%E4" "å" "%E5" "æ" "%E6" "ç" "%E7"
"è" "%E8" "é" "%E9" "ê" "%EA" "ë" "%EB" "ì" "%EC"
"í" "%ED" "î" "%EE" "ï" "%EF" "ð" "%F0" "ñ" "%F1"
"ò" "%F2" "ó" "%F3" "ô" "%F4" "õ" "%F5" "ö" "%F6"
"÷" "%F7" "ø" "%F8" "ù" "%F9" "ú" "%FA" "û" "%FB"
"ü" "%FC" "ý" "%FD" "þ" "%FE" "ÿ" "%FF"
} $arg]
set url "$acronymurl$arg"
if $DEBUGMODE {putlog "\00304\[BABEL DEBUG\]\003 url = $url"}
##### we send the request to the socket
::http::config -useragent $useragent
set token [::http::geturl "$url"]
##### if the connexion is established, we process the received data.
if {[::http::status $token] == "ok"} {
regexp "<tr><th>Acronym</th><th>Definition</th></tr>(.+?)</table>" [::http::data $token] res
if $DEBUGMODE {putlog "\00304\[BABEL DEBUG\]\003 \037PRE-REGSUB\037 $res"}
if { $res != "" } {
regsub -all "<th>Acronym</th><th>Definition</th>" $res "" res
regsub -all "</td></tr>" $res "\n" res
regsub -all "</td><td>" $res " \00312—\003 " res
regsub -all "<\[^<\]*>" $res "" res
set res [encoding convertfrom "utf-8" $res ]
if $DEBUGMODE {putlog "\00304\[BABEL DEBUG\]\003 \037POST-REGSUB\037 $res"}
if {$res != ""} {
set numresults [scount "\n" $res]
set res [split $res "\n"]
set counter 1
if {$numresults == 1} {
puthelp "privmsg $chan :\00314\037$numresults result\037 :\003"
} elseif {$numresults >1} {
puthelp "privmsg $chan :\00314\037$numresults results\037 :\003"
}
foreach line $res {
if {[string trim $line] != "" && [string trim $line] != " "} {
if $DEBUGMODE {putlog "\00304\[BABEL DEBUG\]\003 counter=$counter maxdef=$maxdef"}
if {($counter >= $maxdef) && ($numresults > $maxdef)} { set line "$line \00304(...)\003" }
puthelp "privmsg $chan :$line"
if {($counter >= $maxdef) && ($numresults > $maxdef)} {
set url [tinyurl_conversion [lindex $url]]
puthelp "privmsg $chan :\00314more: \00312\037$url\037\003"
return -code break
}
incr counter
}
}
}
} else {
puthelp "privmsg $chan :\00314$nick > No results\003"
}
} else {
puthelp "privmsg $chan :\00314The connexion to \002The Free Dictionnary\002 can't be established. Maybe the website suffer technical difficulties. Try again later.\003"
}
::http::cleanup $token
}
}
proc scount {subs string} {regsub -all $subs $string $subs string}
##### Syntax
proc acronym_syntax { chan } {
puthelp "privmsg $chan :\037Syntax :\037 \002!acronym\002 \00314<\003acronym\00314> \00307| \003Display the meaning of a given acronym. eg. \002!acronym lol\002"
return done
}
##### Check if the chan is in the allowed chans list
proc channel_check_acronym { chan } {
global acronymchans
if {[lsearch -exact $acronymchans $chan] != -1} {
set permission 1
} else {
set permission 0
}
return $permission
}
##### Conversion of the url into a shorter version.
##### If www.tinyurl.com does not respond quickly enough (10 seconds),
##### the plain url is used instead.
proc tinyurl_conversion { url } {
global useragent
set get_url_timeout "10"
set url [string map -nocase {
"&" "&"
} $url]
set tiny_query "http://tinyurl.com/create.php?url=${url}"
set tiny_token [http::config -useragent $useragent]
set tiny_token [http::geturl $tiny_query -timeout [expr $get_url_timeout * 1000]]
set html_data [http::data $tiny_token]
set all_n [split $html_data \n]
set tiny_link ""
foreach tiny_lines $all_n {
if {[regexp -all -nocase "name\=tinyurl" $tiny_lines]} {
regsub -all -nocase "<input type=hidden name=tinyurl value=\"" $tiny_lines "" tiny_link
regsub -all -nocase "\">" $tiny_link "" tiny_link
}
}
if {$tiny_link != "0" && $tiny_link != ""} {
return $tiny_link
} {
return "!!!${url}"
}
}
putlog "\002*Acronym Fetcher v$acronymversion*\002 by MenzAgitat (\037\00312http://www.boulets-roxx.com\003\037) has been loaded"