forked from playup/ABContactHelper
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ABGetMe.m
97 lines (85 loc) · 3.04 KB
/
ABGetMe.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//
// ABGetMe.m
// Invy
//
// Created by Joeri Djojosoeparto on 1/25/12.
// Copyright (c) 2012 Bread & Pepper. All rights reserved.
//
#import "ABGetMe.h"
NSArray* AccountEmailAddresses(void)
{
static dispatch_once_t once;
dispatch_once(&once, ^{
NSString *systemLibraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSSystemDomainMask, NO) lastObject];
NSString *messageUIPath = [[systemLibraryPath stringByAppendingPathComponent:@"Frameworks"] stringByAppendingPathComponent:@"MessageUI.framework"];
NSBundle *messageUI = [NSBundle bundleWithPath:messageUIPath];
[messageUI load];
});
NSMutableArray *addresses = [NSMutableArray array];
@try
{
NSString *MailAccountProxy = [[NSArray arrayWithObjects:@"Mail", @"Account", @"Proxy", nil] componentsJoinedByString:@""];
NSString *MFMailAccountProxy = [@"MF" stringByAppendingString:MailAccountProxy];
Class MailAccountProxyClass = NSClassFromString(MFMailAccountProxy) ?: NSClassFromString(MailAccountProxy);
SEL reloadAccounts = NSSelectorFromString([[NSArray arrayWithObjects:@"reload", @"Accounts", nil] componentsJoinedByString:@""]);
SEL mailAccounts = NSSelectorFromString([[NSArray arrayWithObjects:@"mail", @"Accounts", nil] componentsJoinedByString:@""]);
SEL emailAddresses = NSSelectorFromString([[NSArray arrayWithObjects:@"email", @"Addresses", nil] componentsJoinedByString:@""]);
[MailAccountProxyClass performSelector:reloadAccounts];
for (id mailAccount in [MailAccountProxyClass performSelector:mailAccounts])
{
for (id emailAddress in [mailAccount performSelector:emailAddresses])
{
if ([emailAddress isKindOfClass:[NSString class]])
[addresses addObject:emailAddress];
}
}
}
@catch (NSException *e) {}
return [NSArray arrayWithArray:addresses];
}
ABRecordRef ABGetMe(ABAddressBookRef addressBook)
{
ABRecordRef me = NULL;
@try
{
Class ABHelper = NSClassFromString([@"AB" stringByAppendingString:@"Helper"]);
SEL sel_sharedHelper = NSSelectorFromString([@"shared" stringByAppendingString:@"Helper"]);
SEL sel_me = NSSelectorFromString([@"m" stringByAppendingString:@"e"]);
me = (ABRecordRef)[[ABHelper performSelector:sel_sharedHelper] performSelector:sel_me];
}
@catch (NSException *exception)
{
me = NULL;
}
if (me)
return me;
NSArray *accountEmailAddresses = AccountEmailAddresses();
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex peopleCount = CFArrayGetCount(people);
for (CFIndex i = 0; i < peopleCount; i++)
{
ABRecordRef record = CFArrayGetValueAtIndex(people, i);
ABMultiValueRef emails = ABRecordCopyValue(record, kABPersonEmailProperty);
if (emails)
{
CFIndex emailCount = ABMultiValueGetCount(emails);
for (CFIndex j = 0; j < emailCount; j++)
{
CFStringRef email = ABMultiValueCopyValueAtIndex(emails, j);
if (email)
{
if ([accountEmailAddresses containsObject:(id)email])
me = record;
CFRelease(email);
}
if (me)
break;
}
CFRelease(emails);
}
if (me)
break;
}
CFRelease(people);
return me;
}