Class: Irc::Bot::Auth::BotUser

Inherits:
Object
  • Object
show all
Defined in:
lib/rbot/botuser.rb,
lib/rbot/botuser.rb,
lib/rbot/core/remote.rb

Overview

We extend the BotUser class to handle remote logins

Direct Known Subclasses

BotOwnerClass, DefaultBotUserClass

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, options = {}) ⇒ BotUser

Create a new BotUser with given username



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/rbot/botuser.rb', line 284

def initialize(username, options={})
  opts = {:transient => false}.merge(options)
  @transient = opts[:transient]

  if @transient
    @username = "*"
    @username << BotUser.sanitize_username(username) if username and not username.to_s.empty?
    @username << BotUser.sanitize_username(object_id)
    reset_password
    @login_by_mask=true
    @autologin=true
  else
    @username = BotUser.sanitize_username(username)
    @password = nil
    
    reset_autologin
  end

  @netmasks = NetmaskList.new
  if opts.key?(:masks) and opts[:masks]
    masks = opts[:masks]
    masks = [masks] unless masks.respond_to?(:each)
    masks.each { |m|
      mask = m.to_irc_netmask
      if @transient and User === m
        mask.nick = "*"
        mask.host = m.host.dup
        mask.user = "*" + m.user.sub(/^\w?[^\w]+/,'')
      end
      add_netmask(mask) unless mask.to_s == "*"
    }
  end
  raise "must provide a usable mask for transient BotUser #{@username}" if @transient and @netmasks.empty?

  @perm = {}
  @perm_temp = {}
end

Instance Attribute Details

#login_by_mask=(value) ⇒ Object (writeonly)

Sets the attribute login_by_mask

Parameters:

  • value

    the value to set the attribute login_by_mask to.



242
243
244
# File 'lib/rbot/botuser.rb', line 242

def (value)
  @login_by_mask = value
end

#netmasksObject (readonly)

Returns the value of attribute netmasks.



239
240
241
# File 'lib/rbot/botuser.rb', line 239

def netmasks
  @netmasks
end

#passwordObject

Returns the value of attribute password.



238
239
240
# File 'lib/rbot/botuser.rb', line 238

def password
  @password
end

#permObject (readonly)

Returns the value of attribute perm.



240
241
242
# File 'lib/rbot/botuser.rb', line 240

def perm
  @perm
end

#perm_tempObject (readonly)

Returns the value of attribute perm_temp.



241
242
243
# File 'lib/rbot/botuser.rb', line 241

def perm_temp
  @perm_temp
end

#transient=(value) ⇒ Object (writeonly)

Sets the attribute transient

Parameters:

  • value

    the value to set the attribute transient to.



243
244
245
# File 'lib/rbot/botuser.rb', line 243

def transient=(value)
  @transient = value
end

#usernameObject (readonly)

Returns the value of attribute username.



237
238
239
# File 'lib/rbot/botuser.rb', line 237

def username
  @username
end

Class Method Details

.sanitize_username(name) ⇒ Object

This method sanitizes a username by chomping, downcasing and replacing any nonalphanumeric character with _



518
519
520
521
522
# File 'lib/rbot/botuser.rb', line 518

def BotUser.sanitize_username(name)
  candidate = name.to_s.chomp.downcase.gsub(/[^a-z0-9]/,"_")
  raise "sanitized botusername #{candidate} too short" if candidate.length < 3
  return candidate
end

Instance Method Details

#add_netmask(mask) ⇒ Object

Adds a Netmask



463
464
465
466
467
468
469
470
# File 'lib/rbot/botuser.rb', line 463

def add_netmask(mask)
  m = mask.to_irc_netmask
  @netmasks << m
  if self.autologin?
    Auth.manager.maskdb.add(self, m)
    Auth.manager.logout_transients(m) if self.permanent?
  end
end

#autologin=(vnew) ⇒ Object



245
246
247
248
249
250
251
252
253
# File 'lib/rbot/botuser.rb', line 245

def autologin=(vnew)
  vold = @autologin
  @autologin = vnew
  if vold && !vnew
    @netmasks.each { |n| Auth.manager.maskdb.remove(self, n) }
  elsif vnew && !vold
    @netmasks.each { |n| Auth.manager.maskdb.add(self, n) }
  end
end

#autologin?Boolean

Do we allow automatic logging in?

Returns:

  • (Boolean)


373
374
375
# File 'lib/rbot/botuser.rb', line 373

def autologin?
  @autologin
end

#default?Boolean

Check if the current BotUser is the default one

Returns:

  • (Boolean)


636
637
638
# File 'lib/rbot/botuser.rb', line 636

def default?
  return DefaultBotUserClass === self
end

#delete_netmask(mask) ⇒ Object

Removes a Netmask



474
475
476
477
478
# File 'lib/rbot/botuser.rb', line 474

def delete_netmask(mask)
  m = mask.to_irc_netmask
  @netmasks.delete(m)
  Auth.manager.maskdb.remove(self, m) if self.autologin?
end

#from_hash(h) ⇒ Object

Restore from hash



378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/rbot/botuser.rb', line 378

def from_hash(h)
  @username = h[:username] if h.has_key?(:username)
  @password = h[:password] if h.has_key?(:password)
  @login_by_mask = h[:login_by_mask] if h.has_key?(:login_by_mask)
  @autologin = h[:autologin] if h.has_key?(:autologin)
  if h.has_key?(:netmasks)
    @netmasks = h[:netmasks]
    debug @netmasks
    @netmasks.each { |n| Auth.manager.maskdb.add(self, n) } if @autologin
    debug @netmasks
  end
  @perm = h[:perm] if h.has_key?(:perm)
end

#inspectObject

Inspection



323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/rbot/botuser.rb', line 323

def inspect
  str = self.__to_s__[0..-2]
  str << " (transient)" if @transient
  str << ":"
  str << " @username=#{@username.inspect}"
  str << " @netmasks=#{@netmasks.inspect}"
  str << " @perm=#{@perm.inspect}"
  str << " @perm_temp=#{@perm_temp.inspect}" unless @perm_temp.empty?
  str << " @login_by_mask=#{@login_by_mask}"
  str << " @autologin=#{@autologin}"
  str << ">"
end

#knows?(usr) ⇒ Boolean

This method checks if BotUser has a Netmask that matches user

Returns:

  • (Boolean)


491
492
493
494
# File 'lib/rbot/botuser.rb', line 491

def knows?(usr)
  user = usr.to_irc_user
  !!@netmasks.find { |n| user.matches? n }
end

#login(user, password = nil) ⇒ Object

This method gets called when User user wants to log in. It returns true or false depending on whether the password is right. If it is, the Netmask of the user is added to the list of acceptable Netmask unless it’s already matched.



500
501
502
503
504
505
506
507
508
# File 'lib/rbot/botuser.rb', line 500

def (user, password=nil)
  if password == @password or (password.nil? and (@login_by_mask || @autologin) and knows?(user))
    add_netmask(user) unless knows?(user)
    debug "#{user} logged in as #{self.inspect}"
    return true
  else
    return false
  end
end

#login_by_mask?Boolean

Do we allow logging in without providing the password?

Returns:

  • (Boolean)


355
356
357
# File 'lib/rbot/botuser.rb', line 355

def 
  @login_by_mask
end

#make_permanent(name) ⇒ Object

Make the BotUser permanent

Raises:

  • (TypeError)


271
272
273
274
275
276
277
278
279
280
281
# File 'lib/rbot/botuser.rb', line 271

def make_permanent(name)
  raise TypeError, "permanent already" if permanent?
  @username = BotUser.sanitize_username(name)
  @transient = false
  reset_autologin
  reset_password # or not?
  @netmasks.dup.each do |m|
    delete_netmask(m)
    add_netmask(m.generalize)
  end
end

#owner?Boolean

Check if the current BotUser is the owner

Returns:

  • (Boolean)


641
642
643
# File 'lib/rbot/botuser.rb', line 641

def owner?
  return BotOwnerClass === self
end

#permanent=(bool) ⇒ Object

Sets if the BotUser is permanent or not



266
267
268
# File 'lib/rbot/botuser.rb', line 266

def permanent=(bool)
  @transient=!bool
end

#permanent?Boolean

Checks if the BotUser is permanent (not transient)

Returns:

  • (Boolean)


261
262
263
# File 'lib/rbot/botuser.rb', line 261

def permanent?
  !@transient
end

#permit?(cmd, chan = nil) ⇒ Boolean

Checks if BotUser is allowed to do something on channel chan, or on all channels if chan is nil

Returns:

  • (Boolean)


447
448
449
450
451
452
453
454
455
456
457
458
459
# File 'lib/rbot/botuser.rb', line 447

def permit?(cmd, chan=nil)
  if chan
    k = chan.to_s.to_sym
  else
    k = :*
  end
  allow = nil
  pt = @perm.merge @perm_temp
  if pt.has_key?(k)
    allow = pt[k].permit?(cmd)
  end
  return allow
end

#remote_login(password) ⇒ Object

A rather simple method to handle remote logins. Nothing special, just a password check.



27
28
29
30
31
32
33
34
# File 'lib/rbot/core/remote.rb', line 27

def (password)
  if password == @password
    debug "remote login for #{self.inspect} succeeded"
    return true
  else
    return false
  end
end

#reset_autologinObject

Reset the autologin option



367
368
369
# File 'lib/rbot/botuser.rb', line 367

def reset_autologin
  @autologin = Auth.manager.bot.config['auth.autologin'] unless defined?(@autologin)
end

#reset_login_by_maskObject

Reset the login-by-mask option



361
362
363
# File 'lib/rbot/botuser.rb', line 361

def 
  @login_by_mask = Auth.manager.bot.config['auth.login_by_mask'] unless defined?(@login_by_mask)
end

#reset_netmasksObject

Reset Netmasks, clearing @netmasks



482
483
484
485
486
487
# File 'lib/rbot/botuser.rb', line 482

def reset_netmasks
  @netmasks.each { |m|
    Auth.manager.maskdb.remove(self, m) if self.autologin?
  }
  @netmasks.clear
end

#reset_passwordObject

Resets the password by creating a new onw



412
413
414
# File 'lib/rbot/botuser.rb', line 412

def reset_password
  @password = Auth.random_password
end

#reset_permission(cmd, chan = "*") ⇒ Object

Resets the permission for command cmd on channel chan



426
427
428
# File 'lib/rbot/botuser.rb', line 426

def reset_permission(cmd, chan ="*")
  set_permission(cmd, nil, chan)
end

#reset_temp_permission(cmd, chan = "*") ⇒ Object

Resets the temporary permission for command cmd on channel chan



440
441
442
# File 'lib/rbot/botuser.rb', line 440

def reset_temp_permission(cmd, chan ="*")
  set_temp_permission(cmd, nil, chan)
end

#set_permission(cmd, val, chan = "*") ⇒ Object

Sets the permission for command cmd to val on channel chan



418
419
420
421
422
# File 'lib/rbot/botuser.rb', line 418

def set_permission(cmd, val, chan="*")
  k = chan.to_s.to_sym
  @perm[k] = PermissionSet.new unless @perm.has_key?(k)
  @perm[k].set_permission(cmd, val)
end

#set_temp_permission(cmd, val, chan = "*") ⇒ Object

Sets the temporary permission for command cmd to val on channel chan



432
433
434
435
436
# File 'lib/rbot/botuser.rb', line 432

def set_temp_permission(cmd, val, chan="*")
  k = chan.to_s.to_sym
  @perm_temp[k] = PermissionSet.new unless @perm_temp.has_key?(k)
  @perm_temp[k].set_permission(cmd, val)
end

#to_hashObject

Convert into a hash



342
343
344
345
346
347
348
349
350
351
# File 'lib/rbot/botuser.rb', line 342

def to_hash
  {
    :username => @username,
    :password => @password,
    :netmasks => @netmasks,
    :perm => @perm,
    :login_by_mask => @login_by_mask,
    :autologin => @autologin,
  }
end

#to_sObject

In strings



337
338
339
# File 'lib/rbot/botuser.rb', line 337

def to_s
  @username
end

#transient?Boolean

Checks if the BotUser is transient

Returns:

  • (Boolean)


256
257
258
# File 'lib/rbot/botuser.rb', line 256

def transient?
  @transient
end