channels.c

channels.c · 32.9 KB

From the archive channels.zip

Download the archive   « Back to the archive contents

/***************************************************************************
 * This code may be used freely within any non-commercial MUD, all I ask   *
 * is that these comments remain in tact and that you give me any feedback *
 * or bug reports you come up with.  The helpfile distributed with this    *
 * code must also remain in tact.                                          *
 *                                  -- Midboss (eclipsing.souls@gmail.com) *
 ***************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "merc.h"
#include "interp.h"
#include "utils.h"

/*
 * Change this to set the requirement for channel creation.  Note that
 * immortals are exempt from requirements.
 *
 *  0: Default setting.  Requires that the user be hero level or higher.
 *  1: Players must have at least 500 hours of play time to create a channel.
 *  2: Gold requirement.
 *  3: No requirements.
 *
 * IMPORTANT: If you set it to 1, remember to uncomment the code in
 * chan_do_create that checks play time.  I had to comment it out because
 * my MUD uses an array to keep track of seconds, minutes, and hours
 * individually.
 */
#define req_type 0

/*
 * Secondary command table for channel commands.
 */
const struct cmd_type chan_cmd_table[] = {
	{"commands",	chan_do_commands, POS_KNOCKOUT,	0,	LOG_NORMAL, 1},
	{"list",		chan_do_list,	  POS_KNOCKOUT,	0,	LOG_NORMAL, 1},
	{"invite",		chan_do_invite,	  POS_KNOCKOUT,	0,	LOG_NORMAL, 1},
	{"join",		chan_do_join,	  POS_KNOCKOUT,	0,	LOG_NORMAL, 1},
	{"leave",		chan_do_leave,	  POS_KNOCKOUT,	0,	LOG_NORMAL, 1},
	{"kick",		chan_do_kick,	  POS_KNOCKOUT,	0,	LOG_NORMAL, 1},
	{"topic",		chan_do_topic,	  POS_KNOCKOUT,	0,	LOG_NORMAL, 1},
	{"config",		chan_do_config,	  POS_KNOCKOUT,	0,	LOG_NORMAL, 1},
	{"create",		chan_do_create,	  POS_KNOCKOUT,	0,	LOG_NORMAL, 1},
	{"grant",		chan_do_admin,	  POS_KNOCKOUT,	0,	LOG_NORMAL, 1},
	{"ban",			chan_do_ban,	  POS_KNOCKOUT,	0,	LOG_NORMAL, 1},
	{"stats",		chan_do_stats,	  POS_KNOCKOUT,	0,	LOG_NORMAL, 1},
	{"",			0,				  POS_KNOCKOUT,	0,	LOG_NORMAL, 0}
};

/*
 * Used to list all commands.
 */
void chan_do_commands (CHAR_DATA * ch, char * argument)
{
    char buf[MAX_STRING_LENGTH];
    int cmd;
    int col;

    col = 0;
    for (cmd = 0; chan_cmd_table[cmd].name[0] != '\0'; cmd++)
    {
        if (chan_cmd_table[cmd].level <= get_trust (ch)
			&& chan_cmd_table[cmd].show)
        {
            sprintf (buf, "%-12s", chan_cmd_table[cmd].name);
            send_to_char (buf, ch);
            if (++col % 6 == 0)
                send_to_char ("\n\r", ch);
        }
    }

    if (col % 6 != 0)
        send_to_char ("\n\r", ch);
    return;
}

/*
 * Used to list all existing channels.
 */
void chan_do_list (CHAR_DATA * ch, char * argument)
{
	CHANNEL_DATA * chan;
	int count = 0;

	send_to_char ("    Channel Name                       "
				  "          Channel Topic      Keyword\n\r", ch);
	for (chan = channel_list; chan != NULL; chan = chan->next)
	{
		++count;
		printf_to_char (ch, " %15s  %45s  %10s\n\r",
						chan->name, chan->topic, chan->keyword);
	}
	printf_to_char (ch, "There %s currently %d channel%s.\n\r",
					count == 1 ? "is" : "are", count, count == 1 ? "" : "s");
	return;
}

/*
 * Used to check your channel's stats.
 */
void chan_do_stats (CHAR_DATA * ch, char * argument)
{
	int aCount = 0, bCount = 0;
	CHANNEL_DATA * chan;
	CHANMEMBER * list;
	char buf[MSL], buf2[MSL];

	//Get the channel.
	if ((chan = get_channel_char (ch)) == NULL)
	{
		send_to_char ("You don't have a channel.\n\r", ch);
		return;
	}

	buf[0] = '\0';
	buf2[0] = '\0';

	/*
	 * Now print the flags.  Certain flags are skipped.
	 */
	if (IS_SET (chan->config, CHANNEL_PRIVATE))
	{
		send_to_char ("Your channel is private.\n\r", ch);

		if (IS_SET (chan->config, CHANNEL_INVITE))
			send_to_char ("Users may freely invite others to your channel.\n\r", ch);
		else
			send_to_char ("Users may not invite others to your channel.\n\r", ch);
	}
	else
		send_to_char ("Your channel is public.\n\r", ch);

	if (IS_SET (chan->config, CHANNEL_ADVANCE))
		send_to_char ("Your admins may grant admin rights.\n\r", ch);
	else
		send_to_char ("Your admins may not grant admin rights.\n\r", ch);

	if (IS_SET (chan->config, CHANNEL_BAN))
		send_to_char ("Your admins may ban users.\n\r", ch);
	else
		send_to_char ("Your admins may not ban users.\n\r", ch);

	if (IS_SET (chan->config, CHANNEL_KICK))
		send_to_char ("Your admins may kick users.\n\r", ch);
	else
		send_to_char ("Your admins may not kick users.\n\r", ch);

	if (IS_SET (chan->config, CHANNEL_TOPIC))
		send_to_char ("Your admins may change the channel topic.\n\r", ch);
	else
		send_to_char ("Your admins may not change the channel topic.\n\r", ch);


	//Now get the counts.
	for (list = chan->lists; list != NULL; list = list->next)
	{
		if (list->type == MEMBER_BAN)
			++bCount;
		else
			++aCount;
	}

	//Admins.
	if (aCount < 1)
		send_to_char ("Your channel has no admins.\n\r", ch);
	else
	{
		aCount = 0;
		send_to_char ("Your channel has the following admins:\n\r", ch);
		for (list = chan->lists; list != NULL; list = list->next)
		{
			if (list->type == MEMBER_BAN) continue;
			aCount++;

			sprintf (buf + strlen (buf), "%12s   %s",
						list->name, aCount % 5 == 0 ? "\n\r" : "");
		}
		send_to_char (buf, ch);
	}

	//Bans.
	if (bCount < 1)
		send_to_char ("Your channel has no bans.\n\r", ch);
	else
	{
		bCount = 0;
		send_to_char ("Your channel has the following bans:\n\r", ch);
		for (list = chan->lists; list != NULL; list = list->next)
		{
			if (list->type != MEMBER_BAN) continue;
			bCount++;

			sprintf (buf + strlen (buf), "%12s   %s",
						list->name, bCount % 5 == 0 ? "\n\r" : "");
		}
		send_to_char (buf, ch);
	}

	return;
}


/*
 * Used to invite someone to a channel you're in.
 */
void chan_do_invite (CHAR_DATA * ch, char * argument)
{
	CHANNEL_DATA * chan;
	CHAR_DATA * vch;
	char arg[MIL], buf[MSL];

	argument = one_argument (argument, arg);

	//No argument?
	if (arg[0] == '\0')
	{
		send_to_char ("Invite who, now?\n\r", ch);
		return;
	}


	//No channel?
	if ((chan = get_channel (argument)) == NULL)
	{
		send_to_char ("Invite them to which channel?\n\r", ch);
		return;
	}

	//Not a member?
	if (!can_hear (ch, chan))
	{
		send_to_char ("You aren't a member of that channel.\n\r", ch);
		return;
	}


	//No victim?
	if ((vch = get_char_world (ch, arg)) == NULL)
	{
		send_to_char ("You can't invite someone you can't find.\n\r", ch);
		return;
	}

	//What a lonely sap...
	if (ch == vch)
	{
		send_to_char ("That seems rather pointless.\n\r", ch);
		return;
	}

	//We don't want things to go boom, do we?
	if (IS_NPC (vch))
	{
		send_to_char ("Mobiles can't use channels.\n\r", ch);
		return;
	}

	//Execute the command.
	if (!is_owner (ch, chan) && !is_admin (ch, chan)
		&& !IS_SET (chan->config, CHANNEL_INVITE))
		//Not allowed?
		send_to_char ("You don't have permission to invite to that channel.\n\r", ch);
	else if (!IS_SET (chan->config, CHANNEL_PRIVATE) || has_invite (vch, chan))
	{
		//Just put a fire under their ass.
		send_to_char ("Feeling impatient, are we?\n\r", ch);
		act ("$n has invited you to $t.", ch, chan->name, vch, TO_VICT);
	}
	else
	{
		act ("$n has been invited to $t.", ch, chan->name, vch, TO_CHAR);
		act ("$n has invited you to $t.", ch, chan->name, vch, TO_VICT);
		sprintf (buf, "/me has invited %s to the channel.", vch->name);
		send_to_channel (ch, chan->keyword, buf, TRUE);

		//Add it to the list.
		if (vch->pcdata->invitations[0] != '\0')
		{
			strcat (vch->pcdata->invitations, " ");
			strcat (vch->pcdata->invitations, chan->keyword);
		}
		else
			vch->pcdata->invitations = str_dup (chan->keyword);
	}
	return;
}

/*
 * Used to join an existing channel.
 */
void chan_do_join (CHAR_DATA * ch, char * argument)
{
	CHANNEL_DATA * chan;

	//No channel?
	if ((chan = get_channel (argument)) == NULL)
	{
		send_to_char ("Join which channel?\n\r", ch);
		return;
	}


	//Are we ALREADY there?
	if (can_hear (ch, chan))
	{
		send_to_char ("You're already a member of that channel.\n\r", ch);
		return;
	}

	//Make sure we're welcome.
	if (   (IS_SET (chan->config, CHANNEL_STAFF) && ch->level < LEVEL_IMMORTAL
			&& !has_invite (ch, chan))
		|| (IS_SET (chan->config, CHANNEL_ADMIN) && ch->level < SUPREME
			&& !has_invite (ch, chan))
		|| (IS_SET (chan->config, CHANNEL_IMP)   && ch->level < IMPLEMENTOR
			&& !has_invite (ch, chan))
		|| (IS_SET (chan->config, CHANNEL_PRIVATE)
			&& !is_owner (ch, chan) && !is_admin (ch, chan)
			&& !has_invite (ch, chan))
		|| is_banned (ch, chan))
	{
		send_to_char ("You aren't welcome in that channel.\n\r", ch);
		return;
	}

	//Add it to our list...
	if (ch->pcdata->channels[0] != '\0')
	{
		strcat (ch->pcdata->channels, " ");
		strcat (ch->pcdata->channels, chan->keyword);
	}
	else
		ch->pcdata->channels = str_dup (chan->keyword);

	act ("You have joined $t.", ch, chan->name, NULL, TO_CHAR);
	send_to_channel (ch, chan->keyword, "/me has joined the channel.", TRUE);
	return;
}

/*
 * Used to leave a channel.
 */
void chan_do_leave (CHAR_DATA * ch, char * argument)
{
	CHANNEL_DATA * chan;

	//No channel?
	if ((chan = get_channel (argument)) == NULL)
	{
		send_to_char ("Leave which channel?\n\r", ch);
		return;
	}

	//Are we there?
	if (!can_hear (ch, chan))
	{
		send_to_char ("You're not a member of that channel.\n\r", ch);
		return;
	}

	//Emote the action.	
	act ("You have left $t.", ch, chan->name, NULL, TO_CHAR);
	send_to_channel (ch, chan->keyword, "/me has left the channel.", TRUE);

	//Leave the channel.
	leave_channel (ch, chan);
	return;
}


/*
 * Channel kicking.
 */
void chan_do_kick (CHAR_DATA * ch, char * argument)
{
	CHANNEL_DATA * chan;
	CHAR_DATA * vch;
	char arg[MIL];

	argument = one_argument (argument, arg);

	//No argument or victim?
	if (arg[0] == '\0' || (vch = get_char_world (ch, arg)) == NULL)
	{
		send_to_char ("Kick who out?\n\r", ch);
		return;
	}

	//No channel?
	if ((chan = get_channel (argument)) == NULL)
	{
		send_to_char ("Kick them out of which channel?\n\r", ch);
		return;
	}

	//Now, are we here?
	if (!can_hear (ch, chan))
	{
		send_to_char ("That'd be a nice thought.  If you were a member.\n\r", ch);
		return;
	}

	//Do we have the rights?
	if ((!is_admin (ch, chan) || !IS_SET (chan->config, CHANNEL_KICK))
		&& !is_owner (ch, chan))
	{
		send_to_char ("You can't do that.\n\r", ch);
		return;
	}

	//They ARE here, right?
	if (!can_hear (vch, chan))
	{
		send_to_char ("They're not in that channel.\n\r", ch);
		return;
	}

	//Emote the action.	
	act ("You have been kicked out of $t by $n.", ch, chan->name, vch, TO_VICT);
	send_to_channel (vch, chan->keyword, "/me has been kicked out of the channel.", TRUE);

	//Leave the channel.
	leave_channel (vch, chan);
	return;
}

/*
 * Used to change a channel's topic.
 */
void chan_do_topic (CHAR_DATA * ch, char * argument)
{
	char arg[MIL], buf[MSL];
	CHANNEL_DATA * chan;

	argument = one_argument (argument, arg);

	//No channel?
	if ((chan = get_channel (arg)) == NULL)
	{
		send_to_char ("Change the topic of which channel?\n\r", ch);
		return;
	}


	//Now, are we here?
	if (!can_hear (ch, chan))
	{
		send_to_char ("That'd be a nice thought.  If you were a member.\n\r", ch);
		return;
	}

	//Do we have the rights?
	if ((!is_admin (ch, chan) || !IS_SET (chan->config, CHANNEL_TOPIC))
		&& !is_owner (ch, chan))
	{
		send_to_char ("You can't do that.\n\r", ch);
		return;
	}

	//No argument?
	if (argument[0] == '\0')
	{
		send_to_char ("Set the topic to what?\n\r", ch);
		return;
	}

	//Too long?
	if (strlen (argument) > 45)
	{
		send_to_char ("Please keep the topic under 45 characters.\n\r", ch);
		return;
	}
	
	//Make sure it's safe.
	smash_tilde (argument);

	//Set the topic.
	free_string (chan->topic);
	chan->topic = str_dup (argument);

	//Emote the action.
	send_to_char ("It is done.\n\r", ch);
	sprintf (buf, "/me has set the channels topic to: %s", argument);
	send_to_channel (ch, chan->keyword, buf, TRUE);
	return;
}

/*
 * Used to change the settings for your channel.
 */
void chan_do_config (CHAR_DATA * ch, char * argument)
{
	CHANNEL_DATA * chan;

	//No channel?
	if ((chan = get_channel_char (ch)) == NULL)
	{
		send_to_char ("You don't own a channel.\n\r", ch);
		return;
	}

	//No argument?
	if (argument[0] == '\0')
	{
		//Note that level-based channels have been left out of config.
		send_to_char ("Toggle which config option?\n\r"
					  "  private  invite  ban\n\r"
					  "  kick  topic  advance\n\r", ch);
		return;
	}

	//Now, are we here?
	if (!can_hear (ch, chan))
	{
		send_to_char ("That'd be a nice thought.  If you were logged in.\n\r", ch);
		return;
	}

	/*
	 * I know, this is ugly.  But I wanted the emotes to look right.
	 * I'm only commenting on one, because they're all the same.
	 */
	//Check the flag to toggle.
	if (!str_prefix (argument, "private"))
	{
		//Not set?
		if (!IS_SET (chan->config, CHANNEL_PRIVATE))
		{
			//Set it and emote the action.
			SET_BIT (chan->config, CHANNEL_PRIVATE);
			send_to_channel (ch, chan->keyword,
					"/me has made the channel private.", TRUE);
		}
		else
		{
			//Else remove it and emote.
			REMOVE_BIT (chan->config, CHANNEL_PRIVATE);
			send_to_channel (ch, chan->keyword,
					"/me has made the channel public.", TRUE);
		}
		//Don't return yet!  That's handled at the end.
	}
	else if (!str_prefix (argument, "invite"))
	{
		//Make sure this is used.
		if (!IS_SET (chan->config, CHANNEL_PRIVATE))
		{
			send_to_char ("Your channel isn't private.\n\r", ch);
			return;
		}

		if (!IS_SET (chan->config, CHANNEL_INVITE))
		{
			SET_BIT (chan->config, CHANNEL_INVITE);
			send_to_channel (ch, chan->keyword,
					"/me has allowed invitations.", TRUE);
		}
		else
		{
			REMOVE_BIT (chan->config, CHANNEL_INVITE);
			send_to_channel (ch, chan->keyword,
					"/me has disallowed invitations.", TRUE);
		}
	}
	else if (!str_prefix (argument, "ban"))
	{
		if (!IS_SET (chan->config, CHANNEL_BAN))
		{
			SET_BIT (chan->config, CHANNEL_BAN);
			send_to_channel (ch, chan->keyword,
					"/me has allowed admins to ban.", TRUE);
		}
		else
		{
			REMOVE_BIT (chan->config, CHANNEL_BAN);
			send_to_channel (ch, chan->keyword,
					"/me has disallowed admins from banning.", TRUE);
		}
	}
	else if (!str_prefix (argument, "kick"))
	{
		if (!IS_SET (chan->config, CHANNEL_KICK))
		{
			SET_BIT (chan->config, CHANNEL_KICK);
			send_to_channel (ch, chan->keyword,
					"/me has allowed admins to kick.", TRUE);
		}
		else
		{
			REMOVE_BIT (chan->config, CHANNEL_KICK);
			send_to_channel (ch, chan->keyword,
					"/me has disallowed admins from kicking.", TRUE);
		}
	}
	else if (!str_prefix (argument, "topic"))
	{
		if (!IS_SET (chan->config, CHANNEL_TOPIC))
		{
			SET_BIT (chan->config, CHANNEL_TOPIC);
			send_to_channel (ch, chan->keyword,
					"/me has allowed admins to change the topic.", TRUE);
		}
		else
		{
			REMOVE_BIT (chan->config, CHANNEL_TOPIC);
			send_to_channel (ch, chan->keyword,
					"/me has disallowed admins from changing the topic.", TRUE);
		}
	}
	else if (!str_prefix (argument, "advance"))
	{
		if (!IS_SET (chan->config, CHANNEL_ADVANCE))
		{
			SET_BIT (chan->config, CHANNEL_ADVANCE);
			send_to_channel (ch, chan->keyword,
					"/me has allowed admins to grant admin rights.", TRUE);
		}
		else
		{
			REMOVE_BIT (chan->config, CHANNEL_ADVANCE);
			send_to_channel (ch, chan->keyword,
					"/me has disallowed admins from granting admin rights.", TRUE);
		}
	}
	else
	{
		//Bad choice, echo the syntax again.
		send_to_char ("Toggle which config option?\n\r"
					  "  private  invite  ban\n\r"
					  "  kick  topic  advance\n\r", ch);
		return;
	}
	
	//All is well?
	send_to_char ("It has been done.\n\r", ch);
	return;
}

/*
 * Used to purchase a new channel.
 */
void chan_do_create (CHAR_DATA * ch, char * argument)
{
	CHANNEL_DATA * chan;
	char arg[MIL];

	argument = one_argument (argument, arg);

	if (get_channel_char (ch) != NULL)
	{
		send_to_char ("You already have a channel!\n\r", ch);
		return;
	}

	switch (req_type)
	{
		//Requires level.
		case 0:
			if (ch->level < LEVEL_HERO)
			{
				send_to_char ("You need to be a hero to create a channel.\n\r", ch);
				return;
			}
			break;
		//Requires play time.
		case 1:
			/*
			 * This check is commented out because my MUD tracks playtime
			 * differently than stock ROM.					-- Midboss
			 */
/*			if ((ch->played + (int) (current_time - ch->logon)) / 3600) < 500
				&& !IS_IMMORTAL (ch))
			{
				send_to_char ("You need to play at least 500 hours"
							  " to create a channel.\n\r", ch);
				return;
			}*/
			break;
		//Requires gold.
		case 2:
			if (ch->gold < 50000 && !IS_IMMORTAL (ch))
			{
				send_to_char ("You need to have at least 50000 gold"
							  " to create a channel.\n\r", ch);
				return;
			}
			break;
		//Requires nothing.
		default: break;
	}

	//Check for a syntax.
	if (arg[0] == '\0' || argument[0] == '\0')
	{
		send_to_char ("You need to specify a name and a keyword.\n\r", ch);
		return;
	}

	//Check name length.
	if (strlen (arg) < 5 || strlen (arg) > 15)
	{
		send_to_char ("Channel names must be between 5 and 15 characters.\n\r", ch);
		return;
	}

	//Check keyword length.
	if (strlen (argument) < 3 || strlen (argument) > 10)
	{
		send_to_char ("Channel keywords must be between 3 and 10 characters.\n\r", ch);
		return;
	}

	smash_tilde (argument);
	smash_tilde (arg);

	if (!legal_channel (argument, arg))
	{
		send_to_char ("Please avoid the creation of conflicting channels.\n\r", ch);
		return;
	}

	chan			= new_channel ();
	chan->name		= str_dup (arg);
	chan->keyword	= str_dup (argument);
	chan->topic		= str_dup ("No topic set.");
	chan->owner		= str_dup (ch->name);
	chan->next		= channel_list;
	channel_list	= chan;
	
	//In case you required gold.
	if (req_type == 2)
		deduct_cost (ch, 500000);

	send_to_char ("Channel created!\n\r", ch);
	chan_do_join (ch, chan->keyword);
	return;
}

/*
 * Used to grant moderator rights on your channel.
 */
void chan_do_admin (CHAR_DATA * ch, char * argument)
{
	CHANMEMBER * member;
	CHANNEL_DATA * chan;
	CHAR_DATA * vch;
	char arg[MIL];

	argument = one_argument (argument, arg);

	//No argument or victim?
	if (arg[0] == '\0' || (vch = get_char_world (ch, arg)) == NULL)
	{
		send_to_char ("Toggle whose admin status?\n\r", ch);
		return;
	}

	//No channel?
	if ((chan = get_channel (argument)) == NULL)
	{
		send_to_char ("Toggle their admin status in which channel?\n\r", ch);
		return;
	}

	//Do we have the rights?
	if ((!is_admin (ch, chan) || !IS_SET (chan->config, CHANNEL_ADVANCE))
		&& !is_owner (ch, chan))
	{
		send_to_char ("You can't do that.\n\r", ch);
		return;
	}

	//They ARE here, right?
	if (!can_hear (vch, chan))
	{
		send_to_char ("They're not in that channel.\n\r", ch);
		return;
	}

	//Now, are we here?
	if (!can_hear (ch, chan))
	{
		send_to_char ("That'd be a nice thought.  If you were a member.\n\r", ch);
		return;
	}

	//Toggles it.
	if (is_admin (vch, chan))
	{
		CHANMEMBER * prev;

		if (!is_owner (ch, chan))
		{
			send_to_char ("You don't have the power.\n\r", ch);
			return;
		}

		//Emote the action.	
		act ("$n has taken away your admin rights on $t.", 
				ch, chan->name, vch, TO_VICT);
		send_to_channel (vch, chan->keyword, "/me has lost admin rights.", TRUE);

		member = get_member_data (ch, chan);

		//Remove the member data.
		if (chan->lists == member)
		{
			chan->lists  = member->next;
			member->next = NULL;
		}
		else
		{
			for (prev = chan->lists  ; prev != NULL; prev = prev->next)
			{
				if (prev->next == member)
				{
					prev->next = member->next;
					member->next = NULL;
					break;
				}
			}
		}
		free_member (member);
	}
	else
	{
		//Emote the action.	
		act ("You have been made an admin on $t by $n.", ch, chan->name, vch, TO_VICT);
		send_to_channel (vch, chan->keyword, "/me has been made an admin.", TRUE);

		//Create the member data.
		member			= new_member ();
		member->name	= str_dup (vch->name);
		member->type	= MEMBER_ADMIN;
		member->next	= chan->lists;
		chan->lists		= member;
	}
	return;
}

/*
 * Banning, obviously.
 */
void chan_do_ban (CHAR_DATA * ch, char * argument)
{
	char arg[MIL], buf[MSL];
	CHANMEMBER * member;
	CHANNEL_DATA * chan;
	CHAR_DATA * vch;

	argument = one_argument (argument, arg);

	//No argument or victim?
	if (arg[0] == '\0' || (vch = get_char_world (ch, arg)) == NULL)
	{
		send_to_char ("Toggle whose ban status?\n\r", ch);
		return;
	}

	//No channel?
	if ((chan = get_channel (argument)) == NULL)
	{
		send_to_char ("Toggle their ban status in which channel?\n\r", ch);
		return;
	}

	//Do we have the rights?
	if ((!is_admin (ch, chan) || !IS_SET (chan->config, CHANNEL_BAN))
		&& !is_owner (ch, chan))
	{
		send_to_char ("You can't do that.\n\r", ch);
		return;
	}

	//We can do it, right?
	if (is_owner (vch, chan))
	{
		send_to_char ("You can't do that.\n\r", ch);
		return;
	}

	if (is_admin (vch, chan) && is_owner (ch, chan))
	{
		send_to_char ("Remove their status first.\n\r", ch);
		return;
	}

	//Toggles it.
	if (is_banned (vch, chan))
	{
		CHANMEMBER * prev;
		//Emote the action.	
		act ("$n has lifted your ban from $t.", 
				ch, chan->name, vch, TO_VICT);
		send_to_char ("It is done.\n\r", ch);
		sprintf (buf, "/me has lifted the ban on %s.", vch->name);
		send_to_channel (ch, chan->keyword, buf, TRUE);

		member = get_member_data (ch, chan);

		//Remove the member data.
		if (chan->lists == member)
		{
			chan->lists  = member->next;
			member->next = NULL;
		}
		else
		{
			for (prev = chan->lists  ; prev != NULL; prev = prev->next)
			{
				if (prev->next == member)
				{
					prev->next = member->next;
					member->next = NULL;
					break;
				}
			}
		}
		free_member (member);
	}
	else
	{
		//Emote the action.	
		act ("You have been banned from $t by $n.", ch, chan->name, vch, TO_VICT);

		if (can_hear (vch, chan))
		{
			send_to_channel (vch, chan->keyword, "/me has been banned.", TRUE);
			leave_channel (vch, chan);
		}

		//Create the member data.
		member			= new_member ();
		member->name	= str_dup (vch->name);
		member->type	= MEMBER_BAN;
		member->next	= chan->lists;
		chan->lists		= member;
	}
	return;

}


/*
 * The actual channel function.  'cmd' is the channel argument.  If the parsed
 * 'cmd' is not a channel, the first channel in ch's list is assumed and 'cmd'
 * is stuck back onto 'string'.
 */
void send_to_channel (CHAR_DATA * ch, char * cmd, char * arg, bool fSkip)
{
	CHANNEL_DATA * chan;
	DESCRIPTOR_DATA * d;
	char buf[MSL], cmd2[MIL];


	//First, look for a channel...
	if ((chan = get_channel (cmd)) == NULL)
	{
		send_to_char ("Send it to WHAT channel?\n\r", ch);
		return;
	}
	if (!can_hear (ch, chan))
	{
		send_to_char ("You aren't a member of that channel.\n\r", ch);
		return;
	}

	//Check for /me.
	one_argument (arg, cmd2);
	if (!str_cmp (cmd2, "/me"))
	{
		//Actually cut it off.
		arg = one_argument (arg, cmd2);
		sprintf (buf, "[%s] %s %s\n\r", chan->name, ch->name, arg); 
	}
	else
		sprintf (buf, "[%s] %s: %s\n\r", chan->name, ch->name, arg); 


	//Now send the buffer to all listeners.
    for (d = descriptor_list; d != NULL; d = d->next)
	{
		if (ch == d->character && fSkip) continue;
		if (d->connected == CON_PLAYING && can_hear (d->character, chan))
			send_to_char (buf, d->character);
	}
}


/*
 * Seperate interpreter for channel commands.  Any line preceded by a period will
 * be passed through here before the regular interpreter.
 */
bool parse_channel (CHAR_DATA * ch, char * arg)
{
	char cmd[MIL];
	int iCmd, str_pos;

	//NPCs don't get channels.
	if (IS_NPC (ch))
		return FALSE;

	//Make sure this is a channel command.
	if (arg[0] != '.')
		return FALSE;

	arg = one_argument (arg, cmd);
	
	//Cut off the period so the command table doesn't need them.
	for (str_pos = 0; cmd[str_pos] != '\0'; str_pos++)
		cmd[str_pos] = cmd[str_pos+1];

	//Look for commands.
	for (iCmd = 0; chan_cmd_table[iCmd].name[0] != '\0'; iCmd++)
	{
        if (!str_prefix (cmd, chan_cmd_table[iCmd].name))
        {
			(*chan_cmd_table[iCmd].do_fun) (ch, arg);
			tail_chain ();
			return TRUE;
        }

	}

	//No command found, send it to a channel.
	send_to_channel (ch, cmd, arg, FALSE);
	return TRUE;
}

/*
 * Channel utilities.
 */
bool can_hear (CHAR_DATA * ch, CHANNEL_DATA * chan)
{
	if (IS_NPC (ch))
		return FALSE;

	if (strstr (ch->pcdata->channels, chan->keyword) != NULL)
		return TRUE;

	return FALSE;
}

bool is_admin (CHAR_DATA * ch, CHANNEL_DATA * chan)
{
	CHANMEMBER * member;

	if (IS_NPC (ch))
		return TRUE;

	for (member = chan->lists; member != NULL; member = member->next)
		if (!str_cmp (member->name, ch->name) && member->type == MEMBER_ADMIN)
			return TRUE;

	return FALSE;
}

bool has_invite (CHAR_DATA * ch, CHANNEL_DATA * chan)
{
	if (IS_NPC (ch))
		return FALSE;

	if (strstr (ch->pcdata->invitations, chan->keyword) != NULL)
		return TRUE;

	return FALSE;
}

bool is_owner (CHAR_DATA * ch, CHANNEL_DATA * chan)
{
	if (get_channel_char (ch) == chan)
		return TRUE;

	return FALSE;
}

bool is_banned (CHAR_DATA * ch, CHANNEL_DATA * chan)
{
	CHANMEMBER * member;

	if (IS_NPC (ch))
		return TRUE;

	for (member = chan->lists; member != NULL; member = member->next)
		if (!str_cmp (member->name, ch->name) && member->type == MEMBER_BAN)
			return TRUE;

	return FALSE;
}

/*
 * Checks ch's channel subscriptions and cleans them up.
 */
void check_channels (CHAR_DATA * ch)
{
	CHANNEL_DATA * chan;
	char new_chanlist[MSL];

	if (IS_NPC (ch))
		return;
	
	//Initialize the string.
	new_chanlist[0] = '\0';

	//Check for existing channels from the list.
	for (chan = channel_list; chan != NULL; chan = chan->next)
		if (can_hear (ch, chan))
			sprintf (new_chanlist + strlen (new_chanlist), "%s ", chan->keyword);

	//Free the string.
	free_string (ch->pcdata->channels);

	//Reset it.
	if (new_chanlist == NULL)
		ch->pcdata->channels = str_dup ("");
	else
		ch->pcdata->channels = str_dup (new_chanlist);
	return;
}

/*
 * Removes ch from the channel.
 */

void leave_channel (CHAR_DATA * ch, CHANNEL_DATA * chan)
{
	CHANNEL_DATA * iChan;
	char new_chanlist[MSL];
	char new_invlist[MSL];

	if (IS_NPC (ch))
		return;
	
	//Initialize the strings...
	new_chanlist[0] = '\0';
	new_invlist[0] = '\0';

	//Loop through the channels.
	for (iChan = channel_list; iChan != NULL; iChan = iChan->next)
	{
		//If they're subscribed and it's not the channel they're leaving...
		if (can_hear (ch, iChan) && iChan != chan)
			sprintf (new_chanlist + strlen (new_chanlist), "%s ", iChan->keyword);
		//If they've been invited and it's not the channel they're leaving...
		if (has_invite (ch, iChan) && iChan != chan)
			sprintf (new_invlist + strlen (new_invlist), "%s ", iChan->keyword);
	}
	//Free the strings.
	free_string (ch->pcdata->channels);
	free_string (ch->pcdata->invitations);

	//Now reset them.
	if (new_chanlist == NULL)
		ch->pcdata->channels = str_dup ("");
	else
		ch->pcdata->channels = str_dup (new_chanlist);

	if (new_invlist == NULL)
		ch->pcdata->invitations = str_dup ("");
	else
		ch->pcdata->invitations = str_dup (new_invlist);
	return;
}


/*
 * Prevent the re-creation of a channel.
 */
bool legal_channel (char * keywd, char * name)
{
	CHANNEL_DATA * chan;

	for (chan = channel_list; chan != NULL; chan = chan->next)
	{
		//These checks have to go both ways.
		if (   !str_prefix (keywd, chan->keyword)
			|| !str_prefix (chan->keyword, keywd))
			return FALSE;

		if (   !str_prefix (name, chan->name)
			|| !str_prefix (chan->name, name))
			return FALSE;
	}

	//Doesn't look like there are problems.
	return TRUE;
}


/*
 * Returns the named channel.
 */
CHANNEL_DATA * get_channel (char * name)
{
	CHANNEL_DATA * chan;

	for (chan = channel_list; chan != NULL; chan = chan->next)
	{
		if (!str_prefix (name, chan->keyword))
			return chan;
	}
	return NULL;
}

/*
 * Returns the character's channel or NULL.
 */
CHANNEL_DATA * get_channel_char (CHAR_DATA * ch)
{
	CHANNEL_DATA * chan;

	if (IS_NPC (ch))
		return NULL;

	for (chan = channel_list; chan != NULL; chan = chan->next)
	{
		if (!str_cmp (chan->owner, ch->name))
			return chan;
	}
	return NULL;
}


/*
 * Returns the character's member data for the channel, or NULL.
 */
CHANMEMBER * get_member_data (CHAR_DATA * ch, CHANNEL_DATA * chan)
{
	CHANMEMBER * memb;

	if (IS_NPC (ch))
		return NULL;

	for (memb = chan->lists; memb != NULL; memb = memb->next)
	{
		if (!str_cmp (memb->name, ch->name))
			return memb;
	}
	return NULL;
}

/*
 * Standard recycling stuff.
 */
CHANNEL_DATA *channel_free;

CHANNEL_DATA *new_channel (void)
{
    static CHANNEL_DATA channel_zero;
    CHANNEL_DATA *channel;

    if (channel_free == NULL)
        channel = alloc_perm (sizeof (*channel));
    else
    {
        channel = channel_free;
        channel_free = channel_free->next;
    }

    *channel = channel_zero;
    VALIDATE (channel);

	channel->owner		= &str_empty[0];
	channel->name		= &str_empty[0];
	channel->topic		= &str_empty[0];
	channel->keyword	= &str_empty[0];

	return channel;
}

void free_channel (CHANNEL_DATA * channel)
{
    if (!IS_VALID (channel))
        return;

	free_string (channel->owner);
	free_string (channel->name);
	free_string (channel->topic);
	free_string (channel->keyword);
	channel->config = 0;

    INVALIDATE (channel);

    channel->next = channel_free;
    channel_free = channel;
}

CHANMEMBER *member_free;

CHANMEMBER *new_member (void)
{
    static CHANMEMBER member_zero;
    CHANMEMBER *member;

    if (member_free == NULL)
        member = alloc_perm (sizeof (*member));
    else
    {
        member = member_free;
        member_free = member_free->next;
    }

    *member = member_zero;
    VALIDATE (member);

	member->name		= &str_empty[0];

	return member;
}

void free_member (CHANMEMBER * member)
{
    if (!IS_VALID (member))
        return;

	free_string (member->name);
	member->type = 1;

    INVALIDATE (member);

    member->next = member_free;
    member_free = member;
}

/*
 * Saves all channels to the chanfile.
 */
void save_channels (void)
{
	FILE * fp;
	CHANNEL_DATA * channel;
	CHANMEMBER * member;

	if ((fp = fopen (CHAN_FILE, "w")) == NULL)
	{
		bug ("Channel file not found.", 0);
		return;
	}

	for (channel = channel_list; channel != NULL; channel = channel->next)
	{
		fprintf (fp, "#%s~\n", channel->name);
		fprintf (fp, "%s~ %s~ %s\n", channel->keyword,
					channel->owner, print_flags (channel->config));
		fprintf (fp, "%s~\n", channel->topic);

		for (member = channel->lists; member != NULL; member = member->next)
		{
			if (member->type == MEMBER_BAN)
				fprintf (fp, "B %s~\n", member->name);
			else
				fprintf (fp, "A %s~\n", member->name);
		}
		fprintf (fp, "$\n");
	}

	fprintf (fp, "END\n");
	fclose (fp);
}

/*
 * Loads all channels from the chanfile.
 */
void load_channels (void)
{
	FILE * fp;
	CHANNEL_DATA * channel;
	CHANMEMBER * member;
	int count = 0;

    if ((fp = fopen (CHAN_FILE, "r")) != NULL)
    {
        for (;;)
        {
            char letter;

            letter = fread_letter (fp);

            if (letter == '*')
            {
                fread_to_eol (fp);
                continue;
            }

            if (letter == 'E')
            {
				log_f("%d channel%s loaded.", count, count == 1 ? "" : "s");
				fclose (fp);
				return;
            }

            if (letter != '#')
            {
                bug ("load_channels: # not found.", 0);
                break;
            }

			channel				= new_channel ();			
			channel->name		= fread_string	(fp);
			channel->keyword	= fread_string	(fp);
			channel->owner		= fread_string	(fp);
			channel->config		= fread_flag	(fp);
			channel->topic		= fread_string	(fp);

			for (;;)
			{
				letter = fread_letter (fp);

				if (letter == '$')
					break;

				else if (letter == 'A')
				{
					member = new_member ();
					member->type = MEMBER_ADMIN;
					member->name = fread_string (fp);
					member->next = channel->lists;
					channel->lists = member;
				}
				else if (letter == 'B')
				{
					member = new_member ();
					member->type = MEMBER_BAN;
					member->name = fread_string (fp);
					member->next = channel->lists;
					channel->lists = member;
				}
			}

			channel->next		= channel_list;
			channel_list		= channel;
			++count;
        }
    }
	else
		bug ("Channel file not found.", 0);

	return;
}

Site Stats

Snippets: 1791
Codebases: 571