2 Copyright (c) The Growl Project, 2004-2005
6 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
9 1. Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14 3. Neither the name of Growl nor the names of its contributors
15 may be used to endorse or promote products derived from this software
16 without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22 #import <Foundation/Foundation.h>
23 #import "GrowlDefines.h"
24 #import "GrowlPathway.h"
25 #include "CFGrowlAdditions.h"
27 #include <mach-o/dyld.h>
30 #include <netinet/in.h>
31 #include <sys/types.h>
32 #include <sys/socket.h>
34 #include <sys/param.h>
36 #define VMON_SOCKET 2106
38 #define STORESTR "store$"
39 #define FETCHSTR "fetch$"
40 #define WARNSTR "warn$"
45 #define NOTIFICATION_NAME CFSTR("OpenAFS Venus Monitor")
46 #define APPLICATION_NAME CFSTR("afshelper")
48 #define STRINGIFY(x) STRINGIFY2(x)
49 #define STRINGIFY2(x) #x
51 static void notificationDismissed(CFNotificationCenterRef center,
55 CFDictionaryRef userInfo) {
56 #pragma unused(center,observer,name,object,userInfo)
57 /*CFRunLoopStop(CFRunLoopGetCurrent());*/
60 void getPath(char **selfPathPtr)
62 uint32_t selfPathSize = MAXPATHLEN;
63 if(!(*selfPathPtr = malloc(selfPathSize)))
67 if(_NSGetExecutablePath(*selfPathPtr, &selfPathSize) == -1)
69 // Try reallocating selfPath with the size returned by the func
70 if(!(*selfPathPtr = realloc(*selfPathPtr, selfPathSize + 1)))
72 NSLog(@"Could not allocate memory to hold executable path.");
75 if(_NSGetExecutablePath(*selfPathPtr, &selfPathSize) != 0)
77 NSLog(@"Could not get executable path.");
84 MyTransmit(CFNotificationCenterRef distCenter, CFDictionaryRef registerInfo, CFMutableDictionaryRef notificationInfo)
86 NSConnection *connection = [NSConnection connectionWithRegisteredName:@"GrowlApplicationBridgePathway" host:nil];
88 //Post to Growl via GrowlApplicationBridgePathway
90 NSDistantObject *theProxy = [connection rootProxy];
91 [theProxy setProtocolForProxy:@protocol(GrowlNotificationProtocol)];
92 id<GrowlNotificationProtocol> growlProxy = (id)theProxy;
93 [growlProxy registerApplicationWithDictionary:(NSDictionary *)registerInfo];
94 [growlProxy postNotificationWithDictionary:(NSDictionary *)notificationInfo];
95 } @catch(NSException *e) {
96 NSLog(@"exception while sending notification: %@", e);
99 //Post to Growl via NSDistributedNotificationCenter
100 NSLog(@"could not find local GrowlApplicationBridgePathway, falling back to NSDNC");
101 CFNotificationCenterPostNotificationWithOptions(distCenter, (CFStringRef)GROWL_APP_REGISTRATION, NULL, registerInfo, kCFNotificationPostToAllSessions);
102 CFNotificationCenterPostNotificationWithOptions(distCenter, (CFStringRef)GROWL_NOTIFICATION, NULL, notificationInfo, kCFNotificationPostToAllSessions);
107 BuildNotificationInfo(char *recvbuf, CFNotificationCenterRef dcref, CFDictionaryRef regref, CFDataRef icon)
110 CFNumberRef priorityNumber;
111 CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault);
112 CFStringRef clickContext = CFUUIDCreateString(kCFAllocatorDefault, uuid);
113 CFMutableDictionaryRef notificationInfo = CFDictionaryCreateMutable(kCFAllocatorDefault ,9, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
114 CFDictionarySetValue(notificationInfo, GROWL_NOTIFICATION_NAME, NOTIFICATION_NAME);
115 CFDictionarySetValue(notificationInfo, GROWL_APP_NAME, APPLICATION_NAME);
116 CFDictionarySetValue(notificationInfo, GROWL_NOTIFICATION_STICKY, kCFBooleanFalse);
117 CFDictionarySetValue(notificationInfo, GROWL_NOTIFICATION_ICON, icon);
118 CFDictionarySetValue(notificationInfo, GROWL_NOTIFICATION_CLICK_CONTEXT, clickContext);
119 CFDictionarySetValue(notificationInfo, GROWL_NOTIFICATION_TITLE, CFSTR("OpenAFS")/*title*/);
122 /* if fetching ever provides more data we could use this */
123 CFDictionarySetValue(notificationInfo, GROWL_NOTIFICATION_PROGRESS, progressNumber);
125 if (!strncmp(recvbuf, FETCHSTR, sizeof(FETCHSTR)-1)) {
126 CFDictionarySetValue(notificationInfo, GROWL_NOTIFICATION_DESCRIPTION, CFStringCreateWithCString(kCFAllocatorDefault, recvbuf+FETCHOFFT, kCFStringEncodingUTF8));
128 CFDictionarySetValue(notificationInfo, GROWL_NOTIFICATION_IDENTIFIER, CFSTR("AFSFetch"));
129 } else if (!strncmp(recvbuf, STORESTR, sizeof(STORESTR)-1)) {
130 CFDictionarySetValue(notificationInfo, GROWL_NOTIFICATION_DESCRIPTION, CFStringCreateWithCString(kCFAllocatorDefault, recvbuf+STOREOFFT, kCFStringEncodingUTF8));
132 CFDictionarySetValue(notificationInfo, GROWL_NOTIFICATION_IDENTIFIER, CFSTR("AFSStore"));
133 } else if (!strncmp(recvbuf, WARNSTR, sizeof(WARNSTR)-1)) {
134 CFDictionarySetValue(notificationInfo, GROWL_NOTIFICATION_DESCRIPTION, CFStringCreateWithCString(kCFAllocatorDefault, recvbuf+WARNOFFT, kCFStringEncodingUTF8));
136 CFDictionarySetValue(notificationInfo, GROWL_NOTIFICATION_IDENTIFIER, CFSTR("AFSWarn"));
138 CFDictionarySetValue(notificationInfo, GROWL_NOTIFICATION_DESCRIPTION, CFStringCreateWithCString(kCFAllocatorDefault, recvbuf, kCFStringEncodingUTF8));
140 CFDictionarySetValue(notificationInfo, GROWL_NOTIFICATION_IDENTIFIER, CFSTR("AFS"));
142 priorityNumber = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &priority);
143 CFDictionarySetValue(notificationInfo, GROWL_NOTIFICATION_PRIORITY, priorityNumber);
144 CFRelease(priorityNumber);
145 CFRelease(clickContext);
146 MyTransmit(dcref, regref, notificationInfo);
149 struct GrowlCBContext {
150 CFNotificationCenterRef dcref;
151 CFDictionaryRef regref;
155 static void MySocketReadCallBack(CFSocketRef socket, CFSocketCallBackType callbackType, CFDataRef address, const void *data, void *info)
157 struct GrowlCBContext* callbackInfo = (struct GrowlCBContext * )info;
158 uint8_t recvBuffer[1024];
160 int recvSocket = CFSocketGetNative( socket );
162 if ( callbackType != kCFSocketReadCallBack ) return;
164 result = recvfrom( recvSocket, recvBuffer, sizeof(recvBuffer), 0, NULL, NULL );
166 recvBuffer[result-1] = '\0';
167 BuildNotificationInfo((char *)recvBuffer, callbackInfo->dcref, callbackInfo->regref, callbackInfo->icon);
171 int main(int argc, const char **argv) {
173 int code = EXIT_SUCCESS;
174 CFDataRef icon = NULL;
175 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
177 // get icon data for application name
180 CFStringRef appPath = CFStringCreateWithCString(kCFAllocatorDefault, selfPath, kCFStringEncodingUTF8);
181 NSURL *appURL = [NSURL fileURLWithPath:(NSString *)appPath];
182 icon = (CFDataRef)copyIconDataForURL(appURL);
185 // Register with Growl
186 CFStringRef name = NOTIFICATION_NAME;
187 CFArrayRef defaultAndAllNotifications = CFArrayCreate(kCFAllocatorDefault, (const void **)&name, 1, &kCFTypeArrayCallBacks);
188 CFTypeRef registerKeys[4] = {
190 GROWL_NOTIFICATIONS_ALL,
191 GROWL_NOTIFICATIONS_DEFAULT,
194 CFTypeRef registerValues[4] = {
196 defaultAndAllNotifications,
197 defaultAndAllNotifications,
200 CFDictionaryRef registerInfo = CFDictionaryCreate(kCFAllocatorDefault, registerKeys, registerValues, 4, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
201 CFRelease(defaultAndAllNotifications);
204 CFNotificationCenterRef distCenter;
206 distCenter = CFNotificationCenterGetDistributedCenter();
208 CFMutableStringRef notificationName = CFStringCreateMutable(kCFAllocatorDefault, 0);
209 CFStringRef applicationName1 = APPLICATION_NAME;
210 CFStringAppend(notificationName, applicationName1);
211 CFStringAppend(notificationName, (CFStringRef)GROWL_NOTIFICATION_CLICKED);
212 CFNotificationCenterAddObserver(distCenter, "openafsgrowl", notificationDismissed, notificationName, NULL, CFNotificationSuspensionBehaviorCoalesce);
213 CFStringReplaceAll(notificationName, applicationName1);
214 CFStringAppend(notificationName, (CFStringRef)GROWL_NOTIFICATION_TIMED_OUT);
215 CFNotificationCenterAddObserver(distCenter, "openafsgrowl", notificationDismissed, notificationName, NULL, CFNotificationSuspensionBehaviorCoalesce);
216 CFRelease(notificationName);
219 struct sockaddr_in sin = { .sin_family = AF_INET, .sin_port = htons(VMON_SOCKET), .sin_addr.s_addr = INADDR_ANY };
220 CFDataRef theAddress = CFDataCreateWithBytesNoCopy(NULL, (UInt8 *)&sin, sizeof(struct sockaddr_in), kCFAllocatorNull);
221 CFSocketSignature MarinerSignature = { PF_INET, SOCK_DGRAM, IPPROTO_UDP, theAddress };
222 struct GrowlCBContext growlContext = {distCenter, registerInfo, icon};
223 CFSocketContext MarinerSocketContext = {0, (void *)&growlContext, NULL, NULL, NULL };
225 CFSocketRef MarinerSocket = CFSocketCreateWithSocketSignature(kCFAllocatorDefault, &MarinerSignature, kCFSocketReadCallBack, &MySocketReadCallBack, &MarinerSocketContext);
227 CFSocketSetSocketFlags(MarinerSocket, kCFSocketCloseOnInvalidate|kCFSocketAutomaticallyReenableReadCallBack);
229 CFRunLoopSourceRef MarinerRunLoopSource = CFSocketCreateRunLoopSource(NULL, MarinerSocket, 0);
230 CFRunLoopAddSource(CFRunLoopGetCurrent(), MarinerRunLoopSource, kCFRunLoopCommonModes);
232 /* Run the run loop until it is manually cancelled */
235 CFRelease(registerInfo);
236 CFRelease(MarinerRunLoopSource);
237 CFSocketInvalidate(MarinerSocket);
238 CFRelease(MarinerSocket);
239 /* CFRelease(notificationInfo); */