<commit_before>#
# Terminus-Bot: An IRC bot to solve all of the problems with IRC bots.
#
# Copyright (C) 2010-2012 <NAME> <<EMAIL>>, <NAME>
# (http://terminus-bot.net/)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#

module Bot
  User = Struct.new(:connection, :nick, :user, :host, :level, :account)

  class UserManager < Hash

    # Create our users object. These are per-connection, so we're passed
    # our parent.
    def initialize connection
      @connection = connection

      # Register events relevant to us.
      Events.create self, "JOIN",    :add_origin
      Events.create self, "352",     :add_352 # WHO reply
      Events.create self, "PRIVMSG", :add_origin
      Events.create self, "NICK",    :change_nick
      Events.create self, "QUIT",    :quit
      Events.create self, "PART",    :part
    end

    # User has quit the network. Forget about them.
    def quit msg
      return unless msg.connection == @connection

      delete_user msg.nick_canon
    end

    # Check if the parting user no longer has any common channels with us.
    # If they don't, forget about them.
    def part msg
      msg.connection.channels.each_value do |chan|
        unless chan.get_user(msg.nick_canon) == nil
          return
        end
      end

      delete_user msg.nick_canon
    end

    # WHO reply
    def add_352 msg
      return if msg.connection != @connection

      add_user(msg.connection.canonize(msg.raw_arr[7]), msg.raw_arr[4], msg.raw_arr[5])
    end

    # Add the user in the origin (first word) position of a raw message
    # to our list. Users are here in PRIVMSG and others.
    def add_origin msg
      return if msg.connection != @connection

      msg.origin =~ /(.+)!(.+)@(.+)/

      add_user msg.connection.canonize($1), $2, $3
    end

    # Actually add a nick!user@host to our list.
    def add_user nick, user, host
      return if has_key? nick

      $log.debug("Users.add_user") { "Adding user #{nick} on #{@connection.name}" }

      self[nick] = User.new @connection, nick, user, host, 0
    end

    # Someone changed nicks. Make the necessary updates.
    def change_nick msg
      return if msg.connection != @connection
      return unless has_key? msg.nick_canon

      $log.debug("Users.add_user") { "Renaming user #{msg.nick} on #{@connection.name}" }

      changed_nick_canon = msg.connection.canonize msg.text

      # Apparently structs don't let you change values. So just make a
      # new user.
      changed_user = User.new @connection, changed_nick_canon,
                              self[msg.nick_canon].user,
                              self[msg.nick_canon].host,
                              self[msg.nick_canon].level,
                              self[msg.nick_canon].account

      delete_user msg.nick_canon

      self[changed_nick_canon] = changed_user
    end

    # Remove a user by nick.
    def delete_user nick
      $log.debug("Users.add_user") { "Removing user #{nick} on #{@connection.name}" }

      delete nick
    end

    # Get the level of the user speaking in msg.
    # Used when checking permissions.
    def get_level msg
      add_origin msg unless has_key? msg.nick_canon

      return self[msg.nick_canon].level
    end

    def to_s
      keys.to_s
    end
  end
end<commit_msg>users: don't create new user object for rename<commit_after>#
# Terminus-Bot: An IRC bot to solve all of the problems with IRC bots.
#
# Copyright (C) 2010-2012 <NAME> <<EMAIL>>, <NAME>
# (http://terminus-bot.net/)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#

module Bot
  User = Struct.new(:connection, :nick, :user, :host, :level, :account)

  class UserManager < Hash

    # Create our users object. These are per-connection, so we're passed
    # our parent.
    def initialize connection
      @connection = connection

      # Register events relevant to us.
      Events.create self, "JOIN",    :add_origin
      Events.create self, "352",     :add_352 # WHO reply
      Events.create self, "PRIVMSG", :add_origin
      Events.create self, "NICK",    :change_nick
      Events.create self, "QUIT",    :quit
      Events.create self, "PART",    :part
    end

    # User has quit the network. Forget about them.
    def quit msg
      return unless msg.connection == @connection

      delete_user msg.nick_canon
    end

    # Check if the parting user no longer has any common channels with us.
    # If they don't, forget about them.
    def part msg
      msg.connection.channels.each_value do |chan|
        unless chan.get_user(msg.nick_canon) == nil
          return
        end
      end

      delete_user msg.nick_canon
    end

    # WHO reply
    def add_352 msg
      return if msg.connection != @connection

      add_user(msg.connection.canonize(msg.raw_arr[7]), msg.raw_arr[4], msg.raw_arr[5])
    end

    # Add the user in the origin (first word) position of a raw message
    # to our list. Users are here in PRIVMSG and others.
    def add_origin msg
      return if msg.connection != @connection

      msg.origin =~ /(.+)!(.+)@(.+)/

      add_user msg.connection.canonize($1), $2, $3
    end

    # Actually add a nick!user@host to our list.
    def add_user nick, user, host
      return if has_key? nick

      $log.debug("Users.add_user") { "Adding user #{nick} on #{@connection.name}" }

      self[nick] = User.new @connection, nick, user, host, 0
    end

    # Someone changed nicks. Make the necessary updates.
    def change_nick msg
      return if msg.connection != @connection
      return unless has_key? msg.nick_canon

      $log.debug("Users.add_user") { "Renaming user #{msg.nick} on #{@connection.name}" }

      changed_nick_canon = msg.connection.canonize msg.text

      temp = self[msg.nick_canon]

      delete msg.nick_canon

      temp.nick = changed_nick_canon

      self[changed_nick_canon] = temp
    end

    # Remove a user by nick.
    def delete_user nick
      $log.debug("Users.add_user") { "Removing user #{nick} on #{@connection.name}" }

      delete nick
    end

    # Get the level of the user speaking in msg.
    # Used when checking permissions.
    def get_level msg
      add_origin msg unless has_key? msg.nick_canon

      return self[msg.nick_canon].level
    end

    def to_s
      keys.to_s
    end
  end
end
{'fraction_non_alphanumeric': 0.0856729726473069, 'fraction_numerical': 0.0057838293770333775, 'mean_word_length': 2.810835629017447, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2790828', 'n_tokens_mistral': 2492, 'n_tokens_neox': 2347, 'n_words': 939}