pyircbot/pyircbot/modules/NickUser.py

95 lines
3.9 KiB
Python
Raw Normal View History

2014-01-07 10:22:21 -08:00
#!/usr/bin/env python
2014-10-02 18:14:42 -07:00
"""
.. module:: NickUser
2015-11-01 18:03:11 -08:00
:synopsis: A module providing a simple login/logout account service
2014-10-02 18:14:42 -07:00
.. moduleauthor:: Dave Pedu <dave@davepedu.com>
"""
2017-01-01 14:59:01 -08:00
from pyircbot.modulebase import ModuleBase, ModuleHook
2014-01-07 10:22:21 -08:00
import hashlib
2017-01-01 14:59:01 -08:00
2014-01-07 10:22:21 -08:00
class NickUser(ModuleBase):
2015-11-01 18:03:11 -08:00
def __init__(self, bot, moduleName):
2017-01-01 14:59:01 -08:00
ModuleBase.__init__(self, bot, moduleName)
self.hooks = [ModuleHook("PRIVMSG", self.gotmsg)]
self.services = ["login"]
2015-11-01 18:03:11 -08:00
def check(self, nick, hostname):
attr = self.bot.getBestModuleForService("attributes")
loggedin = attr.getKey(nick, "loggedinfrom")
2017-01-01 14:59:01 -08:00
if hostname == loggedin:
2015-11-01 18:03:11 -08:00
return True
return False
2017-01-01 14:59:01 -08:00
2015-11-01 18:03:11 -08:00
def ondisable(self):
pass
# TODO: log out all users
2017-01-01 14:59:01 -08:00
2015-11-01 18:03:11 -08:00
def gotmsg(self, args, prefix, trailing):
channel = args[0]
if channel[0] == "#":
# Ignore channel messages
pass
else:
self.handlePm(args, prefix, trailing)
2017-01-01 14:59:01 -08:00
2015-11-01 18:03:11 -08:00
def handlePm(self, args, prefix, trailing):
prefix = self.bot.decodePrefix(prefix)
cmd = self.bot.messageHasCommand(".setpass", trailing)
if cmd:
2017-01-01 14:59:01 -08:00
if len(cmd.args) == 0:
self.bot.act_PRIVMSG(prefix.nick, ".setpass: usage: \".setpass newpass\" or "
"\".setpass oldpass newpass\"")
2015-11-01 18:03:11 -08:00
else:
attr = self.bot.getBestModuleForService("attributes")
oldpass = attr.getKey(prefix.nick, "password")
2017-01-01 14:59:01 -08:00
if oldpass is None:
2015-11-01 18:03:11 -08:00
attr.setKey(prefix.nick, "password", cmd.args[0])
self.bot.act_PRIVMSG(prefix.nick, ".setpass: Your password has been set to \"%s\"." % cmd.args[0])
else:
2017-01-01 14:59:01 -08:00
if len(cmd.args) == 2:
2015-11-01 18:03:11 -08:00
if cmd.args[0] == oldpass:
attr.setKey(prefix.nick, "password", cmd.args[1])
2017-01-01 14:59:01 -08:00
self.bot.act_PRIVMSG(prefix.nick, ".setpass: Your password has been set to \"%s\"." %
cmd.args[1])
2015-11-01 18:03:11 -08:00
else:
self.bot.act_PRIVMSG(prefix.nick, ".setpass: Old password incorrect.")
else:
2017-01-01 14:59:01 -08:00
self.bot.act_PRIVMSG(prefix.nick,
".setpass: You must provide the old password when setting a new one.")
2015-11-01 18:03:11 -08:00
cmd = self.bot.messageHasCommand(".login", trailing)
if cmd:
attr = self.bot.getBestModuleForService("attributes")
userpw = attr.getKey(prefix.nick, "password")
2017-01-01 14:59:01 -08:00
if userpw is None:
2015-11-01 18:03:11 -08:00
self.bot.act_PRIVMSG(prefix.nick, ".login: You must first set a password with .setpass")
else:
2017-01-01 14:59:01 -08:00
if len(cmd.args) == 1:
2015-11-01 18:03:11 -08:00
if userpw == cmd.args[0]:
#################
attr.setKey(prefix.nick, "loggedinfrom", prefix.hostname)
self.bot.act_PRIVMSG(prefix.nick, ".login: You have been logged in from: %s" % prefix.hostname)
#################
else:
self.bot.act_PRIVMSG(prefix.nick, ".login: incorrect password.")
else:
self.bot.act_PRIVMSG(prefix.nick, ".login: usage: \".login password\"")
cmd = self.bot.messageHasCommand(".logout", trailing)
if cmd:
attr = self.bot.getBestModuleForService("attributes")
loggedin = attr.getKey(prefix.nick, "loggedinfrom")
2017-01-01 14:59:01 -08:00
if loggedin is None:
2015-11-01 18:03:11 -08:00
self.bot.act_PRIVMSG(prefix.nick, ".logout: You must first be logged in")
else:
attr.setKey(prefix.nick, "loggedinfrom", None)
self.bot.act_PRIVMSG(prefix.nick, ".logout: You have been logged out.")
2017-01-01 14:59:01 -08:00
2015-11-01 18:03:11 -08:00
def md5(self, data):
m = hashlib.md5()
m.update(data.encode("ascii"))
return m.hexdigest()