public override void OnIncomingTransfer(CallButler.Telecom.TelecomProviderBase telcomProvider, CallButler.Telecom.TransferEventArgs e)
        {
            if (e.ReplacementLineNumber != 0)
            {
                // Find the ts interface for the call from this extension to our remote caller
                TelecomScriptInterface remoteTsInterface = scriptService.TelecomScriptInterfaces[e.ReplacementLineNumber];

                // Find the ts interface for the remote caller
                if (remoteTsInterface.ScriptProcessor is TransferConferenceScriptProcessor)
                {
                    TransferConferenceScriptProcessor remoteProcessor = (TransferConferenceScriptProcessor)remoteTsInterface.ScriptProcessor;

                    int remoteLineNumber = remoteProcessor.callerTsInterface.LineNumber;

                    remoteProcessor.callerTsInterface = null;

                    if (scriptService.TelecomScriptInterfaces[remoteLineNumber].ScriptProcessor is TransferConferenceScriptProcessor)
                    {
                        remoteTsInterface = scriptService.TelecomScriptInterfaces[remoteLineNumber];
                        remoteProcessor   = (TransferConferenceScriptProcessor)remoteTsInterface.ScriptProcessor;

                        // Conference the two lines together
                        remoteProcessor.callerTsInterface = callerTsInterface;

                        if (callerTsInterface.ScriptProcessor is TransferConferenceScriptProcessor)
                        {
                            ((TransferConferenceScriptProcessor)callerTsInterface.ScriptProcessor).callerTsInterface = remoteProcessor.tsInterface;
                        }

                        remoteProcessor.conferenceID = conferenceID;

                        telcomProvider.AddLineToConference(conferenceID, remoteProcessor.tsInterface.LineNumber);
                        telcomProvider.RemoveLineFromConference(tsInterface.LineNumber);

                        // Tell the other caller to stop hold music
                        callerTsInterface.IMLInterpreter.SignalExternalEvent(TransferConferenceExternalEvents.CALLBUTLERINTERNAL_UnholdCall.ToString());

                        //this.conferenceID = 0;

                        this.callerTsInterface = null;

                        telcomProvider.EndCall(e.ReplacementLineNumber);
                        telcomProvider.EndCall(tsInterface.LineNumber);

                        return;
                    }
                }

                callerTsInterface.IMLInterpreter.SignalTransferFailure();
            }
            else
            {
                TransferCaller(telcomProvider, e.CallingToNumber);
            }
        }
 private void ProcessEndCall(CallButler.Telecom.TelecomProviderBase telecomProvider)
 {
     if (outboundTsInterface != null && conferenceID == 0)
     {
         telecomProvider.EndCall(outboundTsInterface.LineNumber);
     }
     else
     {
         telecomProvider.EndConference(conferenceID, true);
     }
 }
        protected override void OnExternalCommand(string command, string commandData, string eventToken, TelecomScriptInterface tsInterface, CallButler.Telecom.TelecomProviderBase telecomProvider, WOSI.CallButler.Data.DataProviders.CallButlerDataProviderBase dataProvider)
        {
            // Parse out our external event action
            if (Enum.IsDefined(typeof(ScheduleReminderExternalCommands), command))
            {
                ScheduleReminderExternalCommands externalCommand = WOSI.Utilities.EnumUtils <ScheduleReminderExternalCommands> .Parse(command);

                switch (externalCommand)
                {
                case ScheduleReminderExternalCommands.CALLBUTLERINTERNAL_GetNextNumber:
                {
                    // If we have a previous call, end it
                    if (telecomProvider.IsLineInUse(tsInterface.LineNumber))
                    {
                        telecomProvider.EndCall(tsInterface.LineNumber);
                    }

                    extensionNumberIndex++;

                    // Get our extension contact numbers
                    WOSI.CallButler.Data.CallButlerDataset.ExtensionContactNumbersRow[] contactNumbers = (WOSI.CallButler.Data.CallButlerDataset.ExtensionContactNumbersRow[])dataProvider.GetExtensionContactNumbers(extension.ExtensionID).Select("", "Priority ASC");

                    while (extensionNumberIndex < contactNumbers.Length)
                    {
                        WOSI.CallButler.Data.CallButlerDataset.ExtensionContactNumbersRow contactNumber = contactNumbers[extensionNumberIndex];

                        // Is the number online?
                        if (contactNumber.Online)
                        {
                            // Does the number have hours?
                            if (!contactNumber.HasHoursOfOperation || (contactNumber.HasHoursOfOperation && ScriptUtils.IsInHoursOfOperation(contactNumber.HoursOfOperation, contactNumber.HoursOfOperationUTCOffset)))
                            {
                                // If we get here, try the number
                                tsInterface.IMLInterpreter.SetLocalVariable("NumberToCall", contactNumber.ContactNumber);

                                tsInterface.IMLInterpreter.SetLocalVariable("ExtensionTimeout", contactNumber.Timeout.ToString());

                                string introDetails = "You have " + reminders.Length + " upcoming appointment";

                                if (reminders.Length > 1)
                                {
                                    introDetails += "s";
                                }

                                introDetails += ".";

                                tsInterface.IMLInterpreter.SetLocalVariable("IntroDetails", introDetails);

                                tsInterface.IMLInterpreter.SignalEventCallback(eventToken);

                                return;
                            }
                        }

                        extensionNumberIndex++;
                    }

                    if (telecomProvider.IsLineInUse(tsInterface.LineNumber))
                    {
                        telecomProvider.EndCall(tsInterface.LineNumber);
                    }

                    tsInterface.IMLInterpreter.StopScript();
                    break;
                }

                case ScheduleReminderExternalCommands.CALLBUTLERINTERNAL_FetchNextReminder:
                {
                    reminderIndex++;

                    if (reminderIndex < reminders.Length)
                    {
                        OutlookReminder reminder = reminders[reminderIndex];

                        tsInterface.IMLInterpreter.SetLocalVariable("ScheduleDetails", GetReminderDetails(reminder));

                        if (reminderIndex == reminders.Length - 1)
                        {
                            tsInterface.IMLInterpreter.SignalExternalEvent(ScheduleReminderExternalEvents.CALLBUTLERINTERNAL_LastReminder.ToString());
                        }
                        else
                        {
                            tsInterface.IMLInterpreter.SignalEventCallback(eventToken);
                        }
                    }
                    else
                    {
                        tsInterface.IMLInterpreter.SignalExternalEvent(ScheduleReminderExternalEvents.CALLBUTLERINTERNAL_EndOfReminders.ToString());
                    }


                    break;
                }
                }
            }
        }
        protected override void OnExternalCommand(string command, string commandData, string eventToken, TelecomScriptInterface tsInterface, CallButler.Telecom.TelecomProviderBase telecomProvider, WOSI.CallButler.Data.DataProviders.CallButlerDataProviderBase dataProvider)
        {
            // Parse out our external event action
            if (Enum.IsDefined(typeof(ExtensionExternalCommands), command))
            {
                ExtensionExternalCommands externalCommand = WOSI.Utilities.EnumUtils <ExtensionExternalCommands> .Parse(command);

                switch (externalCommand)
                {
                case ExtensionExternalCommands.CALLBUTLERINTERNAL_ForwardCall:
                {
                    onholdTsInterface.IMLInterpreter.SetLocalVariable("TransferNumber", commandData);
                    onholdTsInterface.IMLInterpreter.SignalExternalEvent(VoicemailExternalEvents.CALLBUTLERINTERNAL_CallForwarded.ToString());

                    break;
                }

                case ExtensionExternalCommands.CALLBUTLERINTERNAL_ConfirmingTransfer:
                {
                    if (disableCallScreening || !extension.EnableCallScreening)
                    {
                        tsInterface.IMLInterpreter.SignalExternalEvent(ExtensionExternalEvents.CALLBUTLERINTERNAL_SkipConfirmation.ToString());
                    }
                    else
                    {
                        tsInterface.IMLInterpreter.SignalEventCallback(eventToken);
                    }

                    break;
                }

                case ExtensionExternalCommands.CALLBUTLERINTERNAL_GetNextNumber:
                {
                    string callerID     = onholdTsInterface.IMLInterpreter.CallerDisplayName;
                    string callerNumber = onholdTsInterface.IMLInterpreter.CallerUsername;

                    if (callerID == null || callerID.Length == 0)
                    {
                        callerID = "Unknown Caller";
                    }

                    if (callerNumber == null || callerNumber.Length == 0)
                    {
                        callerNumber = "";
                    }

                    // If we have a previous call, end it
                    if (telecomProvider.IsLineInUse(tsInterface.LineNumber))
                    {
                        telecomProvider.EndCall(tsInterface.LineNumber);
                    }
                    else
                    {
                        // Get our extension contact numbers
                        List <WOSI.CallButler.Data.CallButlerDataset.ExtensionContactNumbersRow> contactNumbers = new List <WOSI.CallButler.Data.CallButlerDataset.ExtensionContactNumbersRow>((WOSI.CallButler.Data.CallButlerDataset.ExtensionContactNumbersRow[])dataProvider.GetExtensionContactNumbers(extension.ExtensionID).Select("", "Priority ASC"));

                        if (extensionNumberIndex + 1 >= contactNumbers.Count && parentExtension != null)
                        {
                            extensionNumberIndex = parentExtensionIndex;
                            parentExtensionIndex = -1;

                            extension       = parentExtension;
                            parentExtension = null;

                            contactNumbers.Clear();
                            contactNumbers.AddRange((WOSI.CallButler.Data.CallButlerDataset.ExtensionContactNumbersRow[])dataProvider.GetExtensionContactNumbers(extension.ExtensionID).Select("", "Priority ASC"));
                        }

                        extensionNumberIndex++;

                        List <string> callBlastNumbers  = new List <string>();
                        List <string> callBlastProfiles = new List <string>();
                        int           callBlastTimeout  = Properties.Settings.Default.CallBlastTimeout;

                        while (extensionNumberIndex < contactNumbers.Count)
                        {
                            WOSI.CallButler.Data.CallButlerDataset.ExtensionContactNumbersRow contactNumber = contactNumbers[extensionNumberIndex];

                            // Is the number online?
                            if (contactNumber.Online)
                            {
                                // Does the number have hours?
                                TimeSpan utcOffset = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now);

                                if (!contactNumber.IsHoursOfOperationUTCOffsetNull())
                                {
                                    utcOffset = contactNumber.HoursOfOperationUTCOffset;
                                }

                                if (!contactNumber.HasHoursOfOperation || (contactNumber.HasHoursOfOperation && ScriptUtils.IsInHoursOfOperation(contactNumber.HoursOfOperation, utcOffset)))
                                {
                                    // Check to see if this number is a PBX IP line
                                    if ((contactNumber.CallPBXPhone || (WOSI.CallButler.Data.ExtensionContactNumberType)contactNumber.Type == WOSI.CallButler.Data.ExtensionContactNumberType.IPPhone) && registrarService != null)
                                    {
                                        int extNumber = extension.ExtensionNumber;

                                        // If this was filled in from another extension, we'll need to check the status of that extension
                                        if (contactNumber.ExtensionID != extension.ExtensionID)
                                        {
                                            WOSI.CallButler.Data.CallButlerDataset.ExtensionsRow tmpExtension = dataProvider.GetExtension(Properties.Settings.Default.CustomerID, contactNumber.ExtensionID);

                                            if (tmpExtension != null)
                                            {
                                                extNumber = tmpExtension.ExtensionNumber;
                                            }
                                        }

                                        // Check to see if this pbx phone is online
                                        PBXPresenceInfo[] presInfos = registrarService.GetPresenceInfoForExtension(extNumber);

                                        if (presInfos != null && presInfos.Length > 0)
                                        {
                                            foreach (PBXPresenceInfo presInfo in presInfos)
                                            {
                                                if (presInfo.Status == PBXPresenceStatus.Online)
                                                {
                                                    if (presInfos.Length > 1 || extension.UseCallBlast)
                                                    {
                                                        if (contactNumbers.Count == 1)
                                                        {
                                                            callBlastTimeout = contactNumber.Timeout;
                                                        }

                                                        string callBlastNumber = string.Format("sip:{0}@{1}:{2}", presInfo.ExtensionNumber, presInfo.RemoteAddress, presInfo.RemotePort);

                                                        if (!callBlastNumbers.Contains(callBlastNumber))
                                                        {
                                                            callBlastNumbers.Add(callBlastNumber);
                                                            callBlastProfiles.Add(TelecomScriptInterface.InternalProviderProfileName);
                                                        }
                                                    }
                                                    else
                                                    {
                                                        TryContactNumber(tsInterface, string.Format("sip:{0}@{1}:{2}", presInfo.ExtensionNumber, presInfo.RemoteAddress, presInfo.RemotePort), callerID, callerNumber, TelecomScriptInterface.InternalProviderProfileName, contactNumber.Timeout.ToString(), eventToken);
                                                        return;
                                                    }
                                                }
                                            }

                                            if (!extension.UseCallBlast && callBlastNumbers.Count > 0)
                                            {
                                                break;
                                            }
                                        }
                                    }
                                    else if ((WOSI.CallButler.Data.ExtensionContactNumberType)contactNumber.Type == WOSI.CallButler.Data.ExtensionContactNumberType.TelephoneNumber)
                                    {
                                        if (extension.UseCallBlast)
                                        {
                                            if (!callBlastNumbers.Contains(contactNumber.ContactNumber))
                                            {
                                                callBlastNumbers.Add(contactNumber.ContactNumber);
                                                callBlastProfiles.Add("");
                                            }
                                        }
                                        else
                                        {
                                            TryContactNumber(tsInterface, contactNumber.ContactNumber, callerID, callerNumber, "", contactNumber.Timeout.ToString(), eventToken);
                                            return;
                                        }
                                    }
                                    else if ((WOSI.CallButler.Data.ExtensionContactNumberType)contactNumber.Type == WOSI.CallButler.Data.ExtensionContactNumberType.Extension && parentExtension == null)
                                    {
                                        try
                                        {
                                            // Get our new extension
                                            WOSI.CallButler.Data.CallButlerDataset.ExtensionsRow newExtension = dataProvider.GetExtension(Properties.Settings.Default.CustomerID, new Guid(contactNumber.ContactNumber));

                                            if (newExtension != null)
                                            {
                                                if (extension.UseCallBlast)
                                                {
                                                    WOSI.CallButler.Data.CallButlerDataset.ExtensionContactNumbersRow[] newContacts = (WOSI.CallButler.Data.CallButlerDataset.ExtensionContactNumbersRow[])dataProvider.GetExtensionContactNumbers(newExtension.ExtensionID).Select("Type <> " + (int)WOSI.CallButler.Data.ExtensionContactNumberType.Extension, "Priority ASC");
                                                    contactNumbers.AddRange(newContacts);
                                                }
                                                else
                                                {
                                                    parentExtension      = extension;
                                                    parentExtensionIndex = extensionNumberIndex;

                                                    extensionNumberIndex = -1;
                                                    extension            = newExtension;

                                                    tsInterface.IMLInterpreter.SignalExternalEvent(ExtensionExternalEvents.CALLBUTLERINTERNAL_GetNextNumber.ToString());
                                                    return;
                                                }
                                            }
                                        }
                                        catch
                                        {
                                        }
                                    }
                                }
                            }

                            extensionNumberIndex++;
                        }

                        if (callBlastNumbers.Count > 0)
                        {
                            TryCallBlast(telecomProvider, tsInterface, callBlastNumbers.ToArray(), callBlastProfiles.ToArray(), callerID, callerNumber, callBlastTimeout.ToString());
                            return;
                        }
                        else
                        {
                            tsInterface.IMLInterpreter.SignalExternalEvent(ExtensionExternalEvents.CALLBUTLERINTERNAL_NoMoreNumbers.ToString());
                        }
                    }

                    break;
                }

                case ExtensionExternalCommands.CALLBUTLERINTERNAL_SendToVoicemail:
                {
                    onholdTsInterface.IMLInterpreter.SyncExternalAction -= IMLInterpreter_SyncExternalAction;

                    // Allow this line to answer calls again
                    tsInterface.Locked = false;

                    if (telecomProvider.IsLineInUse(onholdTsInterface.LineNumber))
                    {
                        telecomProvider.SendingToVoicemail(onholdTsInterface.LineNumber);
                    }

                    onholdTsInterface.IMLInterpreter.SignalExternalEvent(VoicemailExternalEvents.CALLBUTLERINTERNAL_ExtensionNotAvailable.ToString());
                    tsInterface.IMLInterpreter.SignalEventCallback(eventToken);

                    break;
                }

                case ExtensionExternalCommands.CALLBUTLERINTERNAL_ConnectCalls:
                {
                    onholdTsInterface.IMLInterpreter.SyncExternalAction -= IMLInterpreter_SyncExternalAction;

                    onholdTsInterface.IMLInterpreter.SignalExternalEvent(ExtensionExternalCommands.CALLBUTLERINTERNAL_ConnectCalls.ToString());

                    // Allow this line to answer calls again
                    tsInterface.Locked = false;

                    if (autoConnect)
                    {
                        if (telecomProvider.IsLineInUse(tsInterface.LineNumber) && telecomProvider.IsLineInUse(onholdTsInterface.LineNumber))
                        {
                            if (extension.IsUseConferenceTransferNull() || !extension.UseConferenceTransfer /*|| !Licensing.Management.AppPermissions.StatIsPermitted("Handoff")*/)
                            {
                                telecomProvider.TransferCallAttended(onholdTsInterface.LineNumber, tsInterface.LineNumber, Properties.Settings.Default.UseBridgedTransfers);
                                tsInterface.IMLInterpreter.SignalEventCallback(eventToken);
                            }
                            else
                            {
                                telecomProvider.StopSound(tsInterface.LineNumber);
                                telecomProvider.StopSound(onholdTsInterface.LineNumber);
                                int conferenceID = telecomProvider.ConferenceLines(tsInterface.LineNumber, onholdTsInterface.LineNumber);

                                // Check to see if the person calling is an internal extension
                                if (onholdTsInterface.Extension != null)
                                {
                                    onholdTsInterface.ScriptProcessor = new TransferConferenceScriptProcessor(conferenceID, scriptService, tsInterface, registrarService, extension, vmMailerService);
                                }
                                else
                                {
                                    onholdTsInterface.ScriptProcessor = new TransferConferenceParticipantScriptProcessor(conferenceID, tsInterface, extension, vmMailerService);
                                }

                                tsInterface.ScriptProcessor = new TransferConferenceScriptProcessor(conferenceID, scriptService, onholdTsInterface, registrarService, extension, vmMailerService);

                                onholdTsInterface.ScriptProcessor.StartProcessing(onholdTsInterface, telecomProvider, dataProvider);
                                tsInterface.ScriptProcessor.StartProcessing(tsInterface, telecomProvider, dataProvider);
                            }
                        }
                        else
                        {
                            tsInterface.IMLInterpreter.SignalEventCallback(eventToken);
                        }
                    }

                    break;
                }
                }
            }
        }