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 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
|
/*
* Skype plugin for libpurple/Pidgin/Adium
* Written by: Eion Robb <eionrobb@gmail.com>
*
* This plugin uses the Skype API to show your contacts in libpurple, and send/receive
* chat messages.
* It requires the Skype program to be running.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <Carbon/Carbon.h>
#include <CoreFoundation/CoreFoundation.h>
typedef struct SkypeDelegate
{
// Required member
CFStringRef clientApplicationName;
// Optional members, can be NULL
void (*SkypeNotificationReceived)(CFStringRef aNotificationString);
void (*SkypeAttachResponse)(unsigned int aAttachResponseCode); // 0 - failed, 1 - success
void (*SkypeBecameAvailable)(CFPropertyListRef aNotification);
void (*SkypeBecameUnavailable)(CFPropertyListRef aNotification);
} SkypeDelegate;
static SkypeDelegate *delegate = NULL;
static int isavailable = 0;
static int client_id = 0;
char *
CFStringToCString(CFStringRef input)
{
if (input == NULL)
return NULL;
int strlen = CFStringGetMaximumSizeForEncoding(CFStringGetLength(input), kCFStringEncodingUTF8);
char *output = NewPtr(strlen+1);
CFStringGetCString(input, output, strlen+1, kCFStringEncodingUTF8);
return output;
}
int
CFNumberToCInt(CFNumberRef input)
{
if (input == NULL)
return 0;
int output;
CFNumberGetValue(input, kCFNumberIntType, &output);
return output;
}
void
availabilityUpdateCallback(
CFNotificationCenterRef center,
void *observer,
CFStringRef name,
const void *object,
CFDictionaryRef userInfo)
{
CFNumberRef number = (CFNumberRef)CFDictionaryGetValue(userInfo, CFSTR("SKYPE_API_AVAILABILITY"));
isavailable = CFNumberToCInt(number);
}
void
debugCallback(
CFNotificationCenterRef center,
void *observer,
CFStringRef name,
const void *object,
CFDictionaryRef userInfo)
{
int i = 0;
printf("Debug callback: %s\n", CFStringToCString(name));
if (!userInfo)
return;
CFIndex count = CFDictionaryGetCount(userInfo);
const void *keys[count];
const void *values[count];
CFDictionaryGetKeysAndValues(userInfo, keys, values);
for(i = 0; i < count; i++)
{
printf("For i=%d, key: %s\n", i,
CFStringToCString((CFStringRef)keys[i]));
}
}
void
apiNotificationCallback(
CFNotificationCenterRef center,
void *observer,
CFStringRef name,
const void *object,
CFDictionaryRef userInfo)
{
CFNumberRef number = (CFNumberRef) CFDictionaryGetValue(userInfo, CFSTR("SKYPE_API_CLIENT_ID"));
int client_number = CFNumberToCInt(number);
if (client_number != 999 && (!client_id || client_id != client_number))
{
return;
}
CFStringRef string = (CFStringRef) CFDictionaryGetValue(userInfo, CFSTR("SKYPE_API_NOTIFICATION_STRING"));
if (string && delegate && delegate->SkypeNotificationReceived)
{
delegate->SkypeNotificationReceived(string);
}
}
void
attachResponseCallback(
CFNotificationCenterRef center,
void *observer,
CFStringRef name,
const void *object,
CFDictionaryRef userInfo)
{
CFNumberRef responseNumber = (CFNumberRef)CFDictionaryGetValue(userInfo, CFSTR("SKYPE_API_ATTACH_RESPONSE"));
int response = CFNumberToCInt(responseNumber);
client_id = response;
if (delegate && delegate->SkypeAttachResponse)
{
delegate->SkypeAttachResponse(response?1:0);
}
}
void
skypeQuitCallback(
CFNotificationCenterRef center,
void *observer,
CFStringRef name,
const void *object,
CFDictionaryRef userInfo)
{
if (delegate && delegate->SkypeBecameAvailable)
delegate->SkypeBecameUnavailable(NULL);
}
void
skypeAvailableCallback(
CFNotificationCenterRef center,
void *observer,
CFStringRef name,
const void *object,
CFDictionaryRef userInfo)
{
if (delegate && delegate->SkypeBecameAvailable)
delegate->SkypeBecameAvailable(NULL);
}
// STANDARD SKYPE.H BITS:
void RemoveSkypeDelegate(void);
void
SetSkypeDelegate(SkypeDelegate *aDelegate)
{
if (!aDelegate->clientApplicationName)
{
printf("Deletegate requires application name\n");
delegate = NULL;
return;
}
if (delegate)
{
RemoveSkypeDelegate();
}
delegate = aDelegate;
CFNotificationCenterRef center = CFNotificationCenterGetDistributedCenter();
CFNotificationCenterAddObserver(
center,
delegate->clientApplicationName,
apiNotificationCallback,
CFSTR("SKSkypeAPINotification"),
NULL,
CFNotificationSuspensionBehaviorDeliverImmediately);
CFNotificationCenterAddObserver(
center,
delegate->clientApplicationName,
skypeQuitCallback,
CFSTR("SKSkypeWillQuit"),
NULL,
CFNotificationSuspensionBehaviorDeliverImmediately);
CFNotificationCenterAddObserver(
center,
delegate->clientApplicationName,
skypeAvailableCallback,
CFSTR("SKSkypeBecameAvailable"),
NULL,
CFNotificationSuspensionBehaviorDeliverImmediately);
CFNotificationCenterAddObserver(
center,
delegate->clientApplicationName,
availabilityUpdateCallback,
CFSTR("SKAvailabilityUpdate"),
NULL,
CFNotificationSuspensionBehaviorDeliverImmediately);
CFNotificationCenterAddObserver(
center,
delegate->clientApplicationName,
attachResponseCallback,
CFSTR("SKSkypeAttachResponse"),
NULL,
CFNotificationSuspensionBehaviorDeliverImmediately);
}
SkypeDelegate *
GetSkypeDelegate(void)
{
return delegate;
}
void
RemoveSkypeDelegate(void)
{
if (delegate && delegate->clientApplicationName)
{
CFNotificationCenterRef center = CFNotificationCenterGetDistributedCenter();
CFNotificationCenterRemoveObserver(
center,
delegate->clientApplicationName,
CFSTR("SKSkypeAPINotification"),
NULL);
CFNotificationCenterRemoveObserver(
center,
delegate->clientApplicationName,
CFSTR("SKSkypeWillQuit"),
NULL);
CFNotificationCenterRemoveObserver(
center,
delegate->clientApplicationName,
CFSTR("SKSkypeBecameAvailable"),
NULL);
CFNotificationCenterRemoveObserver(
center,
delegate->clientApplicationName,
CFSTR("SKAvailabilityUpdate"),
NULL);
CFNotificationCenterRemoveObserver(
center,
delegate->clientApplicationName,
CFSTR("SKSkypeAttachResponse"),
NULL);
}
delegate = NULL;
}
int
IsSkypeAvailable(void)
{
//is skype available?
isavailable = 0;
CFNotificationCenterRef center = CFNotificationCenterGetDistributedCenter();
CFNotificationCenterPostNotification(
center,
CFSTR("SKSkypeAPIAvailabilityRequest"),
NULL,
NULL,
TRUE);
//Should only take 1 second or less to reply
RunCurrentEventLoop(1);
int avail = isavailable;
isavailable = 0;
return avail;
}
int
IsSkypeRunning(void)
{
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;
#if __LP64__
info.processAppRef = NULL;
#else
info.processAppSpec = NULL;
#endif
pid_t pid = 0;
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 (g_str_equal((char *)&procName[1], "Skype"))
{
if (GetProcessPID(&psn, &pid) == noErr)
{
return (int)pid;
}
}
}
}
}
return 0;
}
void
ConnectToSkype(void)
{
if (!delegate || !delegate->clientApplicationName)
{
printf("Error: Delegate not set\n");
return;
}
if (!IsSkypeAvailable())
{
printf("Error: Skype not available\n");
}
CFNotificationCenterRef center = CFNotificationCenterGetDistributedCenter();
//do the connect
CFNotificationCenterPostNotification(
center,
CFSTR("SKSkypeAPIAttachRequest"),
delegate->clientApplicationName,
NULL,
TRUE);
}
void SendSkypeCommand(CFStringRef command)
{
if (delegate == NULL)
{
printf("Can't send message, no delegate set\n");
return;
}
if (command == NULL)
return;
if (!client_id)
{
printf("Can't send message, not connected\n");
return;
}
CFRetain(command);
CFNumberRef id_number = CFNumberCreate(NULL, kCFNumberIntType, &client_id);
CFNotificationCenterRef center = CFNotificationCenterGetDistributedCenter();
const void *keys[] = {(void *)CFSTR("SKYPE_API_COMMAND"), (void *)CFSTR("SKYPE_API_CLIENT_ID")};
const void *values[] = {command, id_number};
CFDictionaryRef userInfo = CFDictionaryCreate(NULL, keys, values, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
//send message
CFNotificationCenterPostNotification(
center,
CFSTR("SKSkypeAPICommand"),
NULL,
userInfo,
TRUE);
CFRelease(command);
CFRelease(id_number);
CFRelease(userInfo);
}
void DisconnectFromSkype(void)
{
CFNotificationCenterRef center = CFNotificationCenterGetDistributedCenter();
if (client_id)
{
CFNumberRef id_number = CFNumberCreate(NULL, kCFNumberIntType, &client_id);
const void *keys[] = {(void *)CFSTR("SKYPE_API_CLIENT_ID")};
const void *values[] = {id_number};
CFDictionaryRef userInfo = CFDictionaryCreate(NULL, keys, values, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
//disconnect
CFNotificationCenterPostNotification(
center,
CFSTR("SKSkypeAPIDetachRequest"),
NULL,
userInfo,
TRUE);
client_id = 0;
}
}
|