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.

ZXEmailDoCoMoResultParser.m 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 "ZXEmailDoCoMoResultParser.h"
  18. #import "ZXResult.h"
  19. static NSRegularExpression *ZX_ATEXT_ALPHANUMERIC = nil;
  20. @implementation ZXEmailDoCoMoResultParser
  21. + (void)initialize {
  22. if ([self class] != [ZXEmailDoCoMoResultParser class]) return;
  23. ZX_ATEXT_ALPHANUMERIC = [[NSRegularExpression alloc] initWithPattern:@"^[a-zA-Z0-9@.!#$%&'*+\\-/=?^_`{|}~]+$"
  24. options:0 error:nil];
  25. }
  26. - (ZXParsedResult *)parse:(ZXResult *)result {
  27. NSString *rawText = [ZXResultParser massagedText:result];
  28. if (![rawText hasPrefix:@"MATMSG:"]) {
  29. return nil;
  30. }
  31. NSArray *tos = [[self class] matchDoCoMoPrefixedField:@"TO:" rawText:rawText trim:YES];
  32. if (tos == nil) {
  33. return nil;
  34. }
  35. for (NSString *to in tos) {
  36. if (![[self class] isBasicallyValidEmailAddress:to]) {
  37. return nil;
  38. }
  39. }
  40. NSString *subject = [[self class] matchSingleDoCoMoPrefixedField:@"SUB:" rawText:rawText trim:NO];
  41. NSString *body = [[self class] matchSingleDoCoMoPrefixedField:@"BODY:" rawText:rawText trim:NO];
  42. return [[ZXEmailAddressParsedResult alloc] initWithTos:tos ccs:nil bccs:nil subject:subject body:body];
  43. }
  44. + (BOOL)isBasicallyValidEmailAddress:(NSString *)email {
  45. return email != nil && [ZX_ATEXT_ALPHANUMERIC numberOfMatchesInString:email options:0 range:NSMakeRange(0, email.length)] > 0 && [email rangeOfString:@"@"].location != NSNotFound;
  46. }
  47. @end