Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright 2012 ZXing authors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "ZXResult.h"
  17. #import "ZXSMSMMSResultParser.h"
  18. #import "ZXSMSParsedResult.h"
  19. @implementation ZXSMSMMSResultParser
  20. - (ZXParsedResult *)parse:(ZXResult *)result {
  21. NSString *rawText = [ZXResultParser massagedText:result];
  22. if (!([rawText hasPrefix:@"sms:"] || [rawText hasPrefix:@"SMS:"] || [rawText hasPrefix:@"mms:"] || [rawText hasPrefix:@"MMS:"])) {
  23. return nil;
  24. }
  25. // Check up front if this is a URI syntax string with query arguments
  26. NSMutableDictionary *nameValuePairs = [self parseNameValuePairs:rawText];
  27. NSString *subject = nil;
  28. NSString *body = nil;
  29. BOOL querySyntax = NO;
  30. if (nameValuePairs != nil && [nameValuePairs count] > 0) {
  31. subject = nameValuePairs[@"subject"];
  32. body = nameValuePairs[@"body"];
  33. querySyntax = YES;
  34. }
  35. // Drop sms, query portion
  36. NSUInteger queryStart = [rawText rangeOfString:@"?" options:NSLiteralSearch range:NSMakeRange(4, [rawText length] - 4)].location;
  37. NSString *smsURIWithoutQuery;
  38. // If it's not query syntax, the question mark is part of the subject or message
  39. if (queryStart == NSNotFound || !querySyntax) {
  40. smsURIWithoutQuery = [rawText substringFromIndex:4];
  41. } else {
  42. smsURIWithoutQuery = [rawText substringWithRange:NSMakeRange(4, queryStart - 4)];
  43. }
  44. int lastComma = -1;
  45. NSInteger comma;
  46. NSMutableArray *numbers = [NSMutableArray arrayWithCapacity:1];
  47. NSMutableArray *vias = [NSMutableArray arrayWithCapacity:1];
  48. while ((comma = [smsURIWithoutQuery rangeOfString:@"," options:NSLiteralSearch range:NSMakeRange(lastComma + 1, (int)[smsURIWithoutQuery length] - lastComma - 1)].location) > lastComma && comma != NSNotFound) {
  49. NSString *numberPart = [smsURIWithoutQuery substringWithRange:NSMakeRange(lastComma + 1, comma - lastComma - 1)];
  50. [self addNumberVia:numbers vias:vias numberPart:numberPart];
  51. lastComma = (int)comma;
  52. }
  53. [self addNumberVia:numbers vias:vias numberPart:[smsURIWithoutQuery substringFromIndex:lastComma + 1]];
  54. return [ZXSMSParsedResult smsParsedResultWithNumbers:numbers
  55. vias:vias
  56. subject:subject
  57. body:body];
  58. }
  59. - (void)addNumberVia:(NSMutableArray *)numbers vias:(NSMutableArray *)vias numberPart:(NSString *)numberPart {
  60. NSUInteger numberEnd = [numberPart rangeOfString:@";"].location;
  61. if (numberEnd == NSNotFound) {
  62. [numbers addObject:numberPart];
  63. [vias addObject:[NSNull null]];
  64. } else {
  65. [numbers addObject:[numberPart substringToIndex:numberEnd]];
  66. NSString *maybeVia = [numberPart substringFromIndex:numberEnd + 1];
  67. if ([maybeVia hasPrefix:@"via="]) {
  68. [vias addObject:[maybeVia substringFromIndex:4]];
  69. } else {
  70. [vias addObject:[NSNull null]];
  71. }
  72. }
  73. }
  74. @end