-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDictionaryAPI.tcl
353 lines (327 loc) · 16.2 KB
/
DictionaryAPI.tcl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
###############################################################################################
#
# Name :
# DictionaryAPI.tcl
#
# Description :
# TCL script for eggdrop that uses Dictionary v2 API to find
# and get word definitions
#
# Script TCL pour eggdrop qui utilise l'API Dictionary v2 pour rechercher et
# obtenir des définitions de mots
#
# Donation :
# github.com/ZarTek-Creole/DONATE
#
# Auteur :
# ZarTek @ github.com/ZarTek-Creole
#
# Website :
# github.com/ZarTek-Creole/TCL-DictionaryAPI
#
# Support :
# github.com/ZarTek-Creole/TCL-DictionaryAPI/issues
#
# Docs :
# github.com/ZarTek-Creole/TCL-DictionaryAPI/wiki
#
# Thanks to :
# CrazyCat - Community french and help of eggdrop : https://www.eggdrop.fr
# MenzAgitat - Tips/Toolbox : https://www.boulets.oqp.me/
# mabrook - Reports bugs and test : https://forum.eggdrop.fr/member.php?action=profile&uid=3935
#
###############################################################################################
if { [::tcl::info::commands ::DictionaryAPI::unload] eq "::DictionaryAPI::unload" } { ::DictionaryAPI::unload }
if { [package vcompare [regexp -inline {^[[:digit:]\.]+} $::version] 1.6.20] == -1 } { putloglev o * "\[DictionaryAPI - erreur\] La version de votre Eggdrop est ${::version}; DictionaryAPI ne fonctionnera correctement que sur les Eggdrops version 1.6.20 ou supérieure." ; return }
if { [::tcl::info::tclversion] < 8.5 } { putloglev o * "\[DictionaryAPI - erreur\] DictionaryAPI nécessite que Tcl 8.5 (ou plus) soit installé pour fonctionner. Votre version actuelle de Tcl est ${::tcl_version}. $ERROR" ; return }
if { [catch { package require tls 1.7} ERROR] } { putloglev o * "\[DictionaryAPI - erreur\] DictionaryAPI nécessite le package tls 1.7 (ou plus) pour fonctionner. Le chargement du script a été annulé. $ERROR" ; return }
if { [catch { package require http 2.9} ERROR] } { putloglev o * "\[DictionaryAPI - erreur\] DictionaryAPI nécessite le package http 2.9 (ou plus) pour fonctionner. Le chargement du script a été annulé. $ERROR" ; return }
if { [catch { package require json 1.3} ERROR] } { putloglev o * "\[DictionaryAPI - erreur\] DictionaryAPI nécessite le package json 1.3 (ou plus) pour fonctionner. Le chargement du script a été annulé. $ERROR" ; return }
namespace eval ::DictionaryAPI {
###############################################################################
### Configuration
###############################################################################
# List of salons where the script will be active put "*" for all channels
# Example to allow #channel1 and #channel2
# define the channels(Allow) "#channel1 #channel2"
#
# Liste des salons où le script sera active mettre "*" pour tout les salons
# Exemple pour autoriser #channel1 et #channel2
# set Channels(Allow) " #channel1 #channel2"
set Channels(Allow) "*"
### Public IRC commands | Commandes IRC publique
# Define the IRC commands that the script should respond to and look for definitions.
# Définissez les commandes IRC auquel le script doit répondre et chercher les definitions.
variable public_cmd ".define !DictionaryAPI .definition"
### Authorizations for public procurement. | Autorisations pour la commande publique
# "-" for all
variable public_cmd_auth "-"
### Current language for output | Langue courante pour la sortie
# List: en,hi,es,fr,ja,ru,de,it,ko,pt-br,ar,tr
variable Lang_current "en"
### The languages available to the user | Les langues disponible à l'utilisateur
variable Lang_available [list \
"en" \
"hi" \
"es" \
"fr" \
"ja" \
"ru" \
"de" \
"it" \
"ko" \
"pt-br" \
"ar" \
"tr" \
]
# Annonce prefix-> devant les annonce irc
variable Annonce_Prefix "\00301,00DictionaryAPI\003> "
### Formatting of text by block type. | Formatage du texte par bloc de type.
# Creation of the style, colors by type of block for the creation of the output display.
# Creation du style, couleurs par type de bloc pour la creation de l'affichage de sortie.
##
# "\${DICT_DEFINITION}" : The definition text | Le texte de définition
# "\${DICT_NUMBER}" : The numbering of the definition | La numerotation de la définition
# "\${DICT_WORD}" : The word corresponding to the definition | Le mot correspondant à la définition
# "\${DICT_TYPE}" : The part of the speech | La partie du discours
# "\${DICT_EXAMPLE}" : Example of use of the word | Exemple d'utilisation du mot
# "\${DICT_SYNONYMS}" : Synonyms of the word | Les synonymes du mot
# "\${DICT_ANTONYMS}" : The antonyms of the word | Les antonymes du mot
# "\${DICT_PHONETICS}" : The phonetics of the word | Les phonétique du mot
# "\n" : Retour a la ligne, nouvelle phrase??
###
variable BLOCK_DEFINITION " > \${DICT_DEFINITION}"
variable BLOCK_NUMBER " \${DICT_NUMBER} "
variable BLOCK_WORD "\002\00301,00\"\${DICT_WORD}\"\003\002"
variable BLOCK_TYPE " - \${DICT_TYPE}"
variable BLOCK_EXAMPLE " > \00302\"\${DICT_EXAMPLE}\"\003"
variable BLOCK_SYNONYMS " > (\${DICT_SYNONYMS})"
variable BLOCK_ANTONYMS " != \${DICT_ANTONYMS}"
variable BLOCK_PHONETICS_TEXT " (\${DICT_PHONETICS_TEXT})"
variable BLOCK_PHONETICS_AUDIO "\n PHONETICS AUDIO: \${DICT_PHONETICS_AUDIO}"
### Creation of the image output by positioning block types | Creation de la sortie l'image en positionnement des types de bloc
# Block type available, if it exists for the word ;
# Type de bloc disponible, si elle existe pour le mot :
##
# "\${BLOCK_DEFINITION}"
# "\${BLOCK_NUMBER}"
# "\${BLOCK_WORD}"
# "\${BLOCK_TYPE}"
# "\${BLOCK_EXAMPLE}"
# "\${BLOCK_SYNONYMS}"
# "\${BLOCK_ANTONYMS}"
# "\${BLOCK_PHONETICS_TEXT}"
# "\${BLOCK_PHONETICS_AUDIO}"
# "\n" : Retour a la ligne, nouvelle phrase
###
# Position them in the variable below in the desired order
# Positionner-les dans la variable ci-dessous dans l'ordre souhaité
# Multi exemple:
#
variable Annonce_Show "\${DICT_WORD}\${DICT_PHONETICS_TEXT}\${DICT_TYPE}\${DICT_SYNONYMS}\${DICT_ANTONYMS}\${DICT_NUMBER}\${DICT_DEFINITION}\${DICT_EXAMPLE}\${DICT_PHONETICS_AUDIO}"
#variable Annonce_Show "\${DICT_WORD}\${DICT_PHONETICS}\${DICT_SYNONYMS}\${DICT_ANTONYMS}\${DICT_NUMBER}\${DICT_DEFINITION}\${DICT_EXAMPLE}"
#variable Annonce_Show "\${DICT_NUMBER}\${DICT_WORD}\${DICT_TYPE}\n\${DICT_DEFINITION}\n\${DICT_EXAMPLE}"
### Block type in case of result not found | Type de bloc en cas de résultat non trouver
# "\${WORD_SEARCH}" : word not found
# "\${URL_Link}" : url not found
###
# Position them in the variable below in the desired order
# Positionner-les dans la variable ci-dessous dans l'ordre souhaité
variable Annonce_notfound "Aucune définition trouvée pour \00306\${WORD_SEARCH}\002\003."
#variable Annonce_notfound "No definition found for \00306\${WORD_SEARCH}\002\003. (\${URL_Link})"
# Maximum number of results | Nombre de resultats maximun
variable max_annonce_default 10
# Maximum number of results defined by the user | Nombre de résultats maximum défini par l'utilisateur
variable max_annonce_user 10
# After how many seconds do we decide that the website used by the script to display
# the definitions is offline (or too ready) without a response from it?
#
# Après combien de secondes décide-t-on que le site web utilisé par le script
# pour afficher les définitions est offline (ou trop lent) en l'absence de
# réponse de sa part ?
variable HTTP_TIMEOUT 10
# USE HTTPS (1) OR HTTP (0)
variable USE_HTTP_SSL 1
# URL (n'y touchez pas à moins d'avoir une bonne raison de le faire)
variable HTTP_URL_API "api.dictionaryapi.dev/api/v2/entries"
# User agent for http
variable HTTP_USERAGENT "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2"
variable HTTP_ERROR "Error of GetURL, see partyline for more details."
###############################################################################
### Fin de la configuration
###############################################################################
#############################################################################
### Initialisation
#############################################################################
array set script [list \
"name" "DictionaryAPI" \
"auteur" "ZarTek @ github.com/ZarTek-Creole" \
"version" "1.2.4"
]
if { $USE_HTTP_SSL == 1 } {
variable HTTP_URL_API "https://${HTTP_URL_API}"
} else {
variable HTTP_URL_API "http://${HTTP_URL_API}"
}
proc unload { args } {
putlog "Désallocation des ressources de ${::DictionaryAPI::script(name)}..."
foreach binding [lsearch -inline -all -regexp [binds *[set ns [::tcl::string::range [namespace current] 2 end]]*] " \{?(::)?$ns"] {
unbind [lindex $binding 0] [lindex $binding 1] [lindex $binding 2] [lindex $binding 4]
}
foreach running_utimer [utimers] {
if { [::tcl::string::match "*[namespace current]::*" [lindex $running_utimer 1]] } { killutimer [lindex $running_utimer 2] }
}
namespace delete ::DictionaryAPI
}
}
proc ::DictionaryAPI::GetURL { URL_Link } {
if { $::DictionaryAPI::USE_HTTP_SSL == 1 } { ::http::register https 443 [list ::tls::socket -autoservername true] }
# on modifie l'urlencoding car dicoreverso.net ne comprend que l'utf-8 dans ses URLs.
array set httpconfig [::http::config]
::http::config -urlencoding utf-8 -useragent $::DictionaryAPI::HTTP_USERAGENT
# on restaure l'urlencoding comme il était avant qu'on y touche
::http::config -urlencoding $httpconfig(-urlencoding)
if { [catch { set URL_TOKEN [::http::geturl $URL_Link -timeout [expr $::DictionaryAPI::HTTP_TIMEOUT * 1000]] } ERR] } {
putlog "::DictionaryAPI::GetURL La connexion à [set URL_Link] n'a pas pu être établie. Il est possible que le site rencontre un problème technique."
putlog "::DictionaryAPI::ERROR $ERR"
if { $::DictionaryAPI::USE_HTTP_SSL == 1 } { ::http::unregister https }
return 0
}
# on extrait la partie qui nous intéresse et sur laquelle on va travailler
set URL_DATA [::http::data $URL_TOKEN]
set URL_DATA [encoding convertfrom utf-8 $URL_DATA]
::http::cleanup $URL_TOKEN
if { $::DictionaryAPI::USE_HTTP_SSL == 1 } { ::http::unregister https }
return $URL_DATA;
}
###############################################################################
### Procédure principale
###############################################################################
proc ::DictionaryAPI::SetBlock { WHAT VALUE } {
set DICT_$WHAT $VALUE
set ::DictionaryAPI::DICT_$WHAT [subst [subst $[join ::DictionaryAPI::BLOCK_$WHAT]]]
}
proc ::DictionaryAPI::Search { nick host hand chan arg } {
if { $::DictionaryAPI::Channels(Allow) != "*" && [lsearch -nocase $::DictionaryAPI::Channels(Allow) $chan] == "-1" } {
set MSG "DictionaryAPI: The channel '$chan' is not in allow channel."
putlog $MSG
putserv "PRIVMSG $chan :$MSG"
return
}
set WORD_SEARCH [stripcodes bcruag $arg]
if { $WORD_SEARCH == "" } {
putserv "PRIVMSG $chan :${::DictionaryAPI::Annonce_Prefix}HELP : [join $::DictionaryAPI::public_cmd "|"] <word> "
putserv "PRIVMSG $chan :${::DictionaryAPI::Annonce_Prefix}LANG : \[-lang=<[join $::DictionaryAPI::Lang_available ,]>\] | Current lang: $::DictionaryAPI::Lang_current "
putserv "PRIVMSG $chan :${::DictionaryAPI::Annonce_Prefix}LIMIT : \[-limit=<1-$::DictionaryAPI::max_annonce_user>\] | default limit: $::DictionaryAPI::max_annonce_default "
putserv "PRIVMSG $chan :${::DictionaryAPI::Annonce_Prefix}EXAMPLE : [lindex $::DictionaryAPI::public_cmd 0] Diccionario -lang=es -limit=6"
putserv "PRIVMSG $chan :${::DictionaryAPI::Annonce_Prefix}HELP : [join $::DictionaryAPI::public_cmd "|"] SetLang <[join $::DictionaryAPI::Lang_available ,]> | Set Current language"
putserv "PRIVMSG $chan :${::DictionaryAPI::Annonce_Prefix}EXAMPLE : [lindex $::DictionaryAPI::public_cmd 0] SetLang es"
return
}
if { [string match -nocase [lindex $arg 0] "SetLang"] } {
set lang_tmp [lindex $arg 1]
if { $lang_tmp != "" } {
if { [lsearch $::DictionaryAPI::Lang_available $lang_tmp] == "-1" } {
putserv "PRIVMSG $chan :${::DictionaryAPI::Annonce_Prefix} The language '$lang_tmp' is not available, choose from the list: [join $::DictionaryAPI::Lang_available ", "]"
unset lang_tmp
return
}
set ::DictionaryAPI::Lang_current [lindex $arg 1]
putserv "PRIVMSG $chan :${::DictionaryAPI::Annonce_Prefix} Current lang is now : $::DictionaryAPI::Lang_current"
} else {
putserv "PRIVMSG $chan :${::DictionaryAPI::Annonce_Prefix} Current lang is : $::DictionaryAPI::Lang_current"
}
unset lang_tmp
return
}
set RE {-limit[\s|=](\S+)}
if { [regexp -nocase -- $RE $WORD_SEARCH -> limit] } {
regsub -nocase -- $RE $WORD_SEARCH {} WORD_SEARCH
set WORD_SEARCH [string trim $WORD_SEARCH]
if { $limit > $::DictionaryAPI::max_annonce_user } {
putserv "PRIVMSG $chan :${::DictionaryAPI::Annonce_Prefix} limit max is $::DictionaryAPI::max_annonce_user"
set limit $::DictionaryAPI::max_annonce_user
}
} else {
set limit $::DictionaryAPI::max_annonce_default
}
set RE {-lang[\s|=](\S+)}
if { [regexp -nocase -- $RE $WORD_SEARCH -> lang] } {
if { [lsearch $::DictionaryAPI::Lang_available $lang] == "-1" } {
putserv "PRIVMSG $chan :${::DictionaryAPI::Annonce_Prefix} The language '$lang' is not available, choose from the list: [join $::DictionaryAPI::Lang_available ", "]"
return
}
regsub -nocase -- $RE $WORD_SEARCH {} WORD_SEARCH
set WORD_SEARCH [string trim $WORD_SEARCH]
set URL_Link "${::DictionaryAPI::HTTP_URL_API}/${lang}/${WORD_SEARCH}"
} else {
set URL_Link "${::DictionaryAPI::HTTP_URL_API}/${::DictionaryAPI::Lang_current}/${WORD_SEARCH}"
}
set URL_DATA [::DictionaryAPI::GetURL $URL_Link]
if { $URL_DATA == 0 } {
putserv "PRIVMSG $chan :$::DictionaryAPI::script(name) > $::DictionaryAPI::HTTP_ERROR"
return 0
}
foreach { PARENT } [::json::json2dict $URL_DATA] {
if { $PARENT == "title" } {
putserv "PRIVMSG $chan :[subst $::DictionaryAPI::Annonce_notfound]"
return
}
# init/reset vars
variable DICT_DEFINITION ""
variable DICT_WORD ""
variable DICT_TYPE ""
variable DICT_NUMBER 0
variable DICT_ANTONYMS ""
variable DICT_PHONETICS_AUDIO ""
variable DICT_PHONETICS_TEXT ""
set SUBCAT [dict get $PARENT]
set TMP_phonetics [dict get $SUBCAT phonetics]
::DictionaryAPI::SetBlock WORD [dict get $SUBCAT word]
if { $TMP_phonetics != "{}" } {
::DictionaryAPI::SetBlock PHONETICS_TEXT $TMP_phonetics
if { [dict exists [join $TMP_phonetics] audio] } {
::DictionaryAPI::SetBlock PHONETICS_AUDIO [string map {"//" "https://"} [dict get [join $TMP_phonetics] audio]]
}
if { [dict exists [join $TMP_phonetics] text] } {
::DictionaryAPI::SetBlock PHONETICS_TEXT [dict get [join $TMP_phonetics] text]
}
unset TMP_phonetics
}
set SUBMEANINGS [dict get $SUBCAT meanings]
foreach { ENFANT } [dict get $SUBCAT meanings] {
set TMP_partOfSpeech [dict get $ENFANT partOfSpeech]
if { $TMP_partOfSpeech != "{}" } {
::DictionaryAPI::SetBlock TYPE $TMP_partOfSpeech
unset TMP_partOfSpeech
}
foreach { SUBDEFINITION } [dict get $ENFANT definitions] {
variable DICT_EXAMPLE ""
variable DICT_SYNONYMS ""
::DictionaryAPI::SetBlock DEFINITION [dict get $SUBDEFINITION definition]
if {
[dict exists $SUBDEFINITION synonyms] \
&& [dict get $SUBDEFINITION synonyms] != ""
} {
::DictionaryAPI::SetBlock SYNONYMS [join [dict get $SUBDEFINITION synonyms] ", "]
}
if { [dict exists $SUBDEFINITION example] } {
::DictionaryAPI::SetBlock EXAMPLE [dict get $SUBDEFINITION example]
}
::DictionaryAPI::SetBlock NUMBER [expr $DICT_NUMBER+1]
foreach Annonce [split [subst ${::DictionaryAPI::Annonce_Show}] "\n"] {
putserv "PRIVMSG $chan :${::DictionaryAPI::Annonce_Prefix}$Annonce"
}
if { $limit == $DICT_NUMBER } { return }
}
}
}
}
###############################################################################
### Binds
###############################################################################
foreach b $::DictionaryAPI::public_cmd {
bind pub $::DictionaryAPI::public_cmd_auth $b ::DictionaryAPI::Search
}
bind evnt - prerehash ::DictionaryAPI::unload
putlog "$::DictionaryAPI::script(name) v$::DictionaryAPI::script(version) by $::DictionaryAPI::script(auteur) loaded."