summaryrefslogtreecommitdiff
path: root/libpurple/plugins/idle.c
blob: 0860dafa829036050ccf74f87648a2fec6dd56e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
 * idle.c - I'dle Mak'er plugin for Purple
 *
 * This file is part of Purple.
 *
 * Purple is the legal property of its developers, whose names are too numerous
 * to list here.  Please refer to the COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 */

#include "internal.h"

#include "connection.h"
#include "debug.h"
#include "notify.h"
#include "plugin.h"
#include "request.h"
#include "server.h"
#include "status.h"
#include "version.h"

/* This plugin no longer depends on gtk */
#define IDLE_PLUGIN_ID "core-idle"

static GList *idled_accts = NULL;

static gboolean
unidle_filter(PurpleAccount *acct)
{
	if (g_list_find(idled_accts, acct))
		return TRUE;

	return FALSE;
}

static gboolean
idleable_filter(PurpleAccount *account)
{
	PurplePlugin *prpl;

	prpl = purple_find_prpl(purple_account_get_protocol_id(account));
	g_return_val_if_fail(prpl != NULL, FALSE);

	return (PURPLE_PLUGIN_PROTOCOL_INFO(prpl)->set_idle != NULL);
}

static void
set_idle_time(PurpleAccount *acct, int mins_idle)
{
	time_t t;
	PurpleConnection *gc = purple_account_get_connection(acct);
	PurplePresence *presence = purple_account_get_presence(acct);

	if (!gc)
		return;

	purple_debug_info("idle",
			"setting idle time for %s to %d\n",
			purple_account_get_username(acct), mins_idle);

	if (mins_idle)
		t = time(NULL) - (60 * mins_idle); /* subtract seconds idle from current time */
	else
		t = 0; /* time idle is irrelevant */

	purple_presence_set_idle(presence, mins_idle ? TRUE : FALSE, t);
}

static void
idle_action_ok(void *ignored, PurpleRequestFields *fields)
{
	int tm = purple_request_fields_get_integer(fields, "mins");
	PurpleAccount *acct = purple_request_fields_get_account(fields, "acct");

	/* only add the account to the GList if it's not already been idled */
	if (!unidle_filter(acct))
	{
		purple_debug_misc("idle",
				"%s hasn't been idled yet; adding to list.\n",
				purple_account_get_username(acct));
		idled_accts = g_list_append(idled_accts, acct);
	}

	set_idle_time(acct, tm);
}

static void
idle_all_action_ok(void *ignored, PurpleRequestFields *fields)
{
	PurpleAccount *acct = NULL;
	GList *list, *iter;
	int tm = purple_request_fields_get_integer(fields, "mins");
	const char *prpl_id = NULL;

	list = purple_accounts_get_all_active();
	for(iter = list; iter; iter = iter->next) {
		acct = (PurpleAccount *)(iter->data);

		if(acct && idleable_filter(acct)) {
			purple_debug_misc("idle", "Idling %s.\n",
					purple_account_get_username(acct));

			set_idle_time(acct, tm);

			if(!g_list_find(idled_accts, acct))
				idled_accts = g_list_append(idled_accts, acct);
		}
	}

	g_list_free(list);
}

static void
unidle_action_ok(void *ignored, PurpleRequestFields *fields)
{
	PurpleAccount *acct = purple_request_fields_get_account(fields, "acct");

	set_idle_time(acct, 0); /* unidle the account */

	/* once the account has been unidled it shouldn't be in the list */
	idled_accts = g_list_remove(idled_accts, acct);
}


static void
idle_action(PurplePluginAction *action)
{
	/* Use the super fancy request API */

	PurpleRequestFields *request;
	PurpleRequestFieldGroup *group;
	PurpleRequestField *field;

	group = purple_request_field_group_new(NULL);

	field = purple_request_field_account_new("acct", _("Account"), NULL);
	purple_request_field_account_set_filter(field, idleable_filter);
	purple_request_field_account_set_show_all(field, FALSE);
	purple_request_field_group_add_field(group, field);

	field = purple_request_field_int_new("mins", _("Minutes"), 10);
	purple_request_field_group_add_field(group, field);

	request = purple_request_fields_new();
	purple_request_fields_add_group(request, group);

	purple_request_fields(action->plugin,
			N_("I'dle Mak'er"),
			_("Set Account Idle Time"),
			NULL,
			request,
			_("_Set"), G_CALLBACK(idle_action_ok),
			_("_Cancel"), NULL,
			NULL, NULL, NULL,
			NULL);
}

static void
unidle_action(PurplePluginAction *action)
{
	PurpleRequestFields *request;
	PurpleRequestFieldGroup *group;
	PurpleRequestField *field;

	if (idled_accts == NULL)
	{
		purple_notify_info(NULL, NULL, _("None of your accounts are idle."), NULL);
		return;
	}

	group = purple_request_field_group_new(NULL);

	field = purple_request_field_account_new("acct", _("Account"), NULL);
	purple_request_field_account_set_filter(field, unidle_filter);
	purple_request_field_account_set_show_all(field, FALSE);
	purple_request_field_group_add_field(group, field);

	request = purple_request_fields_new();
	purple_request_fields_add_group(request, group);

	purple_request_fields(action->plugin,
			N_("I'dle Mak'er"),
			_("Unset Account Idle Time"),
			NULL,
			request,
			_("_Unset"), G_CALLBACK(unidle_action_ok),
			_("_Cancel"), NULL,
			NULL, NULL, NULL,
			NULL);
}

static void
idle_all_action(PurplePluginAction *action)
{
	PurpleRequestFields *request;
	PurpleRequestFieldGroup *group;
	PurpleRequestField *field;

	group = purple_request_field_group_new(NULL);

	field = purple_request_field_int_new("mins", _("Minutes"), 10);
	purple_request_field_group_add_field(group, field);

	request = purple_request_fields_new();
	purple_request_fields_add_group(request, group);

	purple_request_fields(action->plugin,
			N_("I'dle Mak'er"),
			_("Set Idle Time for All Accounts"),
			NULL,
			request,
			_("_Set"), G_CALLBACK(idle_all_action_ok),
			_("_Cancel"), NULL,
			NULL, NULL, NULL,
			NULL);
}

static void
unidle_all_action(PurplePluginAction *action)
{
	GList *l;

	/* freeing the list here will cause segfaults if the user idles an account
	 * after the list is freed */
	for (l = idled_accts; l; l = l->next)
	{
		PurpleAccount *account = l->data;
		set_idle_time(account, 0);
	}

	g_list_free(idled_accts);
	idled_accts = NULL;
}

static GList *
actions(PurplePlugin *plugin, gpointer context)
{
	GList *l = NULL;
	PurplePluginAction *act = NULL;

	act = purple_plugin_action_new(_("Set Account Idle Time"),
			idle_action);
	l = g_list_append(l, act);

	act = purple_plugin_action_new(_("Unset Account Idle Time"),
			unidle_action);
	l = g_list_append(l, act);

	act = purple_plugin_action_new(_("Set Idle Time for All Accounts"),
			idle_all_action);
	l = g_list_append(l, act);

	act = purple_plugin_action_new(
			_("Unset Idle Time for All Idled Accounts"), unidle_all_action);
	l = g_list_append(l, act);

	return l;
}

static void
signing_off_cb(PurpleConnection *gc, void *data)
{
	PurpleAccount *account;

	account = purple_connection_get_account(gc);
	idled_accts = g_list_remove(idled_accts, account);
}

static gboolean
plugin_load(PurplePlugin *plugin)
{
	purple_signal_connect(purple_connections_get_handle(), "signing-off",
						plugin,
						PURPLE_CALLBACK(signing_off_cb), NULL);

	return TRUE;
}

static gboolean
plugin_unload(PurplePlugin *plugin)
{
	unidle_all_action(NULL);

	return TRUE;
}

static PurplePluginInfo info =
{
	PURPLE_PLUGIN_MAGIC,
	PURPLE_MAJOR_VERSION,
	PURPLE_MINOR_VERSION,
	PURPLE_PLUGIN_STANDARD,
	NULL,
	0,
	NULL,
	PURPLE_PRIORITY_DEFAULT,
	IDLE_PLUGIN_ID,

	/* This is a cultural reference.  Dy'er Mak'er is a song by Led Zeppelin.
	   If that doesn't translate well into your language, drop the 's before translating. */
	N_("I'dle Mak'er"),
	DISPLAY_VERSION,
	N_("Allows you to hand-configure how long you've been idle"),
	N_("Allows you to hand-configure how long you've been idle"),
	"Eric Warmenhoven <eric@warmenhoven.org>",
	PURPLE_WEBSITE,
	plugin_load,
	plugin_unload,
	NULL,
	NULL,
	NULL,
	NULL,
	actions,

	/* padding */
	NULL,
	NULL,
	NULL,
	NULL
};

static void
init_plugin(PurplePlugin *plugin)
{
}


PURPLE_INIT_PLUGIN(idle, init_plugin, info)