QuoteParser.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2016-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import "QuoteParser.h"
  21. #import "NSString+Hex.h"
  22. @implementation QuoteParser
  23. + (NSString*)parseQuoteFromMessage:(NSString*)message quotedIdentity:(NSString**)quotedIdentity remainingBody:(NSString**)remainingBody {
  24. if (message == nil)
  25. return nil;
  26. static dispatch_once_t onceToken;
  27. static NSRegularExpression *quoteRegex, *lineQuotesRegex;
  28. dispatch_once(&onceToken, ^{
  29. quoteRegex = [NSRegularExpression regularExpressionWithPattern:@"\\A>\\ ([A-Z0-9\\*][A-Z0-9]{7}): (.*?)^(?!>\\ )(.+)" options:NSRegularExpressionAnchorsMatchLines|NSRegularExpressionDotMatchesLineSeparators error:nil];
  30. lineQuotesRegex = [NSRegularExpression regularExpressionWithPattern:@"^> " options:NSRegularExpressionAnchorsMatchLines error:nil];
  31. });
  32. NSTextCheckingResult *match = [quoteRegex firstMatchInString:message options:0 range:NSMakeRange(0, [message length])];
  33. if (match.numberOfRanges == 4) {
  34. if (quotedIdentity != nil) {
  35. *quotedIdentity = [message substringWithRange:[match rangeAtIndex:1]];
  36. }
  37. if (remainingBody != nil) {
  38. *remainingBody = [[message substringWithRange:[match rangeAtIndex:3]] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  39. }
  40. // Strip quotes at the beginning of each line in quoted text
  41. NSMutableString *quotedText = [NSMutableString stringWithString:[message substringWithRange:[match rangeAtIndex:2]]];
  42. [lineQuotesRegex replaceMatchesInString:quotedText options:0 range:NSMakeRange(0, [quotedText length]) withTemplate:@""];
  43. return [quotedText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  44. }
  45. return nil;
  46. }
  47. + (NSData *)parseQuoteV2FromMessage:(NSString*)message remainingBody:(NSString**)remainingBody {
  48. if (message == nil)
  49. return nil;
  50. static dispatch_once_t onceToken;
  51. static NSRegularExpression *quoteRegex, *lineQuotesRegex;
  52. dispatch_once(&onceToken, ^{
  53. quoteRegex = [NSRegularExpression regularExpressionWithPattern:@"^> quote #[0-9a-f]{16}(\r?\n){2}" options:NSRegularExpressionDotMatchesLineSeparators error:nil];
  54. lineQuotesRegex = [NSRegularExpression regularExpressionWithPattern:@"^> quote #" options:NSRegularExpressionAnchorsMatchLines error:nil];
  55. });
  56. NSTextCheckingResult *match = [quoteRegex firstMatchInString:message options:NSMatchingReportCompletion range:NSMakeRange(0, message.length)];
  57. if (match.numberOfRanges == 2) {
  58. NSRange matchRange = [match rangeAtIndex:0];
  59. if (matchRange.location == 0) {
  60. NSString *quoteString = [[message substringWithRange:[match rangeAtIndex:0]] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  61. *remainingBody = [message substringWithRange:NSMakeRange(matchRange.length, message.length - matchRange.length)];
  62. // Strip quotes at the beginning of each line in quoted text
  63. NSMutableString *messageId = [NSMutableString stringWithString:quoteString];
  64. [lineQuotesRegex replaceMatchesInString:messageId options:0 range:NSMakeRange(0, [quoteString length]) withTemplate:@""];
  65. return [messageId decodeHex];
  66. }
  67. }
  68. return nil;
  69. }
  70. @end