Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ZXEmailAddressResultParser.m 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 "ZXEmailAddressParsedResult.h"
  17. #import "ZXEmailAddressResultParser.h"
  18. #import "ZXEmailDoCoMoResultParser.h"
  19. #import "ZXResult.h"
  20. static NSCharacterSet *ZX_EMAIL_ADDRESS_RESULT_COMMA = nil;
  21. @implementation ZXEmailAddressResultParser
  22. + (void)initialize {
  23. if ([self class] != [ZXEmailAddressResultParser class]) return;
  24. ZX_EMAIL_ADDRESS_RESULT_COMMA = [NSCharacterSet characterSetWithCharactersInString:@","];
  25. }
  26. - (ZXParsedResult *)parse:(ZXResult *)result {
  27. NSString *rawText = [ZXResultParser massagedText:result];
  28. if ([rawText hasPrefix:@"mailto:"] || [rawText hasPrefix:@"MAILTO:"]) {
  29. // If it starts with mailto:, assume it is definitely trying to be an email address
  30. NSString *hostEmail = [rawText substringFromIndex:7];
  31. NSUInteger queryStart = [hostEmail rangeOfString:@"?"].location;
  32. if (queryStart != NSNotFound) {
  33. hostEmail = [hostEmail substringToIndex:queryStart];
  34. }
  35. hostEmail = [[self class] urlDecode:hostEmail];
  36. NSArray *tos;
  37. if (hostEmail.length > 0) {
  38. tos = [hostEmail componentsSeparatedByCharactersInSet:ZX_EMAIL_ADDRESS_RESULT_COMMA];
  39. }
  40. NSMutableDictionary *nameValues = [self parseNameValuePairs:rawText];
  41. NSArray *ccs;
  42. NSArray *bccs;
  43. NSString *subject = nil;
  44. NSString *body = nil;
  45. if (nameValues != nil) {
  46. if (!tos) {
  47. NSString *tosString = nameValues[@"to"];
  48. if (tosString) {
  49. tos = [tosString componentsSeparatedByCharactersInSet:ZX_EMAIL_ADDRESS_RESULT_COMMA];
  50. }
  51. }
  52. NSString *ccString = nameValues[@"cc"];
  53. if (ccString) {
  54. ccs = [ccString componentsSeparatedByCharactersInSet:ZX_EMAIL_ADDRESS_RESULT_COMMA];
  55. }
  56. NSString *bccString = nameValues[@"bcc"];
  57. if (bccString) {
  58. bccs = [bccString componentsSeparatedByCharactersInSet:ZX_EMAIL_ADDRESS_RESULT_COMMA];
  59. }
  60. subject = nameValues[@"subject"];
  61. body = nameValues[@"body"];
  62. }
  63. return [[ZXEmailAddressParsedResult alloc] initWithTos:tos ccs:ccs bccs:bccs subject:subject body:body];
  64. } else {
  65. if (![ZXEmailDoCoMoResultParser isBasicallyValidEmailAddress:rawText]) {
  66. return nil;
  67. }
  68. return [[ZXEmailAddressParsedResult alloc] initWithTo:rawText];
  69. }
  70. }
  71. @end