Class: Irc::User

Inherits:
Netmask show all
Defined in:
lib/rbot/irc.rb,
lib/rbot/botuser.rb,
lib/rbot/core/userdata.rb

Overview

An IRC User is identified by his/her Netmask (which must not have globs). In fact, User is just a subclass of Netmask.

Ideally, the user and host information of an IRC User should never change, and it shouldn’t contain glob patterns. However, IRC is somewhat idiosincratic and it may be possible to know the nick of a User much before its user and host are known. Moreover, some networks (namely Freenode) may change the hostname of a User when (s)he identifies with Nickserv.

As a consequence, we must allow changes to a User host and user attributes. We impose a restriction, though: they may not contain glob patterns, except for the special case of an unknown user/host which is represented by a *.

It is possible to create a totally unknown User (e.g. for initializations) by setting the nick to * too.

TODO list:

  • see if it’s worth to add the other USER data

  • see if it’s worth to add NICKSERV status

Instance Attribute Summary collapse

Attributes inherited from Netmask

#host, #nick, #user

Attributes included from ServerOrCasemap

#server

Instance Method Summary collapse

Methods inherited from Netmask

#<=>, #==, #===, #downcased, #full_downcase, #full_irc_downcase, #fullform, #generalize, #has_irc_glob?, #inspect, #matches?, #to_irc_netmask

Methods included from ServerOrCasemap

#casemap, #downcase, #fits_with_server_and_casemap?, #init_server_or_casemap, #irc_downcase, #irc_upcase, #server_and_casemap, #upcase

Constructor Details

#initialize(str = "", opts = {}) ⇒ User

Create a new IRC User from a given Netmask (or anything that can be converted into a Netmask) provided that the given Netmask does not have globs.

Raises:

  • (ArgumentError)


930
931
932
933
934
935
936
937
# File 'lib/rbot/irc.rb', line 930

def initialize(str="", opts={})
  super
  raise ArgumentError, "#{str.inspect} must not have globs (unescaped * or ?)" if nick.has_irc_glob? && nick != "*"
  raise ArgumentError, "#{str.inspect} must not have globs (unescaped * or ?)" if user.has_irc_glob? && user != "*"
  raise ArgumentError, "#{str.inspect} must not have globs (unescaped * or ?)" if host.has_irc_glob? && host != "*"
  @away = false
  @real_name = String.new
end

Instance Attribute Details

#real_nameObject

Returns the value of attribute real_name.



925
926
927
# File 'lib/rbot/irc.rb', line 925

def real_name
  @real_name
end

Instance Method Details

#away=(msg = "") ⇒ Object

Set the away status of the user. Use away=(nil) or away=(false) to unset away



979
980
981
982
983
984
985
# File 'lib/rbot/irc.rb', line 979

def away=(msg="")
  if msg
    @away = msg
  else
    @away = false
  end
end

#away?Boolean

Is the user away?

Returns:

  • (Boolean)


972
973
974
# File 'lib/rbot/irc.rb', line 972

def away?
  return @away
end

#botdata(key = nil) ⇒ Object Also known as: get_botdata

Retrive Bot data associated with the receiver. This method is intended for data retrieval only. See #set_bot_data() if you need to alter User data.



14
15
16
# File 'lib/rbot/core/userdata.rb', line 14

def botdata(key=nil)
  Irc::Utils.bot.plugins['userdata'].get_data(self,key)
end

#botuserObject

A convenience method to automatically found the botuser associated with the receiver



935
936
937
# File 'lib/rbot/botuser.rb', line 935

def botuser
  Irc::Bot::Auth.manager.irc_to_botuser(self)
end

#host=(newhost) ⇒ Object

We have to allow changing the host of an Irc User due to some networks (e.g. Freenode) changing hostmasks on the fly. We still check if the new host data has glob patterns though.



959
960
961
962
# File 'lib/rbot/irc.rb', line 959

def host=(newhost)
  raise "Can't change the hostname to #{newhost}" if defined?(@host) and newhost.has_irc_glob?
  super
end

#is_op?(channel) ⇒ Boolean

Returns:

  • (Boolean)


1023
1024
1025
1026
1027
1028
1029
1030
1031
# File 'lib/rbot/irc.rb', line 1023

def is_op?(channel)
  case channel
  when Channel
    channel.has_op?(self)
  else
    return @server.channel(channel).has_op?(self) if @server
    raise "Can't resolve channel #{channel}"
  end
end

#is_voice?(channel) ⇒ Boolean

Returns:

  • (Boolean)


1033
1034
1035
1036
1037
1038
1039
1040
1041
# File 'lib/rbot/irc.rb', line 1033

def is_voice?(channel)
  case channel
  when Channel
    channel.has_voice?(self)
  else
    return @server.channel(channel).has_voice?(self) if @server
    raise "Can't resolve channel #{channel}"
  end
end

#known?Boolean

Checks if a User is well-known or not by looking at the hostname and user

Returns:

  • (Boolean)


966
967
968
# File 'lib/rbot/irc.rb', line 966

def known?
  return nick != "*" && user != "*" && host != "*"
end

#modes_on(channel) ⇒ Object



1013
1014
1015
1016
1017
1018
1019
1020
1021
# File 'lib/rbot/irc.rb', line 1013

def modes_on(channel)
  case channel
  when Channel
    channel.modes_of(self)
  else
    return @server.channel(channel).modes_of(self) if @server
    raise "Can't resolve channel #{channel}"
  end
end

#nick=(newnick) ⇒ Object

The nick of a User may be changed freely, but it must not contain glob patterns.



941
942
943
944
# File 'lib/rbot/irc.rb', line 941

def nick=(newnick)
  raise "Can't change the nick to #{newnick}" if defined?(@nick) and newnick.has_irc_glob?
  super
end

#replace(other) ⇒ Object

We can replace everything at once with data from another User



999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
# File 'lib/rbot/irc.rb', line 999

def replace(other)
  case other
  when User
    self.nick = other.nick
    self.user = other.user
    self.host = other.host
    @server = other.server
    @casemap = other.casemap unless @server
    @away = other.away?
  else
    self.replace(other.to_irc_user(server_and_casemap))
  end
end

#set_botdata(key, value = nil, &block) ⇒ Object

This method is used to store Bot data associated with the receiver. If no block is passed, value is stored for the key key; if a block is passed, it will be called with the previous key value as parameter, and its return value will be stored as the new value. If value is present in the block form, it will be used to initialize key if it’s missing.

If you have to do large-scale editing of the Bot data Hash, please use with_botdata.



29
30
31
# File 'lib/rbot/core/userdata.rb', line 29

def set_botdata(key, value=nil, &block)
  Irc::Utils.bot.plugins['userdata'].set_data(self, key, value, &block)
end

#to_irc_user(opts = {}) ⇒ Object

Since to_irc_user runs the same checks on server and channel as to_irc_netmask, we just try that and return self if it works.

Subclasses of User will return self if possible.



992
993
994
995
# File 'lib/rbot/irc.rb', line 992

def to_irc_user(opts={})
  return self if fits_with_server_and_casemap?(opts)
  return self.full_downcase.to_irc_user(opts)
end

#user=(newuser) ⇒ Object

We have to allow changing the user of an Irc User due to some networks (e.g. Freenode) changing hostmasks on the fly. We still check if the new user data has glob patterns though.



950
951
952
953
# File 'lib/rbot/irc.rb', line 950

def user=(newuser)
  raise "Can't change the username to #{newuser}" if defined?(@user) and newuser.has_irc_glob?
  super
end

#with_botdata(&block) ⇒ Object

This method yields the entire Bot data Hash to the block, and stores any changes done to it, returning a copy of the (changed) Hash.



37
38
39
# File 'lib/rbot/core/userdata.rb', line 37

def with_botdata(&block)
  Irc::Utils.bot.plugins['userdata'].with_data(self, &block)
end