Exemplo n.º 1
0
Arquivo: net.c Projeto: SlySven/tintin
void write_line_mud(struct session *ses, char *line, int size)
{
	static int retry;

	push_call("write_line_mud(%p,%p)",line,ses);

	if (ses == gts)
	{
		tintin_printf2(ses, "#NO SESSION ACTIVE. USE: %csession {name} {host} {port} TO START ONE.", gtd->tintin_char);

		pop_call();
		return;
	}

	if (!HAS_BIT(ses->flags, SES_FLAG_CONNECTED))
	{
		tintin_printf2(ses, "#THIS SESSION IS NOT CONNECTED.");

		pop_call();
		return;
	}

	if (write(ses->socket, line, size) == -1)
	{
		if (retry++ < 10)
		{
			usleep(100000);

			write_line_mud(ses, line, size);

			pop_call();
			return;
		}
		perror("write in write_line_mud");

		cleanup_session(ses);

		pop_call();
		return;
	}

	retry = 0;

	check_all_events(ses, SUB_ARG|SUB_SEC, 0, 1, "SEND OUTPUT", line);

	pop_call();
	return;
}
Exemplo n.º 2
0
struct session *connect_session(struct session *ses)
{
	int sock;

	push_call("connection_session(%p)",ses);

	ses->connect_retry = utime() + gts->connect_retry;

	reconnect:

	sock = connect_mud(ses, ses->host, ses->port);

	if (sock == -1)
	{
		cleanup_session(ses);

		pop_call();
		return NULL;
	}

	if (sock)
	{
		gtd->ses    = ses;
		ses->socket = sock;

		ses->connect_retry = 0;

		SET_BIT(ses->flags, SES_FLAG_CONNECTED);

		tintin_printf2(ses, "");

		tintin_printf(ses, "#SESSION '%s' CONNECTED TO '%s' PORT '%s'", ses->name, ses->host, ses->port);

		check_all_events(ses, SUB_ARG|SUB_SEC, 0, 4, "SESSION CONNECTED", ses->name, ses->host, ses->ip, ses->port);

		pop_call();
		return ses;
	}

	if (ses->connect_retry > utime())
	{
		goto reconnect;
	}

	if (ses->connect_error)
	{
		tintin_printf(ses, "#SESSION '%s' FAILED TO CONNECT.", ses->name);
	}

	cleanup_session(ses);

	pop_call();
	return NULL;
}
Exemplo n.º 3
0
char *get_arg_in_brackets(struct session *ses, char *string, char *result)
{
	char *pti, *pto;
	int nest = 1;

	pti = string;
	pto = result;

	if (*pti != '[')
	{
		*pto = 0;

		return pti;
	}

	pti++;

	while (*pti)
	{
		if (HAS_BIT(ses->flags, SES_FLAG_BIG5) && *pti & 128 && pti[1] != 0)
		{
			*pto++ = *pti++;
			*pto++ = *pti++;
			continue;
		}

		if (*pti == '[')
		{
			nest++;
		}
		else if (*pti == ']')
		{
			nest--;

			if (nest == 0)
			{
				break;
			}
		}
		*pto++ = *pti++;
	}

	if (*pti == 0)
	{
		tintin_printf2(NULL, "#UNMATCHED IN BRACKETS ERROR.");
	}
	else
	{
		pti++;
	}
	*pto = 0;

	return pti;
}
Exemplo n.º 4
0
void echo_command(struct session *ses, char *line)
{
	char buffer[STRING_SIZE], result[STRING_SIZE];

	if (HAS_BIT(ses->flags, SES_FLAG_SPLIT))
	{
		sprintf(buffer, "%s%s\033[0m", ses->cmd_color, line);
	}
	else
	{
		sprintf(buffer, "%s", line);
	}

	/*
		Deal with pending output
	*/

	if (ses->more_output[0])
	{
		if (ses->check_output)
		{
			strcpy(result, ses->more_output);
			ses->more_output[0] = 0;

			process_mud_output(ses, result, FALSE);
		}
	}

	DEL_BIT(ses->telopts, TELOPT_FLAG_PROMPT);

	if (HAS_BIT(ses->flags, SES_FLAG_SPLIT))
	{
		if (!HAS_BIT(ses->flags, SES_FLAG_ECHOCOMMAND))
		{
			sprintf(result, "\033[0;37m");
		}
		else
		{
			sprintf(result, "%s%s", ses->more_output, buffer);
		}
		add_line_buffer(ses, buffer, -1);

		SET_BIT(ses->flags, SES_FLAG_SCROLLSTOP);

		tintin_printf2(ses, "%s", result);

		DEL_BIT(ses->flags, SES_FLAG_SCROLLSTOP);
	}
	else
	{
		add_line_buffer(ses, buffer, -1);
	}
}
Exemplo n.º 5
0
void process_mud_output(struct session *ses, char *linebuf, int prompt) {
  char line[STRING_SIZE];

  ses->check_output = 0;

  strip_vt102_codes(linebuf, line);

  check_all_events(ses, SUB_ARG | SUB_SEC, 0, 2, "RECEIVED LINE", linebuf, line);

  if (prompt) {
    check_all_events(ses, SUB_ARG | SUB_SEC, 0, 2, "RECEIVED PROMPT", linebuf, line);
  }

  if (HAS_BIT(ses->flags, SES_FLAG_COLORPATCH)) {
    sprintf(line, "%s%s", ses->color, linebuf);

    get_color_codes(ses->color, linebuf, ses->color);

    linebuf = line;
  }

  do_one_line(linebuf, ses);    /* changes linebuf */

  /*
     Take care of gags, vt102 support still goes
   */

  if (HAS_BIT(ses->flags, SES_FLAG_GAG)) {
    strip_non_vt102_codes(linebuf, ses->more_output);

    printf("%s", ses->more_output);

    ses->more_output[0] = 0;

    DEL_BIT(ses->flags, SES_FLAG_GAG);

    return;
  }

  add_line_buffer(ses, linebuf, prompt);

  if (ses == gtd->ses) {
    printline(ses, linebuf, prompt);
  } else if (HAS_BIT(ses->flags, SES_FLAG_SNOOP)) {
    strip_vt102_codes_non_graph(linebuf, linebuf);

    tintin_printf2(gtd->ses, "[%s] %s", ses->name, linebuf);
  }
}
Exemplo n.º 6
0
void search_line_history(struct session *ses, char *line)
{
	struct listroot *root;
	int i;

	root = ses->list[LIST_HISTORY];

	for (i = root->used - 1 ; i >= 0 ; i--)
	{
		if (!strncmp(root->list[i]->left, &line[1], strlen(&line[1])))
		{
			strcpy(line, root->list[i]->left);

			return;
		}
	}
	tintin_printf2(ses, "#REPEAT: NO MATCH FOUND FOR '%s'", line);
}
Exemplo n.º 7
0
void cleanup_session(struct session *ses)
{
	push_call("cleanup_session(%p)",ses);

	if (HAS_BIT(ses->flags, SES_FLAG_CLOSED))
	{
		tintin_printf2(NULL, "\n#SESSION '%s' IS ALREADY CLOSED.", ses->name);
		dump_stack();

		pop_call();
		return;
	}

	if (ses == gtd->update)
	{
		gtd->update = ses->next;
	}

	UNLINK(ses, gts->next, gts->prev);

	if (ses->socket)
	{
		if (close(ses->socket) == -1)
		{
			syserr("close in cleanup");
		}

		// the PID is stored in the session's port.

		if (HAS_BIT(ses->flags, SES_FLAG_RUN))
		{
			kill(atoi(ses->port), SIGKILL);
		}

	}

	SET_BIT(ses->flags, SES_FLAG_CLOSED);

	if (HAS_BIT(ses->flags, SES_FLAG_CONNECTED))
	{
		check_all_events(ses, SUB_ARG|SUB_SEC, 0, 4, "SESSION DISCONNECTED", ses->name, ses->host, ses->ip, ses->port);

		tintin_printf(gtd->ses, "#SESSION '%s' DIED.", ses->name);
	}
	else
	{
		check_all_events(ses, SUB_ARG|SUB_SEC, 0, 4, "SESSION TIMED OUT", ses->name, ses->host, ses->ip, ses->port);

		tintin_printf(gtd->ses, "#SESSION '%s' TIMED OUT.", ses->name);
	}

	if (ses == gtd->ses)
	{
		gtd->ses = newactive_session();
	}

	if (ses->logfile)
	{
		fclose(ses->logfile);
	}

	if (ses->logline)
	{
		fclose(ses->logline);
	}

	LINK(ses, gtd->dispose_next, gtd->dispose_prev);

	pop_call();
	return;
}
Exemplo n.º 8
0
char *get_arg_in_braces(struct session *ses, char *string, char *result, int flag)
{
	char *pti, *pto;
	int nest = 1;

	pti = space_out(string);
	pto = result;

	if (*pti != DEFAULT_OPEN)
	{
		if (!HAS_BIT(flag, GET_ALL))
		{
			pti = get_arg_stop_spaces(ses, pti, result, flag);
		}
		else
		{
			pti = get_arg_with_spaces(ses, pti, result, flag);
		}
		return pti;
	}

	pti++;

	while (*pti)
	{
		
		if (HAS_BIT(ses->flags, SES_FLAG_BIG5) && *pti & 128 && pti[1] != 0)
		{
			*pto++ = *pti++;
			*pto++ = *pti++;
			continue;
		}

		if (*pti == DEFAULT_OPEN)
		{
			nest++;
		}
		else if (*pti == DEFAULT_CLOSE)
		{
			nest--;

			if (nest == 0)
			{
				break;
			}
		}
		*pto++ = *pti++;
	}

	if (*pti == 0)
	{
		tintin_printf2(NULL, "#Unmatched braces error!");
	}
	else
	{
		pti++;
	}
	*pto = '\0';

	return pti;
}