aboutsummaryrefslogtreecommitdiff
path: root/module/admin.rb
blob: 72c2a14f1bc048844700f8aecb417c862090d85d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
require_relative 'base'
require_relative '../core/store'

module Dinobot
  module Module
    class Admin < Base
      def initialize(bot)
        super

        @store = Dinobot::Core::Store.new('data/admin')

        @commands << :join << :part << :quit << :load << :unload
        @commands << :listadmins << :listmodules << :listchannels

        @admins = @store.data[:admins]
        @admins ||= Array.new
      end

      def add(user)
        @admins << user unless @admins.include?(user)

        @store.data[:admins] = @admins
        @store.save
      end

      def remove(user)
        @admins.delete(user)

        @store.data[:admins] = @admins
        @store.save
      end

      def is_admin?(user)
        # FIXME: Using hostname for testing purposes. Need better solution.
        @admins.include?(user.sub(/.+@/, ''))
      end

      def join(m, args)
        return unless is_admin?(m.user)

        m.response << [:join, args.strip]
      end

      def part(m, args)
        return unless is_admin?(m.user)

        m.response << [:part, args.strip]
      end

      def quit(m, args)
        return unless is_admin?(m.user)

        m.response << [:quit, args ? args.strip : 'Quitting.']
      end

      def load(m, args)
        return unless is_admin?(m.user)

        args.split.each do |x|
          @bot.load_module x.intern
        end
      end

      def unload(m, args)
        return unless is_admin?(m.user)

        args.split.each do |x|
          @bot.unload_module x.intern
        end
      end

      def listadmins(m, args)
        return unless is_admin?(m.user)

        m.response << [:say, m.channel, "Admins: #{@admins.join(' ')}"]
      end

      def listmodules(m, args)
        return unless is_admin?(m.user)

        m.response << [:say, m.channel,
          "Modules: #{@bot.modules.keys.sort.join(' ')}"]
      end

      def listchannels(m, args)
        return unless is_admin?(m.user)

        m.response << [:say, m.channel,
          "Channels: #{@bot.channels.sort.join(' ')}"]
      end
    end
  end
end