French updates
[adiumx.git] / Source / CBActionSupportPlugin.m
blobba8045842c4a16ba0a5235b5cef00cbfe1535857
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  */
17 #import "AIContentController.h"
18 #import "CBActionSupportPlugin.h"
19 #import "AIInterfaceController.h"
20 #import "AIChat.h"
21 #import "AIAccount.h"
23 /*!
24  * @class CBActionSupportPlugin
25  * @brief Simple outgoing content filter to turn "/me blah" into "*blah*"
26  */
27 @implementation CBActionSupportPlugin
29 /*!
30  * @brief Install
31  */
32 - (void)installPlugin
34         [[adium contentController] registerContentFilter:self ofType:AIFilterContent direction:AIFilterOutgoing];
37 #pragma mark -
39 - (BOOL)includesDisplayNameInReplacement
41         return includesDisplayName;
43 - (void)setIncludesDisplayNameInReplacement:(BOOL)flag
45         includesDisplayName = YES;
48 #pragma mark -
50 /*!
51  * @brief Filter
52  */
53 - (NSAttributedString *)filterAttributedString:(NSAttributedString *)inAttributedString context:(id)context
55         NSMutableAttributedString   *ourMessage = nil;
56         
57         if (inAttributedString && 
58                 [inAttributedString length] &&
59                 [[inAttributedString string] rangeOfString:@"/me"
60                                                                                    options:NSLiteralSearch
61                                                                                          range:NSMakeRange(0, [inAttributedString length])].location != NSNotFound) {
62                 NSMutableString *str;
63                 NSString                *startReplacement = @"*", *endReplacement = @"*";
64                 NSRange                 extent;
65                 unsigned                replacementLength;
66                 
67                 ourMessage = [[inAttributedString mutableCopyWithZone:[inAttributedString zone]] autorelease];
68                 str = [ourMessage mutableString];
70                 if(includesDisplayName) {
71                         startReplacement = [startReplacement stringByAppendingString:[[[[[adium interfaceController] activeChat] account] displayName] stringByAppendingString:@" "]];
72                         endReplacement = @"";
73                 }
74                 replacementLength = [startReplacement length] + [endReplacement length];
76                 extent = NSMakeRange(0, [str length]);
77                 do { //while(extent.length)
78                         NSRange lineRange, searchRange, meRange;
79                         signed shift = 0;
80                         unsigned endInsertPoint;
81                         
82                         lineRange = NSMakeRange(extent.location, 1);
83                         endInsertPoint = 0;
84                         [str getLineStart:&lineRange.location
85                                       end:&lineRange.length
86                               contentsEnd:&endInsertPoint
87                                  forRange:lineRange];
88                         lineRange.length -= lineRange.location;
89                         
90                         searchRange = NSMakeRange(lineRange.location, endInsertPoint - lineRange.location);
91                         meRange = [str rangeOfString:@"/me " options:NSLiteralSearch range:searchRange];
92                         
93                         if(meRange.location == lineRange.location && meRange.length == 4) {
94                                 NSAttributedString *endSplat = [[NSAttributedString alloc] initWithString:endReplacement 
95                                                                                                                                                         attributes:[ourMessage attributesAtIndex:endInsertPoint-1
96                                                                                                                                                                                                           effectiveRange:nil]];
97                                 [ourMessage insertAttributedString:endSplat atIndex:endInsertPoint];
98                                 [endSplat release];
100                                 [ourMessage replaceCharactersInRange:meRange withString:startReplacement];
102                                 shift = meRange.length - replacementLength;
103                         }
104                         
105                         shift += lineRange.length;
106                         if(shift > extent.length) shift = extent.length;
107                         extent.location += shift;
108                         extent.length   -= shift;
109                 } while(extent.length);
110         }
111         
112         return (ourMessage ? ourMessage : inAttributedString);
116  * @brief Filter priority
117  */
118 - (float)filterPriority
120         return DEFAULT_FILTER_PRIORITY;
123 @end