-
Notifications
You must be signed in to change notification settings - Fork 1
/
autoaway.c
278 lines (220 loc) · 7.05 KB
/
autoaway.c
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
/*
* AutoAway -- a HexChat plugin to set away on idle
*
* Copyright (C) 2013-2016 Andrey Vihrov <[email protected]>
*
* Based on ideas from
* https://robots.org.uk/src/xchat/idle.c (Copyright 2004 Sam Morris)
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <stdbool.h>
#include <hexchat-plugin.h>
#include <X11/Xlib.h>
#include <X11/extensions/scrnsaver.h>
#include <gtk/gtk.h>
#include <gdk/gdkx.h>
/* Plugin identification */
#define PNAME "AutoAway"
#define PDESC "Set away on idle"
#define PVERSION "2.0"
/* Plugin settings */
#define MAX_LEN 511 /* Maximum IRC message length (RFC 2812 section 2.3) plus one */
static char away_msg[MAX_LEN] = "Idle";
static char away_extra[MAX_LEN] = "";
static char back_extra[MAX_LEN] = "";
static int polling_timeout = 10; /* Seconds */
static int idle_time = 10 * 60; /* Seconds */
#define PREF_AWAY_MSG "away_msg"
#define PREF_AWAY_EXTRA "away_extra"
#define PREF_BACK_EXTRA "back_extra"
#define PREF_POLLING_TIMEOUT "polling_timeout"
#define PREF_IDLE_TIME "idle_time"
/* Old settings that are not used anymore */
#define PREF_AWAY_CMD "away_cmd"
#define PREF_BACK_CMD "back_cmd"
/* Global variables */
static hexchat_plugin *ph;
static Display *display;
static XScreenSaverInfo *ssinfo;
/* RFC 2812 and HexChat constants */
#define RPL_UNAWAY "305"
#define RPL_NOWAWAY "306"
#define HC_TYPE_SERVER 1
#define HC_FLAGS_CONNECTED 0x1
#define HC_FLAGS_MARKED_AWAY 0x4
/* Logging */
#define LOG_ERR "error"
#define LOG_WARN "warning"
#define LOG_DBG "debug"
#define LOG(level, ...) (hexchat_printf(ph, PNAME ": " level ": " __VA_ARGS__))
#ifndef NDEBUG
# define DEBUG(...) (LOG(LOG_DBG, __VA_ARGS__))
#else
# define DEBUG(...) ((void)0)
#endif
static void
set_away (bool away)
{
hexchat_list *ch = hexchat_list_get(ph, "channels");
assert(ch);
/* Loop over all channels, find servers, and update state for each server */
while (hexchat_list_next(ph, ch))
{
if (hexchat_list_int(ph, ch, "type") == HC_TYPE_SERVER)
{
int flags = hexchat_list_int(ph, ch, "flags");
DEBUG("server: %s, flags: 0x%x", hexchat_list_str(ph, ch, "server"), flags);
if (!(flags & HC_FLAGS_CONNECTED)
|| away == (bool)(flags & HC_FLAGS_MARKED_AWAY))
{
continue;
}
hexchat_context *ctx = (hexchat_context *)hexchat_list_str(ph, ch, "context");
assert(ctx);
if (!hexchat_set_context(ph, ctx))
{
LOG(LOG_WARN, "failed to set context for server %s",
hexchat_list_str(ph, ch, "server"));
continue;
}
if (away)
{
DEBUG("set away");
hexchat_commandf(ph, "AWAY %s", away_msg);
}
else
{
DEBUG("set back");
hexchat_command(ph, "BACK");
}
}
}
hexchat_list_free(ph, ch);
}
static int
check_idle (void *unused)
{
(void)unused;
if (!XScreenSaverQueryInfo(display, DefaultRootWindow(display), ssinfo))
{
LOG(LOG_ERR, "XScreenSaverQueryInfo() failed");
return 1;
}
if (ssinfo->state == ScreenSaverOn
|| ssinfo->idle / 1000 >= (unsigned long)idle_time)
{
DEBUG("idle");
set_away(true);
}
else
{
DEBUG("not idle");
set_away(false);
}
return 1;
}
static int
send_extra_cmd (char *word[], char *word_eol[], void *user_data)
{
(void)word; (void)word_eol;
hexchat_command(ph, user_data);
return HEXCHAT_EAT_NONE;
}
void
hexchat_plugin_get_info (char **name, char **desc,
char **version, void **reserved)
{
*name = PNAME;
*desc = PDESC;
*version = PVERSION;
(void)reserved;
}
int
hexchat_plugin_init (hexchat_plugin *plugin_handle,
char **plugin_name, char **plugin_desc,
char **plugin_version, char *arg)
{
/* Init variables */
ph = plugin_handle;
*plugin_name = PNAME;
*plugin_desc = PDESC;
*plugin_version = PVERSION;
(void)arg;
/* Load settings */
(void)hexchat_pluginpref_get_str(ph, PREF_AWAY_MSG, away_msg);
(void)hexchat_pluginpref_get_str(ph, PREF_AWAY_EXTRA, away_extra);
(void)hexchat_pluginpref_get_str(ph, PREF_BACK_EXTRA, back_extra);
int res;
if ((res = hexchat_pluginpref_get_int(ph, PREF_POLLING_TIMEOUT)) > 0)
{
polling_timeout = res;
}
if ((res = hexchat_pluginpref_get_int(ph, PREF_IDLE_TIME)) > 0)
{
idle_time = res;
}
if (!hexchat_pluginpref_delete(ph, PREF_AWAY_CMD)
|| !hexchat_pluginpref_delete(ph, PREF_BACK_CMD))
{
LOG(LOG_WARN, "failed to delete old settings");
}
/* Set up X display */
GtkWindow *win = (GtkWindow *)hexchat_get_info(ph, "gtkwin_ptr");
assert(win);
display = GDK_WINDOW_XDISPLAY(gtk_widget_get_window(GTK_WIDGET(win)));
int event_base, error_base;
if (!XScreenSaverQueryExtension(display, &event_base, &error_base))
{
LOG(LOG_ERR, "XScreenSaver extension not available");
return 0;
}
if (!(ssinfo = XScreenSaverAllocInfo()))
{
LOG(LOG_ERR, "failed to allocate a XScreenSaverInfo structure");
return 0;
}
/* Set up hooks */
hexchat_hook_timer(ph, polling_timeout * 1000, check_idle, NULL);
/* Ensure that extra commands are executed on any away state change */
if (*away_extra)
{
hexchat_hook_server(ph, RPL_NOWAWAY, HEXCHAT_PRI_NORM, send_extra_cmd, away_extra);
}
if (*back_extra)
{
hexchat_hook_server(ph, RPL_UNAWAY, HEXCHAT_PRI_NORM, send_extra_cmd, back_extra);
}
/* All done */
hexchat_print(ph, PNAME " plugin loaded\n");
return 1;
}
int
hexchat_plugin_deinit (void)
{
set_away(false);
/* Save settings */
if (!hexchat_pluginpref_set_str(ph, PREF_AWAY_MSG, away_msg)
|| !hexchat_pluginpref_set_str(ph, PREF_AWAY_EXTRA, away_extra)
|| !hexchat_pluginpref_set_str(ph, PREF_BACK_EXTRA, back_extra)
|| !hexchat_pluginpref_set_int(ph, PREF_POLLING_TIMEOUT, polling_timeout)
|| !hexchat_pluginpref_set_int(ph, PREF_IDLE_TIME, idle_time))
{
LOG(LOG_WARN, "failed to save settings");
}
/* Free resources */
XFree(ssinfo);
return 1;
}