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 <Adium/AIContentControllerProtocol.h>
19 #import <AIHyperlinks/AIHyperlinks.h>
22 * @class AIAutoLinkingPlugin
23 * @brief Filter component ta 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];
41 - (void)uninstallPlugin
43 [[adium contentController] unregisterContentFilter:self];
51 [hyperlinkScanner release];
57 * @brief Filter an attributed string to add links as appropriate
59 - (NSAttributedString *)filterAttributedString:(NSAttributedString *)inAttributedString context:(id)context
61 if(!inAttributedString || ![inAttributedString length]) return inAttributedString;
63 NSMutableAttributedString *replacementMessage = [inAttributedString mutableCopy];
64 NSRange linkRange = NSMakeRange(0,0);
65 unsigned stringLength = [replacementMessage length];
67 for (int i = 0; i < stringLength; i += linkRange.length) {
68 if (![replacementMessage attribute:NSLinkAttributeName atIndex:i longestEffectiveRange:&linkRange inRange:NSMakeRange(i, stringLength - i)]) {
69 /* If there's no link at this index already, process it via the hyperlinkScanner to see if there should be one.
70 * We don't process existing links because (a) it would be duplicative effort and (b) we might mess up a link which had
71 * a linkable item within its text, such as "Check out the new story at adiumx.com" linked to an adiumx.com page.
73 NSAttributedString *replacementPart = [hyperlinkScanner linkifyString:[inAttributedString attributedSubstringFromRange:linkRange]];
74 [replacementMessage replaceCharactersInRange:linkRange
75 withAttributedString:replacementPart];
76 stringLength -= linkRange.length;
77 linkRange.length = [replacementPart length];
78 stringLength += linkRange.length;
82 return [replacementMessage autorelease];
86 * @brief Filter priority
88 * Auto linking overrides other potential filters; do it first
90 - (float)filterPriority
92 return HIGHEST_FILTER_PRIORITY;