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)


953
954
955
956
957
958
959
960
961
962
# File 'lib/rbot/irc.rb', line 953

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
  @idle_since = nil
  @signon = nil
end

Instance Attribute Details

#idle_sinceObject

Returns the value of attribute idle_since.



948
949
950
# File 'lib/rbot/irc.rb', line 948

def idle_since
  @idle_since
end

#real_nameObject

Returns the value of attribute real_name.



948
949
950
# File 'lib/rbot/irc.rb', line 948

def real_name
  @real_name
end

#signonObject

Returns the value of attribute signon.



948
949
950
# File 'lib/rbot/irc.rb', line 948

def signon
  @signon
end

Instance Method Details

#away=(msg = "") ⇒ Object

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



1004
1005
1006
1007
1008
1009
1010
# File 'lib/rbot/irc.rb', line 1004

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

#away?Boolean

Is the user away?

Returns:

  • (Boolean)


997
998
999
# File 'lib/rbot/irc.rb', line 997

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

#channelsObject



1068
1069
1070
1071
1072
1073
1074
# File 'lib/rbot/irc.rb', line 1068

def channels
  if @server
    @server.channels.select { |ch| ch.has_user?(self) }
  else
    Array.new
  end
end

#delete_botdata(*keys) ⇒ Object

This method removes the data associated with the key, returning the value of the deleted key.



44
45
46
# File 'lib/rbot/core/userdata.rb', line 44

def delete_botdata(*keys)
  Irc::Utils.bot.plugins['userdata'].delete_data(self, *keys)
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.



984
985
986
987
# File 'lib/rbot/irc.rb', line 984

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)


1048
1049
1050
1051
1052
1053
1054
1055
1056
# File 'lib/rbot/irc.rb', line 1048

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)


1058
1059
1060
1061
1062
1063
1064
1065
1066
# File 'lib/rbot/irc.rb', line 1058

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)


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

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

#modes_on(channel) ⇒ Object



1038
1039
1040
1041
1042
1043
1044
1045
1046
# File 'lib/rbot/irc.rb', line 1038

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.



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

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



1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
# File 'lib/rbot/irc.rb', line 1024

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.



1017
1018
1019
1020
# File 'lib/rbot/irc.rb', line 1017

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.



975
976
977
978
# File 'lib/rbot/irc.rb', line 975

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