示例#1
0
 private void btnLogin_Click(object sender, EventArgs e)
 {
     if (!string.IsNullOrEmpty(this.txtUsername.Text) && !string.IsNullOrEmpty(this.txtPassword.Text))
     {
         this.username = this.txtUsername.Text;
         string        password   = this.txtPassword.Text;
         SnapConnector sc         = new SnapConnector(this.username, null, null);
         string        result     = sc.Login(this.txtUsername.Text, this.txtPassword.Text);
         JObject       jsonResult = JObject.Parse(result);
         JToken        logged     = jsonResult["logged"];
         if (logged != null)
         {
             bool loggedBool = logged.ToObject <bool>();
             if (loggedBool)
             {
                 //success!
                 this.authToken    = jsonResult["auth_token"].ToObject <string>();
                 this.DialogResult = System.Windows.Forms.DialogResult.OK;
                 this.Close();
             }
             else
             {
                 string message = jsonResult["message"].ToObject <string>();
                 MessageBox.Show(message, "Login failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
             }
         }
         else
         {
             MessageBox.Show(result, "Mmm, something went wrong...", MessageBoxButtons.OK, MessageBoxIcon.Error);
         }
     }
 }
 public byte[] GetStoryMedia(string media_id, string media_key, string media_iv)
 {
     byte[] data = this.get_Raw(string.Format("/story_blob?story_id={0}", media_id));
     if (data != null && data.Length > 0)
     {
         return(SnapConnector.decryptCBC(data, media_key, media_iv));
     }
     return(data);
 }
示例#3
0
        protected void Start()
        {
            //disable controls
            this.grpAuth.Enabled = false;
            this.btnStop.Visible = true;

            //hide
            this.Minimize();

            this.snapconnector = new SnapConnector(this.username, this.authToken, this.reqToken);

            Run = true;

            this.listener = new Thread(new ThreadStart(Listen));
            this.listener.IsBackground = true;
            this.listener.Start();
        }
示例#4
0
        protected bool FetchUpdates()
        {
            try
            {
                string data = this.snapconnector.GetUpdates();

                if (this.dlSnaps)
                {
                    JsonClasses.Snap[] snaps = this.snapconnector.GetSnaps(data);
                    //process snaps
                    foreach (JsonClasses.Snap snap in snaps)
                    {
                        if (snap.st < 2 && !string.IsNullOrEmpty(snap.sn))
                        {
                            bool dlSnap = this.connector.AddSnap(snap);
                            if (dlSnap)
                            {
                                try
                                {
                                    byte[] image = this.snapconnector.GetMedia(snap.id);
                                    if (!SnapConnector.isMedia(image))
                                    {
                                        image = SnapConnector.decryptECB(image);
                                    }
                                    string filename = this.SaveMedia(image, snap);
                                    this.NotifyTray(snap, filename);
                                }
                                catch (WebException w)
                                {
                                    //too bad
                                    HttpWebResponse resp = w.Response as HttpWebResponse;
                                    if (resp.StatusCode != HttpStatusCode.Gone && resp.StatusCode != HttpStatusCode.InternalServerError)
                                    {
                                        throw w;
                                    }
                                }
                            }
                        }
                    }
                }
                if (this.dlStories)
                {
                    JsonClasses.Story[] stories = this.snapconnector.GetStories(data);
                    //process stories
                    foreach (JsonClasses.Story story in stories)
                    {
                        if (story.media_type < 2)
                        {
                            bool dlStory = this.connector.AddStory(story);
                            if (dlStory)
                            {
                                try
                                {
                                    byte[] image    = this.snapconnector.GetStoryMedia(story.media_id, story.media_key, story.media_iv);
                                    string filename = this.SaveMedia(image, story);
                                    if (!string.IsNullOrEmpty(filename))
                                    {
                                        this.NotifyTray(story, filename);
                                    }
                                }
                                catch (WebException w)
                                {
                                    //too bad
                                    if (w.Status != WebExceptionStatus.ProtocolError)
                                    {
                                        throw w;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (WebException wex)
            {
                if (wex.Response != null)
                {
                    HttpWebResponse resp = wex.Response as HttpWebResponse;
                    if (resp.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                    {
                        MessageBox.Show("Invalid credentials", "Auth error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else if (resp.StatusCode == HttpStatusCode.InternalServerError)
                    {
                        //shit happens
                        return(true);
                    }
                    else
                    {
                        MessageBox.Show(wex.Message, "WebException in listener thread", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    this.autoStart = false;
                    return(false);
                }
                return(true);
            }
            catch (Exception ex)
            {
                //TODO: log
                MessageBox.Show(ex.Message, "General Exception in listener thread", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.autoStart = false;
                return(false);
            }
            return(true);
        }