]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/auth.rb
giuseppe.bilotta's patch:
[user/henk/code/ruby/rbot.git] / lib / rbot / auth.rb
1 module Irc
2
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)
11     return false
12   end
13
14   # check if a string is an actual IRC hostmask
15   def Irc.ismask?(mask)
16     mask =~ /^.+!.+@.+$/
17   end
18
19   Struct.new( 'UserData', :level, :password, :hostmasks )
20
21   # User-level authentication to allow/disallow access to bot commands based
22   # on hostmask and userlevel.
23   class IrcAuth
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' )
30
31     # create a new IrcAuth instance.
32     # bot:: associated bot class
33     def initialize(bot)
34       @bot = bot
35       @users = Hash.new do
36         Struct::UserData.new(@bot.config['auth.default_level'], '', [])
37       end
38       @levels = Hash.new(0)
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)
45         }
46       end
47       if(File.exist?("#{@bot.botclass}/levels.rbot"))
48         IO.foreach("#{@bot.botclass}/levels.rbot") do |line|
49           if(line =~ /\s*(\d+)\s*(\S+)/)
50             level = $1.to_i
51             command = $2
52             @levels[command] = level
53           end
54         end
55       end
56     end
57
58     # save current users and levels to files.
59     # levels are written to #{botclass}/levels.rbot
60     # users are written to #{botclass}/users.yaml
61     def save
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
65       end
66       File.open("#{@bot.botclass}/levels.rbot", 'w') do |file|
67         @levels.each do |key, value|
68           file.puts "#{value} #{key}"
69         end
70       end
71     end
72
73     # command:: command user wishes to perform
74     # mask::    hostmask of user
75     # tell::    optional recipient for "insufficient auth" message
76     #
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] )
83           return true
84         else
85           debug "#{mask} is not allowed to perform #{command}"
86           @bot.say tell, "insufficient \"#{command}\" auth (have #{auth}, need #{@levels[command]})" if tell
87           return false
88         end
89     end
90
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
94     end
95
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
100     end
101
102     def usermod( username, item, value=nil )
103       if @users.has_key?( username )
104         case item
105           when 'hostmask'
106             if Irc.ismask?( value )
107               @users[username].hostmasks = [ value ]
108               return true
109             end
110           when '+hostmask'
111             if Irc.ismask?( value )
112               @users[username].hostmasks += [ value ]
113               return true
114             end
115           when '-hostmask'
116             if Irc.ismask?( value )
117               @users[username].hostmasks -= [ value ]
118               return true
119             end
120           when 'password'
121               @users[username].password = value
122               return true
123           when 'level'
124               @users[username].level = value.to_i
125               return true
126           else
127             debug "usermod: Tried to modify unknown item #{item}"
128             # @bot.say tell, "Unknown item #{item}" if tell
129         end
130       end
131       return false
132     end
133
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
139     end
140
141     def matchingUser( mask )
142       currentUser = nil
143       currentLevel = 0
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 )
148               currentUser = user
149               currentLevel = data.level
150             end
151           }
152         end
153       }
154       currentUser
155     end
156
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 )
160       return true
161     end
162
163     # return all currently defined commands (for which auth is required) and
164     # their required authlevels
165     def showlevels
166       reply = 'Current levels are:'
167       @levels.sort.each { |key, value|
168         reply += " #{key}(#{value})"
169       }
170       reply
171     end
172
173     # return all currently defined users and their authlevels
174     def showusers
175       reply = 'Current users are:'
176       @users.sort.each { |key, value|
177         reply += " #{key}(#{value.level})"
178       }
179       reply
180     end
181
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}"
187         }
188       end
189       reply
190     end
191
192     # module help
193     def help(topic='')
194       case topic
195         when 'setlevel'
196           return 'setlevel <command> <level> => Sets required level for <command> to <level> (private addressing only)'
197         when 'useradd'
198           return 'useradd <username> => Add user <mask>, you still need to set him up correctly (private addressing only)'
199         when 'userdel'
200           return 'userdel <username> => Remove user <username> (private addressing only)'
201         when 'usermod'
202           return 'usermod <username> <item> <value> => Modify <username>s settings. Valid <item>s are: hostmask, (+|-)hostmask, password, level (private addressing only)'
203         when 'auth'
204           return 'auth <masterpw> => Create a user with your hostmask and master password as bot master (private addressing only)'
205         when 'levels'
206           return 'levels => list commands and their required levels (private addressing only)'
207         when 'users'
208           return 'users [<username>]=> list users and their levels or details about <username> (private addressing only)'
209         when 'whoami'
210           return 'whoami => Show as whom you are recognized (private addressing only)'
211         when 'identify'
212           return 'identify <username> <password> => Identify your hostmask as belonging to <username> (private addressing only)'
213         else
214           return 'Auth module (User authentication) topics: setlevel, useradd, userdel, usermod, auth, levels, users, whoami, identify'
215       end
216     end
217
218     # privmsg handler
219     def privmsg(m)
220      if(m.address? && m.private?)
221       case m.message
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"
226           end
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"
231           end
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"
236           end
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"
241             else
242               m.reply "Failed to set #$2 of #$1 to #$3"
243             end
244           end
245         when( /^setpassword\s+(\S+)/ )
246           password = $1
247           user = @bot.auth.matchingUser( m.source )
248           if user
249             if @bot.auth.usermod(user, 'password', password)
250               m.reply "Your password has been set to #{password}"
251             else
252               m.reply "Couldn't set password"
253             end
254           else
255             m.reply 'You don\'t belong to any user.'
256           end
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 )
261             else
262               @bot.auth.usermod( 'master', '+hostmask', m.source )
263             end
264             m.reply 'Identified, security level maxed out'
265           else
266             m.reply 'Incorrect password'
267           end
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})"
271           else
272             m.reply 'Incorrect username/password'
273           end
274         when( 'whoami' )
275           user = @bot.auth.matchingUser( m.source )
276           if user
277             m.reply "I recognize you as #{user} (#{@users[user].level})"
278           else
279             m.reply 'You don\'t belong to any user.'
280           end
281         when( /^users\s+(\S+)/ )
282           m.reply @bot.auth.showdetails( $1 ) if( @bot.auth.allow?( 'auth', m.source, m.replyto ) )
283         when ( 'levels' )
284           m.reply @bot.auth.showlevels if( @bot.auth.allow?( 'config', m.source, m.replyto ) )
285         when ( 'users' )
286           m.reply @bot.auth.showusers if( @bot.auth.allow?( 'users', m.source, m.replyto ) )
287       end
288      end
289     end
290   end
291 end