]> git.saurik.com Git - cydia.git/blob - CyteKit/MFMailComposeViewController-MailToURL.mm
Merge numerous (small) improvements from rs/SDURLCache.
[cydia.git] / CyteKit / MFMailComposeViewController-MailToURL.mm
1 /* Cydia - iPhone UIKit Front-End for Debian APT
2 * Copyright (C) 2008-2011 Jay Freeman (saurik)
3 */
4
5 /* Modified BSD License {{{ */
6 /*
7 * Redistribution and use in source and binary
8 * forms, with or without modification, are permitted
9 * provided that the following conditions are met:
10 *
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
18 * distribution.
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.
22 *
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.
37 */
38 /* }}} */
39
40 #include "CyteKit/MFMailComposeViewController-MailToURL.h"
41
42 #include <objc/runtime.h>
43 #include <dlfcn.h>
44
45 static void $MFMailComposeViewController$setMailToURL$(MFMailComposeViewController *self, SEL _cmd, NSURL *url) {
46 NSString *scheme([url scheme]);
47
48 if (scheme == nil || ![[scheme lowercaseString] isEqualToString:@"mailto"])
49 [NSException raise:NSInvalidArgumentException format:@"-[MFMailComposeViewController setMailToURL:] - non-mailto: URL"];
50
51 NSString *href([url absoluteString]);
52 NSRange question([href rangeOfString:@"?"]);
53
54 NSMutableArray *to([NSMutableArray arrayWithCapacity:1]);
55
56 NSString *target, *query;
57 if (question.location == NSNotFound) {
58 target = [href substringFromIndex:7];
59 query = nil;
60 } else {
61 target = [href substringWithRange:NSMakeRange(7, question.location - 7)];
62 query = [href substringFromIndex:(question.location + 1)];
63 }
64
65 if ([target length] != 0)
66 [to addObject:target];
67
68 if (query != nil && [query length] != 0) {
69 NSMutableArray *cc([NSMutableArray arrayWithCapacity:1]);
70 NSMutableArray *bcc([NSMutableArray arrayWithCapacity:1]);
71
72 for (NSString *assign in [query componentsSeparatedByString:@"&"]) {
73 NSRange equal([assign rangeOfString:@"="]);
74 if (equal.location == NSNotFound)
75 continue;
76
77 NSString *name([assign substringToIndex:equal.location]);
78 NSString *value([assign substringFromIndex:(equal.location + 1)]);
79 value = [value stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
80
81 if (false);
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"])
90 [cc addObject:value];
91 else if ([name isEqualToString:@"subject"])
92 [self setSubject:value];
93 else if ([name isEqualToString:@"to"])
94 [to addObject:value];
95 }
96
97 [self setCcRecipients:cc];
98 [self setBccRecipients:bcc];
99 }
100
101 [self setToRecipients:to];
102 }
103
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");
108 }