-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathevents.rb
125 lines (122 loc) · 3.94 KB
/
events.rb
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
##
# In this file you can define callbacks for different aspects of the framework. Below is an example:
##
#
# events.asterisk.before_call.each do |call|
# # This simply logs the extension for all calls going through this Adhearsion app.
# extension = call.variables[:extension]
# ahn_log "Got a new call with extension #{extension}"
# end
#
##
# Asterisk Manager Interface example:
#
# events.asterisk.manager_interface.each do |event|
# ahn_log.events event.inspect
# end
#
# This assumes you gave :events => true to the config.asterisk.enable_ami method in config/startup.rb
#
##
# Here is a list of the events included by default:
#
# - events.asterisk.manager_interface
# - events.after_initialized
# - events.shutdown
# - events.asterisk.before_call
# - events.asterisk.failed_call
# - events.asterisk.hungup_call
#
#
# Note: events are mostly for components to register and expose to you.
##
require 'mail'
I18n.load_path += Dir[File.dirname(__FILE__) + "/config/locales/*.yml"]
I18n.default_locale = :de
# From http://guides.rubyonrails.org/security.html ( Here is the file name sanitizer from the attachment_fu pluginfu/tree/master: )
def sanitize_filename(filename)
# ToDo: this needs some Fixing
filename.strip.tap do |name|
# NOTE: File.basename doesn't work right with Windows paths on Unix
# get only the filename, not the whole path
# name.sub! /\A.*(\\|\/)/, ''
name.sub! /(\A.*)\\|\;/, ''
# Finally, replace all non alphanumeric, underscore
# or periods with underscore
# name.gsub! /[^\w\.\-]/, '_'
end
end
events.asterisk.manager_interface.each do |event|
begin
# ahn_log "="*80
# ahn_log "In Event loop now with event: #{event.name}"
# ahn_log "="*80
if event.name.downcase=="newstate"
# ahn_log "="*80
# ahn_log "Eventstate: #{event.headers["State"]}"
# ahn_log "="*80
if event.headers["State"] == "Up"
# can we play something in events.rb?
#play "goodbye" no we can't
end
end
if event.name=="UserEvent"
ahn_log.events event.inspect
ahn_log.events event.to_yaml
ahn_log "*"*50
ahn_log event.headers.inspect
ahn_log "*"*50
ahn_log "%"*50
# get the event name of the UserEvent
user_event=event.headers["UserEvent"].split("|")
if !user_event.blank? && user_event[0]=="FaxRcvd"
# we received a fax, process the parameters
ahn_log "*"*50
ahn_log "Fax received, params #{user_event[1]}"
ahn_log I18n.translate('Fax Received')
ahn_log "*"*50
# converting fax to pdf.
v_pos="Sanatizing Filename"
ahn_log v_pos
filename=sanitize_filename(event.headers["filename"])
# check if the file exists
if File.exist? filename
v_pos="Converting fax to pdf"
ahn_log v_pos
system("convert #{filename} #{filename.gsub(".tif",".pdf")}")
# check if the pdf file exists
if File.exist? filename.gsub(".tif",".pdf")
v_pos="sending mail"
ahn_log v_pos
Mail.deliver do
from '[email protected]'
to '[email protected]'
subject I18n.translate('Fax Received')
body I18n.translate('Fax Rcv Body')
add_file filename.gsub(".tif",".pdf")
end
ahn_log "Done!"
else
ahn_log "*"*50
ahn_log "Skipping Mail. File #{filename.gsub(".tif",".pdf")} doesn't exist"
ahn_log "*"*50
end
else
ahn_log "*"*50
ahn_log "Skipping. File #{filename} doesn't exist"
ahn_log "*"*50
end
elsif !user_event.blank? && user_event[0]=="FaxSend"
ahn_log "*"*50
ahn_log "We are in sendfax"
ahn_log "*"*50
end
end
rescue Exception => e
ahn_log "*"*50
ahn_log "We crashed during #{v_pos} / #{e.message}"
ahn_log e.backtrace.inspect
ahn_log "*"*50
raise
end
end