Exemplo n.º 1
0
        protected override void ProcessRecord()
        {
            try
            {
                this.Log.Info("Invoke-OSANMA - ProcessRecord - Started");

                var notification =
                    new NMANotification
                    {
                        Description = this.Description,
                        Event = this.Event,
                        Priority = (NMANotificationPriority)Priority
                    };

                NMAClientConfiguration config = new NMAClientConfiguration();
                config.ApplicationName = "OSA";
                config.ApiKeychain = APIKey;

                var client = new NMAClient(config);

                // Post the notification.
                client.PostNotification(notification);

                WriteObject(true);
            }
            catch (Exception exc)
            {
                this.Log.Error("An error occured while trying to run the command invoke-osanma, details", exc);
            }                
        }        
Exemplo n.º 2
0
        private string BuildNotificationRequestUrl(NMANotification notification_)
        {
            if (!(_clientCfg.BaseUrl.EndsWith("/")))
            {
                _clientCfg.BaseUrl += "/";
            }

            var nmaUrlSb = new StringBuilder(_clientCfg.BaseUrl);

            nmaUrlSb.AppendFormat(
                POST_NOTIFICATION_BASE_METHOD,
                HttpUtility.UrlEncode(_clientCfg.ApiKeychain),
                HttpUtility.UrlEncode(_clientCfg.ApplicationName),
                HttpUtility.UrlEncode(notification_.Description),
                HttpUtility.UrlEncode(notification_.Event),
                ((sbyte)(notification_.Priority)));

            if (!String.IsNullOrEmpty(_clientCfg.ProviderKey))
            {
                nmaUrlSb.AppendFormat(
                    POST_NOTIFICATION_PROVIDER_PARAMETER,
                    HttpUtility.UrlEncode(_clientCfg.ProviderKey));
            }

            return(nmaUrlSb.ToString());
        }
Exemplo n.º 3
0
        public void PostNotification(NMANotification notification_)
        {
            notification_.Validate();

            var updateRequest =
                HttpWebRequest.Create(BuildNotificationRequestUrl(notification_)) as HttpWebRequest;

            updateRequest.ContentLength = 0;
            updateRequest.ContentType   = REQUEST_CONTENT_TYPE;
            updateRequest.Method        = REQUEST_METHOD_TYPE;

            var postResponse = default(WebResponse);

            try
            {
                postResponse = updateRequest.GetResponse();
            }
            finally
            {
                if (postResponse != null)
                {
                    postResponse.Close();
                }
            }
        }
Exemplo n.º 4
0
        private string BuildNotificationRequestUrl(NMANotification notification_)
        {
            if (!(_clientCfg.BaseUrl.EndsWith("/"))) _clientCfg.BaseUrl += "/";

            var nmaUrlSb = new StringBuilder(_clientCfg.BaseUrl);

            nmaUrlSb.AppendFormat(
                POST_NOTIFICATION_BASE_METHOD,
                HttpUtility.UrlEncode(_clientCfg.ApiKeychain),
                HttpUtility.UrlEncode(_clientCfg.ApplicationName),
                HttpUtility.UrlEncode(notification_.Description),
                HttpUtility.UrlEncode(notification_.Event),
                ((sbyte)(notification_.Priority)));

            if (!String.IsNullOrEmpty(_clientCfg.ProviderKey))
                nmaUrlSb.AppendFormat(
                    POST_NOTIFICATION_PROVIDER_PARAMETER,
                    HttpUtility.UrlEncode(_clientCfg.ProviderKey));

            return nmaUrlSb.ToString();
        }
Exemplo n.º 5
0
        public void PostNotification(NMANotification notification_)
        {
            notification_.Validate();

            var updateRequest = 
                HttpWebRequest.Create(BuildNotificationRequestUrl(notification_)) as HttpWebRequest;

            updateRequest.ContentLength = 0;
            updateRequest.ContentType = REQUEST_CONTENT_TYPE;
            updateRequest.Method = REQUEST_METHOD_TYPE;

            var postResponse = default(WebResponse);

            try
            {
                postResponse = updateRequest.GetResponse();
            }
            finally
            {
                if (postResponse != null)
                    postResponse.Close();
            }
        }
Exemplo n.º 6
0
        static void Main(string[] args)
        {
            try
            {

                // Command line parsing
                Arguments CommandLine = new Arguments(args, false);

                // If no arguments then display help
                if (args.Length == 0 | CommandLine["?"] != null | CommandLine["help"] != null)
                {
                    Console.WriteLine("Notify My Android Command Line Utility.\n");

                    Console.WriteLine("NMACommand [/A:application /K:key] [/P:priority] /E:event /D:description\n");

                    Console.WriteLine("/A:application       If specified /K must also be included.");
                    Console.WriteLine("                     Specifies the application name to send.");
                    Console.WriteLine("                     If omitted the value in the config file will be used.");
                    Console.WriteLine("/K:apikey            If specified /A must also be included.");
                    Console.WriteLine("                     Specifies the API Key to send the notification.");
                    Console.WriteLine("                     If omitted the value in the config file will be used.");
                    Console.WriteLine("/P:priority          Specifies the priority.");
                    Console.WriteLine("                     Emergency, High, Moderate, Normal, VeryLow.");
                    Console.WriteLine("                     If omitted Normal will be used.");
                    Console.WriteLine("/E:\"event\"           [Mandatory] Specifies the event name to send.");
                    Console.WriteLine("/D:\"description\"     [Mandatory] Specifies the description to send.");

                    Environment.Exit(0);
                }

                if (CommandLine["D"] == null | CommandLine["E"] == null)
                {
                    Console.WriteLine("Invalid command line arguments.\n/D and /E must be specified.\nUse /? to display help.");
                    Environment.Exit(1);
                }

                if (CommandLine["D"].Trim().Length == 0 | CommandLine["E"].Trim().Length == 0)
                {
                    Console.WriteLine("Invalid command line arguments.\n/D and /E must be specified and include a value.\nUse /? to display help.");
                    Environment.Exit(1);
                }

                // Create a client/notification.
                NMAClient Client = new NMAClient();

                if (CommandLine["A"] != null & CommandLine["K"] != null)
                {
                    NMAClientConfiguration clientConfig = new NMAClientConfiguration();
                    clientConfig.ApplicationName = CommandLine["A"];
                    clientConfig.ApiKeychain = CommandLine["K"];
                    Client = new NMAClient(clientConfig);
                }
                else if ((CommandLine["A"] != null & CommandLine["K"] == null) | (CommandLine["A"] == null & CommandLine["K"] != null))
                {
                    Console.WriteLine("Invalid command line arguments.\nIf using either /A or /K both switches must be specified.\nUse /? to display help.");
                    Environment.Exit(1);
                }

                // Create the notification command
                NMANotification notification =
                    new NMANotification
                    {
                        Description = CommandLine["D"],
                        Event = CommandLine["E"],
                        Priority = NMANotificationPriority.Normal
                    };

                // If a priority is set override the default above
                if (CommandLine["P"] != null)
                {
                    switch (CommandLine["P"].ToUpper())
                    {
                        case "EMERGENCY":
                        case "E":
                            notification.Priority = NMANotificationPriority.Emergency;
                            break;
                        case "HIGH":
                        case "H":
                            notification.Priority = NMANotificationPriority.High;
                            break;
                        case "MODERATE":
                        case "M":
                            notification.Priority = NMANotificationPriority.Moderate;
                            break;
                        case "NORMAL":
                        case "N":
                            notification.Priority = NMANotificationPriority.Normal;
                            break;
                        case "VERYLOW":
                        case "L":
                            notification.Priority = NMANotificationPriority.VeryLow;
                            break;
                        default:
                            notification.Priority = NMANotificationPriority.Normal;
                            break;
                    }
                }

                // Post the notification.
                Client.PostNotification(notification);
                Console.WriteLine ("Notification Sent");
            }
            catch (Exception e)
            {
                Console.WriteLine(string.Format("An error occured {0}.", e.Message));
                Environment.Exit(2);
            }

            // Clean exit return code
            Environment.Exit(0);
        }