#!/usr/bin/env ruby # This is the main executable file of Adhearsion. # Copyright (c) 2006 Jay Phillips # Permission is hereby granted, free of charge, to any person obtaining a copy of # this software and associated documentation files (the "Software"), to deal in # the Software without restriction, including without limitation the rights to # use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies # of the Software, and to permit persons to whom the Software is furnished to do # so, subject to the following conditions: # The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. usage = "Usage: ahn create /path/to/directory ahn start [daemon] [directory] ahn version ahn help|-h|--h|--help|-help Under development: ahn install helpername ahn system install systemname ahn search keyword ahn uninstall/remove helpername" $: << File.join(File.dirname(__FILE__), 'lib') ADHEARSION_VERSION = File.read(File.join(File.dirname(__FILE__), '.version')).strip ARGV.unshift 'start' if ARGV.empty? # Set the default operation require 'yaml' adhearsion_config = File.join('config', 'adhearsion.yml') CONFIG = File.readable?(adhearsion_config) ? YAML.load_file(adhearsion_config) : {} $HUTDOWN = [] class << $HUTDOWN def hook(&block) self << block end def now! log "Shutting down gracefully." $HUTDOWN.each { |hook| hook.call } exit end end ['INT','TERM'].each do |sig| trap sig do $HUTDOWN.now! end end require 'logging' register_logger StandardLogger.new(STDOUT) case ARGV.shift when 'help', '-h', '--h', '-help', '--help' then puts usage when 'version' then puts "Adhearsion v#{ADHEARSION_VERSION}" when /^create(:[\w_.]+)?$/ require 'fileutils' include FileUtils dest_dir_relative = ARGV.shift || Dir.pwd dest_dir = File.expand_path dest_dir_relative base_dir = File.expand_path File.dirname(__FILE__) App = $&.index(':') ? $&.split(':')[1..-1] * ':' : "default" abort %'No app "#{App}" found!' unless File.directory? "#{base_dir}/apps/#{App}" from, to = [], [] (Dir["{#{base_dir}/apps/#{App},docs}/**/*"] + ["#{base_dir}/LICENSE"]).each do |key| value = key[base_dir.length..-1] value.sub! "apps/#{App}/", '' if value.index "apps/#{App}/" to << dest_dir + value from << key end #from.each_index { |i| debug "#{from[i]} => #{to[i]}" } # Check overwriting. Abort, Overwrite, Skip from.each_index do |i| if File.directory? from[i] if File.exists? to[i] then puts "Skipping #{to[i]}" else makedirs to[i] end else dir = File.dirname to[i] makedirs dir if !File.exists?(dir) if File.exists? to[i] then puts "Skipping #{to[i]}" else puts "Creating #{to[i]}" copy from[i], to[i] end end end puts <<-MESSAGE Adhearsion project generated! Start your new app with "ahn start #{dest_dir_relative}" For dial plan management, change the appropriate contexts in your /etc/asterisk/extensions.conf file to the following: [your_context_name] exten => _X.,1,AGI(agi://1.2.3.4) ; This IP here To use databases edit config/database.yml for the connection information and, optionally, config/database.rb to change the default database object models. Asterisk Manager interface integration is highly recommended. Edit your /etc/asterisk/manager.conf file and enable the system *securely* with an account for this app. Reload with "asterisk -rx reload manager" and then edit the config/helpers/manager_proxy.yml file appropriately. If you would like a local copy of the Adhearsion wiki, run "rake wiki" in your app folder. Please support the community by contributing documentation improvements by visiting the online, editable version at http://docs.adhearsion.com! MESSAGE when 'start' $DAEMON = false target = Dir.pwd arg = ARGV.shift if arg =~ /daemon(ize)?/ $DAEMON = true arg = ARGV.shift end target = File.expand_path(arg || target) Dir.chdir target register_logger StandardLogger.new('logs/adhearsion.log') puts %{\nStarting Adhearsion v#{ADHEARSION_VERSION} Written by Jay Phillips of Codemecca LLC, et al. http://adhearsion.com\n\n} %w(rubygems uri open-uri abbrev thread).each { |lib| require lib } $: << 'config' require 'adhearsion' require 'database' if CONFIG['enable_database'] require 'servlet_container' # Load appropriate helpers $HELPERS = {} Contexts::Container.new.run_inside do # Start with compiled helpers class << Object aliens = Dir[File.join('helpers', '*.alien.*')] aliens.each do |f| f = File.basename f config_file = File.join %W(config helpers #{f}.yml) config = File.readable?(config_file) ? YAML.load_file(config_file) : {} if config.delete('enabled') != false require 'inline' lang = f[f.rindex('.')+1..-1] log "Loading helper #{f} as #{lang.upcase}" $HELPERS[f] = config inline do |builder| builder.send lang, File.read(File.join('helpers', f)) end end end end # Load Ruby helpers Dir[File.join('helpers', '*.rb')].each do |f| name = File.basename(f)[/^[^.]+/] config_file = File.join %W(config helpers #{name}.yml) config = File.readable?(config_file) ? YAML.load_file(config_file) : {} if config.delete('enabled') != false log "Parsing helper #{name}" $HELPERS[name] = config eval File.read(f) end end end sc = ServletContainer.new(CONFIG['port'] || 4573) $HUTDOWN.hook { sc.shutdown } log "Dallas, we have liftoff!" sc.server.join else puts "Unrecognized command!", usage end