2 * Adium is the legal property of its developers, whose names are listed in the copyright file included
3 * with this source distribution.
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.
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.
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.
17 #import "AIAutoLinkingPlugin.h"
18 #import "AIContentController.h"
19 #import <AIHyperlinks/AIHyperlinks.h>
22 * @class AIAutoLinkingPlugin
23 * @brief Filter component to automatically create links within attributed strings as appropriate
25 * The bulk of this component's work is accomplished by SHHyperlinkScanner
27 @implementation AIAutoLinkingPlugin
34 hyperlinkScanner = [[SHHyperlinkScanner alloc] initWithStrictChecking:NO];
36 [[adium contentController] registerContentFilter:self ofType:AIFilterDisplay direction:AIFilterIncoming];
37 [[adium contentController] registerContentFilter:self ofType:AIFilterMessageDisplay direction:AIFilterIncoming];
38 [[adium contentController] registerContentFilter:self ofType:AIFilterMessageDisplay direction:AIFilterOutgoing];
46 [hyperlinkScanner release];
52 * @brief Filter an attributed string to add links as appropriate
54 - (NSAttributedString *)filterAttributedString:(NSAttributedString *)inAttributedString context:(id)context
56 NSMutableAttributedString *replacementMessage = nil;
58 if(inAttributedString){
59 NSRange linkRange = NSMakeRange(0,0);
61 unsigned stringLength = [inAttributedString length];
63 replacementMessage = [[[NSMutableAttributedString alloc] initWithString:@""] autorelease];
65 // do some quick scanning to avoid overwriting custom titled links that happen to have domain names in them.
66 // e.g. if the entire string "check out the new story on adiumx.com" is linked to a specific story URI, then
67 // adiumx.com should not link to only http://adiumx.com
68 while(index < stringLength){
69 if([inAttributedString attribute:NSLinkAttributeName atIndex:index effectiveRange:&linkRange]){
70 // if the link is found at that index, append the link's whole range to the replacement string
71 [replacementMessage appendAttributedString:[inAttributedString attributedSubstringFromRange:linkRange]];
73 // if not, the range to the next link attribute is returned, and we linkify that range's substring
74 [replacementMessage appendAttributedString:[hyperlinkScanner linkifyString:[inAttributedString attributedSubstringFromRange:linkRange]]];
77 index += linkRange.length;
81 return (replacementMessage);
85 * @brief Filter priority
87 * Auto linking overrides other potential filters; do it first
89 - (float)filterPriority
91 return HIGHEST_FILTER_PRIORITY;