2010-05-24 13:38:06 +00:00
|
|
|
# -*- Encoding: utf-8 -*-
|
2008-10-22 17:31:40 +00:00
|
|
|
###
|
2010-05-24 13:38:06 +00:00
|
|
|
# Copyright (c) 2008-2010 Terence Simpson
|
2017-05-28 00:56:04 +00:00
|
|
|
# Copyright (c) 2017- Krytarik Raido
|
2008-10-22 17:31:40 +00:00
|
|
|
#
|
2010-05-24 13:38:06 +00:00
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of version 2 of the GNU General Public License as
|
|
|
|
# published by the Free Software Foundation.
|
2008-10-22 17:31:40 +00:00
|
|
|
#
|
2010-05-24 13:38:06 +00:00
|
|
|
# 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.
|
2008-10-22 17:31:40 +00:00
|
|
|
#
|
|
|
|
###
|
|
|
|
|
|
|
|
import supybot.utils as utils
|
|
|
|
from supybot.commands import *
|
|
|
|
import supybot.plugins as plugins
|
|
|
|
import supybot.ircutils as ircutils
|
2009-10-12 18:26:35 +00:00
|
|
|
import supybot.ircmsgs as ircmsgs
|
2008-10-22 17:31:40 +00:00
|
|
|
import supybot.callbacks as callbacks
|
|
|
|
import supybot.ircutils as ircutils
|
2009-01-19 22:10:51 +00:00
|
|
|
import supybot.ircdb as ircdb
|
2009-10-12 18:26:35 +00:00
|
|
|
import supybot.conf as conf
|
2008-10-22 17:31:40 +00:00
|
|
|
import os
|
2010-02-04 18:56:07 +00:00
|
|
|
import re
|
2010-04-13 01:38:24 +00:00
|
|
|
import time
|
2017-05-28 00:56:04 +00:00
|
|
|
from imp import reload
|
|
|
|
from . import packages
|
2008-10-22 17:31:40 +00:00
|
|
|
reload(packages)
|
|
|
|
|
2009-01-19 22:26:32 +00:00
|
|
|
def get_user(msg):
|
|
|
|
try:
|
|
|
|
user = ircdb.users.getUser(msg.prefix)
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
return user
|
|
|
|
|
2011-04-19 17:05:48 +00:00
|
|
|
_stripNickChars = """!"#$%&'()*+,./:;<=>?@~"""
|
|
|
|
def stripNick(nick):
|
|
|
|
while nick and nick[-1] in _stripNickChars:
|
|
|
|
nick = nick[:-1]
|
|
|
|
return nick
|
|
|
|
|
2010-04-13 01:12:54 +00:00
|
|
|
## Taken from Encyclopedia ##
|
|
|
|
# Repeat filtering message queue
|
|
|
|
msgcache = {}
|
|
|
|
def queue(irc, to, msg):
|
|
|
|
now = time.time()
|
2017-05-28 00:56:04 +00:00
|
|
|
for m in list(msgcache.keys()):
|
2010-04-13 01:12:54 +00:00
|
|
|
if msgcache[m] < now - 30:
|
|
|
|
msgcache.pop(m)
|
|
|
|
for m in msgcache:
|
|
|
|
if m[0] == irc and m[1] == to:
|
|
|
|
oldmsg = m[2]
|
|
|
|
if msg == oldmsg or oldmsg.endswith(msg):
|
|
|
|
break
|
|
|
|
if msg.endswith(oldmsg):
|
|
|
|
msg = msg[:-len(oldmsg)] + 'please see above'
|
|
|
|
else:
|
|
|
|
msgcache[(irc, to, msg)] = now
|
|
|
|
irc.queueMsg(ircmsgs.privmsg(to, msg))
|
|
|
|
|
2008-10-22 17:31:40 +00:00
|
|
|
class PackageInfo(callbacks.Plugin):
|
|
|
|
"""Lookup package information via apt-cache/apt-file"""
|
|
|
|
threaded = True
|
2010-02-04 18:56:07 +00:00
|
|
|
space_re = re.compile(r' *')
|
2008-10-22 17:31:40 +00:00
|
|
|
|
|
|
|
def __init__(self, irc):
|
|
|
|
self.__parent = super(PackageInfo, self)
|
|
|
|
self.__parent.__init__(irc)
|
|
|
|
self.Apt = packages.Apt(self)
|
|
|
|
|
|
|
|
def callPrecedence(self, irc):
|
|
|
|
before = []
|
|
|
|
for cb in irc.callbacks:
|
|
|
|
if cb.name() == 'IRCLogin':
|
|
|
|
before.append(cb)
|
|
|
|
return (before, [])
|
|
|
|
|
|
|
|
def __getRelease(self, irc, release, channel, doError=True):
|
2011-04-19 17:08:10 +00:00
|
|
|
if release:
|
|
|
|
release = release.strip()
|
2009-10-12 18:26:35 +00:00
|
|
|
defaultRelease = self.registryValue("defaultRelease", channel)
|
|
|
|
if not defaultRelease:
|
2008-10-22 17:31:40 +00:00
|
|
|
if doError:
|
|
|
|
irc.error("'supybot.plugins.PackageInfo.defaultRelease' is not set")
|
2009-10-12 18:26:35 +00:00
|
|
|
return (None, None)
|
|
|
|
if not release:
|
|
|
|
return (defaultRelease, None)
|
|
|
|
(release, rest) = (release.split(' ', 1) + [None])[:2]
|
|
|
|
if release[0] in ('|', '>'):
|
|
|
|
return (defaultRelease, "%s %s" % (release, rest))
|
|
|
|
return (release, rest)
|
2008-10-22 17:31:40 +00:00
|
|
|
|
|
|
|
def __getChannel(self, channel):
|
|
|
|
return ircutils.isChannel(channel) and channel or None
|
|
|
|
|
2010-04-19 02:22:38 +00:00
|
|
|
def __getReplyChars(self, channel):
|
|
|
|
prefix_chars = list(self.registryValue("prefixchar", channel))
|
|
|
|
address_chars = list(str( conf.supybot.reply.whenAddressedBy.chars() ))
|
|
|
|
if channel:
|
|
|
|
address_chars = list(str( conf.supybot.reply.whenAddressedBy.chars.get(channel) ))
|
|
|
|
return tuple(set(prefix_chars + address_chars))
|
|
|
|
|
|
|
|
def __getCommand(self, text, channel):
|
|
|
|
reply_chars = self.__getReplyChars(channel)
|
|
|
|
my_commands = self.listCommands()
|
|
|
|
if text[0] in reply_chars:
|
|
|
|
text = text[1:]
|
|
|
|
return text.strip().lower().split(' ', 1)[0]
|
|
|
|
|
2009-10-12 18:26:35 +00:00
|
|
|
def real_info(self, irc, msg, args, package, release):
|
2008-10-22 17:31:40 +00:00
|
|
|
"""<package> [<release>]
|
|
|
|
|
|
|
|
Lookup information for <package>, optionally in <release>
|
|
|
|
"""
|
|
|
|
channel = self.__getChannel(msg.args[0])
|
2010-04-15 02:08:17 +00:00
|
|
|
reply_target = ircutils.replyTo(msg)
|
2009-10-12 18:26:35 +00:00
|
|
|
(release, rest) = self.__getRelease(irc, release, channel)
|
2008-10-22 17:31:40 +00:00
|
|
|
if not release:
|
|
|
|
return
|
2009-10-12 18:26:35 +00:00
|
|
|
reply = self.Apt.info(package, release)
|
|
|
|
if rest:
|
|
|
|
if rest[0] == '|':
|
|
|
|
try:
|
2011-04-19 17:16:00 +00:00
|
|
|
target = rest
|
2011-04-19 17:05:48 +00:00
|
|
|
while target[0] == '|':
|
|
|
|
target = target[1:].strip()
|
2009-10-12 18:26:35 +00:00
|
|
|
if target.lower() == "me":
|
|
|
|
target = msg.nick
|
2010-04-15 02:08:17 +00:00
|
|
|
queue(irc, reply_target, "%s: %s" % (target, reply))
|
2009-10-12 18:26:35 +00:00
|
|
|
return
|
2017-05-28 00:56:04 +00:00
|
|
|
except Exception as e:
|
2010-05-24 13:38:06 +00:00
|
|
|
self.log.info("PackageInfo: (info) Exception in pipe: %r" % e)
|
2009-10-12 18:26:35 +00:00
|
|
|
pass
|
|
|
|
elif rest[0] == '>':
|
|
|
|
try:
|
2011-04-19 17:05:48 +00:00
|
|
|
while rest[0] == '>':
|
|
|
|
rest = rest[1:].strip()
|
|
|
|
targets = [_ for _ in rest.split() if _] # Split and discard empty parts
|
|
|
|
target = stripNick(targets[0]) # Take the first "nick" and strip off bad chars
|
2009-10-12 18:26:35 +00:00
|
|
|
if target.lower() == "me":
|
2011-04-19 17:05:48 +00:00
|
|
|
target = msg.nick # redirect
|
|
|
|
if not target: # Throw error
|
2017-05-28 00:56:04 +00:00
|
|
|
raise Exception('No target')
|
2010-04-13 01:12:54 +00:00
|
|
|
queue(irc, target, "<%s> wants you to know: %s" % (msg.nick, reply))
|
2009-10-12 18:26:35 +00:00
|
|
|
return
|
2017-05-28 00:56:04 +00:00
|
|
|
except Exception as e:
|
2010-05-24 13:38:06 +00:00
|
|
|
self.log.info("PackageInfo: (info) Exception in redirect: %r" % e)
|
2009-10-12 18:26:35 +00:00
|
|
|
pass
|
|
|
|
|
2010-04-15 02:08:17 +00:00
|
|
|
queue(irc, reply_target, reply)
|
2009-10-12 18:26:35 +00:00
|
|
|
|
|
|
|
info = wrap(real_info, ['anything', optional('text')])
|
|
|
|
|
|
|
|
def real_find(self, irc, msg, args, package, release):
|
2008-10-22 17:31:40 +00:00
|
|
|
"""<package/filename> [<release>]
|
|
|
|
|
|
|
|
Search for <package> or, of that fails, find <filename>'s package(s).
|
|
|
|
Optionally in <release>
|
|
|
|
"""
|
|
|
|
channel = self.__getChannel(msg.args[0])
|
2010-04-15 02:08:17 +00:00
|
|
|
reply_target = ircutils.replyTo(msg)
|
2009-10-12 18:26:35 +00:00
|
|
|
(release, rest) = self.__getRelease(irc, release, channel)
|
2008-10-22 17:31:40 +00:00
|
|
|
if not release:
|
|
|
|
return
|
2009-10-12 18:26:35 +00:00
|
|
|
reply = self.Apt.find(package, release)
|
|
|
|
if rest:
|
|
|
|
if rest[0] == '|':
|
|
|
|
try:
|
2011-04-19 17:16:00 +00:00
|
|
|
target = rest
|
2011-04-19 17:05:48 +00:00
|
|
|
while target[0] == '|':
|
|
|
|
target = target[1:].strip()
|
2009-10-12 18:26:35 +00:00
|
|
|
if target.lower() == "me":
|
|
|
|
target = msg.nick
|
2010-04-15 02:08:17 +00:00
|
|
|
queue(irc, reply_target, "%s: %s" % (target, reply))
|
2009-10-12 18:26:35 +00:00
|
|
|
return
|
2017-05-28 00:56:04 +00:00
|
|
|
except Exception as e:
|
2010-05-24 13:38:06 +00:00
|
|
|
self.log.info("PackageInfo: (find) Exception in pipe: %r" % e)
|
2009-10-12 18:26:35 +00:00
|
|
|
pass
|
|
|
|
elif rest[0] == '>':
|
|
|
|
try:
|
2011-04-19 17:13:00 +00:00
|
|
|
while rest[0] == '>':
|
|
|
|
rest = rest[1:].strip()
|
2011-04-19 17:05:48 +00:00
|
|
|
targets = [_ for _ in rest.split() if _] # Split and discard empty parts
|
|
|
|
target = stripNick(targets[0]) # Take the first "nick" and strip off bad chars
|
2009-10-12 18:26:35 +00:00
|
|
|
if target.lower() == "me":
|
2011-04-19 17:05:48 +00:00
|
|
|
target = msg.nick # redirect
|
|
|
|
if not target: # Throw error
|
2017-05-28 00:56:04 +00:00
|
|
|
raise Exception('No target')
|
2010-04-13 01:12:54 +00:00
|
|
|
queue(irc, target, "<%s> wants you to know: %s" % (msg.nick, reply))
|
2009-10-12 18:26:35 +00:00
|
|
|
return
|
2017-05-28 00:56:04 +00:00
|
|
|
except Exception as e:
|
2010-05-24 13:38:06 +00:00
|
|
|
self.log.info("PackageInfo: (find) Exception in redirect: %r" % e)
|
2009-10-12 18:26:35 +00:00
|
|
|
pass
|
|
|
|
|
2010-04-15 02:08:17 +00:00
|
|
|
queue(irc, reply_target, reply)
|
2009-10-12 18:26:35 +00:00
|
|
|
|
|
|
|
find = wrap(real_find, ['anything', optional('text')])
|
2008-10-22 17:31:40 +00:00
|
|
|
|
2009-01-19 22:26:32 +00:00
|
|
|
def privmsg(self, irc, msg, user):
|
2009-10-12 18:26:35 +00:00
|
|
|
channel = self.__getChannel(msg.args[0])
|
2010-02-04 18:56:07 +00:00
|
|
|
text = self.space_re.subn(' ', msg.args[1].strip())[0]
|
2012-04-12 19:45:02 +00:00
|
|
|
my_commands = self.listCommands()
|
2009-01-19 22:26:32 +00:00
|
|
|
if text[0] == self.registryValue("prefixchar"):
|
2010-02-04 18:56:07 +00:00
|
|
|
text = text[1:].strip()
|
2010-04-15 06:29:20 +00:00
|
|
|
if user and text[0] in list(conf.supybot.reply.whenAddressedBy.chars()):
|
2009-01-19 22:26:32 +00:00
|
|
|
return
|
2009-10-12 18:26:35 +00:00
|
|
|
(cmd, rest) = (text.split(' ', 1) + [None])[:2]
|
2010-04-15 06:29:20 +00:00
|
|
|
if cmd not in my_commands:
|
2009-10-12 18:26:35 +00:00
|
|
|
return
|
|
|
|
if not rest:
|
|
|
|
return
|
|
|
|
(term, rest) = (rest.split(' ', 1) + [None])[:2]
|
|
|
|
if cmd == "find":
|
|
|
|
self.real_find(irc, msg, [], term, rest)
|
2008-11-01 19:03:54 +00:00
|
|
|
else:
|
2009-10-12 18:26:35 +00:00
|
|
|
self.real_info(irc, msg, [], term, rest)
|
2008-11-01 19:03:54 +00:00
|
|
|
|
2009-01-19 22:26:32 +00:00
|
|
|
def chanmsg(self, irc, msg, user):
|
2008-10-22 17:31:40 +00:00
|
|
|
channel = self.__getChannel(msg.args[0])
|
2010-02-04 18:56:07 +00:00
|
|
|
text = self.space_re.subn(' ', msg.args[1].strip())[0]
|
2010-04-15 06:29:20 +00:00
|
|
|
my_commands = self.listCommands()
|
2009-01-19 22:26:32 +00:00
|
|
|
if text[0] != self.registryValue("prefixchar", channel):
|
2008-10-22 17:31:40 +00:00
|
|
|
return
|
2010-04-13 01:38:24 +00:00
|
|
|
text = text[1:]
|
2009-10-12 18:26:35 +00:00
|
|
|
(cmd, rest) = (text.split(' ', 1) + [None])[:2]
|
2010-04-15 06:29:20 +00:00
|
|
|
if cmd not in my_commands:
|
2009-10-12 18:26:35 +00:00
|
|
|
return
|
|
|
|
if not rest:
|
2008-10-22 17:31:40 +00:00
|
|
|
return
|
2009-10-12 18:26:35 +00:00
|
|
|
(term, rest) = (rest.split(' ', 1) + [None])[:2]
|
|
|
|
if cmd == "find":
|
|
|
|
self.real_find(irc, msg, [], term, rest)
|
2009-01-19 22:26:32 +00:00
|
|
|
else:
|
2009-10-12 18:26:35 +00:00
|
|
|
self.real_info(irc, msg, [], term, rest)
|
2008-10-22 17:31:40 +00:00
|
|
|
|
2009-01-19 22:26:32 +00:00
|
|
|
def doPrivmsg(self, irc, msg):
|
|
|
|
if chr(1) in msg.args[1]: # CTCP
|
2008-10-22 17:31:40 +00:00
|
|
|
return
|
2009-10-12 18:26:35 +00:00
|
|
|
if not msg.args[1]:
|
|
|
|
return
|
2009-01-19 22:26:32 +00:00
|
|
|
channel = self.__getChannel(msg.args[0])
|
|
|
|
if not self.registryValue("enabled", channel):
|
2008-10-22 17:31:40 +00:00
|
|
|
return
|
2009-01-19 22:26:32 +00:00
|
|
|
user = get_user(msg)
|
|
|
|
if channel:
|
|
|
|
self.chanmsg(irc, msg, user)
|
2009-10-12 18:26:35 +00:00
|
|
|
else:
|
|
|
|
if user:
|
|
|
|
return
|
2009-01-19 22:26:32 +00:00
|
|
|
self.privmsg(irc, msg, user)
|
2008-10-22 17:31:40 +00:00
|
|
|
|
2009-01-19 22:10:51 +00:00
|
|
|
def inFilter(self, irc, msg):
|
2009-01-19 22:26:32 +00:00
|
|
|
if msg.command != "PRIVMSG":
|
|
|
|
return msg
|
2009-12-03 23:23:09 +00:00
|
|
|
if not conf.supybot.defaultIgnore():
|
|
|
|
return msg
|
2010-02-04 18:56:07 +00:00
|
|
|
text = msg.args[1].strip()
|
|
|
|
if len(text) < 6:
|
|
|
|
return msg
|
2009-01-19 22:26:32 +00:00
|
|
|
user = get_user(msg)
|
|
|
|
channel = self.__getChannel(msg.args[0])
|
2010-04-19 02:22:38 +00:00
|
|
|
reply_chars = self.__getReplyChars(channel)
|
2010-04-15 06:29:20 +00:00
|
|
|
my_commands = self.listCommands()
|
2010-04-19 02:22:38 +00:00
|
|
|
cmd = self.__getCommand(text, channel)
|
|
|
|
|
|
|
|
if cmd not in my_commands:
|
|
|
|
return msg
|
|
|
|
|
2010-04-15 18:43:41 +00:00
|
|
|
if user:
|
|
|
|
if not channel and text[0] == self.registryValue("prefixchar"):
|
2010-04-15 06:29:20 +00:00
|
|
|
msg.args = (msg.args[0], text[1:])
|
|
|
|
return msg
|
2010-04-19 02:22:38 +00:00
|
|
|
|
2009-01-19 22:26:32 +00:00
|
|
|
if channel:
|
2010-04-15 06:29:20 +00:00
|
|
|
if text[0] not in reply_chars:
|
2010-02-04 18:56:07 +00:00
|
|
|
return msg
|
2010-04-19 02:22:38 +00:00
|
|
|
|
2010-04-13 01:38:24 +00:00
|
|
|
# if not hasattr(irc, 'reply'):
|
|
|
|
# irc = callbacks.ReplyIrcProxy(irc, msg)
|
2010-02-04 18:56:07 +00:00
|
|
|
# self.doPrivmsg(irc, msg)
|
2009-01-19 22:26:32 +00:00
|
|
|
else:
|
2010-04-15 06:29:20 +00:00
|
|
|
if text[1] in reply_chars:
|
2010-04-19 02:22:38 +00:00
|
|
|
msg.args = (msg.args[0], text[1:])
|
|
|
|
irc = callbacks.ReplyIrcProxy(irc, msg)
|
|
|
|
self.doPrivmsg(irc, msg)
|
2009-01-19 22:26:32 +00:00
|
|
|
|
2009-01-19 22:10:51 +00:00
|
|
|
return msg
|
|
|
|
|
2008-10-22 17:31:40 +00:00
|
|
|
Class = PackageInfo
|
|
|
|
|
|
|
|
|
|
|
|
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
|