2003-09-05 22:22:43 +02:00
|
|
|
###
|
2004-08-23 15:14:06 +02:00
|
|
|
# Copyright (c) 2002-2004, Jeremiah Fincher
|
2003-09-05 22:22:43 +02: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.
|
|
|
|
###
|
|
|
|
|
|
|
|
"""
|
|
|
|
Commands that use the dictd protocol to snag stuff off a server.
|
|
|
|
"""
|
|
|
|
|
2003-11-25 09:23:47 +01:00
|
|
|
__revision__ = "$Id$"
|
|
|
|
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.plugins as plugins
|
2003-09-05 22:22:43 +02:00
|
|
|
|
2003-10-04 16:56:54 +02:00
|
|
|
import sets
|
2003-09-05 22:22:43 +02:00
|
|
|
import random
|
2003-11-12 01:46:26 +01:00
|
|
|
import socket
|
2003-09-05 22:22:43 +02:00
|
|
|
|
|
|
|
import dictclient
|
|
|
|
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.conf as conf
|
|
|
|
import supybot.utils as utils
|
|
|
|
import supybot.plugins as plugins
|
|
|
|
import supybot.registry as registry
|
|
|
|
import supybot.ircutils as ircutils
|
|
|
|
import supybot.privmsgs as privmsgs
|
|
|
|
import supybot.callbacks as callbacks
|
2004-01-19 22:38:03 +01:00
|
|
|
|
2003-09-05 22:22:43 +02:00
|
|
|
|
2004-01-19 23:37:22 +01:00
|
|
|
def configure(advanced):
|
2004-07-25 20:24:51 +02:00
|
|
|
from supybot.questions import output, expect, anything, something, yn
|
2004-02-06 05:45:14 +01:00
|
|
|
conf.registerPlugin('Dict', True)
|
2004-01-31 23:24:43 +01:00
|
|
|
output('The default dictd server is dict.org.')
|
|
|
|
if yn('Would you like to specify a different dictd server?'):
|
2003-09-05 22:22:43 +02:00
|
|
|
server = something('What server?')
|
2004-01-19 23:37:22 +01:00
|
|
|
conf.supybot.plugins.Dict.server.set(server)
|
2003-09-05 22:22:43 +02:00
|
|
|
|
2004-01-20 00:00:08 +01:00
|
|
|
conf.registerPlugin('Dict')
|
2004-01-19 22:38:03 +01:00
|
|
|
# TODO: We should make this check to see if there's actually a dictd server
|
|
|
|
# running on the host given.
|
2004-01-20 13:29:11 +01:00
|
|
|
conf.registerGlobalValue(conf.supybot.plugins.Dict, 'server',
|
2004-01-19 23:07:41 +01:00
|
|
|
registry.String('dict.org', """Determines what server the bot will
|
|
|
|
retrieve definitions from."""))
|
2004-08-18 20:37:59 +02:00
|
|
|
conf.registerChannelValue(conf.supybot.plugins.Dict, 'default',
|
|
|
|
registry.String('', """Determines the default dictionary the bot will
|
|
|
|
ask for definitions in. If this value is '*' (without the quotes) the bot
|
|
|
|
will use all dictionaries to define words."""))
|
2004-01-19 22:38:03 +01:00
|
|
|
|
|
|
|
class Dict(callbacks.Privmsg):
|
2003-09-05 22:22:43 +02:00
|
|
|
threaded = True
|
|
|
|
def dictionaries(self, irc, msg, args):
|
2004-08-19 13:58:17 +02:00
|
|
|
"""takes no arguments
|
2003-09-05 22:22:43 +02:00
|
|
|
|
|
|
|
Returns the dictionaries valid for the dict command.
|
|
|
|
"""
|
2003-11-12 02:12:57 +01:00
|
|
|
try:
|
2004-01-19 22:38:03 +01:00
|
|
|
server = conf.supybot.plugins.Dict.server()
|
|
|
|
conn = dictclient.Connection(server)
|
2003-11-13 18:11:52 +01:00
|
|
|
dbs = conn.getdbdescs().keys()
|
2003-11-12 02:12:57 +01:00
|
|
|
dbs.sort()
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(utils.commaAndify(dbs))
|
2004-08-18 20:37:59 +02:00
|
|
|
except socket.error, e:
|
|
|
|
irc.error(webutils.strError(e))
|
2003-09-05 22:22:43 +02:00
|
|
|
|
2003-11-12 01:46:26 +01:00
|
|
|
def random(self, irc, msg, args):
|
2004-08-19 13:58:17 +02:00
|
|
|
"""takes no arguments
|
2003-09-05 22:22:43 +02:00
|
|
|
|
|
|
|
Returns a random valid dictionary.
|
|
|
|
"""
|
2003-11-12 02:12:57 +01:00
|
|
|
try:
|
2004-01-19 22:38:03 +01:00
|
|
|
server = conf.supybot.plugins.Dict.server()
|
|
|
|
conn = dictclient.Connection(server)
|
2003-11-13 18:11:52 +01:00
|
|
|
dbs = conn.getdbdescs().keys()
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(random.choice(dbs))
|
2004-08-18 20:37:59 +02:00
|
|
|
except socket.error, e:
|
|
|
|
irc.error(webutils.strError(e))
|
2003-09-05 22:22:43 +02:00
|
|
|
|
|
|
|
def dict(self, irc, msg, args):
|
|
|
|
"""[<dictionary>] <word>
|
|
|
|
|
2003-11-12 01:46:26 +01:00
|
|
|
Looks up the definition of <word> on dict.org's dictd server.
|
2003-09-05 22:22:43 +02:00
|
|
|
"""
|
2003-09-29 18:13:51 +02:00
|
|
|
if not args:
|
|
|
|
raise callbacks.ArgumentError
|
2003-11-12 01:46:26 +01:00
|
|
|
try:
|
2004-01-19 22:38:03 +01:00
|
|
|
server = conf.supybot.plugins.Dict.server()
|
|
|
|
conn = dictclient.Connection(server)
|
2004-08-18 20:37:59 +02:00
|
|
|
except socket.error, e:
|
|
|
|
irc.error(webutils.strError(e))
|
2003-11-12 01:46:26 +01:00
|
|
|
return
|
2003-11-12 02:12:57 +01:00
|
|
|
dbs = sets.Set(conn.getdbdescs())
|
|
|
|
if args[0] in dbs:
|
2003-09-05 22:22:43 +02:00
|
|
|
dictionary = args.pop(0)
|
|
|
|
else:
|
2004-08-18 20:37:59 +02:00
|
|
|
default = self.registryValue('default', msg.args[0])
|
|
|
|
if default in dbs:
|
|
|
|
dictionary = default
|
|
|
|
else:
|
|
|
|
if default:
|
|
|
|
self.log.info('Default dict for %s is not a supported '
|
|
|
|
'dictionary: %s.', msg.args[0], default)
|
|
|
|
dictionary = '*'
|
2003-09-05 22:22:43 +02:00
|
|
|
word = privmsgs.getArgs(args)
|
2004-08-31 20:23:47 +02:00
|
|
|
if not word:
|
|
|
|
irc.error('You must give a word to define.', Raise=True)
|
2003-09-05 22:22:43 +02:00
|
|
|
definitions = conn.define(dictionary, word)
|
|
|
|
dbs = sets.Set()
|
|
|
|
if not definitions:
|
2003-09-05 22:46:39 +02:00
|
|
|
if dictionary == '*':
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply('No definition for %r could be found.' % word)
|
2003-09-05 22:46:39 +02:00
|
|
|
else:
|
2004-01-09 00:03:48 +01:00
|
|
|
irc.reply('No definition for %r could be found in %s' %
|
2003-09-05 22:53:49 +02:00
|
|
|
(word, ircutils.bold(dictionary)))
|
2003-09-05 22:22:43 +02: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)
|
|
|
|
s = utils.normalizeWhitespace(s).rstrip(';.,')
|
|
|
|
L.append('%s: %s' % (db, s))
|
|
|
|
utils.sortBy(len, L)
|
2003-09-14 09:39:39 +02:00
|
|
|
if dictionary == '*' and len(dbs) > 1:
|
2004-07-21 21:36:35 +02:00
|
|
|
s = '%s responded: %s' % (utils.commaAndify(dbs), '; '.join(L))
|
2003-09-05 22:22:43 +02:00
|
|
|
else:
|
2003-09-07 07:08:30 +02:00
|
|
|
s = '; '.join(L)
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(s)
|
2003-09-05 22:22:43 +02:00
|
|
|
|
|
|
|
|
2003-11-12 01:46:26 +01:00
|
|
|
Class = Dict
|
|
|
|
|
2003-09-05 22:22:43 +02:00
|
|
|
|
|
|
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|