public ConversationExporterParameters(ConversationExporterConfiguration exporterConfiguration) { try { InputFilePath = exporterConfiguration.InputFilePath; if (exporterConfiguration.OutputFilePath == null) { OutputFilePath = InputFilePath.Replace(".txt", ".json"); } else { OutputFilePath = exporterConfiguration.OutputFilePath; } } catch (NullReferenceException) { throw new ArgumentNullException("Input File Path"); } FilterByUser = exporterConfiguration.FilterByUser; FilterByKeyword = exporterConfiguration.FilterByKeyword; if (exporterConfiguration.Blacklist != null) { Blacklist = exporterConfiguration.Blacklist.Split(','); } Report = exporterConfiguration.Report; }
/// <summary> /// Initializes a new instance of the <see cref="Conversation"/> class. /// </summary> /// <param name="name"> /// The name of the conversation. /// </param> /// <param name="messages"> /// The messages in the conversation. /// </param> public Conversation(string name, IEnumerable <Message> messages, ConversationExporterConfiguration configuration) { try { this.userFilter = configuration.userFilter; this.keywordInclude = configuration.keywordInclude; this.keywordExclude = configuration.keywordExclude; this.blackList = configuration.blacklistFilter; this.hideCreditCard = configuration.hideCreditCard; this.hidePhone = configuration.hidePhone; this.obfuscateUser = configuration.obfuscateUser; } catch (NullReferenceException) // normally in case of Unit Tests { this.userFilter = null; this.keywordInclude = null; this.keywordExclude = null; this.blackList = null; this.hideCreditCard = false; this.hidePhone = false; this.obfuscateUser = false; } this.name = name; this.filteredMessages = messages; }
/// <summary> /// Parses the given <paramref name="arguments"/> into the exporter configuration. /// </summary> /// <param name="arguments"> /// The command line arguments. /// </param> /// <returns> /// A <see cref="ConversationExporterConfiguration"/> representing the command line arguments. /// </returns> public static ConversationExporterConfiguration ParseCommandLineArguments(string[] arguments) { if (arguments.Length < 1) { throw new ArgumentException("Must specify file to read from."); } if (arguments.Length < 2) { throw new ArgumentException("Must specify file to write to."); } ConversationExporterConfiguration cec = new ConversationExporterConfiguration(arguments[0], arguments[1]); for (int i = 0; i < arguments.Length; i++) { if (arguments[i] == "-fu") { try { cec.userToFilter = arguments[i + 1]; } catch { throw new ArgumentException("Must include userId after -fu."); } } else if (arguments[i] == "-fk") { try { cec.keywordToFilter = arguments[i + 1]; } catch { throw new ArgumentException("Must include keyword after -fk."); } } else if (arguments[i] == "-bw") { try { cec.wordToBlacklist = arguments[i + 1]; } catch { throw new ArgumentException("Must include blacklist word after -bw."); } } else if (arguments[i] == "-bn") { cec.blacklistNumbers = true; } else if (arguments[i] == "-o") { cec.obfuscate = true; } } return(cec); }
public Conversation PerformActions(List <FilterType> actionList, ConversationExporterConfiguration configuration) { foreach (FilterType action in actionList) { if (action == FilterType.KEYWORD) { conversation = ModifyByKey(configuration.keyword, action, conversation.messages); } if (action == FilterType.SENDER_ID) { conversation = ModifyByKey(configuration.user, action, conversation.messages); } if (action == FilterType.BLACKLIST) { conversation = ModifyByBlacklist(configuration.blacklist, conversation.messages); } if (action == FilterType.HIDE_SENSITIVE_DATA) { conversation = ModifyBySensitiveData(conversation.messages); } if (action == FilterType.OBFUSCATE_IDS) { conversation = ModifyByObfiscatingIDs(conversation.messages); } } return(conversation); }
/// <summary> /// Helper method that checks if a <paramref name="msg"/> is sent by usernameFilter in <paramref name="configuration"/> /// </summary> /// <param name="configuration"> /// The Export Configuration /// </param> /// <param name="msg"> /// The message to be checked /// </param> /// <returns></returns> public bool checkUsername(ConversationExporterConfiguration configuration, Message msg) { //Check if username exists in the message if (configuration.usernameFilter != null && msg.senderId.ToLower().Equals(configuration.usernameFilter.ToLower())) { return true; } else if(configuration.usernameFilter == null) { return true; } return false; }
/// <summary> /// Helper method that checks if a <paramref name="msg"/> contains the specified keyword in <paramref name="configuration"/> /// </summary> /// <param name="configuration"> /// The Export Configuration /// </param> /// <param name="msg"> /// The message to be checked /// </param> /// <returns></returns> public bool checkKeyWord(ConversationExporterConfiguration configuration, Message msg) { //Check if keywork exists in the message if (configuration.keyword != null && msg.content.ToLower().Contains(configuration.keyword.ToLower())) { return true; } else if (configuration.keyword == null) { return true; } return false; }
/// <summary> /// Exports the conversation using the <paramref name="configuration"/> as export configuration. /// </summary> /// <param name="configuration"> /// The configuration which containg the inputFilePath, outputFilePath and various filters. /// </param> /// <exception cref="ArgumentException"> /// Thrown when a path is invalid. /// </exception> /// <exception cref="Exception"> /// Thrown when something bad happens. /// </exception> public void ExportConversation(ConversationExporterConfiguration configuration) { Conversation conversation = this.ReadConversation(configuration); if ( conversation == null) { Console.WriteLine("Could not read conversation"); Console.WriteLine("Press any key to continue..."); Console.Read(); } else { this.WriteConversation(conversation, configuration); Console.WriteLine("Conversation exported from '{0}' to '{1}'", configuration.inputFilePath, configuration.outputFilePath); Console.WriteLine("Press any key to continue..."); Console.Read(); } }
public ConversationExporterConfiguration ParseCommandLineArguments(string[] arguments) { if (arguments.Length != 0) { Parser.Default.ParseArguments <Options>(arguments) .WithParsed <Options>(o => { inputPath = o.Input; outputPath = o.Output; if (!File.Exists(inputPath)) { throw new FileNotFoundException(Globals.EXCEPTION_FILE_NOT_FOUND); } if (!Directory.Exists(Path.GetDirectoryName(outputPath))) { throw new DirectoryNotFoundException(Globals.EXCEPTION_DIRECTORY_NOT_FOUND); } configuration = new ConversationExporterConfiguration(inputPath, outputPath); configuration.user = o.User != null ? o.User : null; configuration.keyword = o.Keyword != null ? o.Keyword : null; configuration.blacklist = o.Blacklist != null ? o.Blacklist.ToList() : null; configuration.hideSensitiveData = o.HideSensitiveData; configuration.obfuscateUserIDs = o.ObfuscateUserIDs; }); } else { throw new ArgumentException(Globals.EXCEPTION_ARGUMENT_NOT_FOUND); } return(configuration); }
/// <summary> /// Helper method to write the <paramref name="conversation"/> as JSON to outputFilePath in <paramref name="configuration"/>. /// </summary> /// <param name="conversation"> /// The conversation. /// </param> /// <param name="configuration"> /// The export configuration. /// </param> /// <exception cref="ArgumentException"> /// Thrown when there is a problem with the <paramref name="outputFilePath"/>. /// </exception> /// <exception cref="Exception"> /// Thrown when something else bad happens. /// </exception> public void WriteConversation(Conversation conversation, ConversationExporterConfiguration configuration) { try { var writer = new StreamWriter(new FileStream(configuration.outputFilePath, FileMode.Create, FileAccess.ReadWrite)); var serialized = JsonConvert.SerializeObject(conversation, Formatting.Indented); writer.Write(serialized); writer.Flush(); writer.Close(); } catch (SecurityException) { Console.WriteLine("No permission to file."); configuration.outputFilePath = PromptUserForInput("Please give new file path for output"); WriteConversation(conversation, configuration); } catch (DirectoryNotFoundException) { Console.WriteLine("Path invalid."); configuration.outputFilePath = PromptUserForInput("Please give new file path for output"); WriteConversation(conversation, configuration); } catch (Exception) { Console.WriteLine("Something went wrong in the IO."); configuration.outputFilePath = PromptUserForInput("Please give new file path for output "); WriteConversation(conversation, configuration); } }
/// <summary> /// Helper method to read the conversation from <paramref name="configuration"/>. /// </summary> /// <param name="configuration"> /// The configuration which contains the inputFilePath /// </param> /// <returns> /// A <see cref="Conversation"/> model representing the conversation. /// </returns> /// <exception cref="ArgumentException"> /// User can provide a new path for coversation file /// </exception> /// <exception cref="Exception"> /// User has to give new conversation file path /// </exception> public Conversation ReadConversation(ConversationExporterConfiguration configuration) { string inputFilePath = configuration.inputFilePath; try { var reader = new StreamReader(new FileStream(inputFilePath, FileMode.Open, FileAccess.Read), Encoding.ASCII); string conversationName = reader.ReadLine(); var messages = new List<Message>(); string line; while ((line = reader.ReadLine()) != null) { string timestamp =""; string userID = ""; StringBuilder messageContent = new StringBuilder(); string []chatLine = line.Split(' '); timestamp = chatLine[0].Substring(0, 10); //timestamps are only 10 length userID = chatLine[1]; for (int i = 2; i<chatLine.Length; i++) { messageContent.Append(chatLine[i]+" "); } messageContent.Remove(messageContent.Length-1,1); Message msg = new Message(DateTimeOffset.FromUnixTimeSeconds(Convert.ToInt64(timestamp)), userID.ToString(), messageContent.ToString()); if (configuration.filtersActive && checkUsername(configuration,msg) && checkKeyWord(configuration, msg)) { if (configuration.obfuscateUserIDsFlag) { obfuscateUserIDs(configuration, msg); } hideSpecificWords(configuration, msg); messages.Add( msg ); } else if (!configuration.filtersActive) { if (configuration.obfuscateUserIDsFlag) { obfuscateUserIDs(configuration, msg); } hideSpecificWords(configuration, msg); messages.Add(msg); } }//end of while loop return new Conversation(conversationName, messages); } catch (FileNotFoundException) { Console.WriteLine("The input conversation file was not found."); configuration.inputFilePath = PromptUserForInput("Please give new path for file."); return ReadConversation(configuration); } catch (Exception) { Console.WriteLine("Something went wrong in the IO."); configuration.inputFilePath = PromptUserForInput("Please provide a different conversation or fix chat file."); return ReadConversation(configuration); } }
/// <summary> /// Helper method to Obfuscate user IDs from <paramref name="msg"/> using the list from <paramref name="configuration"/> /// </summary> /// <param name="configuration"> /// The Exporter's configuration /// </param> /// <param name="msg"> the message to obfuscate the user id </param> public void obfuscateUserIDs(ConversationExporterConfiguration configuration, Message msg) { if (configuration.usersMapper.ContainsKey(msg.senderId)) { msg.senderId = configuration.usersMapper[msg.senderId]; } else { int count = configuration.usersMapper.Count; configuration.usersMapper.Add(msg.senderId, "User" + (count + 1)); msg.senderId = configuration.usersMapper[msg.senderId]; } }
/// <summary> /// Helper method that hides specific words in a <paramref name="msg"/> /// </summary> /// <param name="configuration"> /// The Export Configuration /// </param> /// <param name="msg"> /// The message to be checked /// </param> /// <returns></returns> public void hideSpecificWords(ConversationExporterConfiguration configuration, Message msg) { string [] msgContent ; if (configuration.wordsBlacklist != null) { foreach (string wordToHide in configuration.wordsBlacklist) { msgContent = msg.content.Split(' ' ); foreach(string word in msgContent) { if (word.ToLower().Contains(wordToHide.ToLower())) { string temp = word; temp = temp.ToLower().Replace(wordToHide.ToLower(), "*redacted*"); msg.content = msg.content.Replace(word, temp); } } } } }
/// <summary> /// Initialise new instance of <see cref="AdditionalConversationOptions"> /// </summary> /// <param name="exporterConfig"> /// Configuration of commandline arguments /// </param> public AdditionalConversationOptions(ConversationExporterConfiguration exporterConfig) { exporterConfiguration = exporterConfig; }