William Bowling is sharing code with you
Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.
Don't show this again1 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 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 | //
// AIURLHandlerPlugin.m
// Adium
//
// Created by Zachary West on 2009-04-03.
//
#import "AIURLHandlerPlugin.h"
#import "AIURLHandlerAdvancedPreferences.h"
#import "AINewContactWindowController.h"
#import "XtrasInstaller.h"
#import <AIUtilities/AIURLAdditions.h>
#import <AIUtilities/AIStringAdditions.h>
#import <AIUtilities/AIWindowAdditions.h>
#import <Adium/AIContactControllerProtocol.h>
#import <Adium/AIAccountControllerProtocol.h>
#import <Adium/AIInterfaceControllerProtocol.h>
#import <Adium/AIChatControllerProtocol.h>
#import <Adium/AIContentMessage.h>
#import <Adium/AIService.h>
#import <Adium/AIAccount.h>
@interface AIURLHandlerPlugin()
- (void)checkHandledSchemes;
- (void)handleURL:(NSNotification *)notification;
- (void)_openChatToContactWithName:(NSString *)name onService:(NSString *)serviceIdentifier withMessage:(NSString *)body;
- (void)_openAIMGroupChat:(NSString *)roomname onExchange:(NSInteger)exchange;
- (void)_openXMPPGroupChat:(NSString *)name onServer:(NSString *)server withPassword:(NSString *)inPassword;
- (void)_openIRCGroupChat:(NSString *)name onServer:(NSString *)server withPassword:(NSString *)password;
@end
/*!
* @class AIURLHandlerPlugin
*
* The URL handler plugin handles URL events sent to us.
*
* For example, it will convert aim://goim?sn=fuark to open a chat window with
* the user "fuark" on the first available AIM account.
*
* This plugin is also responsible for managing the default application settings
* for the schemes we support, and enforcing if necessary our ownership.
*/
@implementation AIURLHandlerPlugin
/*!
* @brief Install plugin
*
* This sets up our advanced preferences view and checks our defaults.
*/
- (void)installPlugin
{
preferences = [[AIURLHandlerAdvancedPreferences preferencePaneForPlugin:self] retain];
[self checkHandledSchemes];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleURL:)
name:AIURLHandleNotification
object:nil];
}
/*!
* @brief Unisntall
*/
- (void)uninstallPlugin
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[preferences release];
}
#pragma mark Scheme enforcement
/*!
* @brief Set Adium as the default for all handled schemes
*
* For all service schemes (and their aliases), set ourself as their default bundle ID.
*/
- (void)setAdiumAsDefault
{
for (NSString *scheme in self.uniqueSchemes) {
[self setDefaultForScheme:scheme toBundleID:ADIUM_BUNDLE_ID];
}
}
/*!
* @brief Check our handled schemes
*
* If this is the first launch, or the user has "enforce Adium as default" set, set ourself
* as the default for all available service schemes.
*
* Always set ourself as the default for our helper schemes (such as adiumxtra).
*/
- (void)checkHandledSchemes
{
if ((![[adium.preferenceController preferenceForKey:PREF_KEY_SET_DEFAULT_FIRST_TIME
group:GROUP_URL_HANDLING] boolValue]) ||
([[adium.preferenceController preferenceForKey:PREF_KEY_ENFORCE_DEFAULT
group:GROUP_URL_HANDLING] boolValue])) {
// Either this is our first launch, or we're set to enforce our defaults.
// Set ourself as the default for everything.
[self setAdiumAsDefault];
}
for (NSString *scheme in self.helperSchemes) {
[self setDefaultForScheme:scheme toBundleID:ADIUM_BUNDLE_ID];
}
}
#pragma mark Schemes
/*!
* @brief Get a service ID from a scheme
*
* Asking for the service ID of an unknown scheme will return nil.
*
* @param scheme One of our supported schemes
* @returns The serviceID for the service which uses the scheme given
*/
- (NSString *)serviceIDForScheme:(NSString *)scheme
{
static NSDictionary *schemeToServiceDict = nil;
if (!schemeToServiceDict) {
schemeToServiceDict = [[NSDictionary alloc] initWithObjectsAndKeys:
@"AIM", @"aim",
@"Yahoo!", @"ymsgr",
@"Yahoo!", @"yahoo",
@"Jabber", @"xmpp",
@"Jabber", @"jabber",
@"GTalk", @"gtalk",
@"MSN", @"msn",
@"IRC", @"irc",
@"MySpace", @"msim",
nil];
}
return [schemeToServiceDict objectForKey:scheme];
}
/*!
* @brief All of the schemes similar to a given scheme
*
* Several services (such as Yahoo and Jabber) have several scheme
* aliases which are possible schemes for them. Instead of having multiple
* entries each, we simply use a reference one and apply to all aliases.
*
* @param scheme A reference scheme (see -uniqueSchemes)
* @returns An NSArray of all schemes similar to the given one.
*/
- (NSArray *)allSchemesLikeScheme:(NSString *)scheme
{
if ([scheme isEqualToString:@"ymsgr"]) {
return [NSArray arrayWithObjects:@"yahoo", @"ymsgr", nil];
} else if ([scheme isEqualToString:@"xmpp"]) {
return [NSArray arrayWithObjects:@"xmpp", @"jabber", @"gtalk", nil];
} else {
return [NSArray arrayWithObject:scheme];
}
}
/*!
* @brief Unique schemes we support
*
* @returns An NSArray of all of the schemes we support.
*/
- (NSArray *)uniqueSchemes
{
return [NSArray arrayWithObjects:@"aim", @"irc", @"xmpp", @"msn", @"msim", @"ymsgr", nil];
}
/*!
* @brief Helper schemes
*
* Helper schemes are schemes which we should always be registered as the default application
* for. This includes things like the adiumxtra:// installer, and twitterreply:// for the Twitter
* service.
*
* @returns An NSArray of all of the helper schemes we support.
*/
- (NSArray *)helperSchemes
{
return [NSArray arrayWithObjects:@"twitterreply", @"adiumxtra", nil];
}
#pragma mark Default Bundle
/*!
* @brief Set an application as the default for a scheme
*
* @param inScheme The scheme to set the default for
* @param bundleID The bundle ID of an application to set as the default for the given scheme
*/
- (void)setDefaultForScheme:(NSString *)inScheme toBundleID:(NSString *)bundleID
{
for (NSString *scheme in [self allSchemesLikeScheme:inScheme]) {
LSSetDefaultHandlerForURLScheme((CFStringRef)scheme, (CFStringRef)bundleID);
}
}
/*!
* @brief Default application bundle ID for a scheme
*
* @param scheme The scheme to determine the default application for
* @returns An NSString bundle ID for the default application of a scheme
*/
- (NSString *)defaultApplicationBundleIDForScheme:(NSString *)scheme
{
return [[(NSString *)LSCopyDefaultHandlerForURLScheme((CFStringRef)scheme) autorelease] lowercaseString];
}
#pragma mark URL Handling
/*!
* @brief Handle a URL notification
*
* @param notification An NSNotification whose -object is the NSString of a URL.
*/
- (void)handleURL:(NSNotification *)notification
{
NSString *urlString = notification.object;
NSURL *url = [NSURL URLWithString:urlString];
if (!url)
return;
NSString *scheme, *newScheme;
NSString *serviceID;
//make sure we have the // in ://, as it simplifies later processing.
if (![[url resourceSpecifier] hasPrefix:@"//"]) {
urlString = [NSString stringWithFormat:@"%@://%@", [url scheme], [url resourceSpecifier]];
url = [NSURL URLWithString:urlString];
}
scheme = [url scheme];
//map schemes to common aliases (like jabber: for xmpp:).
static NSDictionary *schemeMappingDict = nil;
if (!schemeMappingDict) {
schemeMappingDict = [[NSDictionary alloc] initWithObjectsAndKeys:
@"ymsgr", @"yahoo",
@"xmpp", @"jabber",
nil];
}
newScheme = [schemeMappingDict objectForKey:scheme];
if (newScheme) {
scheme = newScheme;
urlString = [NSString stringWithFormat:@"%@:%@", scheme, [url resourceSpecifier]];
url = [NSURL URLWithString:urlString];
}
if ((serviceID = [self serviceIDForScheme:scheme])) {
NSString *host = [url host];
NSString *query = [url query];
if ([scheme isEqualToString:@"aim"]) {
if ([host caseInsensitiveCompare:@"goim"] == NSOrderedSame) {
// aim://goim?screenname=tekjew
NSString *name = [[[url queryArgumentForKey:@"screenname"] stringByDecodingURLEscapes] compactedString];
if (name) {
[self _openChatToContactWithName:name
onService:serviceID
withMessage:[[url queryArgumentForKey:@"message"] stringByDecodingURLEscapes]];
}
} else if ([host caseInsensitiveCompare:@"addbuddy"] == NSOrderedSame) {
// aim://addbuddy?screenname=tekjew
// aim://addbuddy?listofscreennames=screen+name1,screen+name+2&groupname=buddies
NSString *name = [[[url queryArgumentForKey:@"screenname"] stringByDecodingURLEscapes] compactedString];
AIService *service = [adium.accountController firstServiceWithServiceID:serviceID];
if (name) {
[adium.contactController requestAddContactWithUID:name
service:service
account:nil];
} else {
NSString *listOfNames = [url queryArgumentForKey:@"listofscreennames"];
NSArray *names = [listOfNames componentsSeparatedByString:@","];
for (name in names) {
NSString *decodedName = [[name stringByDecodingURLEscapes] compactedString];
[adium.contactController requestAddContactWithUID:decodedName
service:service
account:nil];
}
}
} else if ([host caseInsensitiveCompare:@"gochat"] == NSOrderedSame) {
// aim://gochat?RoomName=AdiumRocks
NSString *roomname = [[url queryArgumentForKey:@"roomname"] stringByDecodingURLEscapes];
NSString *exchangeString = [url queryArgumentForKey:@"exchange"];
if (roomname) {
NSInteger exchange = 0;
if (exchangeString) {
exchange = [exchangeString integerValue];
}
[self _openAIMGroupChat:roomname onExchange:(exchange ? exchange : 4)];
}
} else if ([url queryArgumentForKey:@"openChatToScreenName"]) {
// aim://openChatToScreenname?tekjew [?]
NSString *name = [[[url queryArgumentForKey:@"openChatToScreenname"] stringByDecodingURLEscapes] compactedString];
if (name) {
[self _openChatToContactWithName:name
onService:serviceID
withMessage:nil];
}
} else if ([host caseInsensitiveCompare:@"BuddyIcon"] == NSOrderedSame) {
//aim:BuddyIcon?src=http://www.nbc.com//Heroes/images/wallpapers/heroes-downloads-icon-single-48x48-07.gif
NSString *urlString = [url queryArgumentForKey:@"src"];
if ([urlString length]) {
NSURL *urlToDownload = [[NSURL alloc] initWithString:urlString];
NSData *imageData = (urlToDownload ? [NSData dataWithContentsOfURL:urlToDownload] : nil);
[urlToDownload release];
//Should prompt for where to apply the icon?
if (imageData &&
[[[NSImage alloc] initWithData:imageData] autorelease]) {
//If we successfully got image data, and that data makes a valid NSImage, set it as our global buddy icon
[adium.preferenceController setPreference:imageData
forKey:KEY_USER_ICON
group:GROUP_ACCOUNT_STATUS];
}
}
}
} else if ([scheme isEqualToString:@"ymsgr"]) {
if ([host caseInsensitiveCompare:@"sendim"] == NSOrderedSame) {
// ymsgr://sendim?tekjew
NSString *name = [[[url query] stringByDecodingURLEscapes] compactedString];
if (name) {
[self _openChatToContactWithName:name
onService:serviceID
withMessage:nil];
}
} else if ([host caseInsensitiveCompare:@"im"] == NSOrderedSame) {
// ymsgr://im?to=tekjew
NSString *name = [[[url queryArgumentForKey:@"to"] stringByDecodingURLEscapes] compactedString];
if (name) {
[self _openChatToContactWithName:name
onService:serviceID
withMessage:nil];
}
}
} else if ([scheme isEqualToString:@"gtalk"]) {
if ([url queryArgumentForKey:@"openChatToScreenName"]) {
// gtalk:chat?jid=foo@gmail.com&from_jid=bar@gmail.com
NSString *name = [[[url queryArgumentForKey:@"jid"] stringByDecodingURLEscapes] compactedString];
if (name) {
[self _openChatToContactWithName:name
onService:serviceID
withMessage:nil];
}
}
} else if ([scheme isEqualToString:@"xmpp"]) {
if ([query rangeOfString:@"message"].location == 0) {
//xmpp:johndoe@jabber.org?message;subject=Subject;body=Body
NSString *msg = [[url queryArgumentForKey:@"body"] stringByDecodingURLEscapes];
[self _openChatToContactWithName:[NSString stringWithFormat:@"%@@%@", [url user], [url host]]
onService:serviceID
withMessage:msg];
} else if ([query rangeOfString:@"roster"].location == 0
|| [query rangeOfString:@"subscribe"].location == 0) {
//xmpp:johndoe@jabber.org?roster;name=John%20Doe;group=Friends
//xmpp:johndoe@jabber.org?subscribe
//Group specification and name specification is currently ignored,
//due to limitations in the AINewContactWindowController API.
AIService *jabberService;
jabberService = [adium.accountController firstServiceWithServiceID:@"Jabber"];
[AINewContactWindowController promptForNewContactOnWindow:nil
name:[NSString stringWithFormat:@"%@@%@", [url user], [url host]]
service:jabberService
account:nil];
} else if ([query rangeOfString:@"remove"].location == 0
|| [query rangeOfString:@"unsubscribe"].location == 0) {
// xmpp:johndoe@jabber.org?remove
// xmpp:johndoe@jabber.org?unsubscribe
} else if ([query rangeOfString:@"join"].location == 0) {
NSString *password = [[url queryArgumentForKey:@"password"] stringByDecodingURLEscapes];
[self _openXMPPGroupChat:[url user]
onServer:[url host]
withPassword:password];
//TODO:
}
} else if ([scheme caseInsensitiveCompare:@"irc"] == NSOrderedSame) {
// irc://server/channel?password
NSString *channelName = [url fragment];
if (!channelName.length && [url.path.lastPathComponent isEqualToString:@"/"]) {
channelName = @"#";
} else if (!channelName.length) {
channelName = url.path.lastPathComponent;
}
if (![channelName hasPrefix:@"#"] && ![channelName hasPrefix:@"&"]) {
channelName = [@"#" stringByAppendingString:channelName];
}
channelName = [channelName stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[self _openIRCGroupChat:channelName onServer:[url host] withPassword:[url query]];
} else if ([scheme caseInsensitiveCompare:@"msim"] == NSOrderedSame) {
NSString *contactName = [url queryArgumentForKey:@"cID"];
if (contactName.length) {
if ([host isEqualToString:@"addContact"]) {
[AINewContactWindowController promptForNewContactOnWindow:nil
name:contactName
service:[adium.accountController firstServiceWithServiceID:serviceID]
account:nil];
} else if ([host isEqualToString:@"sendIM"]) {
[self _openChatToContactWithName:contactName
onService:serviceID
withMessage:nil];
}
}
} else {
//Default to opening the host as a name.
NSString *user = [url user];
NSString *host = [url host];
NSString *name;
if (user && [user length]) {
//jabber://tekjew@jabber.org
//msn://jdoe@hotmail.com
name = [NSString stringWithFormat:@"%@@%@",[url user],[url host]];
} else {
//aim://tekjew
name = host;
}
[self _openChatToContactWithName:[name compactedString]
onService:serviceID
withMessage:nil];
}
} else if ([scheme isEqualToString:@"adiumxtra"]) {
//Installs an adium extra
// adiumxtra://www.adiumxtras.com/path/to/xtra.zip
[[XtrasInstaller installer] installXtraAtURL:url];
}
}
#pragma mark Chat openers
- (void)_openChatToContactWithName:(NSString *)UID onService:(NSString *)serviceID withMessage:(NSString *)message
{
AIListContact *contact = [adium.contactController preferredContactWithUID:UID
andServiceID:serviceID
forSendingContentType:CONTENT_MESSAGE_TYPE];
if (contact) {
//Open the chat and set it as active
[adium.interfaceController setActiveChat:[adium.chatController openChatWithContact:contact
onPreferredAccount:YES]];
//Insert the message text as if the user had typed it after opening the chat
NSResponder *responder = [[NSApp keyWindow] earliestResponderOfClass:[NSTextView class]];
if (message && [responder isKindOfClass:[NSTextView class]] && [(NSTextView *)responder isEditable]) {
[responder insertText:message];
}
} else {
NSBeep();
}
}
- (void)_openIRCGroupChat:(NSString *)name onServer:(NSString *)server withPassword:(NSString *)password
{
AIAccount *ircAccount = nil;
for (AIAccount *account in adium.accountController.accounts) {
if ([account.service.serviceClass isEqualToString:@"IRC"] && [account.host isEqualToString:server]) {
ircAccount = account;
break;
}
}
if (!ircAccount) {
NSRunAlertPanel(AILocalizedString(@"Unable to Join Channel", nil),
AILocalizedString(@"Unable to join the channel %@, no account exists for server %@.", nil),
AILocalizedStringFromTable(@"OK", @"Buttons", "Verb 'OK' on a button"),
nil,
nil,
name,
server);
} else if (name) {
[adium.chatController chatWithName:name
identifier:nil
onAccount:ircAccount
chatCreationInfo:[NSDictionary dictionaryWithObjectsAndKeys:
name, @"channel",
password, @"password", /* may be nil, so should be last */
nil]];
} else {
NSBeep();
}
}
- (void)_openXMPPGroupChat:(NSString *)name onServer:(NSString *)server withPassword:(NSString *)password
{
AIAccount *account = nil;
//Find an XMPP-compatible online account which can create group chats
for (account in adium.accountController.accounts) {
if (account.online &&
[account.service.serviceClass isEqualToString:@"Jabber"] &&
[account.service canCreateGroupChats]) {
break;
}
}
if (name && account) {
[adium.chatController chatWithName:[NSString stringWithFormat:@"%@@%@", name, server]
identifier:nil
onAccount:account
chatCreationInfo:[NSDictionary dictionaryWithObjectsAndKeys:
name, @"room",
server, @"server",
account.displayName, @"handle",
password, @"password", /* may be nil, so should be last */
nil]];
} else {
NSBeep();
}
}
- (void)_openAIMGroupChat:(NSString *)roomname onExchange:(NSInteger)exchange
{
AIAccount *account;
//Find an AIM-compatible online account which can create group chats
for (account in adium.accountController.accounts) {
if (account.online &&
[account.service.serviceClass isEqualToString:@"AIM-compatible"] &&
[account.service canCreateGroupChats]) {
break;
}
}
if (roomname && account) {
[adium.chatController chatWithName:roomname
identifier:nil
onAccount:account
chatCreationInfo:[NSDictionary dictionaryWithObjectsAndKeys:
roomname, @"room",
[NSNumber numberWithInteger:exchange], @"exchange",
nil]];
} else {
NSBeep();
}
}
@end
|