| 1 |
/* |
| 2 |
* Copyright (c) 2005 William Pitcock <nenolod -at- nenolod.net> |
| 3 |
* Rights to this code are as documented in doc/LICENSE. |
| 4 |
* |
| 5 |
* Allows requesting help for a account |
| 6 |
* |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "atheme.h" |
| 10 |
|
| 11 |
DECLARE_MODULE_V1 |
| 12 |
( |
| 13 |
"helpserv/ticket", true, _modinit, _moddeinit, |
| 14 |
PACKAGE_STRING, |
| 15 |
"Atheme Development Group <http://www.atheme.net>" |
| 16 |
); |
| 17 |
|
| 18 |
unsigned int ratelimit_count = 0; |
| 19 |
time_t ratelimit_firsttime = 0; |
| 20 |
|
| 21 |
static void account_drop_request(myuser_t *mu); |
| 22 |
static void account_delete_request(myuser_t *mu); |
| 23 |
static void helpserv_cmd_request(sourceinfo_t *si, int parc, char *parv[]); |
| 24 |
static void helpserv_cmd_list(sourceinfo_t *si, int parc, char *parv[]); |
| 25 |
static void helpserv_cmd_close(sourceinfo_t *si, int parc, char *parv[]); |
| 26 |
static void helpserv_cmd_cancel(sourceinfo_t *si, int parc, char *parv[]); |
| 27 |
|
| 28 |
static void write_ticket_db(database_handle_t *db); |
| 29 |
static void db_h_he(database_handle_t *db, const char *type); |
| 30 |
|
| 31 |
command_t helpserv_request = { "REQUEST", N_("Request help from network staff."), AC_AUTHENTICATED, 1, helpserv_cmd_request, { .path = "helpserv/request" } }; |
| 32 |
command_t helpserv_list = { "LIST", N_("Lists users waiting for help."), PRIV_HELPER, 1, helpserv_cmd_list, { .path = "helpserv/list" } }; |
| 33 |
command_t helpserv_close = { "CLOSE", N_("Close a users' help request."), PRIV_HELPER, 2, helpserv_cmd_close, { .path = "helpserv/close" } }; |
| 34 |
command_t helpserv_cancel = { "CANCEL", N_("Cancel your own pending help request."), AC_AUTHENTICATED, 1, helpserv_cmd_cancel, { .path = "helpserv/cancel" } }; |
| 35 |
|
| 36 |
struct ticket_ { |
| 37 |
stringref nick; |
| 38 |
time_t ticket_ts; |
| 39 |
char *creator; |
| 40 |
char *topic; |
| 41 |
}; |
| 42 |
|
| 43 |
typedef struct ticket_ ticket_t; |
| 44 |
|
| 45 |
mowgli_list_t helpserv_reqlist; |
| 46 |
|
| 47 |
void _modinit(module_t *m) |
| 48 |
{ |
| 49 |
if (!module_find_published("backend/opensex")) |
| 50 |
{ |
| 51 |
slog(LG_INFO, "Module %s requires use of the OpenSEX database backend, refusing to load.", m->name); |
| 52 |
m->mflags = MODTYPE_FAIL; |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
hook_add_event("user_drop"); |
| 57 |
hook_add_user_drop(account_drop_request); |
| 58 |
hook_add_event("myuser_delete"); |
| 59 |
hook_add_myuser_delete(account_delete_request); |
| 60 |
hook_add_db_write(write_ticket_db); |
| 61 |
|
| 62 |
db_register_type_handler("HE", db_h_he); |
| 63 |
|
| 64 |
service_named_bind_command("helpserv", &helpserv_request); |
| 65 |
service_named_bind_command("helpserv", &helpserv_list); |
| 66 |
service_named_bind_command("helpserv", &helpserv_close); |
| 67 |
service_named_bind_command("helpserv", &helpserv_cancel); |
| 68 |
} |
| 69 |
|
| 70 |
void _moddeinit(module_unload_intent_t intent) |
| 71 |
{ |
| 72 |
hook_del_user_drop(account_drop_request); |
| 73 |
hook_del_myuser_delete(account_delete_request); |
| 74 |
hook_del_db_write(write_ticket_db); |
| 75 |
|
| 76 |
db_unregister_type_handler("HE"); |
| 77 |
|
| 78 |
service_named_unbind_command("helpserv", &helpserv_request); |
| 79 |
service_named_unbind_command("helpserv", &helpserv_list); |
| 80 |
service_named_unbind_command("helpserv", &helpserv_close); |
| 81 |
service_named_unbind_command("helpserv", &helpserv_cancel); |
| 82 |
} |
| 83 |
|
| 84 |
static void write_ticket_db(database_handle_t *db) |
| 85 |
{ |
| 86 |
mowgli_node_t *n; |
| 87 |
|
| 88 |
MOWGLI_ITER_FOREACH(n, helpserv_reqlist.head) |
| 89 |
{ |
| 90 |
ticket_t *l = n->data; |
| 91 |
|
| 92 |
db_start_row(db, "HE"); |
| 93 |
db_write_word(db, l->nick); |
| 94 |
db_write_time(db, l->ticket_ts); |
| 95 |
db_write_word(db, l->creator); |
| 96 |
db_write_str(db, l->topic); |
| 97 |
db_commit_row(db); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
static void db_h_he(database_handle_t *db, const char *type) |
| 102 |
{ |
| 103 |
const char *nick = db_sread_word(db); |
| 104 |
time_t ticket_ts = db_sread_time(db); |
| 105 |
const char *creator = db_sread_word(db); |
| 106 |
const char *topic = db_sread_str(db); |
| 107 |
|
| 108 |
ticket_t *l = smalloc(sizeof(ticket_t)); |
| 109 |
l->nick = strshare_get(nick); |
| 110 |
l->ticket_ts = ticket_ts; |
| 111 |
l->creator = sstrdup(creator); |
| 112 |
l->topic = sstrdup(topic); |
| 113 |
mowgli_node_add(l, mowgli_node_create(), &helpserv_reqlist); |
| 114 |
} |
| 115 |
|
| 116 |
static void account_drop_request(myuser_t *mu) |
| 117 |
{ |
| 118 |
mowgli_node_t *n; |
| 119 |
ticket_t *l; |
| 120 |
|
| 121 |
MOWGLI_ITER_FOREACH(n, helpserv_reqlist.head) |
| 122 |
{ |
| 123 |
l = n->data; |
| 124 |
if (!irccasecmp(l->nick, entity(mu)->name)) |
| 125 |
{ |
| 126 |
slog(LG_REGISTER, "HELP:REQUEST:DROPACCOUNT: \2%s\2 \2%s\2", l->nick, l->topic); |
| 127 |
|
| 128 |
mowgli_node_delete(n, &helpserv_reqlist); |
| 129 |
|
| 130 |
strshare_unref(l->nick); |
| 131 |
free(l->creator); |
| 132 |
free(l->topic); |
| 133 |
free(l); |
| 134 |
|
| 135 |
return; |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
static void account_delete_request(myuser_t *mu) |
| 141 |
{ |
| 142 |
mowgli_node_t *n; |
| 143 |
ticket_t *l; |
| 144 |
|
| 145 |
MOWGLI_ITER_FOREACH(n, helpserv_reqlist.head) |
| 146 |
{ |
| 147 |
l = n->data; |
| 148 |
if (!irccasecmp(l->nick, entity(mu)->name)) |
| 149 |
{ |
| 150 |
slog(LG_REGISTER, "HELP:REQUEST:EXPIRE: \2%s\2 \2%s\2", l->nick, l->topic); |
| 151 |
|
| 152 |
mowgli_node_delete(n, &helpserv_reqlist); |
| 153 |
|
| 154 |
strshare_unref(l->nick); |
| 155 |
free(l->creator); |
| 156 |
free(l->topic); |
| 157 |
free(l); |
| 158 |
|
| 159 |
return; |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
/* REQUEST <topic> */ |
| 165 |
static void helpserv_cmd_request(sourceinfo_t *si, int parc, char *parv[]) |
| 166 |
{ |
| 167 |
const char *topic = parv[0]; |
| 168 |
mowgli_node_t *n; |
| 169 |
ticket_t *l; |
| 170 |
|
| 171 |
if (!topic) |
| 172 |
{ |
| 173 |
command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "REQUEST"); |
| 174 |
command_fail(si, fault_needmoreparams, _("Syntax: REQUEST <topic>")); |
| 175 |
return; |
| 176 |
} |
| 177 |
|
| 178 |
if (metadata_find(si->smu, "private:restrict:setter")) |
| 179 |
{ |
| 180 |
command_fail(si, fault_noprivs, _("You have been restricted from requesting help by network staff.")); |
| 181 |
return; |
| 182 |
} |
| 183 |
|
| 184 |
if ((unsigned int)(CURRTIME - ratelimit_firsttime) > config_options.ratelimit_period) |
| 185 |
ratelimit_count = 0, ratelimit_firsttime = CURRTIME; |
| 186 |
|
| 187 |
/* search for it */ |
| 188 |
MOWGLI_ITER_FOREACH(n, helpserv_reqlist.head) |
| 189 |
{ |
| 190 |
l = n->data; |
| 191 |
|
| 192 |
if (!irccasecmp(l->nick, entity(si->smu)->name)) |
| 193 |
{ |
| 194 |
if (!strcmp(topic, l->topic)) |
| 195 |
{ |
| 196 |
command_success_nodata(si, _("You have already requested help about \2%s\2."), topic); |
| 197 |
return; |
| 198 |
} |
| 199 |
if (ratelimit_count > config_options.ratelimit_uses && !has_priv(si, PRIV_FLOOD)) |
| 200 |
{ |
| 201 |
command_fail(si, fault_toomany, _("The system is currently too busy to process your help request, please try again later.")); |
| 202 |
slog(LG_INFO, "HELP:REQUEST:THROTTLED: %s", si->su->nick); |
| 203 |
return; |
| 204 |
} |
| 205 |
free(l->topic); |
| 206 |
l->topic = sstrdup(topic); |
| 207 |
l->ticket_ts = CURRTIME;; |
| 208 |
|
| 209 |
command_success_nodata(si, _("You have requested help about \2%s\2."), topic); |
| 210 |
logcommand(si, CMDLOG_REQUEST, "REQUEST: \2%s\2", topic); |
| 211 |
if (config_options.ratelimit_uses && config_options.ratelimit_period) |
| 212 |
ratelimit_count++; |
| 213 |
return; |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
if (ratelimit_count > config_options.ratelimit_uses && !has_priv(si, PRIV_FLOOD)) |
| 218 |
{ |
| 219 |
command_fail(si, fault_toomany, _("The system is currently too busy to process your help request, please try again later.")); |
| 220 |
slog(LG_INFO, "HELP:REQUEST:THROTTLED: %s", si->su->nick); |
| 221 |
return; |
| 222 |
} |
| 223 |
l = smalloc(sizeof(ticket_t)); |
| 224 |
l->nick = strshare_ref(entity(si->smu)->name); |
| 225 |
l->ticket_ts = CURRTIME;; |
| 226 |
l->creator = sstrdup(get_source_name(si)); |
| 227 |
l->topic = sstrdup(topic); |
| 228 |
|
| 229 |
n = mowgli_node_create(); |
| 230 |
mowgli_node_add(l, n, &helpserv_reqlist); |
| 231 |
|
| 232 |
command_success_nodata(si, _("You have requested help about \2%s\2."), topic); |
| 233 |
logcommand(si, CMDLOG_REQUEST, "REQUEST: \2%s\2", topic); |
| 234 |
if (config_options.ratelimit_uses && config_options.ratelimit_period) |
| 235 |
ratelimit_count++; |
| 236 |
return; |
| 237 |
} |
| 238 |
|
| 239 |
/* CLOSE <nick> [reason] */ |
| 240 |
static void helpserv_cmd_close(sourceinfo_t *si, int parc, char *parv[]) |
| 241 |
{ |
| 242 |
char *nick = parv[0]; |
| 243 |
user_t *u; |
| 244 |
ticket_t *l; |
| 245 |
mowgli_node_t *n; |
| 246 |
|
| 247 |
if (!nick) |
| 248 |
{ |
| 249 |
command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "CLOSE"); |
| 250 |
command_fail(si, fault_needmoreparams, _("Syntax: CLOSE <nick> [reason]")); |
| 251 |
return; |
| 252 |
} |
| 253 |
|
| 254 |
MOWGLI_ITER_FOREACH(n, helpserv_reqlist.head) |
| 255 |
{ |
| 256 |
l = n->data; |
| 257 |
if (!irccasecmp(l->nick, nick)) |
| 258 |
{ |
| 259 |
if ((u = user_find_named(nick)) != NULL) |
| 260 |
{ |
| 261 |
if (parv[1] != NULL) |
| 262 |
notice(si->service->nick, u->nick, "[auto notice] Your help request has been closed: %s", parv[1]); |
| 263 |
else |
| 264 |
notice(si->service->nick, u->nick, "[auto notice] Your help request has been closed."); |
| 265 |
} |
| 266 |
else |
| 267 |
{ |
| 268 |
service_t *svs; |
| 269 |
char buf[BUFSIZE]; |
| 270 |
|
| 271 |
if ((svs = service_find("memoserv")) != NULL && myuser_find(parv[0]) != NULL) |
| 272 |
{ |
| 273 |
if (parv[1] != NULL) |
| 274 |
snprintf(buf, BUFSIZE, "%s [auto memo] Your help request has been closed: %s", parv[0], parv[1]); |
| 275 |
else |
| 276 |
snprintf(buf, BUFSIZE, "%s [auto memo] Your help request has been closed.", parv[0]); |
| 277 |
|
| 278 |
command_exec_split(svs, si, "SEND", buf, svs->commands); |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
if (parv[1] != NULL) |
| 283 |
logcommand(si, CMDLOG_REQUEST, "CLOSE: Help for \2%s\2 about \2%s\2 (\2%s\2)", nick, l->topic, parv[1]); |
| 284 |
else |
| 285 |
logcommand(si, CMDLOG_REQUEST, "CLOSE: Help for \2%s\2 about \2%s\2", nick, l->topic); |
| 286 |
|
| 287 |
mowgli_node_delete(n, &helpserv_reqlist); |
| 288 |
|
| 289 |
strshare_unref(l->nick); |
| 290 |
free(l->creator); |
| 291 |
free(l->topic); |
| 292 |
free(l); |
| 293 |
|
| 294 |
return; |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
command_success_nodata(si, _("Nick \2%s\2 not found in help request database."), nick); |
| 299 |
} |
| 300 |
|
| 301 |
/* LIST */ |
| 302 |
static void helpserv_cmd_list(sourceinfo_t *si, int parc, char *parv[]) |
| 303 |
{ |
| 304 |
ticket_t *l; |
| 305 |
mowgli_node_t *n; |
| 306 |
int x = 0; |
| 307 |
char buf[BUFSIZE]; |
| 308 |
struct tm tm; |
| 309 |
|
| 310 |
MOWGLI_ITER_FOREACH(n, helpserv_reqlist.head) |
| 311 |
{ |
| 312 |
l = n->data; |
| 313 |
x++; |
| 314 |
|
| 315 |
tm = *localtime(&l->ticket_ts); |
| 316 |
strftime(buf, BUFSIZE, TIME_FORMAT, &tm); |
| 317 |
command_success_nodata(si, "#%d Nick:\2%s\2, topic:\2%s\2 (%s - %s)", |
| 318 |
x, l->nick, l->topic, l->creator, buf); |
| 319 |
} |
| 320 |
command_success_nodata(si, "End of list."); |
| 321 |
logcommand(si, CMDLOG_GET, "LIST"); |
| 322 |
} |
| 323 |
|
| 324 |
/* CANCEL */ |
| 325 |
static void helpserv_cmd_cancel(sourceinfo_t *si, int parc, char *parv[]) |
| 326 |
{ |
| 327 |
ticket_t *l; |
| 328 |
mowgli_node_t *n; |
| 329 |
|
| 330 |
MOWGLI_ITER_FOREACH(n, helpserv_reqlist.head) |
| 331 |
{ |
| 332 |
l = n->data; |
| 333 |
|
| 334 |
if (!irccasecmp(l->nick, entity(si->smu)->name)) |
| 335 |
{ |
| 336 |
mowgli_node_delete(n, &helpserv_reqlist); |
| 337 |
|
| 338 |
strshare_unref(l->nick); |
| 339 |
free(l->creator); |
| 340 |
free(l->topic); |
| 341 |
free(l); |
| 342 |
|
| 343 |
command_success_nodata(si, "Your help request has been cancelled."); |
| 344 |
|
| 345 |
logcommand(si, CMDLOG_REQUEST, "CANCEL"); |
| 346 |
return; |
| 347 |
} |
| 348 |
} |
| 349 |
command_fail(si, fault_badparams, _("You do not have a help request to cancel.")); |
| 350 |
} |
| 351 |
|
| 352 |
/* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs |
| 353 |
* vim:ts=8 |
| 354 |
* vim:sw=8 |
| 355 |
* vim:noexpandtab |
| 356 |
*/ |