3 # globmask:: glob to test with
4 # netmask:: netmask to test against
5 # Compare a netmask with a standard IRC glob, e.g foo!bar@baz.com would
6 # match *!*@baz.com, foo!*@*, *!bar@*, etc.
7 def Irc.netmaskmatch( globmask, netmask )
8 regmask = Regexp.escape( globmask )
9 regmask.gsub!( /\\\*/, '.*' )
10 return true if(netmask =~ /#{regmask}/i)
14 # check if a string is an actual IRC hostmask
19 Struct.new( 'UserData', :level, :password, :hostmasks )
21 # User-level authentication to allow/disallow access to bot commands based
22 # on hostmask and userlevel.
24 BotConfig.register BotConfigStringValue.new( 'auth.password',
25 :default => 'rbotauth', :wizard => true,
26 :desc => 'Your password for maxing your auth with the bot (used to associate new hostmasks with your owner-status etc)' )
27 BotConfig.register BotConfigIntegerValue.new( 'auth.default_level',
28 :default => 10, :wizard => true,
29 :desc => 'The default level for new/unknown users' )
31 # create a new IrcAuth instance.
32 # bot:: associated bot class
36 Struct::UserData.new(@bot.config['auth.default_level'], '', [])
39 @currentUsers = Hash.new( nil )
40 if( File.exist?( "#{@bot.botclass}/users.yaml" ) )
41 File.open( "#{@bot.botclass}/users.yaml" ) { |file|
42 # work around YAML not maintaining the default proc
43 @loadedusers = YAML::parse(file).transform
44 @users.update(@loadedusers)
47 if(File.exist?("#{@bot.botclass}/levels.rbot"))
48 IO.foreach("#{@bot.botclass}/levels.rbot") do |line|
49 if(line =~ /\s*(\d+)\s*(\S+)/)
52 @levels[command] = level
58 # save current users and levels to files.
59 # levels are written to #{botclass}/levels.rbot
60 # users are written to #{botclass}/users.yaml
62 Dir.mkdir("#{@bot.botclass}") if(!File.exist?("#{@bot.botclass}"))
63 File.open("#{@bot.botclass}/users.yaml", 'w') do |file|
64 file.puts @users.to_yaml
66 File.open("#{@bot.botclass}/levels.rbot", 'w') do |file|
67 @levels.each do |key, value|
68 file.puts "#{value} #{key}"
73 # command:: command user wishes to perform
74 # mask:: hostmask of user
75 # tell:: optional recipient for "insufficient auth" message
77 # returns true if user with hostmask +mask+ is permitted to perform
78 # +command+ optionally pass tell as the target for the "insufficient auth"
79 # message, if the user is not authorised
80 def allow?( command, mask, tell=nil )
81 auth = @users[matchingUser(mask)].level # Directly using @users[] is possible, because UserData has a default setting
82 if( auth >= @levels[command] )
85 debug "#{mask} is not allowed to perform #{command}"
86 @bot.say tell, "insufficient \"#{command}\" auth (have #{auth}, need #{@levels[command]})" if tell
91 # add user with hostmask matching +mask+ with initial auth level +level+
92 def useradd( username, level=@bot.config['auth.default_level'], password='', hostmask='*!*@*' )
93 @users[username] = Struct::UserData.new( level, password, [hostmask] ) if ! @users.has_key? username
96 # mask:: mask of user to remove
97 # remove user with mask +mask+
98 def userdel( username )
99 @users.delete( username ) if @users.has_key? username
102 def usermod( username, item, value=nil )
103 if @users.has_key?( username )
106 if Irc.ismask?( value )
107 @users[username].hostmasks = [ value ]
111 if Irc.ismask?( value )
112 @users[username].hostmasks += [ value ]
116 if Irc.ismask?( value )
117 @users[username].hostmasks -= [ value ]
121 @users[username].password = value
124 @users[username].level = value.to_i
127 debug "usermod: Tried to modify unknown item #{item}"
128 # @bot.say tell, "Unknown item #{item}" if tell
134 # command:: command to adjust
135 # level:: new auth level for the command
136 # set required auth level of +command+ to +level+
137 def setlevel(command, level)
138 @levels[command] = level
141 def matchingUser( mask )
144 @users.each { |user, data| # TODO Will get easier if YPaths are used...
145 if data.level > currentLevel
146 data.hostmasks.each { |hostmask|
147 if Irc.netmaskmatch( hostmask, mask )
149 currentLevel = data.level
157 def identify( mask, username, password )
158 return false unless @users.has_key?(username) && @users[username].password == password
159 @bot.auth.usermod( username, '+hostmask', mask )
163 # return all currently defined commands (for which auth is required) and
164 # their required authlevels
166 reply = 'Current levels are:'
167 @levels.sort.each { |key, value|
168 reply += " #{key}(#{value})"
173 # return all currently defined users and their authlevels
175 reply = 'Current users are:'
176 @users.sort.each { |key, value|
177 reply += " #{key}(#{value.level})"
182 def showdetails( username )
183 if @users.has_key? username
184 reply = "#{username}(#{@users[username].level}):"
185 @users[username].hostmasks.each { |hostmask|
186 reply += " #{hostmask}"
196 return 'setlevel <command> <level> => Sets required level for <command> to <level> (private addressing only)'
198 return 'useradd <username> => Add user <mask>, you still need to set him up correctly (private addressing only)'
200 return 'userdel <username> => Remove user <username> (private addressing only)'
202 return 'usermod <username> <item> <value> => Modify <username>s settings. Valid <item>s are: hostmask, (+|-)hostmask, password, level (private addressing only)'
204 return 'auth <masterpw> => Create a user with your hostmask and master password as bot master (private addressing only)'
206 return 'levels => list commands and their required levels (private addressing only)'
208 return 'users [<username>]=> list users and their levels or details about <username> (private addressing only)'
210 return 'whoami => Show as whom you are recognized (private addressing only)'
212 return 'identify <username> <password> => Identify your hostmask as belonging to <username> (private addressing only)'
214 return 'Auth module (User authentication) topics: setlevel, useradd, userdel, usermod, auth, levels, users, whoami, identify'
220 if(m.address? && m.private?)
222 when (/^setlevel\s+(\S+)\s+(\d+)$/)
223 if( @bot.auth.allow?( 'auth', m.source, m.replyto ) )
224 @bot.auth.setlevel( $1, $2.to_i )
225 m.reply "level for #$1 set to #$2"
227 when( /^useradd\s+(\S+)/ ) # FIXME Needs review!!! (\s+(\S+)(\s+(\S+)(\s+(\S+))?)?)? Should this part be added to make complete useradds possible?
228 if( @bot.auth.allow?( 'auth', m.source, m.replyto ) )
229 @bot.auth.useradd( $1 )
230 m.reply "added user #$1, please set him up correctly"
232 when( /^userdel\s+(\S+)/ )
233 if( @bot.auth.allow?( 'auth', m.source, m.replyto ) )
234 @bot.auth.userdel( $1 )
235 m.reply "user #$1 is gone"
237 when( /^usermod\s+(\S+)\s+(\S+)\s+(\S+)/ )
238 if( @bot.auth.allow?('auth', m.source, m.replyto ) )
239 if( @bot.auth.usermod( $1, $2, $3 ) )
240 m.reply "Set #$2 of #$1 to #$3"
242 m.reply "Failed to set #$2 of #$1 to #$3"
245 when( /^setpassword\s+(\S+)/ )
247 user = @bot.auth.matchingUser( m.source )
249 if @bot.auth.usermod(user, 'password', password)
250 m.reply "Your password has been set to #{password}"
252 m.reply "Couldn't set password"
255 m.reply 'You don\'t belong to any user.'
257 when (/^auth\s+(\S+)/)
258 if( $1 == @bot.config['auth.password'] )
259 if ! @users.has_key? 'master'
260 @bot.auth.useradd( 'master', 1000, @bot.config['auth.password'], m.source )
262 @bot.auth.usermod( 'master', '+hostmask', m.source )
264 m.reply 'Identified, security level maxed out'
266 m.reply 'Incorrect password'
268 when( /^identify\s+(\S+)\s+(\S+)/ )
269 if @bot.auth.identify( m.source, $1, $2 )
270 m.reply "Identified as #$1 (#{@users[$1].level})"
272 m.reply 'Incorrect username/password'
275 user = @bot.auth.matchingUser( m.source )
277 m.reply "I recognize you as #{user} (#{@users[user].level})"
279 m.reply 'You don\'t belong to any user.'
281 when( /^users\s+(\S+)/ )
282 m.reply @bot.auth.showdetails( $1 ) if( @bot.auth.allow?( 'auth', m.source, m.replyto ) )
284 m.reply @bot.auth.showlevels if( @bot.auth.allow?( 'config', m.source, m.replyto ) )
286 m.reply @bot.auth.showusers if( @bot.auth.allow?( 'users', m.source, m.replyto ) )