Plugins/Twitter Plugin/AILaconicaAccount.m
author Zachary West <zacw@adium.im>
Fri Oct 16 10:12:34 2009 -0400 (2009-10-16)
changeset 2612 cb1fd4d17218
parent 1623 be3d49c65906
child 2795 ffdf2878fc68
permissions -rw-r--r--
Patch from brion to add an SSL option for StatusNet accounts. Fixes #13077.
     1 /* 
     2  * Adium is the legal property of its developers, whose names are listed in the copyright file included
     3  * with this source distribution.
     4  * 
     5  * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
     6  * General Public License as published by the Free Software Foundation; either version 2 of the License,
     7  * or (at your option) any later version.
     8  * 
     9  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
    10  * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
    11  * Public License for more details.
    12  * 
    13  * You should have received a copy of the GNU General Public License along with this program; if not,
    14  * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
    15  */
    16 
    17 #import "AILaconicaAccount.h"
    18 #import "AITwitterURLParser.h"
    19 
    20 @implementation AILaconicaAccount
    21 
    22 - (void)initAccount
    23 {
    24 	[super initAccount];
    25 	[adium.preferenceController registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:
    26 												  [NSNumber numberWithBool:YES], LACONICA_PREFERENCE_SSL, nil]
    27 										forGroup:LACONICA_PREF_GROUP
    28 										  object:self];
    29 }
    30 
    31 - (void)connect
    32 {	
    33 	if (!self.host) {
    34 		[self setLastDisconnectionError:AILocalizedString(@"No Host set", nil)];
    35 		[self didDisconnect];
    36 	} else {
    37 		[super connect];
    38 	}
    39 }
    40 
    41 /*!
    42  * @brief Our default server if none is provided.
    43  *
    44  * Do not set a default server.
    45  */
    46 - (NSString *)defaultServer
    47 {
    48 	return nil;
    49 }
    50 
    51 /*!
    52  * @brief API path
    53  *
    54  * The API path extension for the given host.
    55  */
    56 - (NSString *)apiPath
    57 {
    58 	// We need to guarantee this is an NSString, so -stringByAppendingPathComponent works.
    59 	NSString *path = [self preferenceForKey:LACONICA_PREFERENCE_PATH group:LACONICA_PREF_GROUP] ?: @"";
    60 	
    61 	return [path stringByAppendingPathComponent:@"api"];
    62 }
    63 
    64 /*!
    65  * @brief Our source token
    66  *
    67  * On Laconica, our given source token is "adium".
    68  */
    69 - (NSString *)sourceToken
    70 {
    71 	return @"adium";
    72 }
    73 
    74 /*!
    75  * @brief Our explicit formatted UID
    76  * 
    77  * This includes "additional necessary identifying information".
    78  */
    79 - (NSString *)explicitFormattedUID
    80 {
    81 	if (self.host) {
    82 		return [NSString stringWithFormat:@"%@ (%@)", self.UID, self.host];
    83 	} else {
    84 		return self.UID;
    85 	}
    86 }
    87 
    88 /*!
    89  * @brief Use our host for the servername when storing password
    90  */
    91 - (BOOL)useHostForPasswordServerName
    92 {
    93 	return YES;
    94 }
    95 
    96 /*!
    97  * @brief Not all StatusNet instances support HTTPS connections.
    98  */
    99 - (BOOL)useSSL
   100 {
   101 	return [[self preferenceForKey:LACONICA_PREFERENCE_SSL group:LACONICA_PREF_GROUP] boolValue];
   102 }
   103 
   104 /*!
   105  * @brief Laconica does not yet support OAuth.
   106  */
   107 - (BOOL)useOAuth
   108 {
   109 	return NO;
   110 }
   111 
   112 /*!
   113  * @brief Returns the link URL for a specific type of link
   114  */
   115 - (NSString *)addressForLinkType:(AITwitterLinkType)linkType
   116 						  userID:(NSString *)userID
   117 						statusID:(NSString *)statusID
   118 						 context:(NSString *)context
   119 {
   120 	NSString *address = [super addressForLinkType:linkType userID:userID statusID:statusID context:context];
   121 	
   122 	NSString *fullAddress = [self.host stringByAppendingPathComponent:[self preferenceForKey:LACONICA_PREFERENCE_PATH group:LACONICA_PREF_GROUP]];
   123 	
   124 	NSString *protocol = self.useSSL ? @"https" : @"http";
   125 	
   126 	if (linkType == AITwitterLinkStatus) {
   127 		address = [NSString stringWithFormat:@"%@://%@/notice/%@", protocol, fullAddress, statusID];
   128 	} else if (linkType == AITwitterLinkFriends) {
   129 		address = [NSString stringWithFormat:@"%@://%@/%@/subscriptions", protocol, fullAddress, userID];
   130 	} else if (linkType == AITwitterLinkFollowers) {
   131 		address = [NSString stringWithFormat:@"%@://%@/%@/subscribers", protocol, fullAddress, userID]; 
   132 	} else if (linkType == AITwitterLinkUserPage) {
   133 		address = [NSString stringWithFormat:@"%@://%@/%@", protocol, fullAddress, userID]; 
   134 	} else if (linkType == AITwitterLinkSearchHash) {
   135 		address = [NSString stringWithFormat:@"http://%@/tag/%@", fullAddress, context];
   136 	} else if (linkType == AITwitterLinkGroup) {
   137 		address = [NSString stringWithFormat:@"http://%@/group/%@", fullAddress, context];
   138 	}
   139 	
   140 	return address;
   141 }
   142 
   143 /*!
   144  * @brief Parse an attributed string into a linkified version.
   145  */
   146 - (NSAttributedString *)linkifiedAttributedStringFromString:(NSAttributedString *)inString
   147 {	
   148 	NSAttributedString *attributedString = [super linkifiedAttributedStringFromString:inString];
   149 	
   150 	static NSCharacterSet *groupCharacters = nil;
   151 	
   152 	if (!groupCharacters) {
   153 		NSMutableCharacterSet	*disallowedCharacters = [[NSCharacterSet punctuationCharacterSet] mutableCopy];
   154 		[disallowedCharacters formUnionWithCharacterSet:[NSCharacterSet whitespaceCharacterSet]];
   155 		
   156 		groupCharacters = [[disallowedCharacters invertedSet] retain];
   157 		
   158 		[disallowedCharacters release];	
   159 	}
   160 	
   161 	attributedString = [AITwitterURLParser linkifiedStringFromAttributedString:attributedString
   162 															forPrefixCharacter:@"!"
   163 																   forLinkType:AITwitterLinkGroup
   164 																	forAccount:self
   165 															 validCharacterSet:groupCharacters];
   166 	
   167 	return attributedString;
   168 }
   169 
   170 @end