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 <Adium/AIContentControllerProtocol.h>
18 #import "CBActionSupportPlugin.h"
21 * @class CBActionSupportPlugin
22 * @brief Simple content filter to turn "/me blah" into "<span class='actionMessageUserName'>Name of contact </span><span class="actionMessageBody">blah</span>"
24 @implementation CBActionSupportPlugin
31 [[adium contentController] registerContentFilter:self ofType:AIFilterMessageDisplay direction:AIFilterOutgoing];
32 [[adium contentController] registerContentFilter:self ofType:AIFilterMessageDisplay direction:AIFilterIncoming];
35 - (void)uninstallPlugin
37 [[adium contentController] unregisterContentFilter:self];
45 - (NSAttributedString *)filterAttributedString:(NSAttributedString *)inAttributedString context:(id)context
47 NSMutableAttributedString *ourMessage = nil;
49 if (inAttributedString &&
50 [inAttributedString length] &&
51 [[inAttributedString string] rangeOfString:@"/me"
52 options:NSLiteralSearch
53 range:NSMakeRange(0, [inAttributedString length])].location != NSNotFound) {
55 NSString *startReplacement = @"%actionMessage%", *endReplacement = @"%/actionMessage%";
57 unsigned replacementLength;
59 ourMessage = [[inAttributedString mutableCopyWithZone:[inAttributedString zone]] autorelease];
60 str = [ourMessage mutableString];
62 // startReplacement = [startReplacement stringByAppendingString:[[[[[adium interfaceController] activeChat] account] displayName] stringByAppendingString:@" "]];
63 // endReplacement = @"";
65 replacementLength = [startReplacement length] + [endReplacement length];
67 extent = NSMakeRange(0, [str length]);
68 do { //while (extent.length)
69 NSRange lineRange, searchRange, meRange;
71 unsigned endInsertPoint;
73 lineRange = NSMakeRange(extent.location, 1);
75 [str getLineStart:&lineRange.location
77 contentsEnd:&endInsertPoint
79 lineRange.length -= lineRange.location;
81 searchRange = NSMakeRange(lineRange.location, endInsertPoint - lineRange.location);
82 meRange = [str rangeOfString:@"/me " options:(NSLiteralSearch | NSCaseInsensitiveSearch) range:searchRange];
84 if (meRange.location == lineRange.location && meRange.length == 4) {
85 NSAttributedString *endSplat = [[NSAttributedString alloc] initWithString:endReplacement
86 attributes:[ourMessage attributesAtIndex:endInsertPoint-1
88 [ourMessage insertAttributedString:endSplat atIndex:endInsertPoint];
91 [ourMessage replaceCharactersInRange:meRange withString:startReplacement];
93 shift = meRange.length - replacementLength;
96 shift += lineRange.length;
97 if (shift > extent.length) shift = extent.length;
98 extent.location += shift;
99 extent.length -= shift;
100 } while (extent.length);
103 return (ourMessage ? ourMessage : inAttributedString);
107 * @brief Filter priority
109 - (float)filterPriority
111 return DEFAULT_FILTER_PRIORITY;