Class: HWPing::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/hwping/handler.rb

Instance Method Summary collapse

Constructor Details

#initialize(launcher, config) ⇒ Handler

Set up the handler with the configuration



4
5
6
7
8
9
10
# File 'lib/hwping/handler.rb', line 4

def initialize(launcher, config)
  @targets    = config.targets
  @auth       = config.auth
  @nick       = config.nick
  @webcam     = config.webcam
  @launcher   = launcher
end

Instance Method Details

#channel(event) ⇒ Object

If the event happened in a channel, return just with a symbol



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/hwping/handler.rb', line 13

def channel(event)
  if event.message =~ /^hwping,?\s+(.*)\s*$/
    if authorized?(event.user.to_s)
      if @targets.include?(Regexp.last_match(1))
        @launcher.point_and_fire(@targets[Regexp.last_match(1)])
        return :firing
      else
        return :notarget
      end
    else
      return :unauthorized
    end
  end
end

#private(event) ⇒ Object

If the event happened in a private window, return with an array



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
# File 'lib/hwping/handler.rb', line 29

def private(event)
  if authorized?(event.user.to_s)
    case event.message
    when 'snap'
      return [:image, ip_address(event), Webcam.new(@webcam).save]
    when 'panorama'
      return [:image, ip_address(event), panorama]
    when 'fire'
      @launcher.fire
      return [:fire]
    when 'position'
      return @launcher.position.unshift(:position)
    when 'help'
      return [:help]
    when 'reset'
      @launcher.reset
      return [:reset]
    when 'target list'
      return [:target_list, @targets.keys.join(', ')]
    when /^target set ([^\s]+)( (\d+) (\d+))?$/
      @targets[Regexp.last_match(1)] = begin
        if Regexp.last_match(2)
          [Regexp.last_match(3).to_i, Regexp.last_match(4).to_i]
        else
          @launcher.position
        end
      end
      return [:target_set, @targets[Regexp.last_match(1)]].flatten
    when /^target ((get)|(del)) ([^\s]+)$/
      if @targets.key?($+)
        if Regexp.last_match(1) == 'del'
          @targets.delete($+)
          return [:target_del]
        else
          return [:target_get, @targets[$+]].flatten
        end
      else
        return [:notarget]
      end
    when /^((up)|(down)|(left)|(right)) (\d+)$/
      @launcher.send(Regexp.last_match(1), $+.to_i)
      return [:move]
    else
      return [:badcommand]
    end
  else
    return [:unauthorized]
  end
end