/// <summary> /// Retrieves all the filters in the user's Gmail account. /// </summary> /// <returns>A <see cref="GmailFilterCollection"/> of filters.</returns> // Added by Eric Larson [[email protected]]; 5/2/2005 public GmailFilterCollection GetFilters() { // instantiate output vars GmailFilterCollection output = new GmailFilterCollection(); Uri location = new Uri(GMAIL_HOST_URL + "/mail?&ik=" + System.Web.HttpUtility.UrlEncode(this._session.IdentificationKey) + "&view=pr&pnl=f&zx=" + MakeUniqueUrl()); this._rawDataPackResponse = MakeWebRequest(location, "GET", null, null, false); // sanitize the incoming _rawDataPackResponse this._rawDataPackResponse = this._rawDataPackResponse.Replace("\n", ""); if (this._rawDataPackResponse.Length > 128) { int filterBlockStart = this._rawDataPackResponse.IndexOf("D([\"fi\""); if (filterBlockStart > -1) { // find the filter block filterBlockStart = this._rawDataPackResponse.IndexOf("[", filterBlockStart + 7); int filterBlockEnd = this._rawDataPackResponse.IndexOf("]]);", filterBlockStart) + 1; string filterBlock = this._rawDataPackResponse.Substring(filterBlockStart, filterBlockEnd - filterBlockStart); // parse the filter block into an ArrayList ArrayList filters = Utilities.ParseJSArray(filterBlock); // loop through ArrayList of Filters and insert into collection foreach (ArrayList Filter in filters) { // Using GmailFilter Indeces enum for easy changing // Added by Eric Larson [[email protected]]; 5/10/2005 GmailFilter tmpFilter = new GmailFilter(); tmpFilter.id = Int64.Parse((string)Filter[(int)GmailFilter.Indeces.id]); tmpFilter.Name = (string)Filter[(int)GmailFilter.Indeces.Name]; // Get subarray of filter settings ArrayList tmpArray = (ArrayList)Filter[(int)GmailFilter.Indeces.SubArray]; tmpFilter.From = (string)tmpArray[(int)GmailFilter.SubIndeces.From]; tmpFilter.To = (string)tmpArray[(int)GmailFilter.SubIndeces.To]; tmpFilter.Subject = (string)tmpArray[(int)GmailFilter.SubIndeces.Subject]; tmpFilter.HasWords = (string)tmpArray[(int)GmailFilter.SubIndeces.HasWords]; tmpFilter.DoesntHave = (string)tmpArray[(int)GmailFilter.SubIndeces.DoesntHave]; tmpFilter.HasAttachment = tmpArray[(int)GmailFilter.SubIndeces.HasAttachment].ToString().Length > 0 && bool.Parse((string)tmpArray[(int)GmailFilter.SubIndeces.HasAttachment]); tmpFilter.SkipInbox = tmpArray[(int)GmailFilter.SubIndeces.SkipInbox].ToString().Length > 0 && bool.Parse((string)tmpArray[(int)GmailFilter.SubIndeces.SkipInbox]); tmpFilter.StarIt = tmpArray[(int)GmailFilter.SubIndeces.StarIt].ToString().Length > 0 && bool.Parse((string)tmpArray[(int)GmailFilter.SubIndeces.StarIt]); tmpFilter.ApplyLabel = tmpArray[(int)GmailFilter.SubIndeces.ApplayLabel].ToString().Length > 0 && bool.Parse((string)tmpArray[(int)GmailFilter.SubIndeces.ApplayLabel]); tmpFilter.LabelToApply = (string)tmpArray[(int)GmailFilter.SubIndeces.LabelToApply]; tmpFilter.Forward = tmpArray[(int)GmailFilter.SubIndeces.Forward].ToString().Length > 0 && bool.Parse((string)tmpArray[(int)GmailFilter.SubIndeces.Forward]); tmpFilter.ForwardTo = (string)tmpArray[(int)GmailFilter.SubIndeces.ForwardTo]; tmpFilter.MoveToTrash = tmpArray[(int)GmailFilter.SubIndeces.MoveToTrash].ToString().Length > 0 && bool.Parse((string)tmpArray[(int)GmailFilter.SubIndeces.MoveToTrash]); // I'm not sure why the following fields are sent twice. // If Gmail changes the filter format, these may need to be used. //Filter[GmailFilter.Indeces.StarIt] = StarIt //Filter[GmailFilter.Indeces.LabelToApply] = LabelToApply //Filter[GmailFilter.Indeces.MoveToTrash] = Move To Trash //Filter[GmailFilter.Indeces.MoveToTrash2] = Move To Trash //Filter[GmailFilter.Indeces.ForwardTo] = ForwardTo //Filter[GmailFilter.Indeces.PerpetualFalse] = always seems to be false output.Add(tmpFilter); } } } return output; }
/// <summary> /// Adds a filter into the set of Gmail filters. /// </summary> /// <returns>A <see cref="bool"/> true if completed successfully.</returns> // Added by Eric Larson [[email protected]]; 5/2/2005 public bool AddFilter(GmailFilter filter) { string strLocation = GMAIL_FILTER_URL + "&ik=" + this._session.IdentificationKey + "&view=pr" + "&pnl=f" + "&at=" + this._session.Cookies["GMAIL_AT"].Value + "&act=cf" + "&cf_t=cf" + "&cf1_from=" + System.Web.HttpUtility.UrlEncode(filter.From) + "&cf1_to=" + System.Web.HttpUtility.UrlEncode(filter.To) + "&cf1_subj=" + System.Web.HttpUtility.UrlEncode(filter.Subject) + "&cf1_has=" + System.Web.HttpUtility.UrlEncode(filter.HasWords) + "&cf1_hasnot=" + System.Web.HttpUtility.UrlEncode(filter.DoesntHave) + "&cf1_attach=" + System.Web.HttpUtility.UrlEncode(filter.HasAttachment.ToString().ToLower()) + "&cf2_ar=" + System.Web.HttpUtility.UrlEncode(filter.SkipInbox.ToString().ToLower()) + "&cf2_st=" + System.Web.HttpUtility.UrlEncode(filter.StarIt.ToString().ToLower()) + "&cf2_cat=" + System.Web.HttpUtility.UrlEncode(filter.ApplyLabel.ToString().ToLower()) + "&cf2_sel=" + System.Web.HttpUtility.UrlEncode(filter.LabelToApply) + "&cf2_emc=" + System.Web.HttpUtility.UrlEncode(filter.Forward.ToString().ToLower()) + "&cf2_email=" + System.Web.HttpUtility.UrlEncode(filter.ForwardTo) + "&cf2_tr=" + System.Web.HttpUtility.UrlEncode(filter.MoveToTrash.ToString().ToLower()) + "&zx=" + MakeUniqueUrl(); Uri location = new Uri(strLocation); string referrer = GMAIL_FILTER_REFERRER_URL + "&pnl=f" + "&ik=" + this._session.IdentificationKey + "&search=cf" + "&view=tl" + "&start=0" + "&cf_f=cf1" + "&cf_t=cf2" + "&cf1_from=" + System.Web.HttpUtility.UrlEncode(filter.From) + "&cf1_to=" + System.Web.HttpUtility.UrlEncode(filter.To) + "&cf1_subj=" + System.Web.HttpUtility.UrlEncode(filter.Subject) + "&cf1_has=" + System.Web.HttpUtility.UrlEncode(filter.HasWords) + "&cf1_hasnot=" + System.Web.HttpUtility.UrlEncode(filter.DoesntHave) + "&cf1_attach=" + System.Web.HttpUtility.UrlEncode(filter.HasAttachment.ToString().ToLower()) + "&zx=" + MakeUniqueUrl(); this._rawDataPackResponse = MakeWebRequest(location, "GET", referrer, string.Empty, false); // Parse the data pack to determine if filter was stored successfully. // Added by Eric Larson [[email protected]]; 5/10/2005 ParseDataPack(); // Return Gmail's response to whether the request was successful. // Added by Eric Larson [[email protected]]; 5/10/2005 return this.CommandSuccess; }
/// <summary> /// Deletes a filter from the set of Gmail filters. /// </summary> /// <returns>A <see cref="bool"/> true if completed successfully.</returns> // Added by Eric Larson [[email protected]]; 5/2/2005 public bool DeleteFilter(GmailFilter filter) { return DeleteFilter(filter.id); }