]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/auth.rb
test
[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.merge(@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       usermod( username, '+hostmask', mask ) if @users.has_key? username && @users[username].password == password
159       debug "User identified: #{username}"
160     end
161
162     # return all currently defined commands (for which auth is required) and
163     # their required authlevels
164     def showlevels
165       reply = 'Current levels are:'
166       @levels.sort.each { |key, value|
167         reply += " #{key}(#{value})"
168       }
169       reply
170     end
171
172     # return all currently defined users and their authlevels
173     def showusers
174       reply = 'Current users are:'
175       @users.sort.each { |key, value|
176         reply += " #{key}(#{value.level})"
177       }
178       reply
179     end
180
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}"
186         }
187       end
188       reply
189     end
190
191     # module help
192     def help(topic='')
193       case topic
194         when 'setlevel'
195           return 'setlevel <command> <level> => Sets required level for <command> to <level> (private addressing only)'
196         when 'useradd'
197           return 'useradd <username> => Add user <mask>, you still need to set him up correctly (private addressing only)'
198         when 'userdel'
199           return 'userdel <username> => Remove user <username> (private addressing only)'
200         when 'usermod'
201           return 'usermod <username> <item> <value> => Modify <username>s settings. Valid <item>s are: hostmask, (+|-)hostmask, password, level (private addressing only)'
202         when 'auth'
203           return 'auth <masterpw> => Create a user with your hostmask and master password as bot master (private addressing only)'
204         when 'levels'
205           return 'levels => list commands and their required levels (private addressing only)'
206         when 'users'
207           return 'users [<username>]=> list users and their levels or details about <username> (private addressing only)'
208         when 'whoami'
209           return 'whoami => Show as whom you are recognized (private addressing only)'
210         when 'identify'
211           return 'identify <username> <password> => Identify your hostmask as belonging to <username> (private addressing only)'
212         else
213           return 'Auth module (User authentication) topics: setlevel, useradd, userdel, usermod, auth, levels, users, whoami, identify'
214       end
215     end
216
217     # privmsg handler
218     def privmsg(m)
219      if(m.address? && m.private?)
220       case m.message
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"
225           end
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"
230           end
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"
235           end
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"
240             else
241               m.reply "Failed to set #$2 of #$1 to #$3"
242             end
243           end
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 )
248             else
249               @bot.usermod( 'master', '+hostmask', m.source )
250             end
251             m.reply 'Identified, security level maxed out'
252           else
253             m.reply 'Incorrect password'
254           end
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)}"
258           else
259             m.reply 'Incorrect username/password'
260           end
261         when( 'whoami' )
262           user = @bot.auth.matchingUser( m.source )
263           if user
264             m.reply "I recognize you as #{user}(#{@users[user].level})"
265           else
266             m.reply 'You don\'t belong to any user.'
267           end
268         when( /^users\s+(\S+)/ )
269           m.reply @bot.auth.showdetails( $1 ) if( @bot.auth.allow?( 'auth', m.source, m.replyto ) )
270         when ( 'levels' )
271           m.reply @bot.auth.showlevels if( @bot.auth.allow?( 'config', m.source, m.replyto ) )
272         when ( 'users' )
273           m.reply @bot.auth.showusers if( @bot.auth.allow?( 'users', m.source, m.replyto ) )
274       end
275      end
276     end
277   end
278 end