Module: Drivel::DSL

Defined in:
lib/drivel/base.rb

Instance Method Summary collapse

Instance Method Details

#after(handler = nil, *guards, &block) ⇒ Object



30
31
32
# File 'lib/drivel/base.rb', line 30

def after(handler = nil, *guards, &block)
  filter(:after, handler, *guards, &block)
end

#before(handler = nil, *guards, &block) ⇒ Object



26
27
28
# File 'lib/drivel/base.rb', line 26

def before(handler = nil, *guards, &block)
  filter(:before, handler, *guards, &block)
end

#command(pattern, *arguments, &block) ⇒ Object

Raises:

  • (InvalidArgumentError)


46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/drivel/base.rb', line 46

def command(pattern, *arguments, &block)
  raise InvalidArgumentError unless pattern.is_a?(String) or pattern.is_a?(Regexp)

  safe_nickname   = Regexp.escape(settings[:nickname])
  optional_prefix = /(?:#{safe_nickname}[:,]? |[!$%@])/i

  pattern.gsub!(/((:\w+)|\*)/) do |key|
    (key == '*' ? "(?<splat>.*?)" : "(?<#{key[1..-1]}>[^\s]+)")
  end if pattern.is_a?(String)

  recognize(/\A#{optional_prefix}#{pattern}\Z/, &block)
end

#configureObject



17
18
19
# File 'lib/drivel/base.rb', line 17

def configure
  yield
end

#connected(&block) ⇒ Object



34
35
36
# File 'lib/drivel/base.rb', line 34

def connected(&block)
  handle(:ready, &block)
end

#disconnected(&block) ⇒ Object



38
39
40
# File 'lib/drivel/base.rb', line 38

def disconnected(&block)
  handle(:disconnected, &block)
end

#discover(type, target, node = nil, &block) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/drivel/base.rb', line 149

def discover(type, target, node = nil, &block)
  stanza = {
    info:  ::Blather::Stanza::DiscoInfo,
    items: ::Blather::Stanza::DiscoItems
  }[type].new

  stanza.node = node
  stanza.from = client.jid
  stanza.to   = target

  client.write_with_handler(stanza, &block)
end

#haltObject



145
146
147
# File 'lib/drivel/base.rb', line 145

def halt
  throw :halt
end

#helpers(*helpers, &block) ⇒ Object



21
22
23
24
# File 'lib/drivel/base.rb', line 21

def helpers(*helpers, &block)
  self.instance_eval(&block) if block_given?
  include(*helpers) unless helpers.empty?
end

#join(conference, &block) ⇒ Object



123
124
125
126
127
128
129
130
# File 'lib/drivel/base.rb', line 123

def join(conference, &block)
  room    = ::Blather::JID.new(conference)
  request = ::Blather::Stanza::Presence::MUC.new.tap do |stanza|
    target = ::Blather::JID.new(room.node, room.domain, settings[:nickname])
    stanza.to, stanza.type = target, nil
  end
  client.write_with_handler(request)
end

#leave(conference, &block) ⇒ Object



132
133
134
135
136
137
138
139
# File 'lib/drivel/base.rb', line 132

def leave(conference, &block)
  room = ::Blather::JID.new(conference)
  request = ::Blather::Stanza::Presence::MUC.new.tap do |stanza|
    target = ::Blather::JID.new(room.node, room.domain, settings[:nickname])
    stanza.to, stanza.type = target, :unavailable
  end
  client.write_with_handler(request, &block)
end

#passObject



141
142
143
# File 'lib/drivel/base.rb', line 141

def pass
  throw :pass
end

#ready?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/drivel/base.rb', line 71

def ready?
  jid? and password? and nickname?
end

#recognize(pattern, *arguments, &block) ⇒ Object

Raises:

  • (InvalidArgumentError)


59
60
61
62
63
64
65
66
67
68
69
# File 'lib/drivel/base.rb', line 59

def recognize(pattern, *arguments, &block)
  raise InvalidArgumentError unless pattern.is_a?(String) or pattern.is_a?(Regexp)

  matcher = Regexp.compile(pattern, true)

  handle(:message, :body => matcher) do |message|
    matches  = message.body.match(matcher)
    captures = matches.names.map(&:to_sym).zip(matches.captures)
    yield message, Hash[captures]
  end
end

#respond(message = nil, content = nil) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/drivel/base.rb', line 99

def respond(message = nil, content = nil)
  response = ::Blather::Stanza::Message.new.tap do |stanza|
    if message and content
      stanza.type, stanza.from = message.type, message.to

      case message.type
      when :groupchat
        stanza.to   = message.from.stripped
        stanza.body = message.from.resource + ': ' + content
      when :chat
        stanza.to   = message.from
        stanza.body = content
      else
        raise ArgumentError, 'Cannot respond to messages of type ' + message.type.to_s
      end
    end

    # Evaluate any given block, passing in this message before delivery.
    yield stanza if block_given?
  end

  EM.next_tick { client.write(response) }
end

#run!Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/drivel/base.rb', line 75

def run!
  client.setup(settings[:jid], settings[:password])

  # Reopen standard input and direct it at /dev/null.
  $stdin.reopen('/dev/null')

  # Capture interrupt and terminate and force them to shutdown the connection.
  [:INT, :TERM].each do |signal|
    trap(signal) { shutdown }
  end

  # Start.
  EM.run { client.run }
end

#schedule(interval, repeat = true, &block) ⇒ Object



162
163
164
165
166
167
168
169
170
# File 'lib/drivel/base.rb', line 162

def schedule(interval, repeat = true, &block)
  EM.next_tick do
    if repeat
      EM.add_periodic_timer(interval, &block)
    else
      EM.add_timer(interval, &block)
    end
  end
end

#set(key, *values) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/drivel/base.rb', line 5

def set(key, *values)
  if key.is_a?(Hash) and values.empty?
    key.each { |k, v| set(k, v) }
  else
    create_method("#{key}=") { |value| settings[key] = value }
    create_method("#{key}?") { !!settings[key] }
    create_method("#{key}")  { settings[key] }

    settings[key] = values.first
  end
end

#shutdownObject



95
96
97
# File 'lib/drivel/base.rb', line 95

def shutdown
  client.close
end

#status(mode, message) ⇒ Object



90
91
92
93
# File 'lib/drivel/base.rb', line 90

def status(mode, message)
  mode = nil if mode == :offline
  client.status = mode, message
end

#subscription(&block) ⇒ Object



42
43
44
# File 'lib/drivel/base.rb', line 42

def subscription(&block)
  handle(:subscription, &block)
end