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
<CoreFoundation
/CoreFoundation.h
>
24 #import "GrowlDefines.h"
25 #import "GrowlPathway.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 CFDataRef readFile
(const
char *filename
)
174 // read the file into a CFDataRef
175 FILE
*fp
= fopen(filename
, "r"
);
177 fseek(fp
, 0, SEEK_END
);
178 long dataLength
= ftell(fp
);
179 fseek(fp
, 0, SEEK_SET
);
180 unsigned
char *fileData
= malloc
(dataLength
);
181 fread(fileData
, 1, dataLength
, fp
);
183 data
= CFDataCreateWithBytesNoCopy
(kCFAllocatorDefault
, fileData
, dataLength
, kCFAllocatorMalloc
);
190 int main
(int argc
, const
char **argv
) {
192 int code
= EXIT_SUCCESS
;
193 CFDataRef icon
= NULL;
194 NSAutoreleasePool
*pool
= [[NSAutoreleasePool alloc
] init
];
195 NSString
* myImage
= [[NSBundle mainBundle
] pathForResource
:@"Andy" ofType
:@"icns"
];
196 icon
= (CFDataRef
) readFile
([myImage UTF8String
]);
198 // Register with Growl
199 CFStringRef name
= NOTIFICATION_NAME
;
200 CFArrayRef defaultAndAllNotifications
= CFArrayCreate
(kCFAllocatorDefault
, (const void
**)&name
, 1, &kCFTypeArrayCallBacks
);
201 CFTypeRef registerKeys
[4] = {
203 GROWL_NOTIFICATIONS_ALL
,
204 GROWL_NOTIFICATIONS_DEFAULT
,
207 CFTypeRef registerValues
[4] = {
209 defaultAndAllNotifications
,
210 defaultAndAllNotifications
,
213 CFDictionaryRef registerInfo
= CFDictionaryCreate
(kCFAllocatorDefault
, registerKeys
, registerValues
, 4, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
214 CFRelease
(defaultAndAllNotifications
);
217 CFNotificationCenterRef distCenter
;
219 distCenter
= CFNotificationCenterGetDistributedCenter
();
221 CFMutableStringRef notificationName
= CFStringCreateMutable
(kCFAllocatorDefault
, 0);
222 CFStringRef applicationName1
= APPLICATION_NAME
;
223 CFStringAppend
(notificationName
, applicationName1
);
224 CFStringAppend
(notificationName
, (CFStringRef
)GROWL_NOTIFICATION_CLICKED
);
225 CFNotificationCenterAddObserver
(distCenter
, "openafsgrowl"
, notificationDismissed
, notificationName
, NULL, CFNotificationSuspensionBehaviorCoalesce
);
226 CFStringReplaceAll
(notificationName
, applicationName1
);
227 CFStringAppend
(notificationName
, (CFStringRef
)GROWL_NOTIFICATION_TIMED_OUT
);
228 CFNotificationCenterAddObserver
(distCenter
, "openafsgrowl"
, notificationDismissed
, notificationName
, NULL, CFNotificationSuspensionBehaviorCoalesce
);
229 CFRelease
(notificationName
);
232 struct sockaddr_in
sin = { .sin_family
= AF_INET
, .sin_port
= htons
(VMON_SOCKET
), .sin_addr.s_addr
= INADDR_ANY
};
233 CFDataRef theAddress
= CFDataCreateWithBytesNoCopy
(NULL, (UInt8
*)&sin, sizeof
(struct sockaddr_in
), kCFAllocatorNull
);
234 CFSocketSignature MarinerSignature
= { PF_INET
, SOCK_DGRAM
, IPPROTO_UDP
, theAddress
};
235 struct GrowlCBContext growlContext
= {distCenter
, registerInfo
, icon
};
236 CFSocketContext MarinerSocketContext
= {0, (void
*)&growlContext
, NULL, NULL, NULL };
238 CFSocketRef MarinerSocket
= CFSocketCreateWithSocketSignature
(kCFAllocatorDefault
, &MarinerSignature
, kCFSocketReadCallBack
, &MySocketReadCallBack
, &MarinerSocketContext
);
243 CFSocketSetSocketFlags
(MarinerSocket
, kCFSocketCloseOnInvalidate|kCFSocketAutomaticallyReenableReadCallBack
);
245 CFRunLoopSourceRef MarinerRunLoopSource
= CFSocketCreateRunLoopSource
(NULL, MarinerSocket
, 0);
246 if (!MarinerRunLoopSource
)
249 CFRunLoopAddSource
(CFRunLoopGetCurrent
(), MarinerRunLoopSource
, kCFRunLoopCommonModes
);
251 /* Run the run loop until it is manually cancelled */
254 CFRelease
(registerInfo
);
255 CFRelease
(MarinerRunLoopSource
);
256 CFSocketInvalidate
(MarinerSocket
);
257 CFRelease
(MarinerSocket
);
259 /* CFRelease(notificationInfo); */