Class: Lleidasms::Gateway

Inherits:
Object
  • Object
show all
Defined in:
lib/lleidasms/gateway.rb

Direct Known Subclasses

Client

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = 'sms.lleida.net', port = 2048) ⇒ Gateway

Client



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/lleidasms/gateway.rb', line 8

def initialize host = 'sms.lleida.net', port = 2048
  @host = host
  @port = port
  @my_label = 0

  # prepare global (scriptable) data
  $conected      = false
  $input_buffer  = Queue.new
  $output_buffer = String.new

  $reader      = lambda do |line|
    $input_buffer << line.strip
    parser
  end
  $writer      = lambda do |buffer|
    $server.puts "#{buffer}\r\n"
    puts "<#{buffer}\r\n"
    buffer.replace ''
  end
end

Class Method Details

.event(name, method_name) ⇒ Object



66
67
68
69
# File 'lib/lleidasms/gateway.rb', line 66

def self.event name, method_name
   $callbacks[name] = [] unless $callbacks[name]
  $callbacks[name] << method_name
end

Instance Method Details

#closeObject



53
54
55
# File 'lib/lleidasms/gateway.rb', line 53

def close
  cmd_quit
end

#close!Object



57
58
59
60
# File 'lib/lleidasms/gateway.rb', line 57

def close!
  $server.close
  $conected = false
end

#cmd_acuseack(id, label_response = new_label) ⇒ Object



247
248
249
# File 'lib/lleidasms/gateway.rb', line 247

def cmd_acuseack id, label_response = new_label
  $writer[label_response + " ACUSEACK #{id}"]
end

#cmd_acuseoff(label_response = new_label) ⇒ Object



243
244
245
# File 'lib/lleidasms/gateway.rb', line 243

def cmd_acuseoff label_response = new_label
  $writer[label_response + " ACUSEOFF"]
end

#cmd_acuseon(lang = false, cert = false, mode = 'INTERNAL', label_response = new_label) ⇒ Object

opts

- cert: true/false (default false) use certificated confirmations
    * false (default)
    * :d Default Service
- lang
    * :es Spanish (default)
    * :ca Catalan
    * :en English
    * :fr French
    * :de German
    * :it Italian
    * :nl Dutch
    * :pt Portuguese
    * :pl Polish
    * :se Swedish
- mode
    * email to send notification
    * INTERNAL to use ACUSE


237
238
239
240
241
# File 'lib/lleidasms/gateway.rb', line 237

def cmd_acuseon lang=false, cert=false, mode = 'INTERNAL', label_response = new_label
  l = lang ? "lang=#{lang.to_s.upcase} " : ''
  c = cert ? "cert_type=#{cert.to_s.upcase} " : ''
  $writer[label_response + " ACUSEON #{l}#{c}#{mode}"]
end

#cmd_allowanswer(allow = true, label_response = new_label) ⇒ Object

CMD Recepcion SMS (no premium)



211
212
213
# File 'lib/lleidasms/gateway.rb', line 211

def cmd_allowanswer allow = true, label_response = new_label
  $writer[label_response + " ALLOWANSWER " + (allow ? 'ON' : 'OFF')]
end

#cmd_bsubmit(number, message, label_response = new_label) ⇒ Object



139
140
141
# File 'lib/lleidasms/gateway.rb', line 139

def cmd_bsubmit number, message, label_response = new_label
  $writer[label_response + " BSUBMIT #{number} #{message}"]
end

#cmd_dst(numbers, label_response = new_label) ⇒ Object



163
164
165
# File 'lib/lleidasms/gateway.rb', line 163

def cmd_dst numbers, label_response = new_label
  $writer[label_response + " DST #{numbers}"]
end

#cmd_envia(label_response = new_label) ⇒ Object



205
206
207
# File 'lib/lleidasms/gateway.rb', line 205

def cmd_envia label_response = new_label
  $writer[label_response + " ENVIA"]
end

#cmd_fbsubmit(number, message, label_response = new_label) ⇒ Object



151
152
153
# File 'lib/lleidasms/gateway.rb', line 151

def cmd_fbsubmit number, message, label_response = new_label
  $writer[label_response + " FBSUBMIT #{number} #{message}"]
end

#cmd_filemsg(type, data, label_response = new_label) ⇒ Object

data file contenten base64 encoded Available types:

* :jpeg        image JPEG
* :gif         image GIF
* :midi        polyfonic melody MIDI
* :sp_midi     polyfonic melody SP-MIDI
* :amr         sound AMR
* :mp3         sound MP3
* :gpp         video 3GP
* :java        application JAVA
* :symbian     application Symbian


182
183
184
185
# File 'lib/lleidasms/gateway.rb', line 182

def cmd_filemsg type, data, label_response = new_label
  mime = mimetype type
  $writer[label_response + " FILEMSG #{mime} #{data}"] if mime
end

#cmd_fsubmit(number, message, label_response = new_label) ⇒ Object



147
148
149
# File 'lib/lleidasms/gateway.rb', line 147

def cmd_fsubmit number, message, label_response = new_label
  $writer[label_response + " FSUBMIT #{number} #{message}"]
end

#cmd_fusubmit(number, message, label_response = new_label) ⇒ Object



155
156
157
# File 'lib/lleidasms/gateway.rb', line 155

def cmd_fusubmit number, message, label_response = new_label
  $writer[label_response + " FUSUBMIT #{number} #{message}"]
end

#cmd_incomingmoack(m_id, label_response = new_label) ⇒ Object



215
216
217
# File 'lib/lleidasms/gateway.rb', line 215

def cmd_incomingmoack m_id, label_response = new_label
  $writer[label_response + " INCOMINGMOACK #{m_id}"]
end

#cmd_infonum(number, label_response = new_label) ⇒ Object



121
122
123
# File 'lib/lleidasms/gateway.rb', line 121

def cmd_infonum number, label_response = new_label
  $writer[label_response + " INFONUM #{number}"]
end

#cmd_login(user, password, label_response = new_label) ⇒ Object

CMD Generales



105
106
107
# File 'lib/lleidasms/gateway.rb', line 105

def  user, password, label_response = new_label
  $writer[label_response + " LOGIN #{user} #{password}"]
end

#cmd_mmsmsg(type, data, title, message, label_response = new_label) ⇒ Object

data file contenten base64 encoded

*title* Information title before downloading content
*message* Information text before downloading content

Available types:

* :jpeg        image JPEG
* :gif         image GIF
* :midi        polyfonic melody MIDI
* :sp_midi     polyfonic melody SP-MIDI
* :amr         sound AMR
* :mp3         sound MP3
* :gpp         video 3GP
* :java        application JAVA
* :symbian     application Symbian


200
201
202
203
# File 'lib/lleidasms/gateway.rb', line 200

def cmd_mmsmsg type, data, title, message, label_response = new_label
  mime = mimetype type
  $writer[label_response + " MMSMSG #{mime} #{data} #{title}|#{message}"] if mime
end

#cmd_msg(message, label_response = new_label) ⇒ Object



167
168
169
# File 'lib/lleidasms/gateway.rb', line 167

def cmd_msg message, label_response = new_label
  $writer[label_response + " MSG #{message}"]
end

#cmd_ping(time = Time.now.to_i.to_s, label_response = new_label) ⇒ Object



109
110
111
# File 'lib/lleidasms/gateway.rb', line 109

def cmd_ping time = Time.now.to_i.to_s, label_response = new_label
  $writer[label_response + " PING "+ time]
end

#cmd_pong(time = Time.now.to_i.to_s, label_response = new_label) ⇒ Object



113
114
115
# File 'lib/lleidasms/gateway.rb', line 113

def cmd_pong time = Time.now.to_i.to_s, label_response = new_label
  $writer[label_response + " PONG "+ time]
end

#cmd_quit(label_response = new_label) ⇒ Object



129
130
131
# File 'lib/lleidasms/gateway.rb', line 129

def cmd_quit label_response = new_label
  $writer[label_response + " QUIT"]
end

#cmd_raw(cmd, args, label_response = new_label) ⇒ Object



100
101
102
# File 'lib/lleidasms/gateway.rb', line 100

def cmd_raw cmd, args, label_response = new_label
  $writer[label_response + " #{cmd} #{args}"]
end

#cmd_saldo(label_response = new_label) ⇒ Object



117
118
119
# File 'lib/lleidasms/gateway.rb', line 117

def cmd_saldo label_response = new_label
  $writer[label_response + " SALDO"]
end

#cmd_submit(number, message, label_response = new_label) ⇒ Object

CMD Envios MT



135
136
137
# File 'lib/lleidasms/gateway.rb', line 135

def cmd_submit number, message, label_response = new_label
  $writer[label_response + " SUBMIT #{number} #{message}"]
end

#cmd_tarifa(number, label_response = new_label) ⇒ Object



125
126
127
# File 'lib/lleidasms/gateway.rb', line 125

def cmd_tarifa number, label_response = new_label
  $writer[label_response + " TARIFA #{number}"]
end

#cmd_trans(action, label_response = new_label) ⇒ Object

  • action

    * :begin
    * :end
    * :abort
    


255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/lleidasms/gateway.rb', line 255

def cmd_trans action, label_response = new_label
  type = case action
    when :begin
      'INICIAR'
    when :end
      'FIN'
    when :abort
      'ABORTAR'
    else
      return false
  end
  $writer[label_response + " TRANS #{type}"]
end

#cmd_usubmit(number, message, label_response = new_label) ⇒ Object



143
144
145
# File 'lib/lleidasms/gateway.rb', line 143

def cmd_usubmit number, message, label_response = new_label
  $writer[label_response + " USUBMIT #{number} #{message}"]
end


159
160
161
# File 'lib/lleidasms/gateway.rb', line 159

def cmd_waplink number, message, label_response = new_label
  $writer[label_response + " WAPLINK #{number} #{message}"]
end

#conected?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/lleidasms/gateway.rb', line 49

def conected?
  $conected
end

#connectObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/lleidasms/gateway.rb', line 29

def connect
  begin
    $server = TCPSocket.new @host, @port
    $conected = true
  rescue
    $conected = false
    puts "Unable to open a connection."
    exit
  end
end

#do_event(name) ⇒ Object



71
72
73
# File 'lib/lleidasms/gateway.rb', line 71

def do_event name
  run_event name.to_sym, @label, @cmd, @args
end

#last_labelObject



96
97
98
# File 'lib/lleidasms/gateway.rb', line 96

def last_label
  @my_label
end

#listenerObject



40
41
42
43
44
45
46
47
# File 'lib/lleidasms/gateway.rb', line 40

def listener
  Thread.new $server  do |socket|
    while line = socket.gets
      $reader[line]
    end
    close!
  end
end

#new_labelObject

Callbacks end



91
92
93
94
# File 'lib/lleidasms/gateway.rb', line 91

def new_label
  @my_label += 1
  @my_label.to_s
end

#parserObject

CMD Recepcion SMS (premium) CMD Recepcion SMS (premium) end



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/lleidasms/gateway.rb', line 273

def parser
  until $input_buffer.empty?
    line = $input_buffer.shift
    puts ">#{line}\r\n"
    @args = line.split ' '
    @label = @args.shift
    @cmd   = @args.shift
    case @cmd
    # CMD Generales
    when 'OK'
    when 'NOOK'
    when 'RSALDO'
    when 'PING'
      cmd_pong @args[0], @label
    when 'PONG'
      cmd_ping @args[0], @label
    when 'RINFONUM'
    when 'RTARIFA'
    when 'BYE'
      close!
    # CMD Envios MT
    when 'SUBMITOK'
    when 'BSUBMITOK'
    when 'USUBMITOK'
    when 'FSUBMITOK'
    when 'FBSUBMITOK'
    when 'FUSUBMITOK'
    when 'WAPLINKOK'
    when 'REJDST'
    when 'ACUSE'
    when 'ACUSEACKR'
    when 'RTRANS'
    # CMD Recepcion SMS (no premium)
    when 'INCOMINGMO'

    # CMD Recepcion SMS (premium)

    else
      # unknow
    end
    do_event @cmd.downcase
    do_event :all
  end
end

#run_event(name, *args) ⇒ Object



75
76
77
# File 'lib/lleidasms/gateway.rb', line 75

def run_event name, *args
  run_event_for name.to_sym, self, *args
end

#run_event_for(name, scope, *args) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/lleidasms/gateway.rb', line 79

def run_event_for name, scope, *args
  return unless $callbacks[name.to_sym]
  $callbacks[name.to_sym].each do |callback|
    if callback.kind_of? Symbol
      scope.send callback, *args
    else
      scope.instance_exec *args, &callback
    end
  end
end