login.rb 2.34 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#!/usr/bin/env ruby

# Plugin to do passworded login
class Login

	# This method is called when the plugin is first loaded
	def initialize( status, config, output, irc, timer )

		begin
			require 'bcrypt'
		rescue LoadError
			raise LoadError, "Cannot load bcrypt library. Make sure the bcrypt-ruby gem is installed."
		end

15 16 17 18 19 20 21 22
		@status   = status
		@config   = config
		@output   = output
		@irc      = irc
		@timer    = timer

		@password = ""
		@filename = "password.data"
23 24 25 26 27 28 29 30 31 32 33 34 35

		loadpass
	end

	# Default method, called when no argument is given (optional, but highly recomended)
	def main( nick, user, host, from, msg, arguments, con )
		returnval = ""

		# Check for empty pass
		if( !arguments.nil? && !arguments.empty? )

			# Check password
			if( @password == arguments )
Cool Fire's avatar
Cool Fire committed
36
				@config.opers( @config.opers.push( host ) )
37 38 39 40 41 42 43 44 45 46 47
				returnval = "Login successful, added your host to admin list."
			else
				returnval = "Login unsuccessful, password did not match."
			end
		else
			help( nick, user, host, from, msg, arguments, con )
		end

		if( con )
			@output.c( returnval + "\n" )
		else
48
			@irc.notice( nick, returnval, true )
49 50 51 52
		end
	end

	# Function to set password
Cool Fire's avatar
Cool Fire committed
53
	def set( _nick, _user, host, _from, _msg, arguments, con )
54 55 56 57 58 59 60 61 62
		if( @config.auth( host, con ) )
			if( !arguments.nil? && !arguments.empty? )
				@password = BCrypt::Password.create arguments
				writepass
			end
		end
	end

	# Function to send help about this plugin (Can also be called by the help plugin.)
Cool Fire's avatar
Cool Fire committed
63
	def help( nick, _user, _host, _from, _msg, _arguments, con )
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
		help = [
			"This plugin allows users to log in with a password when their hostmask is not in the oper list.",
			"  login [password]        - Login with password.",
			"  login set [password]    - Set new password."
		]

		# Print out help
		help.each do |line|
			if( con )
				@output.c( line + "\n" )
			else
				@irc.notice( nick, line )
			end
		end
	end

	private

	# Function to load marshaled password on startup
	def loadpass
		# Check if a password is stored on disk
Cool Fire's avatar
Cool Fire committed
85
		if File.exist?( @config.datadir + '/' + @filename )
86 87

			# Read password from file
Cool Fire's avatar
Cool Fire committed
88
			File.open( @config.datadir + '/' + @filename ) do |file|
89 90 91 92 93 94 95 96 97 98 99 100
				@password = Marshal.load( file )
			end
		end
	end

	# Function to marshal out password to file
	def writepass
		File.open( @config.datadir + '/' + @filename, 'w' ) do |file|
			Marshal.dump( @password, file )
		end
	end
end