forked from dhess/contacts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contacts.m
342 lines (282 loc) · 9.42 KB
/
contacts.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
* contacts.m
*
* Makes the Mac OS X address book available from the command-line
*
* AddressBook info available from this URL:
*
* http://developer.apple.com/techpubs/macosx/AdditionalTechnologies/AddressBook
*
* @author Shane Celis <shane@gnufoo.org>
*
*/
/*
* Here are the format specifies:
*
* %u unique identifier
*
* %n name (first and last)
* %fn first name
* %ln last name
* %nn nick name
*
* %p phone (whichever comes up first)
* %hp home phone
* %wp work phone
* %mp mobile phone
* %Mp main phone
* %fp fax phone
* %op other phone
* %pp pager phone
*
* %a addresss (whichever comes up first)
* %ha home address
* %wa work address
*
* %e email (whichever comes up first)
* %he home email
* %we work email
* %oe other email
*
* %t title
* %c company
* %g group
* %w homepage/webpage
*
* %b birthday (full NSCalendarDate description)
* %bb birthday (mm/dd)
* %i instant messaging
* %ai aim IM
* %yi Yahoo IM
* %ji Jabber IM
* %ii ICQ IM
* %mi MSN IM
*
* %N note
*
* Maybe the best way to do this would be to create a map. Use the
* format tag (e.g. "%n") as the key and the header (e.g. "NAME") and
* the printf format tag (e.g. "%-15s") pair as the value. This would
* be a nice solution for _most_ of the entries, however, there are a
* number of special cases that require more than just a map.
*
*/
#import <Foundation/Foundation.h>
#import <AddressBook/AddressBook.h>
#import <unistd.h>
#import "FormatHelper.h"
// <prototypes>
int usage();
void printPeopleWithFormat(NSArray *people, NSString *format);
void showHeader(NSArray* formatters);
int peopleSort(id peep1, id peep2, void *context);
// </prototypes>
// members
BOOL show_headers = YES;
BOOL only_birthdays = NO;
BOOL sort = NO;
// These are declared in FormatHelper.m
extern BOOL loose;
extern BOOL strict;
extern BOOL firstname_first;
/*
Prints usage and returns an error code of 2.
*/
int usage() {
fprintf(stderr, "usage: contacts [-hHsmnlS] [-c|-f format] [search]\n");
// I don't know if I should make it a full-fledged client.
//fprintf(stderr, " contacts -a first-name last-name phone-number\n");
fprintf(stderr, " -h displays help (this)\n");
fprintf(stderr, " -H suppress header\n");
fprintf(stderr, " -s sort list\n");
fprintf(stderr, " -m show me\n");
fprintf(stderr, " -n displays note below each record\n");
fprintf(stderr, " -l loose formatting (doesn't truncate record values)\n");
fprintf(stderr, " -S strict formatting (doesn't add space between columns)\n");
fprintf(stderr, " -c output as calendar(1) file format\n");
fprintf(stderr, " -f accepts a format string (see man page)\n");
fprintf(stderr, "\n");
fprintf(stderr, "displays contacts from the AddressBook database\n");
return 2;
}
int main (int argc, char * argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
BOOL show_me = NO;
BOOL show_all = NO;
BOOL display_note = NO;
char ch;
NSString *format = @"%n %p %i %e";
while ((ch = getopt(argc, argv, "lSHsnmhcf:")) != -1)
switch (ch) {
case 'l':
loose = YES;
break;
case 'f':
format = [NSString stringWithUTF8String: optarg];
break;
case 'c':
show_headers = NO;
strict = YES;
format = @"%bb\t%n";
only_birthdays = YES;
break;
case 'H':
show_headers = NO;
break;
case 'n':
display_note = YES;
break;
case 'm':
show_me = YES;
break;
case 's':
sort = YES;
break;
case 'S':
strict = YES;
break;
case 'h':
default:
return usage();
}
argc -= optind;
argv += optind;
if (argc == 0)
show_all = YES;
if (argc > 1)
return usage();
ABAddressBook *AB = [ABAddressBook sharedAddressBook];
NSArray *peopleFound;
firstname_first = ([AB defaultNameOrdering] == kABFirstNameFirst ?
YES : NO);
if (display_note)
format = [format stringByAppendingString: @" %N"];
if (show_me) {
peopleFound = [NSArray arrayWithObjects: [AB me], nil];
} else if (show_all) {
peopleFound = [AB people];
} else {
// search for the string given
NSString *searchString = [NSString stringWithUTF8String: argv[0]];
ABSearchElement *firstName =
[ABPerson searchElementForProperty:kABFirstNameProperty
label:nil
key:nil
value:searchString
comparison:kABContainsSubStringCaseInsensitive];
//comparison:kABEqualCaseInsensitive];
ABSearchElement *lastName =
[ABPerson searchElementForProperty:kABLastNameProperty
label:nil
key:nil
value:searchString
comparison:kABContainsSubStringCaseInsensitive];
ABSearchElement *companyName =
[ABPerson searchElementForProperty:kABOrganizationProperty
label:nil
key:nil
value:searchString
comparison:kABContainsSubStringCaseInsensitive];
ABSearchElement *homeEmail =
[ABPerson searchElementForProperty:kABEmailProperty
label:kABEmailHomeLabel
key:nil
value:searchString
comparison:kABContainsSubStringCaseInsensitive];
ABSearchElement *workEmail =
[ABPerson searchElementForProperty:kABEmailProperty
label:kABEmailWorkLabel
key:nil
value:searchString
comparison:kABContainsSubStringCaseInsensitive];
ABSearchElement *aimName =
[ABPerson searchElementForProperty:kABAIMInstantProperty
label:kABAIMHomeLabel
key:nil
value:searchString
comparison:kABContainsSubStringCaseInsensitive];
ABSearchElement *notes =
[ABPerson searchElementForProperty:kABAIMInstantProperty
label:kABNoteProperty
key:nil
value:searchString
comparison:kABContainsSubStringCaseInsensitive];
ABSearchElement *criteria =
[ABSearchElement searchElementForConjunction:kABSearchOr
children:[NSArray arrayWithObjects:
firstName,
lastName,
companyName,
homeEmail,
workEmail,
aimName,
notes,
nil]];
peopleFound = [AB recordsMatchingSearchElement: criteria];
}
printPeopleWithFormat(peopleFound, format);
[pool release];
return 0;
}
/*
Prints the headers from the given formatters.
*/
void showHeader(NSArray* formatters) {
NSEnumerator *formatEnumerator = [formatters objectEnumerator];
FormatHelper *formatter;
while((formatter = [formatEnumerator nextObject]) != nil) {
printf([[formatter printfToken] UTF8String],
[[formatter headerName] UTF8String]);
}
printf("\n");
}
/*
Prints people using the given format string. Here's an example
format string: "%n %ph %pw %pm".
*/
void printPeopleWithFormat(NSArray *people,
NSString *format) {
ABPerson *person;
NSEnumerator *peopleEnum;
NSEnumerator *formatEnumerator;
FormatHelper *formatter;
NSArray *formatters = getFormatHelpers(format);
if ([formatters count] == 0) {
fprintf(stderr, "error: no formatter tokens found\n");
exit(3);
}
if ([people count] == 0) {
printf("error: no one found\n");
exit(1);
}
// print the header first
if (show_headers) {
showHeader(formatters);
}
if (sort) {
people = [people sortedArrayUsingFunction: peopleSort
context: [formatters objectAtIndex: 0]];
}
peopleEnum = [people objectEnumerator];
while((person = [peopleEnum nextObject]) != nil) {
if ( !only_birthdays || [person valueForProperty:kABBirthdayProperty] != nil ) {
formatEnumerator = [formatters objectEnumerator];
while((formatter = [formatEnumerator nextObject]) != nil) {
printf([[formatter printfToken] UTF8String],
[[formatter valueForPerson: person] UTF8String]);
}
printf("\n");
}
}
}
/*
Sorts people by the FormatHelper given in the context.
*/
int peopleSort(id peep1, id peep2, void *context) {
ABPerson *p1 = peep1;
ABPerson *p2 = peep2;
FormatHelper *formatter = context;
NSString *n1 = [formatter valueForPerson: p1];
NSString *n2 = [formatter valueForPerson: p2];
return [n1 caseInsensitiveCompare: n2];
}