summaryrefslogtreecommitdiff
path: root/contrib/smsweb/smswsc.rb
blob: 1800e135ee1fecd64cf21f7c15d5e04e1b254544 (plain)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#!/usr/bin/env ruby1.8
#
# vim: set sw=2 ts=2 expandtab:
#
# SMS Web Service client example library
#
# Copyright (C) 2010 by James Maki
#
# Author: James Maki <jamescmaki@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#

require 'rubygems'
require 'net/http'
require 'cgi'
require 'inform'
require 'smswsc/utils'
require 'smswsc/phonebook_entry'
require 'smswsc/sms_message'

module SMSWSC
  module Error
    class General < RuntimeError; end
    class HTTP < General; end
    class XML < General; end
    class BadRequest < General; end
    class Unauthorized < General; end
    class Forbidden < General; end
    class NotFound < General; end
    class ServiceUnavailable < General; end
  end

  SMSWS_PATH = "/smsws/v1"
  PHONEBOOK_PATH = Utils.ref(SMSWS_PATH, "phonebook")
  MESSAGES_PATH = Utils.ref(SMSWS_PATH, "messages")
  SEND_PATH = Utils.ref(SMSWS_PATH, "send")

  class Client
    include Utils

    DEFAULT_PORT = 5857
    VERSION = '0.1'

    attr_accessor :use_ssl
    attr_accessor :http_debug
    attr_accessor :http_read_timeout

    def initialize(username, passwd, host, port = DEFAULT_PORT)
      @host = host
      @port = port
      @username = username
      @passwd = passwd
      @error = ""
      @headers = {
         'Content-Type' => 'application/xml',
         'User-Agent' => "smswsc/#{VERSION}",
      }
      @use_ssl = false
      @http_debug = false
      @http_read_timeout = 300
    end

    def user_agent(ua)
      @headers['User-Agent'] = ua
    end

    def phonebook_query(path = PHONEBOOK_PATH)
      case path
      when PhonebookEntry
        path = path.uri.to_s
      when String
      when URI
        path = path.to_s
      else
        raise ArgumentError, "Invalid param type: #{path.class}"
      end

      phonebook = []

      response = get_resource(path)

      root = process_response(response, Net::HTTPOK)

      root.elements.each("phonebook-entry") do |entry|
        phonebook << PhonebookEntry.load_xml(entry)
      end 

      return phonebook
    end

    def sms_messages_query(path = MESSAGES_PATH)
      case path
      when SMSMessage
        path = path.uri.to_s
      when String
      when URI
        path = path.to_s
      else
        raise ArgumentError, "Invalid param type: #{path.class}"
      end

      msgs = []

      response = get_resource(path)

      root = process_response(response, Net::HTTPOK)

      root.elements.each("sms-message") do |entry|
        msgs << SMSMessage.load_xml(entry)
      end 

      return msgs
    end

    def sms_messages_delete(path = MESSAGES_PATH)
      case path
      when SMSMessage
        path = path.uri.to_s
      when String
      when URI
        path = path.to_s
      else
        raise ArgumentError, "Invalid param type: #{path.class}"
      end

      response = delete_resource(path)

      root = process_response(response, Net::HTTPOK)

      return true
    end

    def sms_send(msg)
      response = post_resource(SEND_PATH, msg.to_xml)

      root = process_response(response, Net::HTTPCreated)

      return true
    end

    private

    def process_response(response, klass)
      root = response_xml(response)
      if root
        message = root.elements['status-message'].text
      else
        message = "response message not given"
      end

      unless response.instance_of?(klass)
        if response.instance_of?(Net::HTTPBadRequest)
          raise Error::BadRequest, "Bad Request: " + message
        elsif response.instance_of?(Net::HTTPUnauthorized)
          raise Error::Unauthorized, "Unauthorized: " + message
        elsif response.instance_of?(Net::HTTPForbidden)
          raise Error::Forbidden, "Forbidden: " + message
        elsif response.instance_of?(Net::HTTPNotFound)
          raise Error::NotFound, "Not Found: " + message
        elsif response.instance_of?(Net::HTTPServiceUnavailable)
          raise Error::ServiceUnavailable, "Service Unavailable: " + message
        else
          raise Error::General, "Failed to fulfill request: " + message
        end
      end

      return root
    end

    def response_xml(response)
      return nil unless response.content_type.to_s.downcase == 'application/xml'

      begin
        document = REXML::Document.new(response.body)
        root = document.root
      rescue REXML::ParseException => e
        Inform.err(e.to_s)
        raise Error::XML, "failed to parse response XML"
      end

      return root
    end

    def authorize(request)
      if @username && @passwd
        request.basic_auth(@username, @passwd)
      end
    end

    def parse_qpath(url)
      url = URI.parse(url)
      qpath = url.path
      if url.query
        qpath += "?" + url.query
      end

      return qpath
    end

    def net_http
      http = Net::HTTP.new(@host, @port)

      http.read_timeout = @http_read_timeout
      http.set_debug_output($stderr) if @http_debug

      if @use_ssl
        http.use_ssl = @use_ssl
        http.verify_mode = OpenSSL::SSL::VERIFY_NONE
      end

      return http
    end

    def post_resource(ref, body, headers = {})
      headers = @headers.merge(headers)
      qpath = parse_qpath(ref)

      begin
        request = Net::HTTP::Post.new(qpath, headers)
        authorize(request)

        response = net_http.start do |http|
          http.request(request, body)
        end
      rescue => e
        Inform.err("POST #{ref} failed: #{e.class}: #{e}")
        raise Error::HTTP, "POST #{ref} failed: #{e}"
      end

      return response
    end

    def delete_resource(ref, headers = {})
      headers = @headers.merge(headers)
      qpath = parse_qpath(ref)

      begin
        request = Net::HTTP::Delete.new(qpath, headers)
        authorize(request)

        response = net_http.start do |http|
          http.request(request)
        end
      rescue => e
        Inform.err("DELETE #{ref} failed: #{e.class}: #{e}")
        raise Error::HTTP, "DELETE #{ref} failed: #{e}"
      end

      return response
    end

    def get_resource(ref, headers = {})
      headers = @headers.merge(headers)
      qpath = parse_qpath(ref)

      begin
        request = Net::HTTP::Get.new(qpath, headers)
        authorize(request)

        response = net_http.start do |http|
          http.request(request)
        end
      rescue => e
        Inform.err("GET #{ref} failed: #{e.class}: #{e}")
        raise Error::HTTP, "GET #{ref} failed: #{e}"
      end

      return response
    end

    def put_resource(ref, body, headers = {})
      headers = @headers.merge(headers)
      qpath = parse_qpath(ref)

      begin
        request = Net::HTTP::Put.new(qpath, headers)
        authorize(request)

        response = net_http.start do |http|
          http.request(request, body)
        end
      rescue => e
        Inform.err("PUT #{ref} failed: #{e.class}: #{e}")
        raise Error::HTTP, "PUT #{ref} failed: #{e}"
      end

      return response
    end
  end
end

if $0 == __FILE__ then
  SMSWS_ROOT = File.expand_path(File.dirname(__FILE__))

  Dir.chdir(SMSWS_ROOT)

  SMSWS_CONFIG = "#{SMSWS_ROOT}/sms.config"
  $config = YAML::load_file(SMSWS_CONFIG)

  ENV['SMS_CONFIG'] = "#{SMSWS_CONFIG}"

  Inform.syslog = false
  if Inform.syslog
    Syslog.open("smswsc", Syslog::LOG_NDELAY, Syslog::LOG_LOCAL7)
  else
    Inform.level = Inform::LOG_ERR
    #Inform.level = Inform::LOG_DEBUG
    Inform.file = $stdout
  end

  client = SMSWSC::Client.new(
    $config["smsws"]["user"],
    $config["smsws"]["passwd"],
    "192.168.2.1",
    $config["smsws"]["port"]
  )

  msgs = client.sms_messages_query
  p msgs

  pb = client.phonebook_query
  p pb

  msg = SMSWSC::SMSMessage.new
  msg.addr = "jcm"
  msg.user_data = "test me web by name"
  client.sms_send(msg)
end