Beispiel #1
0
 public static cmDoConfig Load()
 {
     if (Exists)
     {
         System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(cmDoConfig));
         StreamReader reader = File.OpenText(ConfigPath);
         cmDoConfig   c      = xs.Deserialize(reader) as cmDoConfig;
         reader.Close();
         return(c == null ? new cmDoConfig() : c);
     }
     else
     {
         return(new cmDoConfig());
     }
 }
Beispiel #2
0
        static void Main(string[] args)
        {
            Growler.Initialize();
            bool quiet = false;

            try
            {
                if (args.Contains("-q") || args.Contains("-quiet"))
                {
                    quiet = true;
                }
                if (args.Contains("-config"))
                {
                    OAuthTokenResponse requestToken = OAuthUtility.GetRequestToken(ConsumerKey, ConsumerSecret, "oob");
                    Uri authorizationUri            = OAuthUtility.BuildAuthorizationUri(requestToken.Token);
                    System.Diagnostics.Process.Start(authorizationUri.AbsoluteUri);
                    string verifier = String.Empty;
                    if (InputBox("Enter authorization pin for twt", "Please enter the PIN number from the opened webpage to authorize twt:", ref verifier) == DialogResult.OK)
                    {
                        OAuthTokenResponse accessToken = OAuthUtility.GetAccessToken(ConsumerKey, ConsumerSecret, requestToken.Token, verifier);
                        Cfg.cmDoConfig     cfg         = new Cfg.cmDoConfig()
                        {
                            AccessToken = accessToken, QuietMode = false
                        };
                        cfg.Save();
                    }

                    if (!Growler.Growl(Growler.GeneralNotification, "Configuration Complete", "The configuration for twt is now set."))
                    {
                        System.Windows.Forms.MessageBox.Show("The configuration for twt is now set.", "Configuration Complete");
                    }
                }
                else
                {
                    if (!Cfg.cmDoConfig.Exists)
                    {
                        throw new System.IO.FileNotFoundException("Could not find the configuration file with authentication information.\nPlease run twt with the -config argument.", Cfg.cmDoConfig.ConfigPath);
                    }
                    Cfg.cmDoConfig cfg = Cfg.cmDoConfig.Load();

                    if (args.Length > 2 && args[0].ToLower() == "-set")
                    {
                        switch (args[1].ToLower())
                        {
                        case "quiet":
                            bool quietparam;
                            if (bool.TryParse(args[2], out quietparam))
                            {
                                cfg.QuietMode = quietparam;
                                cfg.Save();
                                if (!Growler.Growl(Growler.GeneralNotification, "Settings", "Quiet mode is now " + (quietparam ? "on" : "off") + "."))
                                {
                                    System.Windows.Forms.MessageBox.Show("Quiet mode is now " + (quietparam ? "on" : "off") + ".", "twt Settings");
                                }
                            }
                            else
                            {
                                throw new Exception("'-set quiet' expects 'true' or 'false' as a parameter.");
                            }
                            break;
                        }
                        return;
                    }


                    if (!quiet)
                    {
                        quiet = cfg.QuietMode;
                    }

                    string tweet = string.Empty;
                    foreach (string word in args)
                    {
                        if (word.StartsWith("-") && string.IsNullOrEmpty(tweet))
                        {
                            continue;
                        }
                        tweet = string.Concat(tweet, word, " ");
                    }
                    tweet = tweet.Trim();

                    OAuthTokens token = new OAuthTokens()
                    {
                        AccessToken       = cfg.AccessToken.Token,
                        AccessTokenSecret = cfg.AccessToken.TokenSecret,
                        ConsumerKey       = ConsumerKey,
                        ConsumerSecret    = ConsumerSecret
                    };

                    TwitterResponse <TwitterStatus> response = TwitterStatus.Update(token, tweet);
                    if (!quiet)
                    {
                        if (response.Result == RequestResult.Success)
                        {
                            if (!Growler.Growl(Growler.SuccessNotification, tweet))
                            {
                                System.Windows.Forms.MessageBox.Show("Tweeted!", tweet);
                            }
                        }
                        else
                        {
                            if (!Growler.Growl(Growler.ErrorNotification, "Tweet not posted: " + response.ErrorMessage))
                            {
                                System.Windows.Forms.MessageBox.Show("Tweet was not posted.", response.ErrorMessage);
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                if (!quiet)
                {
                    if (!Growler.Growl(Growler.ErrorNotification, e.Message))
                    {
                        System.Windows.Forms.MessageBox.Show(e.Message, "Error");
                    }
                }
            }
        }