-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathauto-away.c
226 lines (183 loc) · 4.47 KB
/
auto-away.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
/*
LICENSE: GPLv3
Copyright Ⓒ 2022 Valerie Pond
Automatically sets a user away if they are idle too long
*/
/*** <<<MODULE MANAGER START>>>
module
{
documentation "https://github.com/ValwareIRC/valware-unrealircd-mods/blob/main/auto-away/README.md";
troubleshooting "In case of problems, documentation or e-mail me at v.a.pond@outlook.com";
min-unrealircd-version "6.*";
max-unrealircd-version "6.*";
post-install-text {
"The module is installed. Now all you need to do is add a loadmodule line:";
"loadmodule \"third/auto-away\";";
"Then you specify the time and reason values in the config Here's the defaults to get you started:";
"auto-away {";
" time 2h;";
" reason \"Idle.\";";
"}";
"And /REHASH the IRCd.";
"The module does not need any other configuration.";
}
}
*** <<<MODULE MANAGER END>>>
*/
#include "unrealircd.h"
#define UCONF "auto-away"
EVENT(check_if_away);
void setconf(void);
void freeconf(void);
int autoaway_configtest(ConfigFile *cf, ConfigEntry *ce, int type, int *errs);
int autoaway_configrun(ConfigFile *cf, ConfigEntry *ce, int type);
typedef struct
{
char *reason;
long time;
unsigned short int reason_exists;
unsigned short int time_exists;
} awaystruct;
ModuleHeader MOD_HEADER =
{
"third/auto-away",
"1.0",
"Automatically set someone away after a certain amount of time",
"Valware",
"unrealircd-6",
};
MOD_INIT()
{
MARK_AS_GLOBAL_MODULE(modinfo);
setconf();
EventAdd(modinfo->handle, "check_if_away", check_if_away, NULL, 1500, 0);
HookAdd(modinfo->handle, HOOKTYPE_CONFIGRUN, 0, autoaway_configrun);
return MOD_SUCCESS;
}
MOD_LOAD()
{
return MOD_SUCCESS;
}
MOD_UNLOAD()
{
freeconf();
return MOD_SUCCESS;
}
static awaystruct ourconf;
MOD_TEST()
{
memset(&ourconf, 0, sizeof(ourconf));
HookAdd(modinfo->handle, HOOKTYPE_CONFIGTEST, 0, autoaway_configtest);
return MOD_SUCCESS;
}
void setconf(void)
{
safe_strdup(ourconf.reason, "Idle.");
ourconf.time = 120; /* Default is 2hours inactivity */
}
void freeconf(void)
{
safe_free(ourconf.reason);
}
/* gottem templaet ellemayo */
int autoaway_configtest(ConfigFile *cf, ConfigEntry *ce, int type, int *errs)
{
int errors = 0;
int i;
ConfigEntry *cep, *cep2;
if(type != CONFIG_MAIN)
return 0;
if(!ce || !ce->name)
return 0;
if(strcmp(ce->name, UCONF))
return 0;
for(cep = ce->items; cep; cep = cep->next)
{
if(!cep->name)
{
config_error("%s:%i: blank %s item.", cep->file->filename, cep->line_number, UCONF);
errors++;
continue;
}
if(!strcmp(cep->name, "reason"))
{
if(ourconf.reason_exists)
{
config_error("%s:%i: duplicate %s::%s directive.", cep->file->filename, cep->line_number, UCONF, cep->name);
errors++;
continue;
}
ourconf.reason_exists = 1;
continue;
}
if(!strcmp(cep->name, "time"))
{
if(ourconf.time_exists)
{
config_error("%s:%i: duplicate %s::%s directive", cep->file->filename, cep->line_number, UCONF, cep->name);
errors++;
continue;
}
ourconf.time_exists = 1;
if(config_checkval(cep->value, CFG_TIME) <= 0)
{
config_error("%s:%i: %s::%s must be a time string like '7d10m' or simply '20'", cep->file->filename, cep->line_number, UCONF, cep->name);
errors++;
}
continue;
}
config_warn("%s:%i: unknown item %s::%s", cep->file->filename, cep->line_number, UCONF, cep->name);
}
*errs = errors;
return errors ? -1 : 1;
}
int autoaway_configposttest(int *errs)
{
return 1;
}
int autoaway_configrun(ConfigFile *cf, ConfigEntry *ce, int type)
{
ConfigEntry *cep;
if(type != CONFIG_MAIN)
return 0;
if(!ce || !ce->name)
return 0;
if(strcmp(ce->name, UCONF))
return 0;
for(cep = ce->items; cep; cep = cep->next)
{
if(!cep->name)
continue;
if(!strcmp(cep->name, "reason"))
{
safe_strdup(ourconf.reason, cep->value);
continue;
}
if(!strcmp(cep->name, "time"))
{
ourconf.time = config_checkval(cep->value, CFG_TIME);
continue;
}
}
return 1; // We good
}
EVENT(check_if_away)
{
Client *target;
list_for_each_entry(target, &lclient_list, lclient_node)
{
if (!IsUser(target))
continue;
/* if they're already set away.. */
if (target->user->away)
continue;
char tmp[MAXAWAYLEN] = "[Auto-Away] ";
strlcat(tmp, ourconf.reason, sizeof(tmp));
const char *parv[3];
parv[0] = NULL;
parv[1] = tmp;
parv[2] = NULL;
if (target->local->idle_since <= TStime() - ourconf.time)
do_cmd(target, NULL, "AWAY", 2, parv);
}
}