summaryrefslogtreecommitdiff
path: root/bocelli/base.rb
blob: bc7e6179353cc13dabec72ee98a6d98104e5acd8 (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
require_relative 'core/irc'

module Bocelli
  class Base
    def initialize(str, route, block, metadata)
      @bocelli = {
        str: str,
        route: route,
        block: block,
        metadata: metadata
      }
    end

    def execute
      instance_eval(&@bocelli[:block])
    end

    def out(message)
      self.class.privmsg(@bocelli[:metadata][:channel], message)
    end

    class << self
      include Bocelli::Core::IRC

      def setup
        @routes = {}
        @modules = {}
      end

      def inherited(subclass)
        super

        subclass.setup
      end

      def on(route, &block)
        @routes[route] = block
      end

      def register(mod)
        @modules[mod.name[/[^:]+$/].downcase.intern] ||= mod
      end

      def match?(str, route)
        case route
        when Regexp
          str =~ route
        when String
          str == route
        end
      end

      def match(str)
        @routes.detect { |k, _| match?(str, k) }
      end

      def mod_match(str)
        if str =~ /\A(\S+) (.*)/
          if (mod = Hash[@modules.map { |k, v| [k.to_s, v] }][$1])
            mod.match($2)
          end
        end
      end

      def process(str)
        if str =~ /\A:?(\S+) PRIVMSG (\S+) :?(.*)/
          metadata = {
            user: $1,
            channel: $2,
            message: $3
          }

          if (match = match($3)) || (match = mod_match($3))
            route, block = match

            new(str, route, block, metadata).execute
          end
        end
      end

      def run
        while (str = sgets)
          pong($1) if str =~ /\APING (.*)\z/

          process(str)
        end
      end
    end
  end
end