Name: gateway-mail/server 
1:
#!/usr/bin/ruby
2:
# Apache License, Version 2.0
3:
#
4:
# Copyright (c) 2013 Juergen Mangler
5:
#
6:
# Licensed under the Apache License, Version 2.0 (the "License");
7:
# you may not use this file except in compliance with the License.
8:
# You may obtain a copy of the License at
9:
#
10:
#     http://www.apache.org/licenses/LICENSE-2.0
11:
#
12:
# Unless required by applicable law or agreed to in writing, software
13:
# distributed under the License is distributed on an "AS IS" BASIS,
14:
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15:
# See the License for the specific language governing permissions and
16:
# limitations under the License.
17:
 
18:
require 'rubygems'
19:
require 'mail'
20:
require 'riddl/server'
21:
require 'xml/smart'
22:
require 'fileutils'
23:
require 'erb'
24:
require 'xmlsimple'
25:
 
26:
class Namespace
27:
  def initialize(hash)
28:
    tmp = XmlSimple.xml_in(hash)
29:
    if tmp.is_a?(Hash)
30:
      tmp.each do |key, value|
31:
        singleton_class.send(:define_method, key) { value }
32:
      end
33:
    else  
34:
      singleton_class.send(:define_method, 'data') { tmp }
35:
    end
36:
  end
37:
 
38:
  def get_binding
39:
    binding
40:
  end
41:
end
42:
 
43:
class SendMail < Riddl::Implementation
44:
  def response
45:
    doc = XML::Smart.string(@p[0].value.read)
46:
    doc.register_namespace 'g', 'http://www.fp7-adventure.eu/xmlSchema/Gateways/'
47:
    mop = doc.find('string(//g:header/g:operationName)')
48:
    mto = doc.find('string(//g:payload/g:inputParameter[@paramName="to"])')
49:
    msubject = doc.find('string(//g:payload/g:inputParameter[@paramName="subject"])')
50:
    mbody = doc.find('string(//g:payload/g:inputParameter[@paramName="body"])')
51:
    data = doc.find('string(//g:payload/g:inputParameter[@paramName="data"])')
52:
 
53:
    ns = Namespace.new(data)
54:
    mbody = ERB.new(mbody.gsub("{{", "<%=").gsub("}}", "%>")).result(ns.get_binding)
55:
    msubject = ERB.new(msubject.gsub("{{", "<%=").gsub("}}", "%>")).result(ns.get_binding)
56:
 
57:
    if mop == 'mail' && mto.strip != ''
58:
      mail = Mail.new do
59:
        from     'ADVENTURE Mail Gateway <[email protected]>'
60:
        to       mto
61:
        subject  msubject
62:
        body     mbody
63:
      end
64:
 
65:
      mail.delivery_method :smtp,
66:
        :address => "india672.server4you.de",
67:
        :port => 587,
68:
        :user_name => 'mailman',
69:
        :password => 'rf4TsJYvlB',
70:
        :authentication => 'plain',
71:
        :enable_starttls_auto => true
72:
 
73:
      mail.deliver
74:
    end
75:
  
76:
    client = Riddl::Client.interface("xmpp://" + @h['CPEE_CALLBACK'], File.dirname(__FILE__) + "/callback.xml", :xmpp => @env['xmpp'])
77:
    client.put Riddl::Parameter::Complex.new("gatewayResponse","text/xml",File.read(File.dirname(__FILE__) + "/gatewayresponse.xml"))
78:
    nil
79:
  end
80:
end
81:
 
82:
Riddl::Server.new(File.dirname(__FILE__) + '/gateway.xml', :port => 9302) do
83:
  xmpp '[email protected]', 'adventure_gateway_mail'
84:
  accessible_description true
85:
  cross_site_xhr true
86:
 
87:
  on resource do
88:
    run SendMail if post 'gateway-request'
89:
  end
90:
end.loop!