asterisk-mailcmd 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,119 @@
1
+ require 'mail'
2
+ require 'erb'
3
+
4
+ module Asterisk
5
+ module Mailcmd
6
+ class InvalidTxtTmpl < StandardError;end
7
+ class InvalidHtmlTmpl < StandardError;end
8
+
9
+ class Email
10
+ attr_accessor :rawdata,
11
+ :htmlpart,
12
+ :textpart,
13
+ :mail,
14
+ :astvars,
15
+ :charset,
16
+ :delivery_method,
17
+ :delivery_params
18
+
19
+ class << self
20
+ # Asterisk will send generated text to STDIN
21
+ # Read it and store in instance variable
22
+ def read
23
+ Email.new
24
+ end
25
+
26
+ def set_and_send params
27
+ email = Email.read
28
+ email.extract_vars
29
+ email.set_date params[:date] unless params[:date].nil?
30
+ email.charset = params[:charset] unless params[:charset].nil?
31
+ # set templates
32
+ raise InvalidTxtTmpl, "Text template parameter required" if params[:text_tmpl].nil?
33
+ raise InvalidHtmlTmpl, "HTML template parameter required" if params[:html_tmpl].nil?
34
+ Settings.read params[:text_tmpl], :text
35
+ Settings.read params[:html_tmpl]
36
+ email.html_tmpl Settings.html_tmpl
37
+ email.text_tmpl Settings.text_tmpl
38
+ # delivery
39
+ if params[:delivery_method]
40
+ delivery_params = params[:delivery_params] || {}
41
+ email.set_delivery params[:delivery_method], delivery_params
42
+ end
43
+ email.deliver
44
+ end
45
+ end
46
+
47
+ def initialize
48
+ @rawdata = $stdin.read
49
+ @mail = Mail.read_from_string(@rawdata)
50
+ set_delivery
51
+ end
52
+
53
+ # Valid raw email must endin
54
+ # with full stop (period) on new line
55
+ def raw_valid?
56
+ @rawdata.split.pop.eql? ?.
57
+ end
58
+
59
+ # extract variables from email body
60
+ def extract_vars
61
+ @astvars = Hash.new
62
+ @mail.text_part.body.decoded.lines do |line|
63
+ if /^(?<name>[^:]+):(?<val>.*)$/ =~ line.chomp
64
+ @astvars[name.to_sym] = val
65
+ end
66
+ end
67
+ end
68
+
69
+ def tmpl tmpl_text, type = :html
70
+ tmpl = ERB.new(tmpl_text)
71
+ raise ArgumentError, "No Asterisk variables set" if @astvars.nil?
72
+ instance_variable_set "@#{type}part",
73
+ tmpl.result(binding) rescue raise ArgumentError, "Invalid type #{type.to_sym}"
74
+ end
75
+
76
+ def html_tmpl tmpl_text
77
+ tmpl tmpl_text, :html
78
+ data = @htmlpart
79
+ cnt_type = 'text/html; charset='<<get_charset
80
+ @mail.html_part = Mail::Part.new do
81
+ content_type cnt_type
82
+ body data
83
+ end
84
+ end
85
+
86
+ def text_tmpl tmpl_text
87
+ tmpl tmpl_text, :text
88
+ data = @textpart
89
+ @mail.text_part = Mail::Part.new do
90
+ body data
91
+ end
92
+ end
93
+
94
+ def set_date date = nil
95
+ if date.nil?
96
+ @mail.date Time.now
97
+ else
98
+ raise ArgumentError, "Invalid date" unless date.is_a? Time
99
+ @mail.date date
100
+ end
101
+ end
102
+
103
+ def deliver
104
+ @mail.delivery_method @delivery_method, @delivery_params
105
+ @mail.deliver
106
+ end
107
+
108
+ def set_delivery method = :sendmail, params = {}
109
+ @delivery_method = method
110
+ @delivery_params = params
111
+ end
112
+
113
+ def get_charset
114
+ @charset || 'UTF-8'
115
+ end
116
+
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,30 @@
1
+ require 'singleton'
2
+
3
+ module Asterisk
4
+ module Mailcmd
5
+ class Settings
6
+ include Singleton
7
+ class << self
8
+
9
+ attr :html_tmpl, true
10
+ attr :text_tmpl, true
11
+
12
+ # reading config file
13
+ def read(file, type = :html)
14
+ raise ArgumentError, "Config file #{file} does not exists" unless File.exists? file
15
+ raise ArgumentError, "Can not read config file #{file}" unless File.readable? file
16
+
17
+ case type
18
+ when :html
19
+ @html_tmpl = File.read file
20
+ when :text
21
+ @text_tmpl = File.read file
22
+ else
23
+ raise ArgumentError, "Invalid template type :#{type.to_s}"
24
+ end
25
+ end
26
+
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,5 @@
1
+ module Asterisk
2
+ module Mailcmd
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
Binary file
@@ -0,0 +1,96 @@
1
+ <html>
2
+ <body>
3
+ <div style="">
4
+ <table cellspacing="0" cellpadding="8" border="0" summary=""
5
+ style=
6
+ "width:100%;font-family:Arial,Sans-serif;border:1px Solid #ccc;border-width:1px 2px 2px 1px;background-color:#fff;">
7
+ <tr>
8
+ <td>
9
+ <div style="padding:2px">
10
+ <div style="float:right;font-weight:bold;font-size:13px">
11
+ <a href="http://userportal.example.com/?msg_id=MESSAGE_ID"
12
+ style="color:#20c;white-space:nowrap">
13
+ more details &raquo;</a>
14
+ </br>
15
+ </div>
16
+
17
+ <h3 style="padding:0 0 6px 0;margin:0;font-family:Arial,Sans-serif;font-size:16px;font-weight:bold;color:#222">
18
+ New voicemail message
19
+ </h3>
20
+
21
+ <div style="padding-bottom:15px;font-size:13px;color:#222;white-space:pre-wrap!important;white-space:-moz-pre-wrap!important;white-space:-pre-wrap!important;white-space:-o-pre-wrap!important;white-space:pre;word-wrap:break-word">
22
+ There is a new voicemail in mailbox MAILBOX.
23
+ Original message is attached as WAV file.
24
+ </div>
25
+
26
+ <table cellpadding="0" cellspacing="0" border="0">
27
+ <tr>
28
+ <td style="padding:0 1em 10px 0;font-family:Arial,Sans-serif;font-size:13px;color:#888;white-space:nowrap" valign="top">
29
+ <div>
30
+ <i style="font-style:normal">Date/Time</i>
31
+ </div>
32
+ </td>
33
+ <td style="padding-bottom:10px;font-family:Arial,Sans-serif;font-size:13px;color:#222" valign="top">
34
+ <%= @astvars[:VM_DATE].to_s %>
35
+ </td>
36
+ </tr>
37
+
38
+ <tr>
39
+ <td style="padding:0 1em 10px 0;font-family:Arial,Sans-serif;font-size:13px;color:#888;white-space:nowrap" valign="top">
40
+ <div>
41
+ <i style="font-style:normal">From</i>
42
+ </div>
43
+ </td>
44
+ <td style="padding-bottom:10px;font-family:Arial,Sans-serif;font-size:13px;color:#222" valign="top">
45
+ <%= @astvars[:VM_CALLERID] %>
46
+ </td>
47
+ </tr>
48
+
49
+ <tr>
50
+ <td style="padding:0 1em 10px 0;font-family:Arial,Sans-serif;font-size:13px;color:#888;white-space:nowrap" valign="top">
51
+ <div>
52
+ <i style="font-style:normal">Duration</i>
53
+ </div>
54
+ </td>
55
+ <td style="padding-bottom:10px;font-family:Arial,Sans-serif;font-size:13px;color:#222" valign="top">
56
+ <%= @astvars[:VM_DUR] %>
57
+ </td>
58
+ </tr>
59
+
60
+ <tr>
61
+ <td style="padding:0 1em 10px 0;font-family:Arial,Sans-serif;font-size:13px;color:#888;white-space:nowrap" valign="top">
62
+ <div>
63
+ <i style="font-style:normal">Total messages</i>
64
+ </div>
65
+ </td>
66
+ <td style="padding-bottom:10px;font-family:Arial,Sans-serif;font-size:13px;color:#222" valign="top">
67
+ <%= @astvars[:VM_MSGNUM] %>
68
+ </td>
69
+ </tr>
70
+
71
+ </table>
72
+ </div>
73
+ </td>
74
+ </tr>
75
+
76
+ <tr>
77
+ <td style="background-color:#f6f6f6;color:#888;border-top:1px Solid #ccc;font-family:Arial,Sans-serif;font-size:11px">
78
+ <p>
79
+ Asterisk voicemail system.
80
+ </p>
81
+
82
+ <p>
83
+ This is a private message. If you have received it
84
+ by mistake or if you have any questions please contact techinacal support: 555-SUPPORT
85
+ </p>
86
+
87
+ <p>
88
+ To manage your voicemail messages please visit
89
+ <a href="http://userportal.example.com/">http://userportal.example.com</a>
90
+ </p>
91
+ </td>
92
+ </tr>
93
+ </table>
94
+ </div>
95
+ </body>
96
+ </html>
@@ -0,0 +1,16 @@
1
+ #!/bin/sh
2
+
3
+ # This is as simple wrapper for ruby application asterisk-mailcmd
4
+ # when using RVM (Ruby Version Manager: https://rvm.io/)
5
+
6
+ # path to HTML and TEXT templates in ERB format
7
+ # see samples in "sample" directory
8
+ HTML_TMPL=/etc/asterisk/vmtemplate/html.erb
9
+ TEXT_TMPL=/etc/asterisk/vmtemplate/text.erb
10
+
11
+ # load RVM profile
12
+ source /etc/profile.d/rvm.sh
13
+ # run mail command
14
+ asterisk-mailcmd -t $TEXT_TMPL -m $HTML_TMPL
15
+
16
+ exit 0
@@ -0,0 +1,19 @@
1
+ New voicemail message
2
+
3
+ There is a new voicemail in mailbox MAILBOX.
4
+ Original message is attached as WAV file.
5
+
6
+ Date/Time : <%=@astvars[:VM_DATE] %>
7
+ From : <%=@astvars[:VM_CALLERID] %>
8
+ Duration : <%=@astvars[:VM_DUR] %>
9
+ Total messages: <%=@astvars[:VM_MSGNUM] %>
10
+
11
+ Asterisk voicemail system.
12
+
13
+ **************************************************
14
+ This is a private message. If you have received it
15
+ by mistake or if you have any questions please
16
+ contact techinacal support: 555-SUPPORT
17
+
18
+ To manage your voicemail messages please visit
19
+ http://userportal.example.com
@@ -0,0 +1,43 @@
1
+ Date: Sun, 17 Mar 2013 16:55:00 -0400
2
+ From: Asterisk PBX <asterisk@localhost.localdomain>
3
+ To: "Mark Spencer" <mark.spenser@digium.local>
4
+ Subject: [PBX]: New message 6 in mailbox 55555
5
+ Message-ID: <Asterisk-6-1168436456-55555-10625@localhost.localdomain>
6
+ X-Asterisk-CallerID: 2275550168
7
+ X-Asterisk-CallerIDName: John Doe
8
+ MIME-Version: 1.0
9
+ Content-Type: multipart/mixed; boundary="----voicemail_655555106251918236316"
10
+
11
+
12
+ This is a multi-part message in MIME format.
13
+
14
+ ------voicemail_655555106251918236316
15
+ Content-Type: text/plain; charset=ISO-8859-1
16
+ Content-Transfer-Encoding: 8bit
17
+
18
+ VM_NAME:Mark Spencer
19
+ VM_DUR:0:01
20
+ VM_MSGNUM:6
21
+ VM_MAILBOX:55555
22
+ VM_CALLERID:"John Doe" <2275550168>
23
+ VM_CIDNUM:2275550168
24
+ VM_CIDNAME:John Doe
25
+ VM_DATE:Sunday, March 17, 2013 at 04:55:00 PM
26
+ ------voicemail_655555106251918236316
27
+ Content-Type: audio/x-WAV; name="msg0005.WAV"
28
+ Content-Transfer-Encoding: base64
29
+ Content-Description: Voicemail sound attachment.
30
+ Content-Disposition: attachment; filename="msg0005.WAV"
31
+
32
+ UklGRnkBAABXQVZFZm10IBQAAAAxAAEAQB8AAFkGAABBAAAAAgBAAWZhY3QEAAAAQAYAAGRh
33
+ dGFFAQAA3JaGgYJigCRJkiSHIoAbSZIcpyKAJEmSJCclgCRJkiR3WayUKKoGuJEkSXLSAEiS
34
+ JEmSsAVGbtu2bc0ESJIkSXKXxUqJUk+AJEmSJBcdgNu2bdsGbYAkSZIkhwqAJEmSJHlZrJQo
35
+ sARIkiRJctgESJIkSXKxBEiSJEly2QRIkiRJcpfFSokSS4AkSZIkF0uAJEmSJBdLgCRJkiQX
36
+ S4AkSZIkB27PlCjIBkiSJElyMAK4bdu2bTAAuJEkSZJYALiRJEmS6FgZ1iQjgBtJkiSHJIAk
37
+ SZIkByeAJEmSJKcigCRJkiR3jpVhTSoCSJIkSXIqAkiSJDlyMwJIkiRJclsCSJIkSXLoWBnW
38
+ pCKAJEmSJKcigCRJkiSnIoAkSZIkpyKAJEmSJIeOlWFNKgJIkiRJcioCSJIkSXIqAkiSJEly
39
+ KgJIkiRJcg==
40
+
41
+
42
+ ------voicemail_655555106251918236316--
43
+ .
@@ -0,0 +1,96 @@
1
+ <html>
2
+ <body>
3
+ <div style="">
4
+ <table cellspacing="0" cellpadding="8" border="0" summary=""
5
+ style=
6
+ "width:100%;font-family:Arial,Sans-serif;border:1px Solid #ccc;border-width:1px 2px 2px 1px;background-color:#fff;">
7
+ <tr>
8
+ <td>
9
+ <div style="padding:2px">
10
+ <div style="float:right;font-weight:bold;font-size:13px">
11
+ <a href="http://userportal.example.com/?msg_id=MESSAGE_ID"
12
+ style="color:#20c;white-space:nowrap">
13
+ more details &raquo;</a>
14
+ </br>
15
+ </div>
16
+
17
+ <h3 style="padding:0 0 6px 0;margin:0;font-family:Arial,Sans-serif;font-size:16px;font-weight:bold;color:#222">
18
+ New voicemail message
19
+ </h3>
20
+
21
+ <div style="padding-bottom:15px;font-size:13px;color:#222;white-space:pre-wrap!important;white-space:-moz-pre-wrap!important;white-space:-pre-wrap!important;white-space:-o-pre-wrap!important;white-space:pre;word-wrap:break-word">
22
+ There is a new voicemail in mailbox MAILBOX.
23
+ Original message is attached as WAV file.
24
+ </div>
25
+
26
+ <table cellpadding="0" cellspacing="0" border="0">
27
+ <tr>
28
+ <td style="padding:0 1em 10px 0;font-family:Arial,Sans-serif;font-size:13px;color:#888;white-space:nowrap" valign="top">
29
+ <div>
30
+ <i style="font-style:normal">Date/Time</i>
31
+ </div>
32
+ </td>
33
+ <td style="padding-bottom:10px;font-family:Arial,Sans-serif;font-size:13px;color:#222" valign="top">
34
+ <%= @astvars[:VM_DATE].to_s %>
35
+ </td>
36
+ </tr>
37
+
38
+ <tr>
39
+ <td style="padding:0 1em 10px 0;font-family:Arial,Sans-serif;font-size:13px;color:#888;white-space:nowrap" valign="top">
40
+ <div>
41
+ <i style="font-style:normal">From</i>
42
+ </div>
43
+ </td>
44
+ <td style="padding-bottom:10px;font-family:Arial,Sans-serif;font-size:13px;color:#222" valign="top">
45
+ <%= @astvars[:VM_CALLERID] %>
46
+ </td>
47
+ </tr>
48
+
49
+ <tr>
50
+ <td style="padding:0 1em 10px 0;font-family:Arial,Sans-serif;font-size:13px;color:#888;white-space:nowrap" valign="top">
51
+ <div>
52
+ <i style="font-style:normal">Duration</i>
53
+ </div>
54
+ </td>
55
+ <td style="padding-bottom:10px;font-family:Arial,Sans-serif;font-size:13px;color:#222" valign="top">
56
+ <%= @astvars[:VM_DUR] %>
57
+ </td>
58
+ </tr>
59
+
60
+ <tr>
61
+ <td style="padding:0 1em 10px 0;font-family:Arial,Sans-serif;font-size:13px;color:#888;white-space:nowrap" valign="top">
62
+ <div>
63
+ <i style="font-style:normal">Total messages</i>
64
+ </div>
65
+ </td>
66
+ <td style="padding-bottom:10px;font-family:Arial,Sans-serif;font-size:13px;color:#222" valign="top">
67
+ <%= @astvars[:VM_MSGNUM] %>
68
+ </td>
69
+ </tr>
70
+
71
+ </table>
72
+ </div>
73
+ </td>
74
+ </tr>
75
+
76
+ <tr>
77
+ <td style="background-color:#f6f6f6;color:#888;border-top:1px Solid #ccc;font-family:Arial,Sans-serif;font-size:11px">
78
+ <p>
79
+ Asterisk voicemail system.
80
+ </p>
81
+
82
+ <p>
83
+ This is a private message. If you have received it
84
+ by mistake or if you have any questions please contact techinacal support: 555-SUPPORT
85
+ </p>
86
+
87
+ <p>
88
+ To manage your voicemail messages please visit
89
+ <a href="http://userportal.example.com/">http://userportal.example.com</a>
90
+ </p>
91
+ </td>
92
+ </tr>
93
+ </table>
94
+ </div>
95
+ </body>
96
+ </html>
@@ -0,0 +1,19 @@
1
+ New voicemail message
2
+
3
+ There is a new voicemail in mailbox MAILBOX.
4
+ Original message is attached as WAV file.
5
+
6
+ Date/Time : <%=@astvars[:VM_DATE] %>
7
+ From : <%=@astvars[:VM_CALLERID] %>
8
+ Duration : <%=@astvars[:VM_DUR] %>
9
+ Total messages: <%=@astvars[:VM_MSGNUM] %>
10
+
11
+ Asterisk voicemail system.
12
+
13
+ **************************************************
14
+ This is a private message. If you have received it
15
+ by mistake or if you have any questions please
16
+ contact techinacal support: 555-SUPPORT
17
+
18
+ To manage your voicemail messages please visit
19
+ http://userportal.example.com