-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsanta-dndump.m
64 lines (48 loc) · 1.87 KB
/
santa-dndump.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
#import <AppKit/AppKit.h>
static int observerCount = 0;
static int notificationCount = 1;
static const char* objectToCString(id object) {
return [[object description] cStringUsingEncoding: NSUTF8StringEncoding];
}
static void writeObject(id data, id object) {
if (object) {
printf("[%s] %s\n", objectToCString(object), objectToCString(data));
} else {
printf("%s\n", objectToCString(data));
}
}
typedef void (^ObserverBlock)(id,id);
static void handleNotification(CFNotificationCenterRef center, void *observer, CFNotificationName name, const void *object, CFDictionaryRef userInfo) {
// NSLog(@"%@: %@[%@]", name, object, userInfo);
ObserverBlock block = (__bridge ObserverBlock)observer;
if (block) block((__bridge id)object, (__bridge id)userInfo);
if (notificationCount != -1) {
if (notificationCount > 0) {
notificationCount--;
}
if (notificationCount == 0) {
exit(0);
}
}
}
static void execute(const char* bin, NSString* notificationName) {
CFNotificationCenterRef center = CFNotificationCenterGetDistributedCenter();
ObserverBlock observer = ^(id object, id userInfo) {
NSData* data = [NSJSONSerialization dataWithJSONObject: userInfo options: NSJSONWritingPrettyPrinted error: nil];
printf("%s\n", objectToCString([[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]));
};
CFNotificationCenterAddObserver(center, CFBridgingRetain([observer copy]),
handleNotification,
(__bridge CFStringRef)notificationName, nil, CFNotificationSuspensionBehaviorDeliverImmediately);
observerCount += 1;
}
int main(int argc, const char** argv) {
@autoreleasepool {
int i = 1;
NSString* notificationName = @"com.northpolesec.santa.notification.blockedeexecution";
notificationCount = 1;
execute(argv[0], notificationName);
if (observerCount) CFRunLoopRun();
return 0;
}
}