Permalink
Browse files

Added user auth; modified Bones to use it.

  • Loading branch information...
faeornran committed May 8, 2011
1 parent 0921450 commit ad844fbbf191732db034c9f015477f900714442b
Showing with 137 additions and 22 deletions.
  1. +104 −0 account-manager.rb
  2. +33 −22 bones.rb
View
@@ -0,0 +1,104 @@
+# Bones Account Manager
+# By James Vaughan
+# Authentication system for using Bones' adventure.
+
+require 'digest/sha1'
+
+module AccountManager
+
+ class Accounts
+ attr_accessor :salt, :activeUsers, :accounts
+
+# Reads in account/password combos from a file.
+# Note that the files track old passwords (but they are encrypted).
+ def initialize(file)
+ f = File.open("cryptsalt", 'r')
+ @salt = f.gets
+ f.close
+
+ @activeUsers = []
+
+ f = File.open(file, 'r')
+ @accounts = []
+ while (line = f.gets)
+ set = line.split(":")
+ raise "Invalid user file syntax." if set.length != 2
+ repeat = @accounts.assoc(set[0])
+ repeat[1] = set[1] if !repeat.nil?
+ @accounts << set if repeat.nil?
+ end
+ @accounts.sort!
+ f.close
+ @file = file
+ end # initialize
+
+# Returns the Bones account name of the given IRC Nickname.
+ def [](ircname)
+ user = @activeUsers.assoc(ircname)
+ return user[1] if !user.nil?
+ end # []
+
+# Encrypts the given text using the stored salt text.
+ def encrypt(text)
+ text = text.to_s
+ Digest::SHA1.hexdigest("--#{@salt}--#{text}--") + "\n"
+ end # encrypt
+
+# Authenticates a user.
+ def auth(ircname, username, password)
+ e = encrypt(password)
+ return "User already logged in." if ((@activeUsers ||= []).find { |x| x[1] == username })
+ result = @accounts.find { |x| x[0] == username }
+ return "No such user." if result.nil?
+ return "Invalid password." if result[1] != e
+ @activeUsers << [ircname, result[0]]
+ @activeUsers = @activeUsers.sort_by { |x| x[0] }
+ return "Login successful."
+ end # auth
+
+# Deauthenticates a user.
+ def deauth(ircname)
+ user = @activeUsers.assoc(ircname)
+ @activeUsers.delete(user) if !user.nil?
+ return "#{ircname}'s account, #{user[1]}, logged off." if !user.nil?
+ return "Irc name not found."
+ end # deauth
+
+# Used for IRC name changes; does not change account name.
+# Account names will not be changeable without manual admin power.
+ def nickChange(oldName, newName)
+ entry = @activeUsers.assoc(oldName)
+ return "No such active user." if entry.nil?
+ entry[0] = newName
+ end # nickChange
+
+# Changes the password of the currently logged in user when given
+# their IRC name, their old password, and a new desired password.
+# Appends the new password to the end of the user file.
+ def changePass(ircname, oldPass, newPass)
+ username = self[ircname]
+ return "Not logged in." if username.nil?
+ user = @accounts.find { |x| x[0] == username }
+ eOld = encrypt(oldPass)
+ return "Incorrect old password." if user[1] != eOld
+ return "Bad new password." if newPass == nil || newPass == ""
+ eNew = encrypt(newPass)
+ File.open(@file, 'a') { |f| f.write("#{username}:#{eNew}") }
+ return "Password changed."
+ end # changePass
+
+# Creates a new user and appends their information to the end of the file.
+ def newUser(username, password)
+ return "User name in use." if @accounts.assoc(username)
+ return "Invalid password." if password == nil || password == ""
+ e = encrypt(password)
+ newUser = username + ":" + e
+ File.open(@file, 'a') { |f| f.write(newUser) }
+ @accounts << [username, e]
+ @accounts.sort
+ return "Account created."
+ end # newUser
+
+ end # Accounts
+
+end # AccountManager
View
@@ -11,6 +11,7 @@
require 'socket'
require 'strscan'
require 'dicebox'
+require 'account-manager.rb'
module Bones
class Client # an "instance" of bones; generally only one
@@ -25,9 +26,11 @@ def initialize(nick, server, port, channels)
@rigged = false
@rignum = 1
- @aleksey = "NO AUTH"
- @kurt = "NO AUTH"
- @crom = "NO AUTH"
+ @aleksey = "Aleksey"
+ @kurt = "Kurt"
+ @crom = "Crom"
+
+ @users = AccountManager::Accounts.new("users")
connect()
run()
@@ -74,9 +77,9 @@ def run # go
def auth_change(msg, user)
if msg =~ /PART/
- user.replace "NO AUTH"
+ @users.deauth(user)
elsif msg =~ /NICK/
- user.replace msg.split(/:|\s/)[-1]
+ @users.nickChange(user, msg.split(/:|\s/)[-1])
end
end
@@ -92,13 +95,17 @@ def handle_msg(msg)
message = Message.new(msg)
respond(message)
- if (msg =~ /#{@aleksey}/ && !(msg =~ /#{@kurt}|#{@crom}/))
- auth_change(msg, @aleksey)
- elsif (msg =~ /#{@crom}/ && !(msg =~ /#{@aleksey}|#{@kurt}/))
- auth_change(msg, @crom)
- elsif (msg =~ /#{@kurt}/ && !(msg =~/#{@aleksey}|#{@crom}/))
- auth_change(msg, @kurt)
- end
+ user = msg.split(/:|!/)[1]
+
+ auth_change(msg, user)
+
+ #if (msg =~ /#{@aleksey}/ && !(msg =~ /#{@kurt}|#{@crom}/))
+ # auth_change(msg, @aleksey)
+ #elsif (msg =~ /#{@crom}/ && !(msg =~ /#{@aleksey}|#{@kurt}/))
+ # auth_change(msg, @crom)
+ #elsif (msg =~ /#{@kurt}/ && !(msg =~/#{@aleksey}|#{@crom}/))
+ # auth_change(msg, @kurt)
+ #end
# if msg =~ /PART/
# @aleksey.replace "NO AUTH" if (msg =~ /#{@aleksey}/ && !(msg =~ /#{@kurt}|#{@crom}/))
@@ -112,18 +119,22 @@ def handle_msg(msg)
#nothing
end
end
-
+
def respond(msg)
# msg :name, :hostname, :mode, :origin, :privmsg, :text
- if msg.name == @crom && msg.text == "Bones, quit"
+ user = @users[msg.name]
+ if msg.name == @crom && user && msg.text == "Bones, quit"
quit(msg.text)
- elsif (msg.name == @crom || msg.name == @aleksey) && msg.text =~ /^@@@leave (#.*)/
+ elsif (msg.name == @crom || msg.name == @aleksey) && user && msg.text =~ /^@@@leave (#.*)/
leave $1.to_s
- elsif msg.text =~ /^auth .+/
- @aleksey.replace msg.name if msg.text =~ /PASSWORD/
- @kurt.replace msg.name if msg.text =~ /PASSWORD/
- @crom.replace msg.name if msg.text =~ /PASSWORD/
- elsif msg.name == @crom && msg.text =~ /^whois.*/
+ elsif msg.text =~ /^auth .+ .+/
+ reply(msg, @users.auth(msg.name, $1, $2))
+ #@aleksey.replace msg.name if msg.text =~ /PASSWORD/
+ #@kurt.replace msg.name if msg.text =~ /PASSWORD/
+ #@crom.replace msg.name if msg.text =~ /PASSWORD/
+ elsif msg.text =~ /^register .+ .+/
+ @users.newUser($1, $2)
+ elsif msg.name == @crom && user && msg.text =~ /^whois.*/
reply(msg, "Aleksey: " + @aleksey) if @aleksey != "NO AUTH"
reply(msg, "Adam: " + @kurt) if @kurt != "NO AUTH"
reply(msg, "James: " + @crom) if @crom != "NO AUTH"
@@ -134,11 +145,11 @@ def respond(msg)
# do command - switch statement or use a command handler class
c = command_handler(prefix, command, args)
reply(msg, c) if c
- elsif (msg.name == @crom || msg.name == @aleksey) && msg.text =~ /^@@@join (#.*)/
+ elsif (msg.name == @crom || msg.name == @aleksey) && user && msg.text =~ /^@@@join (#.*)/
join $1.to_s
elsif msg.text == "hay"
reply(msg, "hay :v")
- elsif (msg.name == @crom || msg.name == @kurt || msg.name == @aleksey) && msg.text =~ /^@@@rig (\d+-?\d*)/
+ elsif (msg.name == @crom || msg.name == @kurt || msg.name == @aleksey) && user && msg.text =~ /^@@@rig (\d+-?\d*)/
@rigged = true
@rignum = $1
elsif msg.text =~ /^(!|@)(\S+)( (.*))?/

0 comments on commit ad844fb

Please sign in to comment.