1 /* Cydia - iPhone UIKit Front-End for Debian APT
2 * Copyright (C) 2008-2013 Jay Freeman (saurik)
5 /* GNU General Public License, Version 3 {{{ */
7 * Cydia is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation, either version 3 of the License,
10 * or (at your option) any later version.
12 * Cydia is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Cydia. If not, see <http://www.gnu.org/licenses/>.
22 #include "CyteKit/MFMailComposeViewController-MailToURL.h"
24 #include <objc/runtime.h>
27 static void $MFMailComposeViewController$setMailToURL$(MFMailComposeViewController *self, SEL _cmd, NSURL *url) {
28 NSString *scheme([url scheme]);
30 if (scheme == nil || ![[scheme lowercaseString] isEqualToString:@"mailto"])
31 [NSException raise:NSInvalidArgumentException format:@"-[MFMailComposeViewController setMailToURL:] - non-mailto: URL"];
33 NSString *href([url absoluteString]);
34 NSRange question([href rangeOfString:@"?"]);
36 NSMutableArray *to([NSMutableArray arrayWithCapacity:1]);
38 NSString *target, *query;
39 if (question.location == NSNotFound) {
40 target = [href substringFromIndex:7];
43 target = [href substringWithRange:NSMakeRange(7, question.location - 7)];
44 query = [href substringFromIndex:(question.location + 1)];
47 if ([target length] != 0)
48 [to addObject:target];
50 if (query != nil && [query length] != 0) {
51 NSMutableArray *cc([NSMutableArray arrayWithCapacity:1]);
52 NSMutableArray *bcc([NSMutableArray arrayWithCapacity:1]);
54 for (NSString *assign in [query componentsSeparatedByString:@"&"]) {
55 NSRange equal([assign rangeOfString:@"="]);
56 if (equal.location == NSNotFound)
59 NSString *name([assign substringToIndex:equal.location]);
60 NSString *value([assign substringFromIndex:(equal.location + 1)]);
61 value = [value stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
64 else if ([name isEqualToString:@"attachment"]) {
65 if (NSData *data = [NSData dataWithContentsOfFile:value])
66 [self addAttachmentData:data mimeType:@"application/octet-stream" fileName:[value lastPathComponent]];
67 } else if ([name isEqualToString:@"bcc"])
68 [bcc addObject:value];
69 else if ([name isEqualToString:@"body"])
70 [self setMessageBody:value isHTML:NO];
71 else if ([name isEqualToString:@"cc"])
73 else if ([name isEqualToString:@"subject"])
74 [self setSubject:value];
75 else if ([name isEqualToString:@"to"])
79 [self setCcRecipients:cc];
80 [self setBccRecipients:bcc];
83 [self setToRecipients:to];
86 __attribute__((__constructor__)) static void MFMailComposeViewController_CyteMailToURL() {
87 dlopen("/System/Library/Frameworks/MessageUI.framework/MessageUI", RTLD_GLOBAL | RTLD_LAZY);
88 if (Class MFMailComposeViewController = objc_getClass("MFMailComposeViewController"))
89 class_addMethod(MFMailComposeViewController, @selector(setMailToURL:), (IMP) $MFMailComposeViewController$setMailToURL$, "v12@0:4@8");