1 /* Cydia - iPhone UIKit Front-End for Debian APT
2 * Copyright (C) 2008-2011 Jay Freeman (saurik)
5 /* Modified BSD License {{{ */
7 * Redistribution and use in source and binary
8 * forms, with or without modification, are permitted
9 * provided that the following conditions are met:
11 * 1. Redistributions of source code must retain the
12 * above copyright notice, this list of conditions
13 * and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the
15 * above copyright notice, this list of conditions
16 * and the following disclaimer in the documentation
17 * and/or other materials provided with the
19 * 3. The name of the author may not be used to endorse
20 * or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS''
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
25 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include "CyteKit/MFMailComposeViewController-MailToURL.h"
42 #include <objc/runtime.h>
45 static void $MFMailComposeViewController$setMailToURL$(MFMailComposeViewController *self, SEL _cmd, NSURL *url) {
46 NSString *scheme([url scheme]);
48 if (scheme == nil || ![[scheme lowercaseString] isEqualToString:@"mailto"])
49 [NSException raise:NSInvalidArgumentException format:@"-[MFMailComposeViewController setMailToURL:] - non-mailto: URL"];
51 NSString *href([url absoluteString]);
52 NSRange question([href rangeOfString:@"?"]);
54 NSMutableArray *to([NSMutableArray arrayWithCapacity:1]);
56 NSString *target, *query;
57 if (question.location == NSNotFound) {
58 target = [href substringFromIndex:7];
61 target = [href substringWithRange:NSMakeRange(7, question.location - 7)];
62 query = [href substringFromIndex:(question.location + 1)];
65 if ([target length] != 0)
66 [to addObject:target];
68 if (query != nil && [query length] != 0) {
69 NSMutableArray *cc([NSMutableArray arrayWithCapacity:1]);
70 NSMutableArray *bcc([NSMutableArray arrayWithCapacity:1]);
72 for (NSString *assign in [query componentsSeparatedByString:@"&"]) {
73 NSRange equal([assign rangeOfString:@"="]);
74 if (equal.location == NSNotFound)
77 NSString *name([assign substringToIndex:equal.location]);
78 NSString *value([assign substringFromIndex:(equal.location + 1)]);
79 value = [value stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
82 else if ([name isEqualToString:@"attachment"]) {
83 if (NSData *data = [NSData dataWithContentsOfFile:value])
84 [self addAttachmentData:data mimeType:@"application/octet-stream" fileName:[value lastPathComponent]];
85 } else if ([name isEqualToString:@"bcc"])
86 [bcc addObject:value];
87 else if ([name isEqualToString:@"body"])
88 [self setMessageBody:value isHTML:NO];
89 else if ([name isEqualToString:@"cc"])
91 else if ([name isEqualToString:@"subject"])
92 [self setSubject:value];
93 else if ([name isEqualToString:@"to"])
97 [self setCcRecipients:cc];
98 [self setBccRecipients:bcc];
101 [self setToRecipients:to];
104 __attribute__((__constructor__)) static void MFMailComposeViewController_CyteMailToURL() {
105 dlopen("/System/Library/Frameworks/MessageUI.framework/MessageUI", RTLD_GLOBAL | RTLD_LAZY);
106 if (Class MFMailComposeViewController = objc_getClass("MFMailComposeViewController"))
107 class_addMethod(MFMailComposeViewController, @selector(setMailToURL:), (IMP) $MFMailComposeViewController$setMailToURL$, "v12@0:4@8");