Sha256: 04b1833d98ddf835cb81008f63c9887b70823ef4f52be970047f35ef5830142c

Contents?: true

Size: 1.78 KB

Versions: 4

Compression:

Stored size: 1.78 KB

Contents

require 'action_view'
module Plugins
  class Seen
    class SeenStruct < Struct.new(:who, :where, :what, :time)
      include ActionView::Helpers::DateHelper
      def to_s
        # "[#{time.asctime}] #{who} was seen in #{where} last saying #{what}"
        time_ago = time_ago_in_words(Time.at(time))
        "[ \x1F#{where.to_s.upcase}\x0F ] \x0304#{who}\x0F: \"\x0303#{what[0..300]}\x0F\" \x02#{time_ago}\x0F ago"
      end
    end

    include Cinch::Plugin
    include Cinch::Helpers

    enable_acl(:nobody)

    listen_to :channel
    match /seen (.+)/
    match /sync/, method: :sync

    def initialize(*args)
      super
      @users = load_seen
    end

    def finalize
      save_seen()
    end

    def listen(m)
      @users[m.user.nick] = SeenStruct.new(m.user, m.channel, m.message, Time.now)
    end

    def execute(m, nick)
      nick.rstrip!
      if nick == @bot.nick
        m.reply 'You are a Stupid human!'
      elsif nick == m.user.nick
        m.reply "Unfortunately, I see an idiot by the name of #{m.user.nick}"
      elsif @users.key?(nick)
        m.reply @users[nick].to_s
      else
        m.reply "I haven't seen #{nick}"
      end
    end

    def sync(m)
      save_seen()
    end

    def save_seen
      File.open(File.join(Dir.home, '.zeta', 'cache', 'seen.rb'), 'w+') do |file|
        Marshal.dump(@users, file)
      end

    end

    def load_seen
      if File.exists?(File.join(Dir.home, '.zeta', 'cache', 'seen.rb'))
        File.open(File.join(Dir.home, '.zeta', 'cache', 'seen.rb')) do |file|
          return Marshal.load(file)
        end
      else
        return Hash.new
      end
    end

    def clear_seen
      @users = {}
      File.delete(File.join(Dir.home, '.zeta', 'cache', 'seen.rb'))
    end
  end
end

# AutoLoad
Bot.config.plugins.plugins.push Plugins::Seen

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
zetabot-0.0.4 lib/Zeta/plugins/seen.rb
zetabot-0.0.3 lib/Zeta/plugins/seen.rb
zetabot-0.0.2 lib/Zeta/plugins/seen.rb
zetabot-0.0.1 lib/Zeta/plugins/seen.rb