diff options
author | Eion Robb <eion@robbmob.com> | 2008-03-24 00:25:41 +0300 |
---|---|---|
committer | Eion Robb <eion@robbmob.com> | 2008-03-24 00:25:41 +0300 |
commit | 8a35e830354145d30f916c0a9c5ab38ba5b4936d (patch) | |
tree | 58d3d44c9736db6c2bad042a9dfd47ce48e231ed /skype_messaging_carbon.c | |
parent | 3e7aaef9e1853dd0965f0f517ab0a5811f84f5f7 (diff) |
Added AppleScript to automatically allow Skype API access (if access assistive devices is enabled)
Diffstat (limited to 'skype_messaging_carbon.c')
-rw-r--r-- | skype_messaging_carbon.c | 515 |
1 files changed, 247 insertions, 268 deletions
diff --git a/skype_messaging_carbon.c b/skype_messaging_carbon.c index 40e6ed0..f1eab1c 100644 --- a/skype_messaging_carbon.c +++ b/skype_messaging_carbon.c @@ -1,268 +1,247 @@ -#include <Carbon/Carbon.h>
-//#include <Skype/Skype.h>
-#include <glib.h>
-#include <CoreFoundation/CoreFoundation.h>
-
-#include "AutoreleasePoolInit.h"
-
-//change this to 0 if using an old version of the Skype.framework
-#define SENDSKYPERETURNS 0
-
-#include "skype_messaging_carbon2.c"
-
-static gboolean connected_to_skype = FALSE;
-
-void
-SkypeNotificationReceived(CFStringRef input)
-{
- char *output = NULL;
- GError *error = NULL;
- void *pool = initAutoreleasePool();
- int strlen = CFStringGetMaximumSizeForEncoding(CFStringGetLength(input), kCFStringEncodingUTF8);
-
- printf("Message received");
- output = (char *)CFStringGetCStringPtr(input, kCFStringEncodingUTF8);
- if (!output)
- {
- output = NewPtr(strlen+1);
- CFStringGetCString(input, output, strlen+1, kCFStringEncodingUTF8);
- }
- printf(" %s\n", output);
- //g_thread_create((GThreadFunc)skype_message_received, (void *)output, FALSE, &error);
- skype_message_received(output);
- if (error)
- {
- printf("Could not create new thread!!! %s\n", error->message);
- g_error_free(error);
- }
-
- destroyAutoreleasePool(pool);
-}
-
-void
-SkypeAttachResponse(unsigned int aAttachResponseCode)
-{
- if (aAttachResponseCode)
- {
- printf("Skype attached successfully :)\n");
- connected_to_skype = TRUE;
- }
- else
- {
- printf("Skype couldn't connect :(\n");
- connected_to_skype = FALSE;
- }
-}
-
-void
-SkypeBecameAvailable(CFPropertyListRef aNotification)
-{
- printf("Skype became available\n");
- connected_to_skype = TRUE;
-}
-
-void
-SkypeBecameUnavailable(CFPropertyListRef aNotification)
-{
- printf("Skype became unavailable\n");
- connected_to_skype = FALSE;
- g_thread_create((GThreadFunc)skype_message_received, "CONNSTATUS LOGGEDOUT", FALSE, NULL);
-}
-
-static struct SkypeDelegate skypeDelegate = {
- CFSTR("Adium"), /* clientAppName */
- SkypeNotificationReceived,
- SkypeAttachResponse,
- SkypeBecameAvailable,
- SkypeBecameUnavailable
-};
-
-/*static gboolean
-skype_connect_thread(gpointer data)
-{
- static gboolean started = FALSE;
- if (started)
- return FALSE;
- started = TRUE;
-
- void *pool = initAutoreleasePool();
-
- printf("Start inner event loop\n");
- while(true)
- {
- //RunApplicationEventLoop();
- RunCurrentEventLoop(1);
- }
- printf("End of event loop\n");
- started = FALSE;
-
- destroyAutoreleasePool(pool);
-
- //don't loop this thread
- return FALSE;
-}*/
-
-static gpointer static_pool;
-
-static gboolean
-skype_connect()
-{
- gboolean is_skype_running = FALSE;
-
- if (!static_pool)
- static_pool = initAutoreleasePool();
-
- is_skype_running = IsSkypeRunning();
-
- printf("Is Skype running? '%s'\n", (is_skype_running?"Yes":"No"));
- if (!is_skype_running)
- return FALSE;
-
- if (connected_to_skype)
- skype_disconnect();
-
- SetSkypeDelegate(&skypeDelegate);
- ConnectToSkype();
-
- //g_thread_create((GThreadFunc)skype_connect_thread, NULL, FALSE, NULL);
- while(connected_to_skype == FALSE)
- {
- RunCurrentEventLoop(1);
- allow_app_in_skype_api();
- }
- printf("Connected to skype\n");
- return TRUE;
-}
-
-static void
-skype_disconnect()
-{
- connected_to_skype = FALSE;
- DisconnectFromSkype();
- RemoveSkypeDelegate();
- RunCurrentEventLoop(1);
-}
-
-static void
-send_message(char* message)
-{
- CFStringRef messageString = CFStringCreateWithCString(NULL, message, kCFStringEncodingUTF8);
- if (!connected_to_skype)
- {
- if (message[0] == '#')
- {
- int message_num;
- char error_return[40];
- //And we're expecting a response
- sscanf(message, "#%d ", &message_num);
- sprintf(error_return, "#%d ERROR", message_num);
- g_thread_create((GThreadFunc)skype_message_received, (void *)g_strdup(error_return), FALSE, NULL);
- }
- CFRelease(messageString);
- return;
- }
-
- gpointer pool = initAutoreleasePool();
- printf("Skype send message ");
-#if SENDSKYPERETURNS
- CFStringRef returnString = NULL;
- returnString = SendSkypeCommand(messageString);
- if (returnString)
- SkypeNotificationReceived(returnString);
-#else
- SendSkypeCommand(messageString);
-#endif
- destroyAutoreleasePool(pool);
- printf("%s\n", message);
- CFRelease(messageString);
-}
-
-static void
-hide_skype()
-{
- OSStatus status = noErr;
- ProcessSerialNumber psn = {kNoProcess, kNoProcess};
- unsigned int procNameLength = 32;
- unsigned char procName[procNameLength];
- unsigned int i = 0;
- ProcessInfoRec info;
- info.processInfoLength = sizeof(ProcessInfoRec);
- info.processName = procName;
- info.processAppSpec = NULL;
-
- while(status == noErr)
- {
- for(i = 0; i < procNameLength; i++)
- procName[i] = '\0';
-
- status = GetNextProcess(&psn);
- if (status == noErr)
- if (GetProcessInformation(&psn, &info) == noErr)
- //for some reason first character is poisioned
- if (strcmp((char *)&procName[1], "Skype") == 0)
- {
- ShowHideProcess(&psn, FALSE);
- return;
- }
- }
-}
-
-static gboolean
-exec_skype()
-{
- return g_spawn_command_line_async("/Applications/Skype.app/Contents/MacOS/Skype", NULL);
-}
-
-static gboolean
-is_skype_running()
-{
- return IsSkypeRunning();
-}
-
-static void
-allow_app_in_skype_api()
-{
- WindowRef window = GetWindowList();
- CFStringRef window_title, control_title;
- EventTargetRef window_event_target;
- ControlRef control, root_control;
- UInt16 control_count, i;
- Point point = {1,1};
- //loop through all the windows
- while(window)
- {
- CopyWindowTitleAsCFString(window, &window_title);
- printf("Window title: %s\n", CFStringToCString(window_title));
- //check that it has the title we're looking for
- if (CFStringCompare(window_title, CFSTR("Skype API Security"), 0) == kCFCompareEqualTo)
- {
- window_event_target = GetWindowEventTarget(window);
- SetWindowAlpha(window, 0.5);
- GetRootControl(window, &root_control);
- CountSubControls(root_control, &control_count);
- //loop through controls
- for (i=1; i<=control_count; i++)
- {
- GetIndexedSubControl(root_control, i, &control);
- //click the right ones
- CopyControlTitleAsCFString(control, &control_title);
- if (CFStringCompare(control_title, CFSTR("Allow this application to use Skype"), 0) == kCFCompareEqualTo)
- {
- //click the radio button
- printf("Clicking the radio\n");
- HandleControlClick(control, point, 0, ControlActionUPP(-1));
- }
- CFRelease(control_title);
- }
- //click the ok button
- GetWindowDefaultButton(window, &control);
- if (control)
- {
- printf("Clicking OK");
- HandleControlClick(control, point, 0, ControlActionUPP(-1));
- }
- }
- CFRelease(window_title);
- GetNextWindow(window);
- }
-}
+#include <Carbon/Carbon.h> +//#include <Skype/Skype.h> +#include <glib.h> +#include <CoreFoundation/CoreFoundation.h> + +#include "AutoreleasePoolInit.h" + +//change this to 0 if using an old version of the Skype.framework +#define SENDSKYPERETURNS 0 + +#include "skype_messaging_carbon2.c" + +static gboolean connected_to_skype = FALSE; + + +static void allow_app_in_skype_api(void); + +void +SkypeNotificationReceived(CFStringRef input) +{ + char *output = NULL; + GError *error = NULL; + void *pool = initAutoreleasePool(); + int strlen = CFStringGetMaximumSizeForEncoding(CFStringGetLength(input), kCFStringEncodingUTF8); + + printf("Message received"); + output = (char *)CFStringGetCStringPtr(input, kCFStringEncodingUTF8); + if (!output) + { + output = NewPtr(strlen+1); + CFStringGetCString(input, output, strlen+1, kCFStringEncodingUTF8); + } + printf(" %s\n", output); + //g_thread_create((GThreadFunc)skype_message_received, (void *)output, FALSE, &error); + skype_message_received(output); + if (error) + { + printf("Could not create new thread!!! %s\n", error->message); + g_error_free(error); + } + + destroyAutoreleasePool(pool); +} + +void +SkypeAttachResponse(unsigned int aAttachResponseCode) +{ + if (aAttachResponseCode) + { + printf("Skype attached successfully :)\n"); + connected_to_skype = TRUE; + } + else + { + printf("Skype couldn't connect :(\n"); + connected_to_skype = FALSE; + } +} + +void +SkypeBecameAvailable(CFPropertyListRef aNotification) +{ + printf("Skype became available\n"); + //connected_to_skype = TRUE; + allow_app_in_skype_api(); +} + +void +SkypeBecameUnavailable(CFPropertyListRef aNotification) +{ + printf("Skype became unavailable\n"); + connected_to_skype = FALSE; + g_thread_create((GThreadFunc)skype_message_received, "CONNSTATUS LOGGEDOUT", FALSE, NULL); +} + +static struct SkypeDelegate skypeDelegate = { + CFSTR("Adium"), /* clientAppName */ + SkypeNotificationReceived, + SkypeAttachResponse, + SkypeBecameAvailable, + SkypeBecameUnavailable +}; + +/*static gboolean +skype_connect_thread(gpointer data) +{ + static gboolean started = FALSE; + if (started) + return FALSE; + started = TRUE; + + void *pool = initAutoreleasePool(); + + printf("Start inner event loop\n"); + while(true) + { + //RunApplicationEventLoop(); + RunCurrentEventLoop(1); + } + printf("End of event loop\n"); + started = FALSE; + + destroyAutoreleasePool(pool); + + //don't loop this thread + return FALSE; +}*/ + +static gpointer static_pool; + +static gboolean +skype_connect() +{ + gboolean is_skype_running = FALSE; + + if (!static_pool) + static_pool = initAutoreleasePool(); + + is_skype_running = IsSkypeRunning(); + + printf("Is Skype running? '%s'\n", (is_skype_running?"Yes":"No")); + if (!is_skype_running) + return FALSE; + + if (connected_to_skype) + skype_disconnect(); + + SetSkypeDelegate(&skypeDelegate); + ConnectToSkype(); + + //g_thread_create((GThreadFunc)skype_connect_thread, NULL, FALSE, NULL); + while(connected_to_skype == FALSE) + { + RunCurrentEventLoop(1); + } + printf("Connected to skype\n"); + return TRUE; +} + +static void +skype_disconnect() +{ + connected_to_skype = FALSE; + DisconnectFromSkype(); + RemoveSkypeDelegate(); + RunCurrentEventLoop(1); +} + +static void +send_message(char* message) +{ + CFStringRef messageString = CFStringCreateWithCString(NULL, message, kCFStringEncodingUTF8); + if (!connected_to_skype) + { + if (message[0] == '#') + { + int message_num; + char error_return[40]; + //And we're expecting a response + sscanf(message, "#%d ", &message_num); + sprintf(error_return, "#%d ERROR", message_num); + g_thread_create((GThreadFunc)skype_message_received, (void *)g_strdup(error_return), FALSE, NULL); + } + CFRelease(messageString); + return; + } + + gpointer pool = initAutoreleasePool(); + printf("Skype send message "); +#if SENDSKYPERETURNS + CFStringRef returnString = NULL; + returnString = SendSkypeCommand(messageString); + if (returnString) + SkypeNotificationReceived(returnString); +#else + SendSkypeCommand(messageString); +#endif + destroyAutoreleasePool(pool); + printf("%s\n", message); + CFRelease(messageString); +} + +static void +hide_skype() +{ + OSStatus status = noErr; + ProcessSerialNumber psn = {kNoProcess, kNoProcess}; + unsigned int procNameLength = 32; + unsigned char procName[procNameLength]; + unsigned int i = 0; + ProcessInfoRec info; + info.processInfoLength = sizeof(ProcessInfoRec); + info.processName = procName; + info.processAppSpec = NULL; + + while(status == noErr) + { + for(i = 0; i < procNameLength; i++) + procName[i] = '\0'; + + status = GetNextProcess(&psn); + if (status == noErr) + if (GetProcessInformation(&psn, &info) == noErr) + //for some reason first character is poisioned + if (strcmp((char *)&procName[1], "Skype") == 0) + { + ShowHideProcess(&psn, FALSE); + return; + } + } +} + +static gboolean +exec_skype() +{ + return g_spawn_command_line_async("/Applications/Skype.app/Contents/MacOS/Skype", NULL); +} + +static gboolean +is_skype_running() +{ + return IsSkypeRunning(); +} + +static void +allow_app_in_skype_api() +{ + static const char *script_string = "tell application \"System Events\" to tell process \"Skype\"\n" + "if window \"Skype API Security\" exists then\n" + "click radio button \"Allow this application to use Skype\" of radio group 1 of window \"Skype API Security\"\n" + "delay 1\n" + "click button \"OK\" of window \"Skype API Security\"\n" + "end if\n" + "end tell"; + AEDesc script_data; + OSAID script_id; + OSAError err; + ComponentInstance script = OpenDefaultComponent(kOSAComponentType, typeAppleScript); + AECreateDesc(typeChar, script_string, strlen(script_string), &script_data); + OSACompile(script, &script_data, kOSAModeNull, &script_id); + err = OSAExecute(script, script_id, kOSANullScript, kOSAModeNull, &script_id); + if (err == -1753) + { + //theres an error that the access assistive devices isn't enabled + //see http://images.apple.com/applescript/uiscripting/gfx/gui.03.jpg for details + } +}
\ No newline at end of file |