Unix: make plugin py3k-friendly.

This commit is contained in:
Valentin Lorentz 2012-08-04 22:20:20 +02:00
parent 65eb79b8f2
commit 6537137b3f

View File

@ -109,7 +109,7 @@ class Unix(callbacks.Plugin):
irc.reply(format('%i', os.getpid()), private=True) irc.reply(format('%i', os.getpid()), private=True)
pid = wrap(pid, [('checkCapability', 'owner')]) pid = wrap(pid, [('checkCapability', 'owner')])
_cryptre = re.compile(r'[./0-9A-Za-z]') _cryptre = re.compile(b'[./0-9A-Za-z]')
@internationalizeDocstring @internationalizeDocstring
def crypt(self, irc, msg, args, password, salt): def crypt(self, irc, msg, args, password, salt):
"""<password> [<salt>] """<password> [<salt>]
@ -120,12 +120,12 @@ class Unix(callbacks.Plugin):
based crypt rather than the standard DES based crypt. based crypt rather than the standard DES based crypt.
""" """
def makeSalt(): def makeSalt():
s = '\x00' s = b'\x00'
while self._cryptre.sub('', s) != '': while self._cryptre.sub(b'', s) != b'':
s = struct.pack('<h', random.randrange(-(2**15), 2**15)) s = struct.pack('<h', random.randrange(-(2**15), 2**15))
return s return s
if not salt: if not salt:
salt = makeSalt() salt = makeSalt().decode()
irc.reply(crypt.crypt(password, salt)) irc.reply(crypt.crypt(password, salt))
crypt = wrap(crypt, ['something', additional('something')]) crypt = wrap(crypt, ['something', additional('something')])
@ -157,15 +157,15 @@ class Unix(callbacks.Plugin):
irc.error(e, Raise=True) irc.error(e, Raise=True)
ret = inst.poll() ret = inst.poll()
if ret is not None: if ret is not None:
s = inst.stderr.readline() s = inst.stderr.readline().decode('utf8')
if not s: if not s:
s = inst.stdout.readline() s = inst.stdout.readline().decode('utf8')
s = s.rstrip('\r\n') s = s.rstrip('\r\n')
s = s.lstrip('Error: ') s = s.lstrip('Error: ')
irc.error(s, Raise=True) irc.error(s, Raise=True)
(out, err) = inst.communicate(word) (out, err) = inst.communicate(word.encode())
inst.wait() inst.wait()
lines = filter(None, out.splitlines()) lines = [x.decode('utf8') for x in out.splitlines() if x]
lines.pop(0) # Banner lines.pop(0) # Banner
if not lines: if not lines:
irc.error(_('No results found.'), Raise=True) irc.error(_('No results found.'), Raise=True)
@ -251,7 +251,7 @@ class Unix(callbacks.Plugin):
(out, _) = inst.communicate() (out, _) = inst.communicate()
inst.wait() inst.wait()
if out: if out:
response = out.splitlines()[0].strip() response = out.decode('utf8').splitlines()[0].strip()
response = utils.str.normalizeWhitespace(response) response = utils.str.normalizeWhitespace(response)
irc.reply(response) irc.reply(response)
else: else:
@ -299,9 +299,9 @@ class Unix(callbacks.Plugin):
'not available (%s).' % e, Raise=True) 'not available (%s).' % e, Raise=True)
result = inst.communicate() result = inst.communicate()
if result[1]: # stderr if result[1]: # stderr
irc.error(' '.join(result[1].split())) irc.error(' '.join(result[1].decode('utf8').split()))
else: else:
response = result[0].split("\n"); response = result[0].decode('utf8').split("\n");
if response[1]: if response[1]:
irc.reply(' '.join(response[1].split()[3:5]).split(':')[0] irc.reply(' '.join(response[1].split()[3:5]).split(':')[0]
+ ': ' + ' '.join(response[-3:])) + ': ' + ' '.join(response[-3:]))
@ -333,7 +333,7 @@ class Unix(callbacks.Plugin):
(out, err) = inst.communicate() (out, err) = inst.communicate()
inst.wait() inst.wait()
lines = out.splitlines() lines = out.splitlines()
lines = map(str.rstrip, lines) lines = [x.decode('utf8').rstrip() for x in lines]
lines = filter(None, lines) lines = filter(None, lines)
irc.replies(lines, joiner=' ') irc.replies(lines, joiner=' ')
else: else:
@ -389,9 +389,9 @@ class Unix(callbacks.Plugin):
'not available (%s).' % e, Raise=True) 'not available (%s).' % e, Raise=True)
result = inst.communicate() result = inst.communicate()
if result[1]: # stderr if result[1]: # stderr
irc.error(' '.join(result[1].split())) irc.error(' '.join(result[1].decode('utf8').split()))
if result[0]: # stdout if result[0]: # stdout
response = result[0].split("\n"); response = result[0].decode('utf8').split("\n");
response = [l for l in response if l] response = [l for l in response if l]
irc.replies(response) irc.replies(response)
call = thread(wrap(call, ["owner", "text"])) call = thread(wrap(call, ["owner", "text"]))