Пример #1
0
        private static MailAddress[] ValidateMailTo(NameValueCollection settings, string section)
        {
            try
            {
                if (settings[section] == null)
                {
                    return new MailAddress[] {}
                }
                ;

                string[]      emails    = settings[section].Split(',');
                MailAddress[] addresses = new MailAddress[emails.Length];

                for (int i = 0; i < emails.Length; i++)
                {
                    addresses[i] = new MailAddress(emails[i]);
                }

                return(addresses);
            }
            catch
            {
                Event.WriteError("Settings", "Invalid email specified for " + section);
                throw;
            }
        }
Пример #2
0
        static void client_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                Event.WriteError("WebNotification", "An error occurred sending notification to " + e.UserState.ToString() + "\r\n" + e.Error.Message);

                lock (subscriptions_lock)
                    subscriptions.Remove(e.UserState.ToString());
            }
        }
Пример #3
0
 private static bool ValidateBool(NameValueCollection settings, string section)
 {
     try
     {
         return(Boolean.Parse(settings[section]));
     }
     catch
     {
         Event.WriteError("Settings", "Invalid bool specified for " + section);
         throw;
     }
 }
Пример #4
0
 private static int ValidateInt(NameValueCollection settings, string section)
 {
     try
     {
         return(Int32.Parse(settings[section]));
     }
     catch
     {
         Event.WriteError("Settings", "Invalid integer specified for " + section);
         throw;
     }
 }
Пример #5
0
 private static MailAddress ValidateMailFrom(NameValueCollection settings, string section)
 {
     try
     {
         return(new MailAddress(settings[section]));
     }
     catch
     {
         Event.WriteError("Settings", "Invalid email specified for " + section);
         throw;
     }
 }
Пример #6
0
        private static NameValueCollection LoadCollection(string sFile)
        {
            NameValueCollection settings = new NameValueCollection();

            try
            {
                FileStream   fs = new FileStream(sFile, FileMode.Open, FileAccess.Read);
                StreamReader sr = new StreamReader(fs);

                while (true)
                {
                    string line = sr.ReadLine();

                    if (line == null)
                    {
                        break;
                    }

                    if (line.StartsWith("#"))
                    {
                        continue;
                    }

                    int pos = line.IndexOf('=', 0);

                    if (pos == -1)
                    {
                        continue;
                    }

                    string key   = line.Substring(0, pos).Trim();
                    string value = line.Substring(pos + 1).Trim();

                    settings.Add(key, value);
                }

                sr.Close();
                fs.Close();
            }
            catch (FileNotFoundException)
            {
                Event.WriteError("Settings", "Unable to parse settings file " + sFile);
                throw;
            }

            return(settings);
        }
Пример #7
0
        private static string ValidateDirectory(NameValueCollection settings, string section)
        {
            try
            {
                if (!Directory.Exists(settings[section]))
                {
                    Directory.CreateDirectory(settings[section]);
                }

                return(settings[section]);
            }
            catch
            {
                Event.WriteError("Settings", "Invalid directory specified for " + section);
                throw;
            }
        }
Пример #8
0
        private static string[] ValidateMultipleStrings(NameValueCollection settings, string section)
        {
            try
            {
                if (settings[section] == null)
                {
                    return new string[] { }
                }
                ;

                return(settings[section].Split(','));
            }
            catch
            {
                Event.WriteError("Settings", "Invalid string specified for " + section);
                throw;
            }
        }
Пример #9
0
        private static int ValidatePort(NameValueCollection settings, string section)
        {
            try
            {
                int port = Int32.Parse(settings[section]);

                if (port < 1 || port > 65534)
                {
                    throw new Exception();
                }

                return(port);
            }
            catch
            {
                Event.WriteError("Settings", "Invalid port specified for " + section);
                throw;
            }
        }
Пример #10
0
        public void Start()
        {
            Uri uri = new Uri("http://0.0.0.0:" + Global.webapi_port + "/");

            host = new WebServiceHost(typeof(HAIService), uri);

            try
            {
                ServiceEndpoint ep = host.AddServiceEndpoint(typeof(IHAIService), new WebHttpBinding(), "");
                host.Open();

                Event.WriteInfo("WebService", "Listening on " + uri.ToString());
            }
            catch (CommunicationException ex)
            {
                Event.WriteError("WebService", "An exception occurred: " + ex.Message);
                host.Abort();
            }
        }
Пример #11
0
 private static bool ValidateYesNo(NameValueCollection settings, string section)
 {
     if (settings[section] == null)
     {
         return(false);
     }
     if (string.Compare(settings[section], "yes", true) == 0)
     {
         return(true);
     }
     else if (string.Compare(settings[section], "no", true) == 0)
     {
         return(false);
     }
     else
     {
         Event.WriteError("Settings", "Invalid yes/no specified for " + section);
         throw new Exception();
     }
 }
Пример #12
0
        private static IPAddress ValidateIP(NameValueCollection settings, string section)
        {
            if (settings[section] == "*")
            {
                return(IPAddress.Any);
            }

            if (settings[section] == "")
            {
                return(IPAddress.None);
            }

            try
            {
                return(IPAddress.Parse(section));
            }
            catch
            {
                Event.WriteError("Settings", "Invalid IP specified for " + section);
                throw;
            }
        }
Пример #13
0
        public static void Send(string type, string body)
        {
            string[] send;
            lock (subscriptions_lock)
                send = subscriptions.ToArray();

            foreach (string subscription in send)
            {
                WebClient client = new WebClient();
                client.Headers.Add(HttpRequestHeader.ContentType, "application/json");
                client.Headers.Add("type", type);
                client.UploadStringCompleted += client_UploadStringCompleted;

                try
                {
                    client.UploadStringAsync(new Uri(subscription), "POST", body, subscription);
                }
                catch (Exception ex)
                {
                    Event.WriteError("WebNotification", "An error occurred sending notification to " + subscription + "\r\n" + ex.ToString());
                    subscriptions.Remove(subscription);
                }
            }
        }
Пример #14
0
 static void client_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
 {
     if(e.Error != null)
         Event.WriteError("ProwlNotification", "An error occurred sending notification\r\n" + e.Error.Message);
 }