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 "AIAutoReplyPlugin.h"
18 #import "AIContactController.h"
19 #import "AIContentController.h"
20 #import "AIPreferenceController.h"
21 #import "AIStatusController.h"
22 #import <AIUtilities/AIArrayAdditions.h>
23 #import <Adium/AIAccount.h>
24 #import <Adium/AIChat.h>
25 #import <Adium/AIContentMessage.h>
26 #import <Adium/AIContentObject.h>
29 * @class AIAutoReplyPlugin
30 * @brief Provides AutoReply functionality for the state system
32 * This class implements the state system behavior for auto-reply. If auto-reply status is active on an account,
33 * initial messages recieved on that account will be replied to automatically. Subsequent messages will not receive
34 * a reply unless the chat window is closed.
36 * This is the expected behavior on certain protocols such as AIM, and considered a convenience on other protocols.
38 @implementation AIAutoReplyPlugin
41 * @brief Initialize the auto-reply system
43 * Initialize the auto-reply system to monitor account status. When an account auto-reply flag is set we begin to
44 * monitor chat messaging and auto-reply as necessary.
49 receivedAutoReply = [[NSMutableArray alloc] init];
52 [[adium notificationCenter] addObserver:self
53 selector:@selector(didReceiveContent:)
54 name:CONTENT_MESSAGE_RECEIVED object:nil];
55 [[adium notificationCenter] addObserver:self
56 selector:@selector(didSendContent:)
57 name:CONTENT_MESSAGE_SENT object:nil];
58 [[adium notificationCenter] addObserver:self
59 selector:@selector(chatWillClose:)
60 name:Chat_WillClose object:nil];
62 [[adium contactController] registerListObjectObserver:self];
70 [[adium contactController] unregisterListObjectObserver:self];
71 [receivedAutoReply release];
77 * @brief Account status changed.
79 * Update our chat monitoring in response to account status changes.
81 * TODO: If there are no accounts with an auto-reply flag set we SHOULD stop monitoring messages for optimal performance.
83 - (NSSet *)updateListObject:(AIListObject *)inObject keys:(NSSet *)inModifiedKeys silent:(BOOL)silent
85 if([inObject isKindOfClass:[AIAccount class]] &&
86 [inModifiedKeys containsObject:@"StatusState"]){
88 //Reset our list of contacts who have already received an auto-reply
89 [receivedAutoReply release];
90 receivedAutoReply = [[NSMutableArray alloc] init];
97 * @brief Respond to a received message
99 * Respond to a received message by sending out the current auto-reply. We only send the auto-reply once to each
100 * contact, and then their name is added to a list and additional received messages are ignored.
102 - (void)didReceiveContent:(NSNotification *)notification
104 AIContentObject *contentObject = [[notification userInfo] objectForKey:@"AIContentObject"];
105 AIChat *chat = [contentObject chat];
107 //We will not respond to the received message if it is an auto-reply, over a chat where we have already responded,
108 //or over a chat with a name.
109 //XXX - Using the presence of a name on the chat to determine if it's multi-user or not is not clean -ai
110 if([[contentObject type] isEqualToString:CONTENT_MESSAGE_TYPE] &&
111 ![(AIContentMessage *)contentObject isAutoreply] &&
112 ![receivedAutoReply containsObjectIdenticalTo:chat] &&
115 [self sendAutoReplyFromAccount:[contentObject destination]
116 toContact:[contentObject source]
118 [receivedAutoReply addObject:chat];
123 * @brief Send an auto-reply
125 * Sends our current auto-reply to the specified contact.
126 * @param source Account sending the object
127 * @param destination Contact receiving the object
128 * @param chat Chat the communication is occuring over
130 - (void)sendAutoReplyFromAccount:(id)source toContact:(id)destination onChat:(AIChat *)chat
132 AIContentMessage *responseContent;
133 NSAttributedString *autoReply;
135 if(autoReply = [[[chat account] statusState] autoReply]){
136 responseContent = [AIContentMessage messageInChat:chat
138 destination:destination
143 [[adium contentController] sendContentObject:responseContent];
148 * @brief Respond to our user sending messages
150 * For convenience, when our user messages a contact while away we exclude that contact from receiving our auto-away
151 * on future messages.
153 - (void)didSendContent:(NSNotification *)notification
155 AIContentObject *contentObject = [[notification userInfo] objectForKey:@"AIContentObject"];
156 AIChat *chat = [contentObject chat];
158 if([[contentObject type] isEqualToString:CONTENT_MESSAGE_TYPE]){
159 if(![receivedAutoReply containsObjectIdenticalTo:chat]){
160 [receivedAutoReply addObject:chat];
166 * @brief Respond to a chat closing
168 * Once a chat is closed we forget about whether it has received an auto-response. If the chat is re-opened, it will
169 * receive our auto-response again. This behavior is not necessarily desired, but is a side effect of basing our
170 * already-received list on chats and not contacts. However, many users have come to expect this behavior and it's
171 * presence is neither strongly negative or positive.
173 - (void)chatWillClose:(NSNotification *)notification
175 [receivedAutoReply removeObjectIdenticalTo:[notification object]];