Adapt migration for files
[pidgin-git.git] / libpurple / plugins / offlinemsg.c
blobf0f77889585784d22ea5aed6024fe8b42ddc39e0
1 /*
2 * Offline Message Emulation - Save messages sent to an offline user as pounce
3 * Copyright (C) 2004
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.
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.
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.
20 #include "internal.h"
22 #define PLUGIN_ID "core-plugin_pack-offlinemsg"
23 #define PLUGIN_NAME N_("Offline Message Emulation")
24 #define PLUGIN_CATEGORY N_("Utility")
25 #define PLUGIN_STATIC_NAME offlinemsg
26 #define PLUGIN_SUMMARY N_("Save messages sent to an offline user as pounce.")
27 #define PLUGIN_DESCRIPTION N_("Save messages sent to an offline user as pounce.")
28 #define PLUGIN_AUTHORS {"Sadrul H Chowdhury <sadrul@users.sourceforge.net>", NULL}
30 /* Purple headers */
31 #include <version.h>
33 #include <buddylist.h>
34 #include <conversation.h>
35 #include <core.h>
36 #include <debug.h>
37 #include <pounce.h>
38 #include <request.h>
40 #define PREF_PREFIX "/plugins/core/" PLUGIN_ID
41 #define PREF_ALWAYS PREF_PREFIX "/always"
43 typedef struct _OfflineMsg OfflineMsg;
45 typedef enum
47 OFFLINE_MSG_NONE,
48 OFFLINE_MSG_YES,
49 OFFLINE_MSG_NO
50 } OfflineMessageSetting;
52 struct _OfflineMsg
54 PurpleAccount *account;
55 PurpleConversation *conv;
56 char *who;
57 char *message;
60 static void
61 discard_data(OfflineMsg *offline)
63 g_free(offline->who);
64 g_free(offline->message);
65 g_free(offline);
68 static void
69 cancel_poune(OfflineMsg *offline)
71 g_object_set_data(G_OBJECT(offline->conv), "plugin_pack:offlinemsg",
72 GINT_TO_POINTER(OFFLINE_MSG_NO));
73 purple_conversation_send_with_flags(offline->conv, offline->message, 0);
74 discard_data(offline);
77 static void
78 record_pounce(OfflineMsg *offline)
80 PurplePounce *pounce;
81 PurplePounceEvent event;
82 PurplePounceOption option;
83 PurpleConversation *conv;
84 char *temp;
86 event = PURPLE_POUNCE_SIGNON;
87 option = PURPLE_POUNCE_OPTION_NONE;
89 pounce = purple_pounce_new(purple_core_get_ui(), offline->account, offline->who,
90 event, option);
92 purple_pounce_action_set_enabled(pounce, "send-message", TRUE);
94 temp = g_strdup_printf("(%s) %s", _("Offline message"),
95 offline->message);
96 purple_pounce_action_set_attribute(pounce, "send-message", "message",
97 temp);
98 g_free(temp);
100 conv = offline->conv;
101 if (!g_object_get_data(G_OBJECT(conv), "plugin_pack:offlinemsg")) {
102 purple_conversation_write_system_message(conv,
103 _("The rest of the messages will be saved "
104 "as pounces. You can edit/delete the pounce from the `Buddy "
105 "Pounce' dialog."), 0);
107 g_object_set_data(G_OBJECT(conv), "plugin_pack:offlinemsg",
108 GINT_TO_POINTER(OFFLINE_MSG_YES));
110 /* TODO: use a reference to a PurpleMessage */
111 purple_conversation_write_message(conv,
112 purple_message_new_outgoing(offline->who, offline->message, 0));
114 discard_data(offline);
117 static void
118 sending_msg_cb(PurpleAccount *account, PurpleMessage *msg, gpointer handle)
120 PurpleBuddy *buddy;
121 OfflineMsg *offline;
122 PurpleConversation *conv;
123 OfflineMessageSetting setting;
124 const gchar *who = purple_message_get_recipient(msg);
126 if (purple_message_is_empty(msg))
127 return;
129 buddy = purple_blist_find_buddy(account, who);
130 if (!buddy)
131 return;
133 if (purple_presence_is_online(purple_buddy_get_presence(buddy)))
134 return;
136 if (purple_account_supports_offline_message(account, buddy))
138 purple_debug_info("offlinemsg", "Account \"%s\" supports offline messages.\n",
139 purple_account_get_username(account));
140 return;
143 conv = PURPLE_CONVERSATION(purple_conversations_find_im_with_account(who, account));
145 if (!conv)
146 return;
148 setting = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(conv), "plugin_pack:offlinemsg"));
149 if (setting == OFFLINE_MSG_NO)
150 return;
152 offline = g_new0(OfflineMsg, 1);
153 offline->conv = conv;
154 offline->account = account;
155 offline->who = g_strdup(who);
156 offline->message = g_strdup(purple_message_get_contents(msg));
157 purple_message_set_contents(msg, NULL);
159 if (purple_prefs_get_bool(PREF_ALWAYS) || setting == OFFLINE_MSG_YES)
160 record_pounce(offline);
161 else if (setting == OFFLINE_MSG_NONE)
163 char *ask;
164 ask = g_strdup_printf(_("\"%s\" is currently offline. Do you want to save the "
165 "rest of the messages in a pounce and automatically send them "
166 "when \"%s\" logs back in?"), who, who);
168 purple_request_action(handle, _("Offline Message"), ask,
169 _("You can edit/delete the pounce from the `Buddy Pounces' dialog"),
170 0, purple_request_cpar_from_conversation(offline->conv),
171 offline, 2,
172 _("Yes"), record_pounce,
173 _("No"), cancel_poune);
174 g_free(ask);
178 static PurplePluginPrefFrame *
179 get_plugin_pref_frame(PurplePlugin *plugin)
181 PurplePluginPrefFrame *frame;
182 PurplePluginPref *pref;
184 frame = purple_plugin_pref_frame_new();
186 pref = purple_plugin_pref_new_with_label(_("Save offline messages in pounce"));
187 purple_plugin_pref_frame_add(frame, pref);
189 pref = purple_plugin_pref_new_with_name_and_label(PREF_ALWAYS,
190 _("Do not ask. Always save in pounce."));
191 purple_plugin_pref_frame_add(frame, pref);
193 return frame;
196 static PurplePluginInfo *
197 plugin_query(GError **error)
199 const gchar * const authors[] = PLUGIN_AUTHORS;
201 return purple_plugin_info_new(
202 "id", PLUGIN_ID,
203 "name", PLUGIN_NAME,
204 "version", DISPLAY_VERSION,
205 "category", PLUGIN_CATEGORY,
206 "summary", PLUGIN_SUMMARY,
207 "description", PLUGIN_DESCRIPTION,
208 "authors", authors,
209 "website", PURPLE_WEBSITE,
210 "abi-version", PURPLE_ABI_VERSION,
211 "pref-frame-cb", get_plugin_pref_frame,
212 NULL
216 static gboolean
217 plugin_load(PurplePlugin *plugin, GError **error)
219 purple_prefs_add_none(PREF_PREFIX);
220 purple_prefs_add_bool(PREF_ALWAYS, FALSE);
222 purple_signal_connect_priority(purple_conversations_get_handle(), "sending-im-msg",
223 plugin, PURPLE_CALLBACK(sending_msg_cb), plugin, PURPLE_SIGNAL_PRIORITY_HIGHEST);
224 return TRUE;
227 static gboolean
228 plugin_unload(PurplePlugin *plugin, GError **error)
230 return TRUE;
233 PURPLE_PLUGIN_INIT(PLUGIN_STATIC_NAME, plugin_query, plugin_load, plugin_unload);