Sha256: 4274ad7d21186b85e3fef31a7801e9e9efa8c913a118473ce73a9f15ad1d29c6

Contents?: true

Size: 1.93 KB

Versions: 15

Compression:

Stored size: 1.93 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)
      return if m.channel == '#staff'
      return if m.channel == '#netops'
      @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'))
        begin
          File.open(File.join(Dir.home, '.zeta', 'cache', 'seen.rb')) do |file|
            return Marshal.load(file)
          end
        rescue
          return Hash.new
        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

15 entries across 15 versions & 1 rubygems

Version Path
zetabot-1.0.7 lib/Zeta/plugins/seen.rb
zetabot-1.0.6 lib/Zeta/plugins/seen.rb
zetabot-1.0.5 lib/Zeta/plugins/seen.rb
zetabot-1.0.4 lib/Zeta/plugins/seen.rb
zetabot-1.0.3 lib/Zeta/plugins/seen.rb
zetabot-1.0.2 lib/Zeta/plugins/seen.rb
zetabot-1.0.1 lib/Zeta/plugins/seen.rb
zetabot-1.0.0 lib/Zeta/plugins/seen.rb
zetabot-0.0.22 lib/Zeta/plugins/seen.rb
zetabot-0.0.21 lib/Zeta/plugins/seen.rb
zetabot-0.0.19 lib/Zeta/plugins/seen.rb
zetabot-0.0.18 lib/Zeta/plugins/seen.rb
zetabot-0.0.17 lib/Zeta/plugins/seen.rb
zetabot-0.0.16 lib/Zeta/plugins/seen.rb
zetabot-0.0.15 lib/Zeta/plugins/seen.rb