2 ctrlproxy: A modular IRC proxy
3 (c) 2002-2003 Jelmer Vernooij <jelmer@nl.linux.org>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU 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., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * * One client connected. User does /AWAY msg.
23 * * Client rejoins. If client_limit=0, the connection is set as not away
24 * => there should be distinction between auto-away and client-away?
27 #include "ctrlproxy.h"
30 #define DEFAULT_TIME (10 * 60)
32 struct auto_away_data {
37 struct global *global;
42 static gboolean check_time(gpointer user_data)
44 struct auto_away_data *d = (struct auto_away_data *)user_data;
46 if (time(NULL) - d->last_message > d->max_idle_time) {
48 for (sl = d->global->networks; sl; sl = sl->next) {
49 struct network *s = (struct network *)sl->data;
50 if (s->connection.state == NETWORK_CONNECTION_STATE_MOTD_RECVD &&
51 s->state != NULL && !s->state->is_away &&
52 (d->client_limit < 0 || g_list_length(s->clients) <= d->client_limit)) {
53 network_send_args(s, "AWAY",
54 d->message != NULL?d->message:"Auto Away",
56 if (d->nick != NULL) {
57 network_send_args(s, "NICK", d->nick, NULL);
66 static gboolean log_data(struct network *n, const struct line *l,
67 enum data_direction dir, void *userdata)
69 struct auto_away_data *d = userdata;
72 if (dir == TO_SERVER && !g_strcasecmp(l->args[0], "AWAY")) {
73 d->last_message = time(NULL);
76 if (dir == TO_SERVER &&
77 (!g_strcasecmp(l->args[0], "PRIVMSG") ||
78 !g_strcasecmp(l->args[0], "NOTICE"))) {
79 d->last_message = time(NULL);
80 for (sl = d->global->networks; sl; sl = sl->next) {
81 struct network *s = (struct network *)sl->data;
82 if (s->connection.state == NETWORK_CONNECTION_STATE_MOTD_RECVD &&
83 s->state != NULL && s->state->is_away)
84 network_send_args(s, "AWAY", NULL);
90 static gboolean new_client(struct client *c, void *userdata)
92 struct auto_away_data *d = userdata;
94 if (d->client_limit >= 0 && d->client_limit < g_list_length(c->network->clients)+1)
95 network_send_args(c->network, "AWAY", NULL);
100 static void load_config(struct global *global)
102 struct auto_away_data *d;
103 GKeyFile *kf = global->config->keyfile;
105 if (!g_key_file_has_group(kf, "auto-away")) {
106 del_server_filter("auto-away");
110 d = g_new0(struct auto_away_data, 1);
113 add_server_filter("auto-away", log_data, d, -1);
115 d->last_message = time(NULL);
116 d->message = g_key_file_get_string(kf, "auto-away", "message", NULL);
117 d->nick = g_key_file_get_string(kf, "auto-away", "nick", NULL);
118 if (g_key_file_has_key(kf, "auto-away", "client_limit", NULL)) {
119 d->client_limit = g_key_file_get_integer(kf, "auto-away", "client_limit", NULL);
120 if (g_key_file_has_key(kf, "auto-away", "only_noclient", NULL))
121 log_global(LOG_WARNING, "auto-away: not using only_noclient because client_limit is set");
123 else if (g_key_file_has_key(kf, "auto-away", "only_noclient", NULL)) {
124 d->client_limit = g_key_file_get_boolean(kf, "auto-away", "only_noclient", NULL) ? 0 : -1;
125 log_global(LOG_WARNING, "auto-away: only_noclient is deprecated, please use client_limit instead");
128 d->client_limit = -1;
129 if (g_key_file_has_key(kf, "auto-away", "time", NULL))
130 d->max_idle_time = g_key_file_get_integer(kf, "auto-away", "time", NULL);
132 d->max_idle_time = DEFAULT_TIME;
134 d->timeout_id = g_timeout_add(1000, check_time, d);
135 add_new_client_hook("auto-away", new_client, d);
138 static gboolean init_plugin()
140 register_load_config_notify(load_config);
144 struct plugin_ops plugin = {
146 .version = CTRLPROXY_PLUGIN_VERSION,