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

Inherits:
Object
  • Object
show all
Defined in:
/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb,
/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb

Overview

This is the basic class for bot users: they have a username, a password, a list of netmasks to match against, and a list of permissions. A BotUser can be marked as 'transient', usually meaning it's not intended for permanent storage. Transient BotUsers have lower priority than nontransient ones for autologin purposes.

To initialize a BotUser, you pass a username and an optional hash of options. Currently, only two options are recognized:

transient

true or false, determines if the BotUser is transient or permanent (default is false, permanent BotUser).

Transient BotUsers are initialized by prepending an asterisk (*) to the username, and appending a sanitized version of the object_id. The username can be empty. A random password is generated.

Permanent Botusers need the username as is, and no password is generated.

masks

an array of Netmasks to initialize the NetmaskList. This list is used as-is for permanent BotUsers.

Transient BotUsers will alter the list elements which are Irc::User by globbing the nick and any initial nonletter part of the ident.

The masks option is optional for permanent BotUsers, but obligatory (non-empty) for transients.

Direct Known Subclasses

BotOwnerClass, DefaultBotUserClass

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (BotUser) initialize(username, options = {})

Create a new BotUser with given username



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
321
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 285

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

- (Object) login_by_mask=(value) (writeonly)

Sets the attribute login_by_mask

Parameters:

  • value

    the value to set the attribute login_by_mask to.



243
244
245
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 243

def (value)
  @login_by_mask = value
end

- (Object) netmasks (readonly)

Returns the value of attribute netmasks



240
241
242
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 240

def netmasks
  @netmasks
end

- (Object) password

Returns the value of attribute password



239
240
241
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 239

def password
  @password
end

- (Object) perm (readonly)

Returns the value of attribute perm



241
242
243
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 241

def perm
  @perm
end

- (Object) perm_temp (readonly)

Returns the value of attribute perm_temp



242
243
244
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 242

def perm_temp
  @perm_temp
end

- (Object) transient=(value) (writeonly)

Sets the attribute transient

Parameters:

  • value

    the value to set the attribute transient to.



244
245
246
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 244

def transient=(value)
  @transient = value
end

- (Object) username (readonly)

Returns the value of attribute username



238
239
240
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 238

def username
  @username
end

Class Method Details

+ (Object) sanitize_username(name)

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



519
520
521
522
523
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 519

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

- (Object) add_netmask(mask)

Adds a Netmask



464
465
466
467
468
469
470
471
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 464

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

- (Object) autologin=(vnew)



246
247
248
249
250
251
252
253
254
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 246

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

- (Boolean) autologin?

Do we allow automatic logging in?

Returns:

  • (Boolean)


374
375
376
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 374

def autologin?
  @autologin
end

- (Boolean) default?

Check if the current BotUser is the default one

Returns:

  • (Boolean)


637
638
639
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 637

def default?
  return DefaultBotUserClass === self
end

- (Object) delete_netmask(mask)

Removes a Netmask



475
476
477
478
479
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 475

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

- (Object) from_hash(h)

Restore from hash



379
380
381
382
383
384
385
386
387
388
389
390
391
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 379

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

- (Object) inspect

Inspection



324
325
326
327
328
329
330
331
332
333
334
335
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 324

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

- (Boolean) knows?(usr)

This method checks if BotUser has a Netmask that matches user

Returns:

  • (Boolean)


492
493
494
495
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 492

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

- (Object) login(user, password = nil)

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.



501
502
503
504
505
506
507
508
509
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 501

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

- (Boolean) login_by_mask?

Do we allow logging in without providing the password?

Returns:

  • (Boolean)


356
357
358
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 356

def 
  @login_by_mask
end

- (Object) make_permanent(name)

Make the BotUser permanent

Raises:

  • (TypeError)


272
273
274
275
276
277
278
279
280
281
282
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 272

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

- (Boolean) owner?

Check if the current BotUser is the owner

Returns:

  • (Boolean)


642
643
644
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 642

def owner?
  return BotOwnerClass === self
end

- (Object) permanent=(bool)

Sets if the BotUser is permanent or not



267
268
269
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 267

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

- (Boolean) permanent?

Checks if the BotUser is permanent (not transient)

Returns:

  • (Boolean)


262
263
264
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 262

def permanent?
  !@transient
end

- (Boolean) permit?(cmd, chan = nil)

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

Returns:

  • (Boolean)


448
449
450
451
452
453
454
455
456
457
458
459
460
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 448

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

- (Object) reset_autologin

Reset the autologin option



368
369
370
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 368

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

- (Object) reset_login_by_mask

Reset the login-by-mask option



362
363
364
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 362

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

- (Object) reset_netmasks

Reset Netmasks, clearing @netmasks



483
484
485
486
487
488
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 483

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

- (Object) reset_password

Resets the password by creating a new onw



413
414
415
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 413

def reset_password
  @password = Auth.random_password
end

- (Object) reset_permission(cmd, chan = "*")

Resets the permission for command cmd on channel chan



427
428
429
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 427

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

- (Object) reset_temp_permission(cmd, chan = "*")

Resets the temporary permission for command cmd on channel chan



441
442
443
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 441

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

- (Object) set_permission(cmd, val, chan = "*")

Sets the permission for command cmd to val on channel chan



419
420
421
422
423
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 419

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

- (Object) set_temp_permission(cmd, val, chan = "*")

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



433
434
435
436
437
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 433

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

- (Object) to_hash

Convert into a hash



343
344
345
346
347
348
349
350
351
352
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 343

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

- (Object) to_s

In strings



338
339
340
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 338

def to_s
  @username
end

- (Boolean) transient?

Checks if the BotUser is transient

Returns:

  • (Boolean)


257
258
259
# File '/home/apoc/projects/ruby/rbot/lib/rbot/botuser.rb', line 257

def transient?
  @transient
end