2005-01-20 00:34:25 +01:00
|
|
|
###
|
|
|
|
# Copyright (c) 2002-2004, Jeremiah Fincher
|
2012-09-01 16:16:48 +02:00
|
|
|
# Copyright (c) 2008, James McCoy
|
2021-08-01 21:47:37 +02:00
|
|
|
# Copyright (c) 2010-2021, The Limnoria Contributors
|
2005-01-20 00:34:25 +01:00
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions are met:
|
|
|
|
#
|
|
|
|
# * Redistributions of source code must retain the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer.
|
|
|
|
# * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer in the
|
|
|
|
# documentation and/or other materials provided with the distribution.
|
|
|
|
# * Neither the name of the author of this software nor the name of
|
|
|
|
# contributors to this software may be used to endorse or promote products
|
|
|
|
# derived from this software without specific prior written consent.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
# POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
###
|
|
|
|
|
|
|
|
import socket
|
|
|
|
|
|
|
|
import supybot.conf as conf
|
|
|
|
import supybot.utils as utils
|
|
|
|
from supybot.commands import *
|
|
|
|
import supybot.ircutils as ircutils
|
|
|
|
import supybot.callbacks as callbacks
|
2010-10-17 11:22:07 +02:00
|
|
|
from supybot.i18n import PluginInternationalization, internationalizeDocstring
|
|
|
|
_ = PluginInternationalization('Dict')
|
2005-01-20 00:34:25 +01:00
|
|
|
|
2010-04-08 06:04:44 +02:00
|
|
|
import random
|
|
|
|
|
2015-08-11 16:50:23 +02:00
|
|
|
from .local import dictclient
|
2008-12-18 05:40:13 +01:00
|
|
|
|
2005-02-09 08:04:04 +01:00
|
|
|
class Dict(callbacks.Plugin):
|
2014-11-30 08:18:44 +01:00
|
|
|
"""This plugin provides a function to look up words from different
|
|
|
|
dictionaries."""
|
2005-01-20 00:34:25 +01:00
|
|
|
threaded = True
|
2010-10-17 11:22:07 +02:00
|
|
|
@internationalizeDocstring
|
2005-01-20 00:34:25 +01:00
|
|
|
def dictionaries(self, irc, msg, args):
|
|
|
|
"""takes no arguments
|
|
|
|
|
|
|
|
Returns the dictionaries valid for the dict command.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
server = conf.supybot.plugins.Dict.server()
|
|
|
|
conn = dictclient.Connection(server)
|
2005-01-31 15:52:27 +01:00
|
|
|
dbs = list(conn.getdbdescs().keys())
|
2005-01-20 00:34:25 +01:00
|
|
|
dbs.sort()
|
2005-01-31 15:52:27 +01:00
|
|
|
irc.reply(format('%L', dbs))
|
2014-01-20 15:49:15 +01:00
|
|
|
except socket.error as e:
|
2005-01-29 20:20:15 +01:00
|
|
|
irc.error(utils.web.strError(e))
|
2005-01-20 00:34:25 +01:00
|
|
|
dictionaries = wrap(dictionaries)
|
|
|
|
|
2010-10-17 11:22:07 +02:00
|
|
|
@internationalizeDocstring
|
2005-01-20 00:34:25 +01:00
|
|
|
def random(self, irc, msg, args):
|
|
|
|
"""takes no arguments
|
|
|
|
|
|
|
|
Returns a random valid dictionary.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
server = conf.supybot.plugins.Dict.server()
|
|
|
|
conn = dictclient.Connection(server)
|
|
|
|
dbs = conn.getdbdescs().keys()
|
2005-01-31 16:22:48 +01:00
|
|
|
irc.reply(utils.iter.choice(dbs))
|
2014-01-20 15:49:15 +01:00
|
|
|
except socket.error as e:
|
2005-01-29 20:20:15 +01:00
|
|
|
irc.error(utils.web.strError(e))
|
2005-01-20 00:34:25 +01:00
|
|
|
random = wrap(random)
|
|
|
|
|
2010-10-17 11:22:07 +02:00
|
|
|
@internationalizeDocstring
|
2005-01-20 00:34:25 +01:00
|
|
|
def dict(self, irc, msg, args, words):
|
|
|
|
"""[<dictionary>] <word>
|
|
|
|
|
2010-09-01 00:46:05 +02:00
|
|
|
Looks up the definition of <word> on the dictd server specified by
|
|
|
|
the supybot.plugins.Dict.server config variable.
|
2005-01-20 00:34:25 +01:00
|
|
|
"""
|
|
|
|
try:
|
|
|
|
server = conf.supybot.plugins.Dict.server()
|
|
|
|
conn = dictclient.Connection(server)
|
2014-01-20 15:49:15 +01:00
|
|
|
except socket.error as e:
|
2005-01-29 20:20:15 +01:00
|
|
|
irc.error(utils.web.strError(e), Raise=True)
|
2005-01-29 22:24:17 +01:00
|
|
|
dbs = set(conn.getdbdescs())
|
2005-01-20 00:34:25 +01:00
|
|
|
if words[0] in dbs:
|
|
|
|
dictionary = words.pop(0)
|
|
|
|
else:
|
2019-08-24 17:50:05 +02:00
|
|
|
default = self.registryValue('default', msg.channel, irc.network)
|
2005-01-20 00:34:25 +01:00
|
|
|
if default in dbs:
|
|
|
|
dictionary = default
|
|
|
|
else:
|
|
|
|
if default:
|
2019-08-24 17:50:05 +02:00
|
|
|
self.log.info('Default dict for %s @ %s is not a supported '
|
|
|
|
'dictionary: %s.', msg.channel, irc.network, default)
|
2005-01-20 00:34:25 +01:00
|
|
|
dictionary = '*'
|
|
|
|
if not words:
|
2010-10-17 11:22:07 +02:00
|
|
|
irc.error(_('You must give a word to define.'), Raise=True)
|
2005-01-20 00:34:25 +01:00
|
|
|
word = ' '.join(words)
|
|
|
|
definitions = conn.define(dictionary, word)
|
2005-01-29 22:24:17 +01:00
|
|
|
dbs = set()
|
2005-01-20 00:34:25 +01:00
|
|
|
if not definitions:
|
|
|
|
if dictionary == '*':
|
2010-10-17 11:22:07 +02:00
|
|
|
irc.reply(format(_('No definition for %q could be found.'),
|
|
|
|
word))
|
2005-01-20 00:34:25 +01:00
|
|
|
else:
|
2010-10-17 11:22:07 +02:00
|
|
|
irc.reply(format(_('No definition for %q could be found in '
|
|
|
|
'%s'), word, ircutils.bold(dictionary)))
|
2005-01-20 00:34:25 +01:00
|
|
|
return
|
|
|
|
L = []
|
|
|
|
for d in definitions:
|
|
|
|
dbs.add(ircutils.bold(d.getdb().getname()))
|
|
|
|
(db, s) = (d.getdb().getname(), d.getdefstr())
|
|
|
|
db = ircutils.bold(db)
|
2005-01-27 07:59:08 +01:00
|
|
|
s = utils.str.normalizeWhitespace(s).rstrip(';.,')
|
2005-01-20 00:34:25 +01:00
|
|
|
L.append('%s: %s' % (db, s))
|
2005-02-15 14:57:57 +01:00
|
|
|
utils.sortBy(len, L)
|
2015-02-22 00:30:52 +01:00
|
|
|
if dictionary == '*' and len(dbs) > 1 and \
|
2019-08-24 17:50:05 +02:00
|
|
|
self.registryValue("showDictName", msg.channel, irc.network):
|
2010-10-17 11:22:07 +02:00
|
|
|
s = format(_('%L responded: %s'), list(dbs), '; '.join(L))
|
2005-01-20 00:34:25 +01:00
|
|
|
else:
|
|
|
|
s = '; '.join(L)
|
|
|
|
irc.reply(s)
|
|
|
|
dict = wrap(dict, [many('something')])
|
|
|
|
|
2010-04-08 06:04:44 +02:00
|
|
|
def synonym(self, irc, msg, args, words):
|
|
|
|
"""<word> [<word> ...]
|
2011-06-28 19:43:21 +02:00
|
|
|
|
2014-11-24 03:48:34 +01:00
|
|
|
Gets a random synonym from the Moby Thesaurus (moby-thesaurus) database.
|
2011-06-28 19:43:21 +02:00
|
|
|
|
2010-04-08 06:04:44 +02:00
|
|
|
If given many words, gets a random synonym for each of them.
|
2011-06-28 19:43:21 +02:00
|
|
|
|
2010-04-08 06:04:44 +02:00
|
|
|
Quote phrases to have them treated as one lookup word.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
server = conf.supybot.plugins.Dict.server()
|
|
|
|
conn = dictclient.Connection(server)
|
2014-01-20 15:49:15 +01:00
|
|
|
except socket.error as e:
|
2010-04-08 06:04:44 +02:00
|
|
|
irc.error(utils.web.strError(e), Raise=True)
|
2011-06-28 19:43:21 +02:00
|
|
|
|
2014-11-24 03:48:34 +01:00
|
|
|
dictionary = 'moby-thesaurus'
|
2010-04-08 06:04:44 +02:00
|
|
|
response = []
|
|
|
|
for word in words:
|
|
|
|
definitions = conn.define(dictionary, word)
|
|
|
|
if not definitions:
|
|
|
|
asynonym = word
|
|
|
|
else:
|
|
|
|
defstr = definitions[0].getdefstr()
|
|
|
|
synlist = ' '.join(defstr.split('\n')).split(': ', 1)[1].split(',')
|
|
|
|
asynonym = random.choice(synlist).strip()
|
|
|
|
response.append(asynonym)
|
|
|
|
irc.reply(' '.join(response))
|
|
|
|
synonym = wrap(synonym, [many('something')])
|
2005-01-20 00:34:25 +01:00
|
|
|
|
|
|
|
Class = Dict
|
|
|
|
|
|
|
|
|
2006-02-11 16:52:51 +01:00
|
|
|
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
|