<commit_before>#
# Logging plugin for CyBot.
#

class Logger < PluginBase

  # We want these.
#  Channel_Instances = true

  # Some per-channel init.
  def initialize(*args)
    @brief_help = 'Records various channel activities.'
    @seen = {}
    @logs = {}
    @corrections_for = {}
    super(*args)
  end
  
  # Load/save database.
  def load
    begin
      @seen = YAML.load_file(file_name('seen.db'))
    rescue
      @seen = {}
    end
  end
  def save
    open_file('seen.db', 'w') do |f|
      f.puts '# CyBot logger plugin: Seen database.'
      YAML.dump(@seen, f)
      f.puts ''
    end
  end

  def log(channel, text)
    @logs[channel] ||= open_file(channel + '.txt', 'a')
    now = Time.now
    @logs[channel].puts("[%d:%d] %s" % [now.hour, now.min, text])
  end

  # Log types.
  LogPrivmsg  = 1
  LogNotice   = 2
  LogJoin     = 3
  LogPart     = 4
  LogNick     = 5

  # Hook for PRIVMSGs to a channel the bot is in.
  def hook_privmsg_chan(irc, msg)
    if @corrections_for[irc.from.nnick] and not @corrections_for[irc.from.nnick].empty?
      if msg == 'no'
        @corrections_for[irc.from.nnick].shift
        if @corrections_for[irc.from.nnick].size == 1
          irc.reply "last try: did you mean #{@corrections_for[irc.from.nnick].first.first}?"
        elsif not @corrections_for[irc.from.nnick].empty?
          irc.reply "did you mean #{@corrections_for[irc.from.nnick].first.first}?"
        end
      elsif msg == 'yes'
        chan_seen(irc, irc.channel, @corrections_for[irc.from.nnick].first.first)
      else
        @corrections_for[irc.from.nnick] = nil unless msg =~ /^\$seen/
      end
    end
    l = @seen[cn = irc.channel.name] || (@seen[cn] = {})
    if msg =~ /^\001ACTION (.*)\001$/
      log(irc.channel.name, "Action: %s %s" % [irc.from.nnick, msg])
    else
      log(irc.channel.name, "<%s> %s" % [irc.from.nnick, msg])
    end
    l[irc.from.nnick] = [Time.now, LogPrivmsg, msg]
  end

  def hook_notice_chan(irc, msg)
    l = @seen[cn = irc.channel.name] || (@seen[cn] = {})
    l[irc.from.nnick] = [Time.now, LogNotice, msg]
  end

  def hook_join_chan(irc)
    l = @seen[cn = irc.channel.name] || (@seen[cn] = {})
    log(irc.channel.name, "%s has joined %s" % [irc.from.nnick, irc.channel.name])
    l[irc.from.nnick] = [Time.now, LogJoin]
  end

  def hook_part_chan(irc)
    l = @seen[cn = irc.channel.name] || (@seen[cn] = {})
    log(irc.channel.name, "PART: " + irc.from.nnick)
    l[irc.from.nnick] = [Time.now, LogPart]
  end

  def hook_command_serv(irc, handled, cmd, *args)
    if cmd == 'NICK'
      p [irc, handled, cmd, *args]
      @seen.each_value do |c|
        c[irc.from.nnick] = [Time.now, LogNick, args[0]]
      end
    end
  end
<commit_msg>Make Logger stop spewing out tons of info on each NICK command<commit_after>#
# Logging plugin for CyBot.
#

class Logger < PluginBase

  # We want these.
#  Channel_Instances = true

  # Some per-channel init.
  def initialize(*args)
    @brief_help = 'Records various channel activities.'
    @seen = {}
    @logs = {}
    @corrections_for = {}
    super(*args)
  end
  
  # Load/save database.
  def load
    begin
      @seen = YAML.load_file(file_name('seen.db'))
    rescue
      @seen = {}
    end
  end
  def save
    open_file('seen.db', 'w') do |f|
      f.puts '# CyBot logger plugin: Seen database.'
      YAML.dump(@seen, f)
      f.puts ''
    end
  end

  def log(channel, text)
    @logs[channel] ||= open_file(channel + '.txt', 'a')
    now = Time.now
    @logs[channel].puts("[%d:%d] %s" % [now.hour, now.min, text])
  end

  # Log types.
  LogPrivmsg  = 1
  LogNotice   = 2
  LogJoin     = 3
  LogPart     = 4
  LogNick     = 5

  # Hook for PRIVMSGs to a channel the bot is in.
  def hook_privmsg_chan(irc, msg)
    if @corrections_for[irc.from.nnick] and not @corrections_for[irc.from.nnick].empty?
      if msg == 'no'
        @corrections_for[irc.from.nnick].shift
        if @corrections_for[irc.from.nnick].size == 1
          irc.reply "last try: did you mean #{@corrections_for[irc.from.nnick].first.first}?"
        elsif not @corrections_for[irc.from.nnick].empty?
          irc.reply "did you mean #{@corrections_for[irc.from.nnick].first.first}?"
        end
      elsif msg == 'yes'
        chan_seen(irc, irc.channel, @corrections_for[irc.from.nnick].first.first)
      else
        @corrections_for[irc.from.nnick] = nil unless msg =~ /^\$seen/
      end
    end
    l = @seen[cn = irc.channel.name] || (@seen[cn] = {})
    if msg =~ /^\001ACTION (.*)\001$/
      log(irc.channel.name, "Action: %s %s" % [irc.from.nnick, msg])
    else
      log(irc.channel.name, "<%s> %s" % [irc.from.nnick, msg])
    end
    l[irc.from.nnick] = [Time.now, LogPrivmsg, msg]
  end

  def hook_notice_chan(irc, msg)
    l = @seen[cn = irc.channel.name] || (@seen[cn] = {})
    l[irc.from.nnick] = [Time.now, LogNotice, msg]
  end

  def hook_join_chan(irc)
    l = @seen[cn = irc.channel.name] || (@seen[cn] = {})
    log(irc.channel.name, "%s has joined %s" % [irc.from.nnick, irc.channel.name])
    l[irc.from.nnick] = [Time.now, LogJoin]
  end

  def hook_part_chan(irc)
    l = @seen[cn = irc.channel.name] || (@seen[cn] = {})
    log(irc.channel.name, "PART: " + irc.from.nnick)
    l[irc.from.nnick] = [Time.now, LogPart]
  end

  def hook_command_serv(irc, handled, cmd, *args)
    if cmd == 'NICK'
      @seen.each_value do |c|
        c[irc.from.nnick] = [Time.now, LogNick, args[0]]
      end
    end
  end

{'fraction_non_alphanumeric': 0.16208287495416207, 'fraction_numerical': 0.004767143381004767, 'mean_word_length': 2.5676913015042513, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '1021951', 'n_tokens_mistral': 2214, 'n_tokens_neox': 2088, 'n_words': 464}