/* X-Chat
* Copyright (C) 1998 Peter Zelezny.
*
* 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 2 of the License, 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "hexchat.h"
#include "hexchatc.h"
#include "modes.h"
#include "server.h"
#include "text.h"
#include "fe.h"
#include "util.h"
#include "inbound.h"
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#include <glib/gprintf.h>
typedef struct
{
server *serv;
char *op;
char *deop;
char *voice;
char *devoice;
} mode_run;
static int is_prefix_char (server * serv, char c);
static void record_chan_mode (session *sess, char sign, char mode, char *arg);
static char *mode_cat (char *str, char *addition);
static void handle_single_mode (mode_run *mr, char sign, char mode, char *nick,
char *chan, char *arg, int quiet, int is_324,
const message_tags_data *tags_data);
static int mode_has_arg (server *serv, char sign, char mode);
static void mode_print_grouped (session *sess, char *nick, mode_run *mr,
const message_tags_data *tags_data);
static int mode_chanmode_type (server * serv, char mode);
/* word[] - list of nicks.
wpos - index into word[]. Where nicks really start.
end - index into word[]. Last entry plus one.
sign - a char, e.g. '+' or '-'
mode - a mode, e.g. 'o' or 'v' */
void
send_channel_modes (session *sess, char *tbuf, char *word[], int wpos,
int end, char sign, char mode, int modes_per_line)
{
int usable_modes, orig_len, len, wlen, i, max;
server *serv = sess->server;
/* sanity check. IRC RFC says three per line. */
if (serv->modes_per_line < 3)
serv->modes_per_line = 3;
if (modes_per_line < 1)
modes_per_line = serv->modes_per_line;
/* RFC max, minus length of "MODE %s " and "\r\n" and 1 +/- sign */
/* 512 - 6 - 2 - 1 - strlen(chan) */
max = 503 - strlen (sess->channel);
while (wpos < end)
{
tbuf[0] = '\0';
orig_len = len = 0;
/* we'll need this many modechars too */
len += modes_per_line;
/* how many can we fit? */
for (i = 0; i < modes_per_line; i++)
{
/* no more nicks left? */
if (wpos + i >= end)
break;
wlen = strlen (word[wpos + i]) + 1;
if (wlen + len > max)
break;
len += wlen; /* length of our whole string so far */
}
if (i < 1)
return;
usable_modes = i; /* this is how many we'll send on this line */
/* add the +/-modemodemodemode */
len = orig_len;
tbuf[len] = sign;
len++;
for (i = 0; i < usable_modes; i++)
{
tbuf[len] = mode;
len++;
}
tbuf[len] = 0; /* null terminate for the strcat() to work */
/* add all the nicknames */
for (i = 0; i < usable_modes; i++)
{
strcat (tbuf, " ");
strcat (tbuf, word[wpos + i]);
}
serv->p_mode (serv, sess->channel, tbuf);
wpos += usable_modes;
}
}
/* does 'chan' have a valid prefix? e.g. # or & */
int
is_channel (server * serv, char *chan)
{
if (strchr (serv->chantypes, chan[0]))
return 1;
return 0;
}
/* is the given char a valid nick mode char? e.g. @ or + */
static int
is_prefix_char (server * serv, char c)
{
int pos = 0;
char *np = serv->nick_prefixes;
while (np[0])
{
if (np[0] == c)
return pos;
pos++;
np++;
}
if (serv->bad_prefix)
{
if (strchr (serv->bad_nick_prefixes, c))
/* valid prefix char, but mode unknown */
return -2;
}
return -1;
}
/* returns '@' for ops etc... */
char
get_nick_prefix (server * serv, unsigned int access)
{
int pos;
char c;
for (pos = 0; pos < USERACCESS_SIZE; pos++)
{
c = serv->nick_prefixes[pos