void tsInterface_TransferCall(object sender, WOSI.IVR.IML.TransferEventArgs e)
        {
            TelecomScriptInterface tsInterface = (TelecomScriptInterface)sender;

            if (e.UseBridge)
            {
                // Bridge the call with an outbound call
                if (MakeOutboundCall(tsInterface, e.TransferTo, "", -1, false, false))
                {
                    tsInterface.ScriptProcessor.StartProcessing(tsInterface, telecomProvider, dataProvider);
                }
                else
                {
                    tsInterface.IMLInterpreter.SignalTransferFailure();
                }
            }
            else if (!e.IsExtension)
            {
                // If the call is a call to telephone number, blind transfer it
                telecomProvider.TransferCall(tsInterface.LineNumber, e.TransferTo);
                tsInterface.IMLInterpreter.SignalEventCallback(e.EventToken);
            }
            else
            {
                TransferToExtension(e.TransferTo, tsInterface, false);
                tsInterface.IMLInterpreter.SignalEventCallback(e.EventToken);
            }
        }
        internal void SetupAutoAttendantAnswer(int lineNumber, TelecomScriptInterface tsInterface)
        {
            // Should we be using our custom script schedule?
            if (Properties.Settings.Default.ExpertMode /*&& Licensing.Management.AppPermissions.StatIsPermitted("CustomScripting")*/)
            {
                string scriptLocation = GetCurrentExpertScript();

                if (scriptLocation != null && File.Exists(scriptLocation))
                {
                    tsInterface.ScriptProcessor = new ExpertScriptProcessor(scriptLocation);
                    return;
                }
            }

            // If we get here we should attach our standard script processor to this script interface
            tsInterface.ScriptProcessor = new StandardScriptProcessor(pluginManager, registrarService);
        }
        public bool MakeOutboundCall(TelecomScriptInterface tsInterface, string numberToDial, string callerDisplayName, int internalCallerExtension, bool trimPrefix, bool autoStart)
        {
            string outboundNumber = numberToDial;

            // Trim the prefix off the dialed number
            if (trimPrefix && Properties.Settings.Default.OutboundDialingPrefix != null && Properties.Settings.Default.OutboundDialingPrefix.Length > 0)
            {
                outboundNumber = outboundNumber.Substring(Properties.Settings.Default.OutboundDialingPrefix.Length);
            }

            // Look for an available line
            int openLineNumber = FindAndHoldOpenLine();

            TelecomScriptInterface outboundTsInterface = null;

            // If there is a line available, start our outbound call.
            if (openLineNumber != 0)
            {
                outboundTsInterface = tsInterfaces[openLineNumber];
                outboundTsInterface.ScriptProcessor = new OutboundCalleeScriptProcessor(outboundNumber, callerDisplayName);
            }

            if (telecomProvider.IsLineInUse(tsInterface.LineNumber))
            {
                tsInterface.ScriptProcessor = new OutboundScriptProcessor(tsInterface, outboundTsInterface, internalCallerExtension);

                if (autoStart)
                {
                    tsInterface.ScriptProcessor.StartProcessing(tsInterface, telecomProvider, dataProvider);
                }
            }
            else
            {
                tsInterfaces[openLineNumber].Locked = false;
                return(false);
            }

            return(true);
        }
        //private ExtensionStateService extStateService;

        public ScriptService(TelecomProviderBase telecomProvider, CallButlerDataProviderBase dataProvider, VoicemailService vmService, VoicemailMailerService vmMailerService, Utilities.PluginManagement.PluginManager pluginManager, PBXRegistrarService registrarService /*, ExtensionStateService extStateService*/)
        {
            this.registrarService = registrarService;
            this.dataProvider     = dataProvider;
            this.telecomProvider  = telecomProvider;
            this.pluginManager    = pluginManager;
            //this.extStateService = extStateService;

            // Create our services
            this.vmService       = vmService;
            this.vmMailerService = vmMailerService;

            // Create a new script processor for each of our inbound lines
            tsInterfaces = new Dictionary <int, TelecomScriptInterface>();

            int lineCount = Properties.Settings.Default.LineCount; // Math.Min(Properties.Settings.Default.LineCount, Licensing.Management.AppPermissions.StatGetPermissionScalar("MaxLineCount"));

            for (int index = 1; index <= lineCount; index++)
            {
                TelecomScriptInterface tsInterface = new TelecomScriptInterface(telecomProvider, dataProvider, pluginManager, registrarService /*, extStateService*/, index);
                tsInterfaces.Add(index, tsInterface);

                tsInterface.TransferCall += new EventHandler <WOSI.IVR.IML.TransferEventArgs>(tsInterface_TransferCall);
                tsInterface.IMLInterpreter.ScriptStarted  += new EventHandler(IMLInterpreter_ScriptStarted);
                tsInterface.IMLInterpreter.ScriptFinished += new EventHandler(IMLInterpreter_ScriptFinished);
            }

            // Add a startup entry in our log
            LoggingService.AddLogEntry(LogLevel.Basic, Services.PrivateLabelService.ReplaceProductName(Services.PrivateLabelService.ReplaceProductName("CallButler Service Started")), false);

            // Attach to our telecom provider events
            telecomProvider.IncomingCall     += new EventHandler <CallEventArgs>(telecomProvider_IncomingCall);
            telecomProvider.Error            += new EventHandler <CallButler.Telecom.ErrorEventArgs>(telecomProvider_Error);
            telecomProvider.CallConnected    += new EventHandler <CallEventArgs>(telecomProvider_CallConnected);
            telecomProvider.CallEnded        += new EventHandler <LineEventArgs>(telecomProvider_CallEnded);
            telecomProvider.CallFailed       += new EventHandler <CallFailureEventArgs>(telecomProvider_CallFailed);
            telecomProvider.IncomingBusyCall += new EventHandler <BusyCallEventArgs>(telecomProvider_IncomingBusyCall);
        }
 public void ProcessAutoAttendantAnswer(int lineNumber, TelecomScriptInterface tsInterface, bool enableAnswerDelay)
 {
     SetupAutoAttendantAnswer(lineNumber, tsInterface);
     ProcessAnswerCall(lineNumber, tsInterface.Extension == null ? false : true, enableAnswerDelay);
 }
        void telecomProvider_IncomingCall(object sender, CallEventArgs e)
        {
            UpdatePerformanceCounters();

            // Add a log entry
            LoggingService.AddLogEntry(LogLevel.Basic, "(Line " + e.LineNumber + ") Incoming call from " + e.CallerDisplayName + " " + e.CallerPhoneNumber + " to " + e.CallingToNumber + " " + e.CallingToMiscInfo, false);

            // Check to see if the service is supposed to be running and that we have a valid license.
            if (/*(LicenseService.IsLicensed() || LicenseService.IsTrialLicense() || Properties.Settings.Default.IsFreeVersion) &&*/ Properties.Settings.Default.ServiceEnabled)
            {
                TelecomScriptInterface tsInterface = tsInterfaces[e.LineNumber];

                // Populate our caller variables into the IML Interpreter so they can be used in our script
                tsInterface.IMLInterpreter.CallerDisplayName = e.CallerDisplayName;
                tsInterface.IMLInterpreter.CallerHost        = e.CallerMiscInfo;
                tsInterface.IMLInterpreter.CallerUsername    = e.CallerPhoneNumber;
                tsInterface.IMLInterpreter.DialedUsername    = e.CallingToNumber;
                tsInterface.IMLInterpreter.DialedHost        = e.CallingToMiscInfo;

                // Set the profile parameter of our tsInterface. This is usually the profile for the incoming call.
                //tsInterface.Profile = e.Tag;

                // Check to see if this caller is asking for a direct extension number
                WOSI.CallButler.Data.CallButlerDataset.ExtensionsRow extension = null;
                tsInterface.Extension = null;
                int internalCallerExtension = 0;

                // Check to see if this is an internal caller
                if (registrarService != null)
                {
                    try
                    {
                        WOSI.CallButler.Data.CallButlerDataset.ExtensionsRow internalExtensionRow = dataProvider.GetExtensionNumber(Properties.Settings.Default.CustomerID, Convert.ToInt32(e.CallerPhoneNumber));

                        if (internalExtensionRow != null)
                        {
                            tsInterface.Extension   = internalExtensionRow;
                            internalCallerExtension = internalExtensionRow.ExtensionNumber;

                            // Update our extension status

                            /*if (Properties.Settings.Default.EnableKinesisServer)
                             * {
                             *  tsInterface.UpdateExtensionCallStatus(CallStatus.Dialing);
                             * }*/
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                }

                try
                {
                    int extensionNumber = Convert.ToInt32(e.CallingToNumber);
                    extension = dataProvider.GetExtensionNumber(Properties.Settings.Default.CustomerID, extensionNumber);
                }
                catch
                {
                }

                if (extension != null)
                {
                    // If this extension is calling itself, send it to the voicemail management script, otherwise send it to the extension.
                    if (e.CallerPhoneNumber == e.CallingToNumber)
                    {
                        // Start the voicemail processing script
                        tsInterface.ScriptProcessor = new VoicemailManagementScriptProcessor(extension, registrarService);

                        if (telecomProvider.IsLineInUse(e.LineNumber))
                        {
                            telecomProvider.AnswerCall(e.LineNumber, tsInterface.Extension == null ? false : true);
                        }
                    }
                    else
                    {
                        // Send the caller to the requested extension
                        if (telecomProvider.IsLineInUse(e.LineNumber))
                        {
                            telecomProvider.AnswerCall(e.LineNumber, tsInterface.Extension == null ? false : true);
                        }

                        TransferToExtension(extension.ExtensionNumber.ToString(), tsInterface, true);
                    }

                    return;
                }

                if (tsInterface.Extension != null)
                {
                    // If dialing "*", send the internal caller to the main menu
                    if (e.CallingToNumber == "*" || e.CallingToNumber.Trim() == "")
                    {
                    }
                    // This is an internal caller trying to make an outbound call
                    else if (Properties.Settings.Default.AllowOutboundDialing && e.CallingToNumber.StartsWith(Properties.Settings.Default.OutboundDialingPrefix))
                    {
                        if (MakeOutboundCall(tsInterface, e.CallingToNumber, e.CallerDisplayName, internalCallerExtension, true, false))
                        {
                            telecomProvider.AnswerCall(e.LineNumber, tsInterface.Extension == null ? false : true);

                            return;
                        }
                    }
                }

                // Should we be trying the receptionist first?
                if (Properties.Settings.Default.TryReceptionistFirst)
                {
                    WOSI.CallButler.Data.CallButlerDataset.ExtensionsRow recepExtension = dataProvider.GetExtension(Properties.Settings.Default.CustomerID, Properties.Settings.Default.ReceptionistExtensionID);

                    if (recepExtension != null)
                    {
                        ReceptionistFinderScriptProcessor recepSp = new ReceptionistFinderScriptProcessor(recepExtension, this);
                        TransferToExtension(recepExtension.ExtensionNumber.ToString(), recepSp, tsInterface, true);
                        return;
                    }
                }

                ProcessAutoAttendantAnswer(e.LineNumber, tsInterface, true);
            }
        }
        public void TransferToExtension(string extensionNumber, ScriptProcessorBase onHoldScriptProcessor, TelecomScriptInterface tsInterface, bool disableCallScreening)
        {
            try
            {
                int extNumber = Convert.ToInt32(extensionNumber);
                WOSI.CallButler.Data.CallButlerDataset.ExtensionsRow extension;

                // If the transfer is an extension, find the extension
                if (extNumber == 0)
                {
                    // Get our receptionist extension
                    extension = dataProvider.GetExtension(Properties.Settings.Default.CustomerID, Properties.Settings.Default.ReceptionistExtensionID);
                }
                else
                {
                    extension = dataProvider.GetExtensionNumber(Properties.Settings.Default.CustomerID, extNumber);
                }

                if (extension != null)
                {
                    // Start the voicemail processing script
                    if (onHoldScriptProcessor == null)
                    {
                        tsInterface.ScriptProcessor = new VoicemailScriptProcessor(extension, vmService, registrarService, pluginManager);
                    }
                    else
                    {
                        tsInterface.ScriptProcessor = onHoldScriptProcessor;
                    }

                    tsInterface.ScriptProcessor.StartProcessing(tsInterface, telecomProvider, dataProvider);

                    if (!extension.DoNotDisturb /*&& Licensing.Management.AppPermissions.StatIsPermitted("Extensions.TelephoneNumbers")*/)
                    {
                        int openLineNumber = FindAndHoldOpenLine();

                        if (openLineNumber != 0)
                        {
                            // If we get here, we have an open line and we should start processing our find me follow me
                            tsInterfaces[openLineNumber].ScriptProcessor = new ExtensionScriptProcessor(this, tsInterface, extension, vmMailerService, registrarService /*, extStateService*/, disableCallScreening, true, false);
                            tsInterfaces[openLineNumber].ScriptProcessor.StartProcessing(tsInterfaces[openLineNumber], telecomProvider, dataProvider);

                            return;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LoggingService.AddLogEntry(LogLevel.ErrorsOnly, Utils.ErrorUtils.FormatErrorString(ex), true);
            }

            // If we get here, the transfer has failed
            tsInterface.IMLInterpreter.SignalTransferFailure();
        }
 public void TransferToExtension(string extensionNumber, TelecomScriptInterface tsInterface, bool disableCallScreening)
 {
     TransferToExtension(extensionNumber, null, tsInterface, disableCallScreening);
 }