1 /*
2  * Offline Message Emulation - Save messages sent to an offline user as pounce
3  * Copyright (C) 2004
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  * 02111-1301, USA.
19  */
20 #include "internal.h"
21 
22 #define PLUGIN_ID			"core-plugin_pack-offlinemsg"
23 #define PLUGIN_NAME			N_("Offline Message Emulation")
24 #define PLUGIN_STATIC_NAME	offlinemsg
25 #define PLUGIN_SUMMARY		N_("Save messages sent to an offline user as pounce.")
26 #define PLUGIN_DESCRIPTION	N_("Save messages sent to an offline user as pounce.")
27 #define PLUGIN_AUTHOR		"Sadrul H Chowdhury <sadrul@users.sourceforge.net>"
28 
29 /* Purple headers */
30 #include <version.h>
31 
32 #include <blist.h>
33 #include <conversation.h>
34 #include <core.h>
35 #include <debug.h>
36 #include <pounce.h>
37 #include <request.h>
38 
39 #define	PREF_PREFIX		"/plugins/core/" PLUGIN_ID
40 #define	PREF_ALWAYS		PREF_PREFIX "/always"
41 
42 typedef struct _OfflineMsg OfflineMsg;
43 
44 typedef enum
45 {
46 	OFFLINE_MSG_NONE,
47 	OFFLINE_MSG_YES,
48 	OFFLINE_MSG_NO
49 } OfflineMessageSetting;
50 
51 struct _OfflineMsg
52 {
53 	PurpleAccount *account;
54 	PurpleConversation *conv;
55 	char *who;
56 	char *message;
57 };
58 
59 static void
discard_data(OfflineMsg * offline)60 discard_data(OfflineMsg *offline)
61 {
62 	g_free(offline->who);
63 	g_free(offline->message);
64 	g_free(offline);
65 }
66 
67 static void
cancel_poune(OfflineMsg * offline)68 cancel_poune(OfflineMsg *offline)
69 {
70 	purple_conversation_set_data(offline->conv, "plugin_pack:offlinemsg",
71 				GINT_TO_POINTER(OFFLINE_MSG_NO));
72 	purple_conv_im_send_with_flags(PURPLE_CONV_IM(offline->conv), offline->message, 0);
73 	discard_data(offline);
74 }
75 
76 static void
record_pounce(OfflineMsg * offline)77 record_pounce(OfflineMsg *offline)
78 {
79 	PurplePounce *pounce;
80 	PurplePounceEvent event;
81 	PurplePounceOption option;
82 	PurpleConversation *conv;
83 
84 	event = PURPLE_POUNCE_SIGNON;
85 	option = PURPLE_POUNCE_OPTION_NONE;
86 
87 	pounce = purple_pounce_new(purple_core_get_ui(), offline->account, offline->who,
88 					event, option);
89 
90 	purple_pounce_action_set_enabled(pounce, "send-message", TRUE);
91 	purple_pounce_action_set_attribute(pounce, "send-message", "message", offline->message);
92 
93 	conv = offline->conv;
94 	if (!purple_conversation_get_data(conv, "plugin_pack:offlinemsg"))
95 		purple_conversation_write(conv, NULL, _("The rest of the messages will be saved "
96 							"as pounces. You can edit/delete the pounce from the `Buddy "
97 							"Pounce' dialog."),
98 							PURPLE_MESSAGE_SYSTEM, time(NULL));
99 	purple_conversation_set_data(conv, "plugin_pack:offlinemsg",
100 				GINT_TO_POINTER(OFFLINE_MSG_YES));
101 
102 	purple_conv_im_write(PURPLE_CONV_IM(conv), offline->who, offline->message,
103 				PURPLE_MESSAGE_SEND, time(NULL));
104 
105 	discard_data(offline);
106 }
107 
108 static void
sending_msg_cb(PurpleAccount * account,const char * who,char ** message,gpointer handle)109 sending_msg_cb(PurpleAccount *account, const char *who, char **message, gpointer handle)
110 {
111 	PurpleBuddy *buddy;
112 	OfflineMsg *offline;
113 	PurpleConversation *conv;
114 	OfflineMessageSetting setting;
115 
116 	if (message == NULL || *message == NULL ||
117 			**message == '\0')
118 		return;
119 
120 	buddy = purple_find_buddy(account, who);
121 	if (!buddy)
122 		return;
123 
124 	if (purple_presence_is_online(purple_buddy_get_presence(buddy)))
125 		return;
126 
127 	if (purple_account_supports_offline_message(account, buddy))
128 	{
129 		purple_debug_info("offlinemsg", "Account \"%s\" supports offline messages.\n",
130 					purple_account_get_username(account));
131 		return;
132 	}
133 
134 	conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM,
135 					who, account);
136 
137 	if (!conv)
138 		return;
139 
140 	setting = GPOINTER_TO_INT(purple_conversation_get_data(conv, "plugin_pack:offlinemsg"));
141 	if (setting == OFFLINE_MSG_NO)
142 		return;
143 
144 	offline = g_new0(OfflineMsg, 1);
145 	offline->conv = conv;
146 	offline->account = account;
147 	offline->who = g_strdup(who);
148 	offline->message = *message;
149 	*message = NULL;
150 
151 	if (purple_prefs_get_bool(PREF_ALWAYS) || setting == OFFLINE_MSG_YES) {
152 		record_pounce(offline);
153 	} else if (setting == OFFLINE_MSG_NONE) {
154 		char *ask;
155 		ask = g_strdup_printf(_("\"%s\" is currently offline. Do you want to save the "
156 						"rest of the messages in a pounce and automatically send them "
157 						"when \"%s\" logs back in?"), who, who);
158 
159 		purple_request_action(handle, _("Offline Message"), ask,
160 					_("You can edit/delete the pounce from the `Buddy Pounces' dialog"),
161 					0,
162 					offline->account, offline->who, offline->conv,
163 					offline, 2,
164 					_("Yes"), record_pounce,
165 					_("No"), cancel_poune);
166 		g_free(ask);
167 	} else {
168 		discard_data(offline);
169 	}
170 }
171 
172 static gboolean
plugin_load(PurplePlugin * plugin)173 plugin_load(PurplePlugin *plugin)
174 {
175 	purple_signal_connect_priority(purple_conversations_get_handle(), "sending-im-msg",
176 					plugin, PURPLE_CALLBACK(sending_msg_cb), plugin, PURPLE_SIGNAL_PRIORITY_HIGHEST);
177 	return TRUE;
178 }
179 
180 static gboolean
plugin_unload(PurplePlugin * plugin)181 plugin_unload(PurplePlugin *plugin)
182 {
183 	return TRUE;
184 }
185 
186 static PurplePluginPrefFrame *
get_plugin_pref_frame(PurplePlugin * plugin)187 get_plugin_pref_frame(PurplePlugin *plugin)
188 {
189 	PurplePluginPrefFrame *frame;
190 	PurplePluginPref *pref;
191 
192 	frame = purple_plugin_pref_frame_new();
193 
194 	pref = purple_plugin_pref_new_with_label(_("Save offline messages in pounce"));
195 	purple_plugin_pref_frame_add(frame, pref);
196 
197 	pref = purple_plugin_pref_new_with_name_and_label(PREF_ALWAYS,
198 					_("Do not ask. Always save in pounce."));
199 	purple_plugin_pref_frame_add(frame, pref);
200 
201 	return frame;
202 }
203 
204 static PurplePluginUiInfo prefs_info = {
205 	get_plugin_pref_frame,
206 	0,
207 	NULL,
208 
209 	/* padding */
210 	NULL,
211 	NULL,
212 	NULL,
213 	NULL
214 };
215 
216 static PurplePluginInfo info =
217 {
218 	PURPLE_PLUGIN_MAGIC,			/* Magic				*/
219 	PURPLE_MAJOR_VERSION,			/* Purple Major Version	*/
220 	PURPLE_MINOR_VERSION,			/* Purple Minor Version	*/
221 	PURPLE_PLUGIN_STANDARD,			/* plugin type			*/
222 	NULL,					/* ui requirement		*/
223 	0,					/* flags				*/
224 	NULL,					/* dependencies			*/
225 	PURPLE_PRIORITY_DEFAULT,			/* priority				*/
226 
227 	PLUGIN_ID,				/* plugin id			*/
228 	PLUGIN_NAME,				/* name					*/
229 	DISPLAY_VERSION,			/* version				*/
230 	PLUGIN_SUMMARY,				/* summary				*/
231 	PLUGIN_DESCRIPTION,			/* description			*/
232 	PLUGIN_AUTHOR,				/* author				*/
233 	PURPLE_WEBSITE,				/* website				*/
234 
235 	plugin_load,				/* load					*/
236 	plugin_unload,				/* unload				*/
237 	NULL,					/* destroy				*/
238 
239 	NULL,					/* ui_info				*/
240 	NULL,					/* extra_info			*/
241 	&prefs_info,				/* prefs_info			*/
242 	NULL,					/* actions				*/
243 
244 	/* padding */
245 	NULL,
246 	NULL,
247 	NULL,
248 	NULL
249 };
250 
251 static void
init_plugin(PurplePlugin * plugin)252 init_plugin(PurplePlugin *plugin)
253 {
254 	purple_prefs_add_none(PREF_PREFIX);
255 	purple_prefs_add_bool(PREF_ALWAYS, FALSE);
256 }
257 
258 PURPLE_INIT_PLUGIN(PLUGIN_STATIC_NAME, init_plugin, info)
259