Now, groups can retrieve a list of the contacts in that group. I probably should...
[adiumx.git] / Source / CBActionSupportPlugin.m
blobfd2e23bac371fc6636100c10387d5c7fc2f03f4c
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 <Adium/AIContentControllerProtocol.h>
18 #import "CBActionSupportPlugin.h"
20 /*!
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>"
23  */
24 @implementation CBActionSupportPlugin
26 /*!
27  * @brief Install
28  */
29 - (void)installPlugin
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];
40 #pragma mark -
42 /*!
43  * @brief Filter
44  */
45 - (NSAttributedString *)filterAttributedString:(NSAttributedString *)inAttributedString context:(id)context
47         NSMutableAttributedString   *ourMessage = nil;
48         
49         if (inAttributedString && 
50                 [inAttributedString length] &&
51                 [[inAttributedString string] rangeOfString:@"/me"
52                                                                                    options:NSLiteralSearch
53                                                                                          range:NSMakeRange(0, [inAttributedString length])].location != NSNotFound) {
54                 NSMutableString *str;
55                 NSString                *startReplacement = @"%actionMessage%", *endReplacement = @"%/actionMessage%";
56                 NSRange                 extent;
57                 unsigned                replacementLength;
58                 
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;
70                         signed shift = 0;
71                         unsigned endInsertPoint;
72                         
73                         lineRange = NSMakeRange(extent.location, 1);
74                         endInsertPoint = 0;
75                         [str getLineStart:&lineRange.location
76                                       end:&lineRange.length
77                               contentsEnd:&endInsertPoint
78                                  forRange:lineRange];
79                         lineRange.length -= lineRange.location;
80                         
81                         searchRange = NSMakeRange(lineRange.location, endInsertPoint - lineRange.location);
82                         meRange = [str rangeOfString:@"/me " options:(NSLiteralSearch | NSCaseInsensitiveSearch) range:searchRange];
83                         
84                         if (meRange.location == lineRange.location && meRange.length == 4) {
85                                 NSAttributedString *endSplat = [[NSAttributedString alloc] initWithString:endReplacement 
86                                                                                                                                                         attributes:[ourMessage attributesAtIndex:endInsertPoint-1
87                                                                                                                                                                                                           effectiveRange:nil]];
88                                 [ourMessage insertAttributedString:endSplat atIndex:endInsertPoint];
89                                 [endSplat release];
91                                 [ourMessage replaceCharactersInRange:meRange withString:startReplacement];
93                                 shift = meRange.length - replacementLength;
94                         }
95                         
96                         shift += lineRange.length;
97                         if (shift > extent.length) shift = extent.length;
98                         extent.location += shift;
99                         extent.length   -= shift;
100                 } while (extent.length);
101         }
102         
103         return (ourMessage ? ourMessage : inAttributedString);
107  * @brief Filter priority
108  */
109 - (float)filterPriority
111         return DEFAULT_FILTER_PRIORITY;
114 @end