File: comms.c

package info (click to toggle)
koala 0.3.2a-4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,036 kB
  • ctags: 581
  • sloc: ansic: 29,450; makefile: 1,664; xml: 11
file content (119 lines) | stat: -rw-r--r-- 2,821 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* @(#) comms.c 1.11 @(#) */
/***************************************************************\
*	Copyright (c) 1999 First Step Internet Services, Inc.
*		All Rights Reserved
*	Distributed under the BSD Licenese
*
*	Module: COMMANDS
\***************************************************************/

#define _KOALAMUD_COMMANDCOMMS_C "@(#) nitehawk@winghove.1ststep.net|lib/basecommand/comms.c|20001105031356|30753 @(#)"

#include "autoconf.h"

#include "version.h"
#include "koalatypes.h"
#include "network.h"
#include "buffer.h"
#include "commands.h"
#include "memory.h"
#include "llist.h"
#include "log.h"
#include "kparser.h"

/* Handler for 'say' command */
koalaerror do_say(pdescriptor desc, argument *arglist[])
{
	char linemods[] = " says, ''\r\n";
	listnodeptr tmplist;
	pdescriptor tmpdesc;
	char *sayline;
	char *linecur;
	int len = LINELENSTEP;
	koalaerror kerr;
	int saylen;
	char *output;

	/* Read the line from the buffer */
	sayline = kmalloc(len, ALLOC_TEMP);
	if (sayline == NULL)
	{
		logmsg(LOGCRIT, "Unable to allocate memory for line");
		return KENOMEM;
	}
	linecur = sayline;

	/* Loop until we have the entire line */
	while ((kerr = buffer_readline(desc, linecur, LINELENSTEP)) == KENOMEM)
	{
		linecur = krealloc(sayline, len + LINELENSTEP, ALLOC_TEMP);
		if (sayline == NULL)
		{
			logmsg(LOGCRIT, "Unable to allocate memory for line");
			return KENOMEM;
		}
		sayline = linecur;
		linecur = sayline + len;
		len += LINELENSTEP;
	}
	if (kerr == KENOTENOUGH)
	{
		/* No data buffered, fall out */
		kmfree(sayline, ALLOC_TEMP);
		return KESUCCESS;
	}

	/* Did we actually get something to say? */
	if (strlen(sayline) == 0)
	{
		kmfree(sayline, ALLOC_TEMP);
		buffer_queue(desc, "What do you want to say?\r\n",
				strlen("What do you want to say?\r\n"));
		return KESUCCESS;
	}

	/* Figure out how much space we need in the string we are sending out */
	saylen = strlen(linemods) + 30 /*(max player name)*/
		+ strlen(sayline);

	/* Allocate memory for the output string */
	output = kmalloc(saylen + 2, ALLOC_TEMP);
	if (output == NULL)
	{
		logmsg(LOGCRIT, "Unable to allocate memory for line");
		return KENOMEM;
	}

	/* Get master list head */
	tmplist = getdescriptorlist();
	for (; tmplist; tmplist = listnextnode(tmplist))
	{
		tmpdesc = tmplist->data.desc;

		if (tmpdesc == desc)
		{
			/* Prepare output string */
			snprintf(output, saylen + 1, "You say, '%s'\r\n",
					sayline);
		}
		else
		{
			/* Prepare output string */
			snprintf(output, saylen + 1, "%s says, '%s'\r\n",
					desc->data.character->name, sayline);
		}

		if (tmpdesc->type != DESCRIPTOR_PLAYER)
		{
			continue;
		}

		/* This descriptor is a player, send the message to them */
		buffer_queue(tmpdesc, output, saylen);
	}

	kmfree(output, ALLOC_TEMP);
	kmfree(sayline, ALLOC_TEMP);

	return KESUCCESS;
}