private void RemovePresenceInfo(PBXPresenceInfo presenceInfo)
        {
            lock (presenceDataLock)
            {
                presenceInfo.Status = PBXPresenceStatus.Offline;
                presenceData.Remove(presenceInfo);
                PerformanceCounterService.PhonesRegistered = presenceData.Count;
            }

            UpdateExtensionState(presenceInfo);
        }
 private void UpdateExtensionState(PBXPresenceInfo presenceInfo)
 {
     /*if (extStateService != null)
      * {
      *  extStateService.UpdateExtensionState(true, presenceInfo.ExtensionNumber, StateEventType.PhoneStateUpdate,
      *      new ExtensionStateParameter(StateParameterType.PhoneStatus, (int)presenceInfo.Status),
      *      new ExtensionStateParameter(StateParameterType.PhonePort, presenceInfo.RemotePort),
      *      new ExtensionStateParameter(StateParameterType.PhoneIPAddress, presenceInfo.RemoteAddress),
      *      new ExtensionStateParameter(StateParameterType.PhoneDisplayName, presenceInfo.Name));
      * }*/
 }
Ejemplo n.º 3
0
 private void UpdateExtensionState(PBXPresenceInfo presenceInfo)
 {
     /*if (extStateService != null)
     {
         extStateService.UpdateExtensionState(true, presenceInfo.ExtensionNumber, StateEventType.PhoneStateUpdate,
             new ExtensionStateParameter(StateParameterType.PhoneStatus, (int)presenceInfo.Status),
             new ExtensionStateParameter(StateParameterType.PhonePort, presenceInfo.RemotePort),
             new ExtensionStateParameter(StateParameterType.PhoneIPAddress, presenceInfo.RemoteAddress),
             new ExtensionStateParameter(StateParameterType.PhoneDisplayName, presenceInfo.Name));
     }*/
 }
Ejemplo n.º 4
0
        private void RemovePresenceInfo(PBXPresenceInfo presenceInfo)
        {
            lock (presenceDataLock)
            {
                presenceInfo.Status = PBXPresenceStatus.Offline;
                presenceData.Remove(presenceInfo);
                PerformanceCounterService.PhonesRegistered = presenceData.Count;
            }

            UpdateExtensionState(presenceInfo);
        }
Ejemplo n.º 5
0
        void ProcessRegister(PreprocessSipRequestEventArgs e)
        {
            e.Handled = true;
            bool isNewRegistration = false;

            // Remove the Record-Route field as per RFC-3261 10.3
            if (e.Request.HeaderFields.Contains("Record-Route", null))
                e.Request.HeaderFields.Remove("Record-Route");

            try
            {
                SIPURI addressOfRecord = new SIPURI(e.Request.HeaderFields["To", "t"].FieldValue);

                // Make sure the request is authorized
                if (ProcessRequestAuthorization(e, addressOfRecord, false))
                {
                    int extensionNumber = 0;

                    try
                    {
                        extensionNumber = Convert.ToInt32(addressOfRecord.User);
                    }
                    catch
                    {
                        ipClient.SendSipResponse(e.Request.CreateResponse(404, "Not Found"));
                        return;
                    }

                    PBXPresenceInfo presenceInfo = FindPresenceInfo(extensionNumber, e.Request.SentFrom, e.Request.SentFromPort, true);

                    if (e.Request.HeaderFields.Contains("Contact", "m"))
                    {
                        HeaderField[] contactFields = e.Request.HeaderFields.GetMultiHeaderFields("Contact", "m");

                        bool containsStar = false;

                        foreach (HeaderField contactField in contactFields)
                        {
                            if (contactField.FieldValue == "*")
                            {
                                containsStar = true;
                            }
                        }

                        if (containsStar)
                        {
                            if (contactFields.Length > 1 || !e.Request.HeaderFields.Contains("Expires", null) || e.Request.HeaderFields["Expires", null].FieldValue.Trim() != "0")
                            {
                                ipClient.SendSipResponse(e.Request.CreateResponse(400, "Invalid Request"));
                                return;
                            }
                        }

                        int requestedExpiration = 0;

                        if (contactFields[0].ContainsParameter("Expires"))
                            requestedExpiration = Convert.ToInt32(contactFields[0].Parameters["Expires"]);
                        else if (e.Request.HeaderFields.Contains("Expires", null))
                            requestedExpiration = Convert.ToInt32(e.Request.HeaderFields["Expires", null].FieldValue);
                        else
                            requestedExpiration = Properties.Settings.Default.PresenceTimeout;

                        if (presenceInfo != null)
                        {
                            // Check the Call-ID
                            string callID = e.Request.HeaderFields["Call-ID", "i"].FieldValue;

                            if (callID == presenceInfo.SessionID)
                            {
                                // Parse our CSeq
                                int cSeq = Convert.ToInt32(e.Request.HeaderFields["CSeq", null].FieldValue.Split(' ')[0]);

                                if (cSeq < presenceInfo.CSeq)
                                {
                                    RemovePresenceInfo(presenceInfo);

                                    ipClient.SendSipResponse(e.Request.CreateResponse(400, "Invalid Request"));

                                    return;
                                }
                            }

                            if (requestedExpiration == 0)
                            {
                                RemovePresenceInfo(presenceInfo);
                                ipClient.SendSipResponse(e.Request.CreateResponse(200, "OK"));
                                return;
                            }
                        }
                        else
                        {
                            presenceInfo = new PBXPresenceInfo();
                            isNewRegistration = true;
                        }

                        presenceInfo.AddressOfRecord = new SIPURI(e.Request.HeaderFields["To", "t"].FieldValue).BasicURIStringWithoutParameters;
                        presenceInfo.CSeq = Convert.ToInt32(e.Request.HeaderFields["CSeq", null].FieldValue.Split(' ')[0]);
                        presenceInfo.ExpiresInSeconds = requestedExpiration;
                        presenceInfo.ExtensionNumber = extensionNumber;
                        presenceInfo.LastRegistration = DateTime.Now;
                        presenceInfo.Name = addressOfRecord.DisplayName;
                        presenceInfo.RemoteAddress = e.Request.SentFrom;
                        presenceInfo.RemotePort = e.Request.SentFromPort;
                        presenceInfo.SessionID = e.Request.HeaderFields["Call-ID", "i"].FieldValue;
                        presenceInfo.Status = PBXPresenceStatus.Online;

                        if (isNewRegistration)
                        {
                            lock (presenceDataLock)
                            {
                                presenceData.Add(presenceInfo);
                                LoggingService.AddLogEntry(WOSI.CallButler.ManagementInterface.LogLevel.Extended, "Extension " + presenceInfo.ExtensionNumber + " registered for " + presenceInfo.RemoteAddress, false);

                                UpdateExtensionState(presenceInfo);
                            }
                        }
                    }

                    if (presenceInfo != null)
                    {
                        SipResponse response = e.Request.CreateResponse(200, "OK");

                        response.HeaderFields.InsertAfter("CSeq", e.Request.HeaderFields["Contact", "m"]);
                        response.HeaderFields["Contact", "m"].Parameters["expires"] = presenceInfo.ExpiresInSeconds.ToString();
                        response.HeaderFields.InsertAfter("Contact", "Date", DateTime.Now.ToUniversalTime().ToString("ddd, d MMM yyyy HH:mm:ss G\\MT"));

                        ipClient.SendSipResponse(response);

                        // Send our message waiting notification
                        if(isNewRegistration)
                            SendMessageWaitingNotification(extensionNumber);

                        PerformanceCounterService.PhonesRegistered = presenceData.Count;
                    }
                    else
                    {
                        ipClient.SendSipResponse(e.Request.CreateResponse(404, "Not Found"));
                    }

                    return;
                }
            }
            catch (Exception ex)
            {
                ipClient.SendSipResponse(e.Request.CreateResponse(500, "Server Error"));
                LoggingService.AddLogEntry(WOSI.CallButler.ManagementInterface.LogLevel.ErrorsOnly, Utils.ErrorUtils.FormatErrorString(ex), true);
            }
        }
        void ProcessRegister(PreprocessSipRequestEventArgs e)
        {
            e.Handled = true;
            bool isNewRegistration = false;

            // Remove the Record-Route field as per RFC-3261 10.3
            if (e.Request.HeaderFields.Contains("Record-Route", null))
            {
                e.Request.HeaderFields.Remove("Record-Route");
            }

            try
            {
                SIPURI addressOfRecord = new SIPURI(e.Request.HeaderFields["To", "t"].FieldValue);

                // Make sure the request is authorized
                if (ProcessRequestAuthorization(e, addressOfRecord, false))
                {
                    int extensionNumber = 0;

                    try
                    {
                        extensionNumber = Convert.ToInt32(addressOfRecord.User);
                    }
                    catch
                    {
                        ipClient.SendSipResponse(e.Request.CreateResponse(404, "Not Found"));
                        return;
                    }

                    PBXPresenceInfo presenceInfo = FindPresenceInfo(extensionNumber, e.Request.SentFrom, e.Request.SentFromPort, true);

                    if (e.Request.HeaderFields.Contains("Contact", "m"))
                    {
                        HeaderField[] contactFields = e.Request.HeaderFields.GetMultiHeaderFields("Contact", "m");

                        bool containsStar = false;

                        foreach (HeaderField contactField in contactFields)
                        {
                            if (contactField.FieldValue == "*")
                            {
                                containsStar = true;
                            }
                        }

                        if (containsStar)
                        {
                            if (contactFields.Length > 1 || !e.Request.HeaderFields.Contains("Expires", null) || e.Request.HeaderFields["Expires", null].FieldValue.Trim() != "0")
                            {
                                ipClient.SendSipResponse(e.Request.CreateResponse(400, "Invalid Request"));
                                return;
                            }
                        }

                        int requestedExpiration = 0;

                        if (contactFields[0].ContainsParameter("Expires"))
                        {
                            requestedExpiration = Convert.ToInt32(contactFields[0].Parameters["Expires"]);
                        }
                        else if (e.Request.HeaderFields.Contains("Expires", null))
                        {
                            requestedExpiration = Convert.ToInt32(e.Request.HeaderFields["Expires", null].FieldValue);
                        }
                        else
                        {
                            requestedExpiration = Properties.Settings.Default.PresenceTimeout;
                        }

                        if (presenceInfo != null)
                        {
                            // Check the Call-ID
                            string callID = e.Request.HeaderFields["Call-ID", "i"].FieldValue;

                            if (callID == presenceInfo.SessionID)
                            {
                                // Parse our CSeq
                                int cSeq = Convert.ToInt32(e.Request.HeaderFields["CSeq", null].FieldValue.Split(' ')[0]);

                                if (cSeq < presenceInfo.CSeq)
                                {
                                    RemovePresenceInfo(presenceInfo);

                                    ipClient.SendSipResponse(e.Request.CreateResponse(400, "Invalid Request"));

                                    return;
                                }
                            }

                            if (requestedExpiration == 0)
                            {
                                RemovePresenceInfo(presenceInfo);
                                ipClient.SendSipResponse(e.Request.CreateResponse(200, "OK"));
                                return;
                            }
                        }
                        else
                        {
                            presenceInfo      = new PBXPresenceInfo();
                            isNewRegistration = true;
                        }

                        presenceInfo.AddressOfRecord  = new SIPURI(e.Request.HeaderFields["To", "t"].FieldValue).BasicURIStringWithoutParameters;
                        presenceInfo.CSeq             = Convert.ToInt32(e.Request.HeaderFields["CSeq", null].FieldValue.Split(' ')[0]);
                        presenceInfo.ExpiresInSeconds = requestedExpiration;
                        presenceInfo.ExtensionNumber  = extensionNumber;
                        presenceInfo.LastRegistration = DateTime.Now;
                        presenceInfo.Name             = addressOfRecord.DisplayName;
                        presenceInfo.RemoteAddress    = e.Request.SentFrom;
                        presenceInfo.RemotePort       = e.Request.SentFromPort;
                        presenceInfo.SessionID        = e.Request.HeaderFields["Call-ID", "i"].FieldValue;
                        presenceInfo.Status           = PBXPresenceStatus.Online;

                        if (isNewRegistration)
                        {
                            lock (presenceDataLock)
                            {
                                presenceData.Add(presenceInfo);
                                LoggingService.AddLogEntry(WOSI.CallButler.ManagementInterface.LogLevel.Extended, "Extension " + presenceInfo.ExtensionNumber + " registered for " + presenceInfo.RemoteAddress, false);

                                UpdateExtensionState(presenceInfo);
                            }
                        }
                    }

                    if (presenceInfo != null)
                    {
                        SipResponse response = e.Request.CreateResponse(200, "OK");

                        response.HeaderFields.InsertAfter("CSeq", e.Request.HeaderFields["Contact", "m"]);
                        response.HeaderFields["Contact", "m"].Parameters["expires"] = presenceInfo.ExpiresInSeconds.ToString();
                        response.HeaderFields.InsertAfter("Contact", "Date", DateTime.Now.ToUniversalTime().ToString("ddd, d MMM yyyy HH:mm:ss G\\MT"));

                        ipClient.SendSipResponse(response);

                        // Send our message waiting notification
                        if (isNewRegistration)
                        {
                            SendMessageWaitingNotification(extensionNumber);
                        }

                        PerformanceCounterService.PhonesRegistered = presenceData.Count;
                    }
                    else
                    {
                        ipClient.SendSipResponse(e.Request.CreateResponse(404, "Not Found"));
                    }

                    return;
                }
            }
            catch (Exception ex)
            {
                ipClient.SendSipResponse(e.Request.CreateResponse(500, "Server Error"));
                LoggingService.AddLogEntry(WOSI.CallButler.ManagementInterface.LogLevel.ErrorsOnly, Utils.ErrorUtils.FormatErrorString(ex), true);
            }
        }