/// <summary> /// Takes a filepath from the calling method, checks if it exists, and reads the data from it. /// </summary> /// <param name="filePath">String representing the filepath to read from.</param> /// <returns> /// <param name="=parsedDetails">Returns the RCDetails class deserialized from the JSON object.</param> /// </returns> public RCDetails GetJson(string filePath) { try { //Confirms file exists if (File.Exists(Path.Combine(Directory.GetCurrentDirectory().ToString() + jsonFilePath))) { //Initialize json variable to a string, then deserialize it into an instance of RCDetails var json = File.ReadAllText(Path.Combine(Directory.GetCurrentDirectory().ToString() + filePath)); RCDetails parsedDetails = JsonConvert.DeserializeObject <RCDetails>(json); //Return deserialized JSON return(parsedDetails); } else { return(null); } } //Generic exception handling catch (Exception ex) { DebugLog(ex); return(null); } }
/// <summary> /// Takes user Reddit credentials, encrypts them using <see cref="EncodePassword(string)"/>, and saves them to JSON object /// </summary> /// <param name="user">String representing user's Reddit user name.</param> /// <param name="password">String representing user's Reddit password</param> /// <returns> /// <c>true</c> if login credentials are valid and successfully saved, otherwise <c>false</c>. /// </returns> public bool NewLogin(string user, string password) { try { //Initialize instance of RCDetails class RCDetails json = GetJson(jsonFilePath); //If GetJson() is unsuccessful, set RCDetails to a new instance. if (json == null) { json = new RCDetails(); } //Confirms internet connection is present, throwing an exception if not. if (rcConnectivity.IsNetworkAvailable(0) == false) { throw new Exception("Please check your internet connection"); } //Attempt a login to confirm the validity of the reddit credentials. var reddit = new Reddit(); var login = reddit.LogIn(user, password); //Write credentials to JSON file. WriteToFile(json); return(true); } //Generic exception handling, geared towards failure of Reddit login catch { DebugLog(new Exception("Login credentials are invalid. Please confirm them in rcConfig")); return(false); } }
/// <summary> /// Takes a string from the passing method, and checks file existence, then uses <see cref="GetJson(string)"/> to retrieve /// saved JSON object. The JSON's email and password attributes are then decoded using <see cref="DecodePassword(string)"/> /// and used to send a MailMessage object using SmtpClient to the user, informing them of a posted match. /// </summary> /// <param name="result">Passed string for notification purposes.</param> /// <param name="url">Passed URL for ease of access</param> public void NotifyUser(List <string> result, List <string> url) { //Initialize helper class rcHelper rc = new rcHelper(); //Confirms rcData.json exists if (!File.Exists(Path.Combine(Directory.GetCurrentDirectory().ToString() + jsonFilePath))) { rc.DebugLog(new Exception("JSON has not been created")); Environment.Exit(0); } //Retrieves json object from file path RCDetails json = rc.GetJson(jsonFilePath); string email = rc.DecodePassword(json.email); string password = rc.DecodePassword(json.ePass); //If credentials exist, continue. if (email != null && password != null) { //Send details of post to the user in an email. try { //Initialize mail object & client MailMessage mail = new MailMessage(); SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com"); //Adds details to mail object StringBuilder strMailContent = new StringBuilder(); mail.From = new MailAddress(email); mail.To.Add(email); mail.Subject = "New Reddit Post!"; for (int i = 0; i < result.Count(); i++) { strMailContent.Append(result[i] + "\n" + url[i] + "\n\n"); } mail.Body = "The following posts were created " + DateTime.Now.ToShortDateString() + ":\n\n" + strMailContent; //Sets port, credentials, SSL, and sends mail object. SmtpServer.Port = 587; SmtpServer.Credentials = new System.Net.NetworkCredential(email, password); SmtpServer.EnableSsl = true; SmtpServer.Send(mail); Console.WriteLine("mail Sent"); SmtpServer.Dispose(); mail.Dispose(); } //Generic exception handling catch (Exception ex) { rc.DebugLog(ex); } } }
/// <summary> /// Takes an instance of the RCDetails class, serializes it & saves it as a Json object /// </summary> /// <param name="rcd">Instance of classe detailing all saved information</param> public void WriteToFile(RCDetails rcd) { //Serialize input instance of RCDetails class string JSONresult = JsonConvert.SerializeObject(rcd, Formatting.Indented); //Remove old JSON object if (File.Exists(Path.Combine(Directory.GetCurrentDirectory().ToString() + jsonFilePath))) { File.Delete(Path.Combine(Directory.GetCurrentDirectory().ToString() + jsonFilePath)); } //Create new JSON object with updated details File.WriteAllText(Path.Combine(Directory.GetCurrentDirectory().ToString() + jsonFilePath), JSONresult); }
/// <summary> /// Takes user email credentials, encrypts them using <see cref="EncodePassword(string)"/>, and saves them to JSON object /// </summary> /// <param name="email">String representing user's email address</param> /// <param name="pass">String representing user's email password</param> /// <returns> /// <c>true</c> if credentials are valid and successfully saved, otherwise <c>false</c>. /// </returns> public bool NewEmail(string email, string pass) { //Initialize deserialized JSON RCDetails json = GetJson(jsonFilePath); try { //Create new instance of RCDetails if json is not successfully retrieved if (json == null) { json = new RCDetails(); } //Encodes credentials and saves them to JSON json.email = EncodePassword(email); json.ePass = EncodePassword(pass); //Tests input credentials by sending a test email. SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com"); SmtpServer.Port = 587; SmtpServer.Credentials = new System.Net.NetworkCredential(email, pass); SmtpServer.EnableSsl = true; MailMessage mail = new MailMessage(); mail.From = new MailAddress(email); mail.To.Add(email); mail.Subject = "Redditcrawler Email Test"; mail.Body = "Your RedditCrawler email has been successfully verified!"; SmtpServer.Send(mail); //Assuming no exception is thrown, write to JSON WriteToFile(json); return(true); } //Generic exception handling catch (Exception ex) { DebugLog(ex); return(false); } }
/// <summary> /// Method for confirming whether or not all fields of JSON object have been initialized /// </summary> /// <returns><c>true</c> if all fields are input, otherwise <c>false</c></returns> public bool ApplicationReady() { //Initialize helper class rcHelper rc = new rcHelper(); //Initialize instance of RCDetails class RCDetails json = rc.GetJson(jsonFilePath); //If json retrieval fails, return false if (json == null) { return(false); } //If all fields of RCDetails class are valid, return true else if (json.email != null && json.ePass != null && json.sub != null && json.searchCriteria != null) { return(true); } //Return false by default return(false); }
/// <summary> /// Handles saving of whether or not toast notifications are enabled through the UI /// </summary> /// <returns> /// <c>true</c> if toast notifications are enabled, otherwise <c>false</c>. /// </returns> public void ToggleToast(string toastStatus) { try { //Retrieves deserialized JSON RCDetails json = GetJson(jsonFilePath); //Creates new instance of RCDetails if retrieval fails if (json == null) { json = new RCDetails(); } //Saves new toastStatus attribute to JSON json.toast = toastStatus; WriteToFile(json); } //Generic exception handling catch (Exception ex) { DebugLog(ex); } }
///<summary> ///Takes search criteria as a List, and appends it to the json object /// </summary> /// <param name="sr">List containing all user's search criteria separated into individual strings</param> public void NewSearchCriteria(List <string> sr) { try { //Retrieves deserialized JSON RCDetails json = GetJson(jsonFilePath); //If retrieval fails, create new RCDetails if (json == null) { json = new RCDetails(); } //Change searchCriteria attribute of JSON and save it json.searchCriteria = sr; WriteToFile(json); } //Generic exception handling catch (Exception ex) { DebugLog(ex); } }
/// <summary> /// Takes subreddit details in string form, and saves them to a local JSON object for future use. /// </summary> /// <param name="sub">Subreddit for RedditCrawler to monitor in string format</param>. public void NewSub(string sub) { try { //Initialize deserialized JSON object RCDetails json = GetJson(jsonFilePath); //Confirm json was successfully retrieved, instantiating a new instance of RCDetails if not. if (json == null) { json = new RCDetails(); } //Change json attribute & write it to object json.sub = sub; WriteToFile(json); } //Generic exception handling catch (Exception ex) { DebugLog(ex); } }