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-11-27 18:58:20 -08:00
|
|
|
from pyircbot.modulebase import ModuleBase, hook
|
|
|
|
from pyircbot.common import messageHasCommand
|
|
|
|
from pyircbot.modules.ModInfo import info
|
2014-01-07 10:22:21 -08:00
|
|
|
|
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.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
|
|
|
|
2017-11-27 18:58:20 -08:00
|
|
|
@hook("PRIVMSG")
|
|
|
|
def gotmsg(self, msg, cmd):
|
|
|
|
if msg.args[0][0] == "#":
|
2015-11-01 18:03:11 -08:00
|
|
|
# Ignore channel messages
|
2017-11-27 18:58:20 -08:00
|
|
|
return
|
2015-11-01 18:03:11 -08:00
|
|
|
else:
|
2017-11-27 18:58:20 -08:00
|
|
|
self.handlePm(msg.prefix, msg.trailing)
|
2017-01-01 14:59:01 -08:00
|
|
|
|
2017-11-27 18:58:20 -08:00
|
|
|
@info("setpass [<oldpass>] <password> set or change password", cmds=["setpass"])
|
|
|
|
@info("login <password> authenticate with the bot", cmds=["login"])
|
|
|
|
@info("logout log out of the bot", cmds=["logout"])
|
|
|
|
def handlePm(self, prefix, trailing):
|
|
|
|
cmd = messageHasCommand(".setpass", trailing)
|
2015-11-01 18:03:11 -08:00
|
|
|
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.")
|
|
|
|
|
2017-11-27 18:58:20 -08:00
|
|
|
cmd = messageHasCommand(".login", trailing)
|
2015-11-01 18:03:11 -08:00
|
|
|
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\"")
|
2017-11-27 18:58:20 -08:00
|
|
|
cmd = messageHasCommand(".logout", trailing)
|
2015-11-01 18:03:11 -08:00
|
|
|
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.")
|