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 "AIContentController.h"
18 #import "CBActionSupportPlugin.h"
19 #import "AIInterfaceController.h"
24 * @class CBActionSupportPlugin
25 * @brief Simple outgoing content filter to turn "/me blah" into "*blah*"
27 @implementation CBActionSupportPlugin
34 [[adium contentController] registerContentFilter:self ofType:AIFilterContent direction:AIFilterOutgoing];
39 - (BOOL)includesDisplayNameInReplacement
41 return includesDisplayName;
43 - (void)setIncludesDisplayNameInReplacement:(BOOL)flag
45 includesDisplayName = YES;
53 - (NSAttributedString *)filterAttributedString:(NSAttributedString *)inAttributedString context:(id)context
55 NSMutableAttributedString *ourMessage = nil;
57 if (inAttributedString &&
58 [inAttributedString length] &&
59 [[inAttributedString string] rangeOfString:@"/me"
60 options:NSLiteralSearch
61 range:NSMakeRange(0, [inAttributedString length])].location != NSNotFound) {
63 NSString *startReplacement = @"*", *endReplacement = @"*";
65 unsigned replacementLength;
67 ourMessage = [[inAttributedString mutableCopyWithZone:[inAttributedString zone]] autorelease];
68 str = [ourMessage mutableString];
70 if(includesDisplayName) {
71 startReplacement = [startReplacement stringByAppendingString:[[[[[adium interfaceController] activeChat] account] displayName] stringByAppendingString:@" "]];
74 replacementLength = [startReplacement length] + [endReplacement length];
76 extent = NSMakeRange(0, [str length]);
77 do { //while(extent.length)
78 NSRange lineRange, searchRange, meRange;
80 unsigned endInsertPoint;
82 lineRange = NSMakeRange(extent.location, 1);
84 [str getLineStart:&lineRange.location
86 contentsEnd:&endInsertPoint
88 lineRange.length -= lineRange.location;
90 searchRange = NSMakeRange(lineRange.location, endInsertPoint - lineRange.location);
91 meRange = [str rangeOfString:@"/me " options:NSLiteralSearch range:searchRange];
93 if(meRange.location == lineRange.location && meRange.length == 4) {
94 NSAttributedString *endSplat = [[NSAttributedString alloc] initWithString:endReplacement
95 attributes:[ourMessage attributesAtIndex:endInsertPoint-1
97 [ourMessage insertAttributedString:endSplat atIndex:endInsertPoint];
100 [ourMessage replaceCharactersInRange:meRange withString:startReplacement];
102 shift = meRange.length - replacementLength;
105 shift += lineRange.length;
106 if(shift > extent.length) shift = extent.length;
107 extent.location += shift;
108 extent.length -= shift;
109 } while(extent.length);
112 return (ourMessage ? ourMessage : inAttributedString);
116 * @brief Filter priority
118 - (float)filterPriority
120 return DEFAULT_FILTER_PRIORITY;