4
mirror of git://git.acid.vegas/unrealircd.git synced 2024-12-26 16:26:38 +00:00
unrealircd/src/api-usermode.c

402 lines
9.7 KiB
C
Raw Normal View History

2020-03-29 09:16:53 +00:00
/************************************************************************
* IRC - Internet Relay Chat, src/api-usermode.c
* (C) 1999-2000 Carsten Munk (Techie/Stskeeps) <stskeeps@tspre.org>
*
* See file AUTHORS in IRC package for additional names of
* the programmers.
*
* 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 1, 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "unrealircd.h"
char umodestring[UMODETABLESZ+1];
2022-01-15 05:16:34 +00:00
/** User modes and their handlers */
Umode *usermodes = NULL;
2020-03-29 09:16:53 +00:00
long UMODE_INVISIBLE = 0L; /* makes user invisible */
long UMODE_OPER = 0L; /* Operator */
long UMODE_REGNICK = 0L; /* Nick set by services as registered */
long UMODE_SERVNOTICE = 0L; /* server notices such as kill */
long UMODE_HIDE = 0L; /* Hide from Nukes */
long UMODE_SECURE = 0L; /* User is a secure connect */
long UMODE_DEAF = 0L; /* Deaf */
long UMODE_HIDEOPER = 0L; /* Hide oper mode */
long UMODE_SETHOST = 0L; /* Used sethost */
long UMODE_HIDLE = 0L; /* Hides the idle time of opers */
long SNO_KILLS = 0L;
long SNO_CLIENT = 0L;
long SNO_FLOOD = 0L;
long SNO_FCLIENT = 0L;
long SNO_JUNK = 0L;
long SNO_VHOST = 0L;
long SNO_EYES = 0L;
long SNO_TKL = 0L;
long SNO_NICKCHANGE = 0L;
long SNO_FNICKCHANGE = 0L;
long SNO_QLINE = 0L;
long SNO_SPAMF = 0L;
long SNO_SNOTICE = 0L;
long SNO_OPER = 0L;
long AllUmodes; /* All umodes */
long SendUmodes; /* All umodes which are sent to other servers (global umodes) */
2020-04-20 19:12:33 +00:00
/* Forward declarations */
int umode_hidle_allow(Client *client, int what);
2022-01-15 05:16:34 +00:00
static void unload_usermode_commit(Umode *m);
2020-04-20 19:12:33 +00:00
2022-01-15 05:16:34 +00:00
void umode_init(void)
2020-03-29 09:16:53 +00:00
{
2022-01-15 05:16:34 +00:00
/* Some built-in modes */
2020-03-29 09:16:53 +00:00
UmodeAdd(NULL, 'i', UMODE_GLOBAL, 0, umode_allow_all, &UMODE_INVISIBLE);
UmodeAdd(NULL, 'o', UMODE_GLOBAL, 1, umode_allow_opers, &UMODE_OPER);
UmodeAdd(NULL, 'r', UMODE_GLOBAL, 0, umode_allow_none, &UMODE_REGNICK);
UmodeAdd(NULL, 's', UMODE_LOCAL, 0, umode_allow_all, &UMODE_SERVNOTICE);
UmodeAdd(NULL, 'x', UMODE_GLOBAL, 0, umode_allow_all, &UMODE_HIDE);
UmodeAdd(NULL, 'z', UMODE_GLOBAL, 0, umode_allow_none, &UMODE_SECURE);
UmodeAdd(NULL, 'd', UMODE_GLOBAL, 0, umode_allow_all, &UMODE_DEAF);
UmodeAdd(NULL, 'H', UMODE_GLOBAL, 1, umode_allow_opers, &UMODE_HIDEOPER);
UmodeAdd(NULL, 't', UMODE_GLOBAL, 0, umode_allow_unset, &UMODE_SETHOST);
2020-04-20 19:12:33 +00:00
UmodeAdd(NULL, 'I', UMODE_GLOBAL, 0, umode_hidle_allow, &UMODE_HIDLE);
2020-03-29 09:16:53 +00:00
}
void make_umodestr(void)
{
2022-01-15 05:16:34 +00:00
Umode *um;
char *p = umodestring;
2020-03-29 09:16:53 +00:00
2022-01-15 05:16:34 +00:00
for (um=usermodes; um; um = um->next)
if (um->letter)
*p++ = um->letter;
*p = '\0';
2020-03-29 09:16:53 +00:00
}
static char previous_umodestring[256];
void umodes_check_for_changes(void)
{
make_umodestr();
2022-01-15 05:16:34 +00:00
safe_strdup(me.server->features.usermodes, umodestring);
2020-03-29 09:16:53 +00:00
if (!*previous_umodestring)
{
strlcpy(previous_umodestring, umodestring, sizeof(previous_umodestring));
return; /* not booted yet. then we are done here. */
}
if (*previous_umodestring && strcmp(umodestring, previous_umodestring))
{
2022-01-15 05:16:34 +00:00
unreal_log(ULOG_INFO, "mode", "USER_MODES_CHANGED", NULL,
"User modes changed at runtime: $old_user_modes -> $new_user_modes",
log_data_string("old_user_modes", previous_umodestring),
log_data_string("new_user_modes", umodestring));
2020-03-29 09:16:53 +00:00
/* Broadcast change to all (locally connected) servers */
sendto_server(NULL, 0, 0, NULL, "PROTOCTL USERMODES=%s", umodestring);
}
strlcpy(previous_umodestring, umodestring, sizeof(previous_umodestring));
}
2022-01-15 05:16:34 +00:00
void usermode_add_sorted(Umode *n)
2020-03-29 09:16:53 +00:00
{
2022-01-15 05:16:34 +00:00
Umode *m;
if (usermodes == NULL)
2020-03-29 09:16:53 +00:00
{
2022-01-15 05:16:34 +00:00
usermodes = n;
return;
}
for (m = usermodes; m; m = m->next)
{
if (m->letter == '\0')
abort();
if (sort_character_lowercase_before_uppercase(n->letter, m->letter))
2020-03-29 09:16:53 +00:00
{
2022-01-15 05:16:34 +00:00
/* Insert us before */
if (m->prev)
m->prev->next = n;
2020-03-29 09:16:53 +00:00
else
2022-01-15 05:16:34 +00:00
usermodes = n; /* new head */
n->prev = m->prev;
n->next = m;
m->prev = n;
return;
2020-03-29 09:16:53 +00:00
}
2022-01-15 05:16:34 +00:00
if (!m->next)
2020-03-29 09:16:53 +00:00
{
2022-01-15 05:16:34 +00:00
/* Append us at end */
m->next = n;
n->prev = m;
return;
2020-03-29 09:16:53 +00:00
}
}
}
2022-01-15 05:16:34 +00:00
/* UmodeAdd:
* Add a usermode with character 'ch', if global is set to 1 the usermode is global
* (sent to other servers) otherwise it's a local usermode
*/
Umode *UmodeAdd(Module *module, char ch, int global, int unset_on_deoper, int (*allowed)(Client *client, int what), long *mode)
2020-03-29 09:16:53 +00:00
{
2022-01-15 05:16:34 +00:00
Umode *um;
int existing = 0;
2020-03-29 09:16:53 +00:00
2022-01-15 05:16:34 +00:00
for (um=usermodes; um; um = um->next)
2020-03-29 09:16:53 +00:00
{
2022-01-15 05:16:34 +00:00
if (um->letter == ch)
2020-03-29 09:16:53 +00:00
{
2022-01-15 05:16:34 +00:00
if (um->unloaded)
2020-03-29 09:16:53 +00:00
{
2022-01-15 05:16:34 +00:00
um->unloaded = 0;
existing = 1;
2020-03-29 09:16:53 +00:00
break;
2022-01-15 05:16:34 +00:00
} else {
2020-03-29 09:16:53 +00:00
if (module)
module->errorcode = MODERR_EXISTS;
return NULL;
}
}
}
2022-01-15 05:16:34 +00:00
if (!um)
2020-03-29 09:16:53 +00:00
{
2022-01-15 05:16:34 +00:00
/* Not found, create */
long l, found = 0;
for (l = 1; l < LONG_MAX/2; l *= 2)
2020-03-29 09:16:53 +00:00
{
2022-01-15 05:16:34 +00:00
found = 0;
for (um=usermodes; um; um = um->next)
{
if (um->mode == l)
{
found = 1;
break;
}
}
if (!found)
break;
}
/* If 'found' is still true, then we are out of space */
if (found)
{
unreal_log(ULOG_ERROR, "module", "USER_MODE_OUT_OF_SPACE", NULL,
"UmodeAdd: out of space!!!");
if (module)
module->errorcode = MODERR_NOSPACE;
return NULL;
2020-03-29 09:16:53 +00:00
}
2022-01-15 05:16:34 +00:00
um = safe_alloc(sizeof(Umode));
um->letter = ch;
um->mode = l;
usermode_add_sorted(um);
2020-03-29 09:16:53 +00:00
}
2022-01-15 05:16:34 +00:00
um->letter = ch;
um->allowed = allowed;
um->unset_on_deoper = unset_on_deoper;
make_umodestr();
AllUmodes |= um->mode;
if (global)
SendUmodes |= um->mode;
*mode = um->mode;
um->owner = module;
if (module)
2020-03-29 09:16:53 +00:00
{
2022-01-15 05:16:34 +00:00
ModuleObject *umodeobj = safe_alloc(sizeof(ModuleObject));
umodeobj->object.umode = um;
umodeobj->type = MOBJ_UMODE;
AddListItem(umodeobj, module->objects);
module->errorcode = MODERR_NOERROR;
2020-03-29 09:16:53 +00:00
}
2022-01-15 05:16:34 +00:00
return um;
2020-03-29 09:16:53 +00:00
}
2022-01-15 05:16:34 +00:00
void UmodeDel(Umode *umode)
2020-03-29 09:16:53 +00:00
{
2022-01-15 05:16:34 +00:00
/* Always free the module object */
if (umode->owner)
2020-03-29 09:16:53 +00:00
{
2022-01-15 05:16:34 +00:00
ModuleObject *umodeobj;
for (umodeobj = umode->owner->objects; umodeobj; umodeobj = umodeobj->next)
2020-03-29 09:16:53 +00:00
{
2022-01-15 05:16:34 +00:00
if (umodeobj->type == MOBJ_UMODE && umodeobj->object.umode == umode)
{
DelListItem(umodeobj, umode->owner->objects);
safe_free(umodeobj);
2020-03-29 09:16:53 +00:00
break;
}
}
2022-01-15 05:16:34 +00:00
umode->owner = NULL;
2020-03-29 09:16:53 +00:00
}
2022-01-15 05:16:34 +00:00
/* Whether we can actually (already) free the Umode depends... */
if (loop.rehashing)
umode->unloaded = 1;
else
unload_usermode_commit(umode);
2020-03-29 09:16:53 +00:00
}
int umode_allow_all(Client *client, int what)
{
return 1;
}
int umode_allow_unset(Client *client, int what)
{
if (!MyUser(client))
return 1;
if (what == MODE_DEL)
return 1;
return 0;
}
int umode_allow_none(Client *client, int what)
{
if (MyUser(client))
return 0;
return 1;
}
int umode_allow_opers(Client *client, int what)
{
if (MyUser(client))
return IsOper(client) ? 1 : 0;
else
return 1;
}
2020-04-20 19:12:33 +00:00
int umode_hidle_allow(Client *client, int what)
{
if (!MyUser(client))
return 1;
if (iConf.hide_idle_time == HIDE_IDLE_TIME_OPER_USERMODE)
return IsOper(client) ? 1 : 0;
if (iConf.hide_idle_time == HIDE_IDLE_TIME_USERMODE)
return 1;
return 0; /* if set::hide-idle-time is 'never' or 'always' then +I makes no sense */
}
2022-01-15 05:16:34 +00:00
static void unload_usermode_commit(Umode *um)
2020-03-29 09:16:53 +00:00
{
Client *client;
2022-01-15 05:16:34 +00:00
long removed_umode;
if (!um)
2020-03-29 09:16:53 +00:00
return;
2022-01-15 05:16:34 +00:00
removed_umode = um->mode;
/* First send the -mode regarding all users */
2020-03-29 09:16:53 +00:00
list_for_each_entry(client, &lclient_list, lclient_node)
{
2022-01-15 05:16:34 +00:00
if (MyUser(client) && (client->umodes & removed_umode))
2020-03-29 09:16:53 +00:00
{
2022-01-15 05:16:34 +00:00
long oldumode = client->umodes;
client->umodes &= ~(removed_umode);
send_umode_out(client, 1, oldumode);
2020-03-29 09:16:53 +00:00
}
}
2022-01-15 05:16:34 +00:00
/* Then unload the mode */
DelListItem(um, usermodes);
safe_free(um);
2020-03-29 09:16:53 +00:00
make_umodestr();
}
2022-01-15 05:16:34 +00:00
void unload_all_unused_umodes(void)
2020-03-29 09:16:53 +00:00
{
2022-01-15 05:16:34 +00:00
Umode *um, *um_next;
2020-03-29 09:16:53 +00:00
2022-01-15 05:16:34 +00:00
for (um=usermodes; um; um = um_next)
2020-03-29 09:16:53 +00:00
{
2022-01-15 05:16:34 +00:00
um_next = um->next;
if (um->letter && um->unloaded)
unload_usermode_commit(um);
2020-03-29 09:16:53 +00:00
}
}
/**
* This function removes any oper-only snomasks when the user is no
* longer an IRC Operator.
* This used to be a bit more complex but nowadays we just erase all
* snomasks since all of them are IRCOp-only. Easy.
*/
2022-01-15 05:16:34 +00:00
void remove_all_snomasks(Client *client)
2020-03-29 09:16:53 +00:00
{
2022-01-15 05:16:34 +00:00
safe_free(client->user->snomask);
client->umodes &= ~UMODE_SERVNOTICE;
2020-03-29 09:16:53 +00:00
}
/*
* This function removes any oper-only user modes from the user.
2022-01-15 05:16:34 +00:00
* You may also want to call remove_all_snomasks(), see above.
2020-03-29 09:16:53 +00:00
*/
void remove_oper_modes(Client *client)
{
2022-01-15 05:16:34 +00:00
Umode *um;
2020-03-29 09:16:53 +00:00
2022-01-15 05:16:34 +00:00
for (um = usermodes; um; um = um->next)
2020-03-29 09:16:53 +00:00
{
2022-01-15 05:16:34 +00:00
if (um->unset_on_deoper)
client->umodes &= ~um->mode;
2020-03-29 09:16:53 +00:00
}
2020-04-20 19:12:33 +00:00
/* Bit of a hack, since this is a dynamic permission umode */
if (iConf.hide_idle_time == HIDE_IDLE_TIME_OPER_USERMODE)
client->umodes &= ~UMODE_HIDLE;
2020-03-29 09:16:53 +00:00
}
void remove_oper_privileges(Client *client, int broadcast_mode_change)
{
long oldumodes = client->umodes;
remove_oper_modes(client);
2022-01-15 05:16:34 +00:00
remove_all_snomasks(client);
2020-03-29 09:16:53 +00:00
if (broadcast_mode_change && (client->umodes != oldumodes))
send_umode_out(client, 1, oldumodes);
if (MyUser(client)) /* only do if it's our client, remote servers will send a SWHOIS cmd */
swhois_delete(client, "oper", "*", &me, NULL);
}
/** Return long integer mode for a user mode character (eg: 'x' -> 0x10) */
2022-01-15 05:16:34 +00:00
long find_user_mode(char letter)
2020-03-29 09:16:53 +00:00
{
2022-01-15 05:16:34 +00:00
Umode *um;
for (um = usermodes; um; um = um->next)
if ((um->letter == letter) && !um->unloaded)
return um->mode;
2020-03-29 09:16:53 +00:00
return 0;
}
/** Returns 1 if user has this user mode set and 0 if not */
int has_user_mode(Client *client, char mode)
{
long m = find_user_mode(mode);
if (client->umodes & m)
return 1; /* Yes, user has this mode */
return 0; /* Mode does not exist or not set */
}