Esempio n. 1
0
        public async Task <ActionResult> EditBlockedNumber(string number)
        {
            using (BlockedNumberDataProvider dataProvider = new BlockedNumberDataProvider()) {
                EditModel model         = new EditModel {
                };
                BlockedNumberEntry data = await dataProvider.GetItemAsync(number);

                if (data == null)
                {
                    throw new Error(this.__ResStr("notFound", "Blocked phone number \"{0}\" not found"), number);
                }
                model.SetData(data);
                Module.Title = this.__ResStr("modTitle", "Blocked Phone Number \"{0}\"", number);
                return(View(model));
            }
        }
Esempio n. 2
0
        public async Task <ActionResult> EditBlockedNumber_Partial(EditModel model)
        {
            using (BlockedNumberDataProvider dataProvider = new BlockedNumberDataProvider()) {
                BlockedNumberEntry data = await dataProvider.GetItemAsync(model.OriginalNumber);// get the original item

                if (data == null)
                {
                    ModelState.AddModelError("Number", this.__ResStr("alreadyDeleted", "Blocked number {0} has been removed and can no longer be updated", model.OriginalNumber));
                    return(PartialView(model));
                }
                ObjectSupport.CopyData(data, model, ReadOnly: true); // update read only properties in model in case there is an error
                if (!ModelState.IsValid)
                {
                    return(PartialView(model));
                }

                data = model.GetData(data); // merge new data into original
                model.SetData(data);        // and all the data back into model for final display

                switch (await dataProvider.UpdateItemAsync(data))
                {
                default:
                case UpdateStatusEnum.RecordDeleted:
                    ModelState.AddModelError("Name", this.__ResStr("alreadyDeleted", "Blocked number {0} has been removed and can no longer be updated", model.OriginalNumber));
                    return(PartialView(model));

                case UpdateStatusEnum.NewKeyExists:
                    ModelState.AddModelError("Name", this.__ResStr("alreadyExists", "Blocked number {0} already exists", model.Number));
                    return(PartialView(model));

                case UpdateStatusEnum.OK:
                    break;
                }
                return(FormProcessed(model, this.__ResStr("okSaved", "Blocked number saved"), OnPopupClose: OnPopupCloseEnum.ReloadModule));
            }
        }
Esempio n. 3
0
        public async Task <ActionResult> Process(string request, string extension, int errCount, string token)
        {
            LogCall(request, extension);

            TwilioData twilioConfig = await TwilioConfigDataProvider.GetConfigCondAsync();

            if (twilioConfig == null || !twilioConfig.IsConfigured())
            {
                return(RejectResult("Twilio is not configured"));
            }
            string    authToken = twilioConfig.TestMode ? twilioConfig.TestAuthToken : twilioConfig.LiveAuthToken;
            IVRConfig ivrConfig = await IVRConfigDataProvider.GetConfigCondAsync();

            if (ivrConfig == null || string.IsNullOrWhiteSpace(ivrConfig.PublicKey) || string.IsNullOrWhiteSpace(ivrConfig.PrivateKey))
            {
                return(RejectResult("Config settings not available"));
            }

#if !DEBUG
            // There is something very peculiar about twilio verification. The initial request will validate correctly, but anything after that will not.
            // Even if the only difference is CallStatus (tested with a redirect to Request=Main. So I gave up and validate just the first one
            // and add my own token validation (as argument)
            if (string.IsNullOrWhiteSpace(token))
            {
                if (!Verify.VerifyTwilio(authToken, twilioConfig.TestMode ? ivrConfig.TestVerificationProcessCallUrl : ivrConfig.LiveVerificationProcessCallUrl))
                {
                    return(RejectResult("Twilio verification failed"));
                }
            }
            else
            {
                // verify token. If it wasn't generated within the last 5 minutes, reject it.
                string decryptedToken;
                RSACrypto.Decrypt(ivrConfig.PrivateKey, token, out decryptedToken);
                DateTime tokenTime = new DateTime(Convert.ToInt64(decryptedToken));
                if (tokenTime < DateTime.UtcNow.AddMinutes(-5))
                {
                    return(RejectResult("Token verification failed"));
                }
            }
#endif
            if (string.IsNullOrWhiteSpace(request))
            {
                // call log

                using (CallLogDataProvider callLogDP = new CallLogDataProvider()) {
                    await callLogDP.AddItemAsync(new CallLogEntry {
                        Caller        = GetForm("From")?.Truncate(Globals.MaxPhoneNumber),
                        CallerCity    = GetForm("CallerCity")?.Truncate(CallLogEntry.MaxCity),
                        CallerCountry = GetForm("CallerCountry")?.Truncate(CallLogEntry.MaxCountry),
                        CallerState   = GetForm("CallerState")?.Truncate(CallLogEntry.MaxState),
                        CallerZip     = GetForm("CallerZip")?.Truncate(CallLogEntry.MaxZip),
                        To            = GetForm("Called")?.Truncate(Globals.MaxPhoneNumber),
                    });
                }

                // check for blocked numbers
                using (BlockedNumberDataProvider blockedDP = new BlockedNumberDataProvider()) {
                    BlockedNumberEntry blockedEntry = await blockedDP.GetItemAsync(GetForm("From"));

                    if (blockedEntry != null)
                    {
                        return(RejectResult($"Blocked number {GetForm("From")}"));
                    }
                }

                // notify (new call)
                foreach (ExtensionPhoneNumber notifyNumber in ivrConfig.NotificationNumbers)
                {
                    if (notifyNumber.SendSMS)
                    {
                        SendSMS sendSMS = new SendSMS();
                        await sendSMS.SendMessageAsync(notifyNumber.PhoneNumber,
                                                       this.__ResStr("notifySMS", "Incoming call received from {0} ({1}, {2}, {3}, {4}) - {5}",
                                                                     GetForm("Caller"), GetForm("CallerCity"), GetForm("CallerState"), GetForm("CallerZip"), GetForm("CallerCountry"), GetForm("To")),
                                                       ThrowError : false);
                    }
                }

                // determine main action to run

                request = SECTION_MAIN;
                using (HolidayEntryDataProvider holidayDP = new HolidayEntryDataProvider()) {
                    HolidayEntry holiday = await holidayDP.GetItemAsync(DateTime.Now.Date.ToUniversalTime());

                    if (holiday != null)
                    {
                        request = SECTION_MAINHOLIDAY;
                    }
                    else if (ivrConfig.OpeningHours.IsClosed(DateTime.UtcNow))
                    {
                        request = SECTION_MAINCLOSED;
                    }
                }
            }

            string called;
            if (!TryGetForm("CalledVia", out called))
            {
                called = GetForm("Called");
            }

            if (ivrConfig.MaxErrors != 0 && errCount >= ivrConfig.MaxErrors)
            {
                request = SECTION_MAINGOODBYE;
            }

            request = request.ToLower();

            using (ScriptDataProvider scriptDP = new ScriptDataProvider()) {
                ScriptData script = await scriptDP.GetScriptAsync(called);

                if (script == null)
                {
                    return(RejectResult($"Script not found for {called}"));
                }

                // See if a valid extension was entered
                if (request == SECTION_GATHEREXTENSION.ToLower())
                {
                    string digits;
                    if (TryGetForm("Digits", out digits))
                    {
                        Extension ext = script.FindExtension(digits);
                        if (ext != null)
                        {
                            extension = ext.Digits;
                            request   = SECTION_ENTEREDEXTENSION.ToLower(); // a valid extension was entered, run EnteredExtension instead
                        }
                    }
                }

                // find the entry that matches the name and parameters
                List <ScriptEntry> entries = (from e in script.Scripts where e.Tag == request select e).ToList();
                foreach (ScriptEntry entry in entries)
                {
                    if (entry.Parms.Count > 0)
                    {
                        // check parms
                        bool valid = true;
                        foreach (ScriptParm parm in entry.Parms)
                        {
                            if (GetForm(parm.Name) != parm.Value)
                            {
                                valid = false;
                                break;
                            }
                        }
                        if (!valid)
                        {
                            continue;
                        }
                    }
                    return(await RunEntryAsync(ivrConfig, script, called, extension, entry, errCount));
                }
                throw new InternalError($"Nothing to execute - tag \"{request}\" for {called}");
            }
        }