Skip to content

Commit

Permalink
extended the ban command to accept a timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
drscholl committed Aug 4, 2000
1 parent f6c0efd commit af4a3d2
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 71 deletions.
8 changes: 8 additions & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,14 @@ specifying what input it expects.
meaningless (and are set to 0 in this implmentation). The
client can then download as they would any other mp3 file.

612 ban user/ip [CLIENT]

Format: <user|ip> [ "reason" [timeout] ]

opennap adds the optional `timeout' fields which specifies the time,
in seconds, that the ban should be enforced. Note that if `timeout'
is present, the `reason' field MUST be present, even if empty.

* Non-standard messages

The following messages are not present in the official napster servers, but
Expand Down
103 changes: 72 additions & 31 deletions ban.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,38 +52,26 @@ normalize_ban(/*const*/ char *src, char *dest, int destlen)
return dest;
}

/* 612 [ :<sender> ] <user!ip> [ "<reason>" ] */
/* 612 [ :<sender> ] <user!ip> [ "<reason>" [time] ] */
HANDLER (ban)
{
BAN *b;
LIST *list;
int ac = -1;
char *av[2], *sender;
char *av[3], *sendernick;
char *banptr, realban[256];
USER *sender;
int timeout = 0;

(void) len;
ASSERT (validate_connection (con));
/* servers have to sync bans, so we don't authenticate the sender,
we assume the other servers do their job */
if (ISSERVER (con))
{
if (*pkt != ':')
{
log ("ban(): missing sender name for server message");
return;
}
pkt++;
sender = next_arg (&pkt);
}
else

if(pop_user_server(con,tag,&pkt,&sendernick,&sender))
return;
if(sender && sender->level < LEVEL_MODERATOR)
{
/* make sure this user has privilege */
if (con->user->level < LEVEL_MODERATOR)
{
permission_denied (con);
return;
}
sender = con->user->nick;
}
if (pkt)
ac = split_line (av, FIELDS (av), pkt);
Expand All @@ -93,6 +81,17 @@ HANDLER (ban)
return;
}

if(ac>2)
{
timeout = atoi (av[2]);
if(timeout < 0)
{
if(ISUSER(con))
send_cmd(con,MSG_SERVER_NOSUCH,"invalid ban timeout");
return;
}
}

banptr=normalize_ban(av[0], realban, sizeof(realban));

/* check to see if this user is already banned */
Expand All @@ -106,9 +105,12 @@ HANDLER (ban)
return;
}
}

if (ac > 1)
truncate_reason (av[1]);
pass_message_args (con, tag, ":%s %s \"%s\"", sender, av[0], ac>1?av[1]:"");

pass_message_args (con, tag, ":%s %s \"%s\" %d", sendernick,
av[0], ac>1?av[1]:"", timeout);

do
{
Expand All @@ -117,20 +119,28 @@ HANDLER (ban)
break;
if (!(b->target = STRDUP (banptr)))
break;
if (!(b->setby = STRDUP (sender)))
if (!(b->setby = STRDUP (sendernick)))
break;
if (!(b->reason = STRDUP (ac > 1 ? av[1] : "")))
break;
b->when = Current_Time;
/* determine if this ban is on an ip or a user */
b->timeout = timeout;

list = CALLOC (1, sizeof (LIST));
if (!list)
{
OUTOFMEMORY("ban");
break;
}
list->data = b;
list->next = Bans;
Bans = list;
notify_mods (BANLOG_MODE, "%s banned %s: %s", sender, b->target,
b->reason);
notify_mods (BANLOG_MODE,
"%s banned %s%s%s%s: %s", sender, b->target,
(timeout>0) ? " for " : "",
(timeout>0) ? av[2] : "",
(timeout>0) ? " seconds" : "",
b->reason);
return;
}
while (1);
Expand Down Expand Up @@ -210,8 +220,8 @@ HANDLER (banlist)
{
ban = list->data;
send_cmd (con, MSG_SERVER_IP_BANLIST /* 616 */ ,
"%s %s \"%s\" %ld 0", ban->target, ban->setby,
ban->reason, ban->when);
"%s %s \"%s\" %ld %d", ban->target, ban->setby,
ban->reason, ban->when, ban->timeout);
}
/* terminate the banlist */
send_cmd (con, MSG_CLIENT_BANLIST /* 615 */ , "");
Expand All @@ -228,7 +238,8 @@ check_ban (CONNECTION * con, const char *nick, const char *host)
for (list = Bans; list; list = list->next)
{
ban = list->data;
if(glob_match(ban->target,mask))
if((ban->timeout == 0 || ban->when + ban->timeout > Current_Time) &&
glob_match(ban->target,mask))
{
notify_mods (BANLOG_MODE,
"Connection from %s: %s banned: %s",
Expand Down Expand Up @@ -278,8 +289,8 @@ save_bans (void)
for (list = Bans; list; list = list->next)
{
b = list->data;
fprintf (fp, "%s %s %d \"%s\"", b->target, b->setby,
(int) b->when, b->reason);
fprintf (fp, "%s %s %d \"%s\" %d", b->target, b->setby,
(int) b->when, b->reason, b->timeout);
#ifdef WIN32
fputc ('\r', fp);
#endif
Expand All @@ -300,7 +311,7 @@ load_bans (void)
LIST *list, **last = &Bans;
BAN *b;
int ac;
char *av[4], path[_POSIX_PATH_MAX];
char *av[5], path[_POSIX_PATH_MAX];
char *banptr, realban[256];

snprintf (path, sizeof (path), "%s/bans", Config_Dir);
Expand Down Expand Up @@ -336,6 +347,8 @@ load_bans (void)
b->when = atol (av[2]);
truncate_reason (av[3]);
b->reason = STRDUP (av[3]);
if(ac>4)
b->timeout = atoi (av[4]);
}
else
{
Expand All @@ -360,3 +373,31 @@ load_bans (void)
fclose (fp);
return 0;
}

/* reap expired bans from the list */
void
expire_bans (void)
{
LIST **list, *tmp;
BAN *b;

list=&Bans;
while(*list)
{
b=(*list)->data;
if(b->timeout > 0 && b->when + b->timeout < Current_Time)
{
tmp=*list;
*list=(*list)->next;
FREE(tmp);
/* make sure all servers are synched up */
pass_message_args(NULL,MSG_CLIENT_UNBAN,":%s %s \"expired after %d seconds\"",
Server_Name, b->target, b->timeout);
notify_mods(BANLOG_MODE,"%s removed ban on %s: expired after %d seconds",
Server_Name, b->target, b->timeout);
free_ban(b);
continue;
}
list=&(*list)->next;
}
}
1 change: 1 addition & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@ main (int argc, char **argv)
#endif /* RESUME */
add_timer (Stat_Click, -1, (timer_cb_t) update_stats, 0);
add_timer (User_Db_Interval, -1, (timer_cb_t) dump_state, 0);
add_timer (60, -1, (timer_cb_t) expire_bans, 0);

/* initialize so we get the correct delta for the first call to
update_stats() */
Expand Down
10 changes: 6 additions & 4 deletions opennap.h
Original file line number Diff line number Diff line change
Expand Up @@ -306,10 +306,11 @@ DATUM;

typedef struct _ban
{
char *target;
char *setby;
char *reason;
time_t when;
char *target; /* target of the ban */
char *setby; /* user/server that set the ban */
char *reason; /* reason for the ban */
time_t when; /* when the ban was set */
int timeout; /* how many secs the ban is active for */
}
BAN;

Expand Down Expand Up @@ -637,6 +638,7 @@ USERDB *create_db (USER *);
void dump_channels(void);
void exec_timers (time_t);
void expand_hex (char *, int);
void expire_bans (void);
void fdb_garbage_collect (HASH *);
void finalize_compress (SERVER *);
CHANNEL *find_channel (LIST *, const char *);
Expand Down
69 changes: 35 additions & 34 deletions redirect.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,45 +19,46 @@ HANDLER (redirect_client)
int port;

(void) len;
if(pop_user_server(con,tag,&pkt,&sendernick,&sender))
if (pop_user_server (con, tag, &pkt, &sendernick, &sender))
return;

if(pkt)
ac=split_line(av,FIELDS(av),pkt);
if(ac < 3)
if (pkt)
ac = split_line (av, FIELDS (av), pkt);
if (ac < 3)
{
unparsable(con);
unparsable (con);
return;
}

if(sender->level < LEVEL_ADMIN)
if (sender->level < LEVEL_ADMIN)
{
permission_denied(con);
permission_denied (con);
return;
}

port = atoi (av[2]);
if(port < 0 || port > 65535)
if (port < 0 || port > 65535)
{
if(ISUSER(con))
send_cmd(con,MSG_SERVER_NOSUCH,"invalid port");
if (ISUSER (con))
send_cmd (con, MSG_SERVER_NOSUCH, "invalid port");
return;
}
user = hash_lookup(Users, av[0]);
if(!user)

user = hash_lookup (Users, av[0]);
if (!user)
{
nosuchuser(con);
nosuchuser (con);
return;
}

if(ISUSER(user->con))
send_cmd(user->con,tag,"%s %s", av[1], av[2]);
if (ISUSER (user->con))
send_cmd (user->con, tag, "%s %s", av[1], av[2]);

notify_mods(CHANGELOG_MODE,"%s redirected %s to %s:%s",
sendernick, av[0], av[1], av[2]);
notify_mods (CHANGELOG_MODE, "%s redirected %s to %s:%s",
sendernick, av[0], av[1], av[2]);

pass_message_args(con,tag,":%s %s %s %s", sendernick, av[0], av[1], av[2]);
pass_message_args (con, tag, ":%s %s %s %s", sendernick, av[0], av[1],
av[2]);
}

/* 822 [ :sender ] <nick> <server>
Expand All @@ -70,33 +71,33 @@ HANDLER (cycle_client)
char *nick, *server;

(void) len;
if(pop_user_server(con,tag,&pkt,&sendernick,&sender))
if (pop_user_server (con, tag, &pkt, &sendernick, &sender))
return;

if(sender->level< LEVEL_ADMIN)
if (sender->level < LEVEL_ADMIN)
{
permission_denied(con);
permission_denied (con);
return;
}
nick=next_arg(&pkt);
server=next_arg(&pkt);
if(!nick||!server)
nick = next_arg (&pkt);
server = next_arg (&pkt);
if (!nick || !server)
{
unparsable(con);
unparsable (con);
return;
}
user=hash_lookup(Users,nick);
if(!user)
user = hash_lookup (Users, nick);
if (!user)
{
nosuchuser(con);
nosuchuser (con);
return;
}

if(ISUSER(user->con))
send_cmd(user->con,tag,"%s",server);
if (ISUSER (user->con))
send_cmd (user->con, tag, "%s", server);

notify_mods(CHANGELOG_MODE,"%s cycled %s to %s", sendernick,
sendernick, nick, server);
notify_mods (CHANGELOG_MODE, "%s cycled %s to %s", sendernick,
sendernick, nick, server);

pass_message_args(con,tag,":%s %s %s", sendernick, nick, server);
pass_message_args (con, tag, ":%s %s %s", sendernick, nick, server);
}
4 changes: 2 additions & 2 deletions synch.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ sync_banlist (CONNECTION * con)
{
b = list->data;
ASSERT (b != 0);
send_cmd (con, MSG_CLIENT_BAN, ":%s %s \"%s\"", b->setby,
b->target, b->reason);
send_cmd (con, MSG_CLIENT_BAN, ":%s %s \"%s\" %d", b->setby,
b->target, b->reason, b->timeout);
}
}

Expand Down

0 comments on commit af4a3d2

Please sign in to comment.