view sources/parser.cpp @ 4:a7051ac7118b

added primary parser implementation renamed some functions to well-formed format bool => false, pointers => NULL removed useless eTime()
author Vlad Glagolev <enqlave@gmail.com>
date Thu, 24 Jan 2008 19:38:34 +0300
parents 19227b0b7cc1
children 9be05a31b7f9
line wrap: on
line source

/**
 * parser.cpp (2007-05-13)
 *
 * -- CABAL -- irc commands parser
 *
 * Copyright (c) 2007-2008 Vlad Glagolev <enqlave@gmail.com>
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "cabal.hpp" // required: parser.hpp

Parser::Parser(Connection *S, string parsing)
{
	if (parsing.substr(0, 6) == "PING :") {
		parsing[1] = 'O';
		S->sendMsg(parsing);
	} else {
		char *line = new char[parsing.length() + 1];

		strcpy(line, parsing.c_str());

		char *who = strtok(line, " ");
		char *what = strtok(NULL, " ");
		char *where = strtok(NULL, " ");
		char *message = strtok(NULL, "\n");

		if (strchr(who, '!')) {
			alienID = strchr(who, '!');
			alienID = alienID.substr(1);
		}

		if (strtok(who, "!"))
			alien = strtok(who, "!");

		alienAction = what;

		if (*where == ':')
			alienLocation = ++where;
		else {
			alienLocation = where;

			if (message) {
				if (alienLocation == S->nick)
					alienLocation = alien;

				alienMessage = (*message == ':') ? ++message: message;

				if (*message == CABAL->commander && ++message) {
					if (strchr(message, ' ')) {
						alienCommand = strtok(message, " ");
						alienMessage = strtok(NULL, "\n");
					} else
						alienCommand = message;
				}
			}
		}

		delete[] line;

/* DEBUG
		cout
			<< "-----------------------------------\n" \
			<< "Alien:      \t" << alien << endl \
			<< "Alien ID:\t" << alienID << endl \
			<< "Alien action:\t" << alienAction << endl \
			<< "Alien location:\t" << alienLocation << endl \
			<< "Alien command:\t" << alienCommand << endl \
			<< "Alien message:\t" << alienMessage << endl \
			<< "-----------------------------------\n";
*/	}
}

void Parser::Parse(Connection *S)
{
	if (alienCommand == "die" && alienID == "stealth@ru.raver")
		shut("Requested shutdown");
}