<commit_before>
require 'adhearsion/voip/menu_state_machine/menu_class'

module Adhearsion
  module VoIP
    module Asterisk
      module Commands
        
        RESPONSE_PREFIX = "200 result=" unless defined? RESPONSE_PREFIX
        
        # These are the status messages that asterisk will issue after a dial command is executed.
        # More information here: http://www.voip-info.org/wiki/index.php?page=Asterisk+variable+DIALSTATUS
        # Here is a current list of dial status messages which are not all necessarily supported by adhearsion:
        # 
        # ANSWER: Call is answered. A successful dial. The caller reached the callee.
        # BUSY: Busy signal. The dial command reached its number but the number is busy.
        # NOANSWER: No answer. The dial command reached its number, the number rang for too long, then the dial timed out.
        # CANCEL: Call is cancelled. The dial command reached its number but the caller hung up before the callee picked up.
        # CONGESTION: Congestion. This status is usually a sign that the dialled number is not recognised.
        # CHANUNAVAIL: Channel unavailable. On SIP, peer may not be registered.
        # DONTCALL: Privacy mode, callee rejected the call
        # TORTURE: Privacy mode, callee chose to send caller to torture menu
        # INVALIDARGS: Error parsing Dial command arguments (added for Asterisk 1.4.1, SVN r53135-53136)
        #
        #
        DIAL_STATUSES   = Hash.new(:unknown).merge(:answer      => :answered,
                                                   :congestion  => :congested, 
                                                   :busy        => :busy,
                                                   :cancel      => :cancelled,
                                                   :noanswer    => :unanswered,
                                                   :cancelled   => :cancelled,
                                                   :chanunavail => :channel_unavailable) unless defined? DIAL_STATUSES
        
        DYNAMIC_FEATURE_EXTENSIONS = {
          :attended_transfer => lambda do |options|
            variable "TRANSFER_CONTEXT" => options[:context] if options && options.has_key?(:context)
            extend_dynamic_features_with "atxfer"
          end,
          :blind_transfer => lambda do
            variable "TRANSFER_CONTEXT" => options[:context] if options && options.has_key?(:context)
            extend_dynamic_features_with 'blindxfer'
          end
        } unless defined? DYNAMIC_FEATURE_EXTENSIONS
        
        def write(message)
          to_pbx.print(message)
        end
        
        def read
          returning from_pbx.gets do |message|
            raise Hangup if message.nil?
            ahn_log.agi.debug "<<< #{message}"
          end
        end
        
        # This method is the underlying method executed by nearly all the command methods in this module.
        # It is used to send the plaintext commands in the proper AGI format over TCP/IP back to an Asterisk server via the
        # FAGI protocol.
        # It is not recommended that you call this method directly unless you plan to write a new command method
        # in which case use this method you to communicate directly with an Asterisk server via the FAGI protocol.
        # For more information about FAGI visit: http://www.voip-info.org/wiki/view/Asterisk+FastAGI
        def raw_response(message = nil)
          ahn_log.agi.debug ">>> #{message}"
          write message if message
          read
        end
        
        # The answer command must be called first before any other commands can be issued.
        # In typical adhearsion applications the answer command is called by default as soon
        # as a call is transfered to a valid context in dialplan.rb.
        # If you do not want your adhearsion application to automatically issue an answer command,
        # then you must edit your startup.rb file and configure this setting.
        # Keep in mind that you should not need to issue another answer command after 
        # an answer command has already been issued either explicitly by your code or implicitly
        # by the standard adhearsion configuration.
        def answer
          raw_response "ANSWER"
          true
        end
        
        # This asterisk dialplan command allows you to instruct Asterisk to start applications
        # which are typically run from extensions.conf.  For a complete list of these commands
        # please visit: http://www.voip-info.org/wiki/view/Asterisk+-+documentation+of+application+commands
        #
        # The most common commands are already made available through the FAGI interface provided
        # by this code base.  For commands that do not fall into this category, then exec is what you
        # should use.
        #
        # For example, if there are specific asterisk modules you have loaded that will not 
        # available through the standard commands provided through FAGI - then you can used EXEC.
        #
        # Example:
        # execute 'SIPAddHeader', '"Call-Info: answer-after=0"
        # 
        # Using execute in this way will add a header to an existing SIP call.
        # 
        def execute(application, *arguments)
          result = raw_response("EXEC #{application} #{arguments * '|'}")
          return false if error?(result)
          result
        end
        
        # Hangs up the current channel.
        # After this command is issued, your application will stop executing.
        # This should be used in the same way you would call the ruby exit() method to exit an application.
        # If it is necessary to do some additional cleanup tasks before returning control back to asterisk, then
        # make sure you have setup a begin...ensure block in the context of your adhearsion application dialplan.
        def hangup
          raw_response 'HANGUP'
        end
        
        # Plays the specified sound file names. This method will handle Time/DateTime objects (e.g. Time.now),
        # Fixnums (e.g. 1000), Strings which are valid Fixnums (e.g "123"), and direct sound files. When playing
        # numbers, Adhearsion assumes you're saying the number, not the digits. For example, play("100")
        # is pronounced as "one hundred" instead of "one zero zero".
        #
        # Note: it's not necessary to supply a sound file extension; Asterisk will try to find a sound
        # file encoded using the current channel's codec, if one exists. If not, it will transcode from
        # the default codec (GSM). Asterisk stores its sound files in /var/lib/asterisk/sounds.
        #
        # Usage:
        #
        #   play 'hello-world'
        #   play Time.now
        #   play %w"a-connect-charge-of 22 cents-per-minute will-apply"<commit_msg>Fixing the documentation of AGI hangup(). Closes #71<commit_after>
require 'adhearsion/voip/menu_state_machine/menu_class'

module Adhearsion
  module VoIP
    module Asterisk
      module Commands
        
        RESPONSE_PREFIX = "200 result=" unless defined? RESPONSE_PREFIX
        
        # These are the status messages that asterisk will issue after a dial command is executed.
        # More information here: http://www.voip-info.org/wiki/index.php?page=Asterisk+variable+DIALSTATUS
        # Here is a current list of dial status messages which are not all necessarily supported by adhearsion:
        # 
        # ANSWER: Call is answered. A successful dial. The caller reached the callee.
        # BUSY: Busy signal. The dial command reached its number but the number is busy.
        # NOANSWER: No answer. The dial command reached its number, the number rang for too long, then the dial timed out.
        # CANCEL: Call is cancelled. The dial command reached its number but the caller hung up before the callee picked up.
        # CONGESTION: Congestion. This status is usually a sign that the dialled number is not recognised.
        # CHANUNAVAIL: Channel unavailable. On SIP, peer may not be registered.
        # DONTCALL: Privacy mode, callee rejected the call
        # TORTURE: Privacy mode, callee chose to send caller to torture menu
        # INVALIDARGS: Error parsing Dial command arguments (added for Asterisk 1.4.1, SVN r53135-53136)
        #
        #
        DIAL_STATUSES   = Hash.new(:unknown).merge(:answer      => :answered,
                                                   :congestion  => :congested, 
                                                   :busy        => :busy,
                                                   :cancel      => :cancelled,
                                                   :noanswer    => :unanswered,
                                                   :cancelled   => :cancelled,
                                                   :chanunavail => :channel_unavailable) unless defined? DIAL_STATUSES
        
        DYNAMIC_FEATURE_EXTENSIONS = {
          :attended_transfer => lambda do |options|
            variable "TRANSFER_CONTEXT" => options[:context] if options && options.has_key?(:context)
            extend_dynamic_features_with "atxfer"
          end,
          :blind_transfer => lambda do
            variable "TRANSFER_CONTEXT" => options[:context] if options && options.has_key?(:context)
            extend_dynamic_features_with 'blindxfer'
          end
        } unless defined? DYNAMIC_FEATURE_EXTENSIONS
        
        def write(message)
          to_pbx.print(message)
        end
        
        def read
          returning from_pbx.gets do |message|
            raise Hangup if message.nil?
            ahn_log.agi.debug "<<< #{message}"
          end
        end
        
        # This method is the underlying method executed by nearly all the command methods in this module.
        # It is used to send the plaintext commands in the proper AGI format over TCP/IP back to an Asterisk server via the
        # FAGI protocol.
        # It is not recommended that you call this method directly unless you plan to write a new command method
        # in which case use this method you to communicate directly with an Asterisk server via the FAGI protocol.
        # For more information about FAGI visit: http://www.voip-info.org/wiki/view/Asterisk+FastAGI
        def raw_response(message = nil)
          ahn_log.agi.debug ">>> #{message}"
          write message if message
          read
        end
        
        # The answer command must be called first before any other commands can be issued.
        # In typical adhearsion applications the answer command is called by default as soon
        # as a call is transfered to a valid context in dialplan.rb.
        # If you do not want your adhearsion application to automatically issue an answer command,
        # then you must edit your startup.rb file and configure this setting.
        # Keep in mind that you should not need to issue another answer command after 
        # an answer command has already been issued either explicitly by your code or implicitly
        # by the standard adhearsion configuration.
        def answer
          raw_response "ANSWER"
          true
        end
        
        # This asterisk dialplan command allows you to instruct Asterisk to start applications
        # which are typically run from extensions.conf.  For a complete list of these commands
        # please visit: http://www.voip-info.org/wiki/view/Asterisk+-+documentation+of+application+commands
        #
        # The most common commands are already made available through the FAGI interface provided
        # by this code base.  For commands that do not fall into this category, then exec is what you
        # should use.
        #
        # For example, if there are specific asterisk modules you have loaded that will not 
        # available through the standard commands provided through FAGI - then you can used EXEC.
        #
        # Example:
        # execute 'SIPAddHeader', '"Call-Info: answer-after=0"
        # 
        # Using execute in this way will add a header to an existing SIP call.
        # 
        def execute(application, *arguments)
          result = raw_response("EXEC #{application} #{arguments * '|'}")
          return false if error?(result)
          result
        end
        
        ##
        # Hangs up the current channel. After this command is issued, you will not be able to send any more AGI
        # commands but the dialplan Thread will still continue, allowing you to do any post-call work.
        #
        def hangup
          raw_response 'HANGUP'
        end
        
        # Plays the specified sound file names. This method will handle Time/DateTime objects (e.g. Time.now),
        # Fixnums (e.g. 1000), Strings which are valid Fixnums (e.g "123"), and direct sound files. When playing
        # numbers, Adhearsion assumes you're saying the number, not the digits. For example, play("100")
        # is pronounced as "one hundred" instead of "one zero zero".
        #
        # Note: it's not necessary to supply a sound file extension; Asterisk will try to find a sound
        # file encoded using the current channel's codec, if one exists. If not, it will transcode from
        # the default codec (GSM). Asterisk stores its sound files in /var/lib/asterisk/sounds.
        #
        # Usage:
        #
        #   play 'hello-world'
        #   play Time.now
        #   play %w"a-connect-charge-of 22 cents-per-minute will-apply"
{'fraction_non_alphanumeric': 0.05968385285862018, 'fraction_numerical': 0.004431969271679716, 'mean_word_length': 2.031571876399463, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5537423', 'n_tokens_mistral': 3416, 'n_tokens_neox': 3302, 'n_words': 1444}