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.merge(@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 usermod( username, '+hostmask', mask ) if @users.has_key? username && @users[username].password == password
159 debug "User identified: #{username}"
162 # return all currently defined commands (for which auth is required) and
163 # their required authlevels
165 reply = 'Current levels are:'
166 @levels.sort.each { |key, value|
167 reply += " #{key}(#{value})"
172 # return all currently defined users and their authlevels
174 reply = 'Current users are:'
175 @users.sort.each { |key, value|
176 reply += " #{key}(#{value.level})"
181 def showdetails( username )
182 if @users.has_key? username
183 reply = "#{username}(#{@users[username].level}):"
184 @users[username].hostmasks.each { |hostmask|
185 reply += " #{hostmask}"
195 return 'setlevel <command> <level> => Sets required level for <command> to <level> (private addressing only)'
197 return 'useradd <username> => Add user <mask>, you still need to set him up correctly (private addressing only)'
199 return 'userdel <username> => Remove user <username> (private addressing only)'
201 return 'usermod <username> <item> <value> => Modify <username>s settings. Valid <item>s are: hostmask, (+|-)hostmask, password, level (private addressing only)'
203 return 'auth <masterpw> => Create a user with your hostmask and master password as bot master (private addressing only)'
205 return 'levels => list commands and their required levels (private addressing only)'
207 return 'users [<username>]=> list users and their levels or details about <username> (private addressing only)'
209 return 'whoami => Show as whom you are recognized (private addressing only)'
211 return 'identify <username> <password> => Identify your hostmask as belonging to <username> (private addressing only)'
213 return 'Auth module (User authentication) topics: setlevel, useradd, userdel, usermod, auth, levels, users, whoami, identify'
219 if(m.address? && m.private?)
221 when (/^setlevel\s+(\S+)\s+(\d+)$/)
222 if( @bot.auth.allow?( 'auth', m.source, m.replyto ) )
223 @bot.auth.setlevel( $1, $2.to_i )
224 m.reply "level for #$1 set to #$2"
226 when( /^useradd\s+(\S+)/ ) # FIXME Needs review!!! (\s+(\S+)(\s+(\S+)(\s+(\S+))?)?)? Should this part be added to make complete useradds possible?
227 if( @bot.auth.allow?( 'auth', m.source, m.replyto ) )
228 @bot.auth.useradd( $1 )
229 m.reply "added user #$1, please set him up correctly"
231 when( /^userdel\s+(\S+)/ )
232 if( @bot.auth.allow?( 'auth', m.source, m.replyto ) )
233 @bot.auth.userdel( $1 )
234 m.reply "user #$1 is gone"
236 when( /^usermod\s+(\S+)\s+(\S+)\s+(\S+)/ )
237 if( @bot.auth.allow?('auth', m.source, m.replyto ) )
238 if( @bot.auth.usermod( $1, $2, $3 ) )
239 m.reply "Set #$2 of #$1 to #$3"
241 m.reply "Failed to set #$2 of #$1 to #$3"
244 when (/^auth\s+(\S+)/)
245 if( $1 == @bot.config['auth.password'] )
246 if ! @users.has_key? 'master'
247 @bot.auth.useradd( 'master', 1000, @bot.config['auth.password'], m.source )
249 @bot.usermod( 'master', '+hostmask', m.source )
251 m.reply 'Identified, security level maxed out'
253 m.reply 'Incorrect password'
255 when( /^identify\s+(\S+)\s+(\S+)/ )
256 if( @bot.auth.identify( m.source, $1, $2 ) )
257 m.reply "Identified as #$1(#{@users[$1].level($1)}"
259 m.reply 'Incorrect username/password'
262 user = @bot.auth.matchingUser( m.source )
264 m.reply "I recognize you as #{user}(#{@users[user].level})"
266 m.reply 'You don\'t belong to any user.'
268 when( /^users\s+(\S+)/ )
269 m.reply @bot.auth.showdetails( $1 ) if( @bot.auth.allow?( 'auth', m.source, m.replyto ) )
271 m.reply @bot.auth.showlevels if( @bot.auth.allow?( 'config', m.source, m.replyto ) )
273 m.reply @bot.auth.showusers if( @bot.auth.allow?( 'users', m.source, m.replyto ) )