diff sources/parser.cpp @ 0:bafff9de2a76

Initial commit since summer'07
author Vlad Glagolev <enqlave@gmail.com>
date Sun, 20 Jan 2008 19:25:25 +0300
parents
children 19227b0b7cc1
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sources/parser.cpp	Sun Jan 20 19:25:25 2008 +0300
@@ -0,0 +1,127 @@
+/**
+ * parser.cpp (2007-05-13)
+ *
+ * -- CABAL -- irc commands parser
+ *
+ * Copyright (c) 2007 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.h"	// required: parser.h
+
+Parser::Parser(Connection *S, string parsing)
+	:
+	alien(""),
+	alienid(""),
+	alienact(""),
+	alienloc(""),
+	aliencmd(""),
+	alienmsg ("")
+{
+	if (parsing.substr(0, 6) == "PING :") {
+		string pong = parsing;
+		pong[1] = 'O';
+		S->msgSend(pong);
+	} else {
+		char *query = new char[parsing.size() + 1];
+
+		int e = 0;
+
+		while ((query[e] = parsing[e]))
+			e++;
+
+		char *who = new char[e];
+		char *what = new char[e];
+		char *where = new char[e];
+		char *message = new char[e];
+
+		if (sscanf (query, "%s %s %s", who, what, where) == 3) {
+			alienact = what;
+
+			if ((alienid = strchr (who, '!'))) {
+				alienid++, alien = strtok (who, "!");
+
+				if (*where == ':')
+					alienloc = where + 1;
+				else {
+					alienloc = where;
+
+					if ((aliencmd = strchr (strstr (query, alienloc), ' ')) \
+						&& *++aliencmd == ':') {
+						if (!strcmp (alienloc, S->nick))
+							alienloc = alien;
+
+						if (*++aliencmd == CABAL->commander) {
+							if ((alienmsg = strchr (aliencmd, ' ')))
+								alienmsg++;
+							else
+								alienmsg = "";
+
+							aliencmd = strtok (aliencmd, " "), aliencmd++;
+						}
+						else
+							alienmsg = aliencmd, aliencmd = "";
+					}
+					else
+						alienmsg = strtok (aliencmd, " "), aliencmd = "";
+				}
+			}
+			else
+				alienid = "";
+/* DEBUG
+			cout
+				<< "-----------------------------------\n" \
+				<< "Alien:	\t" << alien << endl \
+				<< "Alien ID:\t" << alienid << endl \
+				<< "Alien action:\t" << alienact << endl \
+				<< "Alien location:\t" << alienloc << endl \
+				<< "Alien command:\t" << aliencmd << endl \
+				<< "Alien message:\t" << alienmsg << endl \
+				<< "-----------------------------------\n";
+*/		}
+
+		delete[] who;
+		delete[] what;
+		delete[] where;
+		delete[] message;
+		delete[] query;
+	}
+}
+
+Parser::~Parser()
+{
+	alien = alienid = alienact = alienloc = aliencmd = alienmsg = "";
+}
+
+void Parser::Parse (Connection *S)
+{
+	if (!strcmp (aliencmd, "die") && !strcmp (alienid, "stealth@ru.raver"))
+		shut ("Requested shutdown");
+	else if (!strcmp (aliencmd, "hi"))
+		S->msgSend ("PRIVMSG " + (string)alienloc + " :" + "Hello, " + \
+			(string)alien + " (" + (string)alienid + ")");
+	else if (!strcmp (aliencmd, "nick"))
+		S->ircNick (alienmsg);
+	else if (!strcmp (aliencmd, "away"))
+		S->ircAway (alienmsg);
+	else if (!strcmp (aliencmd, "join"))
+		S->ircJoin (alienmsg, "");
+	else if (!strcmp (aliencmd, "part"))
+		S->ircPart (alienmsg);
+	else if (!strcmp (aliencmd, "cycle") && strcmp (alienloc, S->nick))
+		S->ircCycle (alienloc);
+	else if (!strcmp (alienact, "KICK") && !strcmp (alienmsg, S->nick))
+		S->ircJoin (alienloc, "");
+}