changeset 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 cd60300cae0b
children 0faceb076254
files sources/connection.cpp sources/core.cpp sources/engine.cpp sources/headers/network.hpp sources/headers/parser.hpp sources/network.cpp sources/parser.cpp
diffstat 7 files changed, 74 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/sources/connection.cpp	Mon Jan 21 01:17:06 2008 +0300
+++ b/sources/connection.cpp	Thu Jan 24 19:38:34 2008 +0300
@@ -19,7 +19,7 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-#include "cabal.hpp"	// required: core.h
+#include "cabal.hpp" // required: core.h
 
 Connection::Connection():
 	nick(NICK),
@@ -50,18 +50,18 @@
 	if (!connected && !connecting) {
 		cout << "CONNECTION: connecting to server: " << current->host << endl;
 
-		if (!hostResolve (current->host, &address)) {
+		if (!resolveHost(current->host, &address)) {
 			cout << "Error: unable to resolve host " << current->host << endl;
 
 			current = current->next;
-		} else if (!tcpActivate (current->port, virtualhost)) {
+		} else if (!activateTCP(current->port, virtualhost)) {
 			cout << "Error: unable to open socket\n";
 
 			return;
 		}
 	}
 
-	if (!hostOpen())
+	if (!openHost())
 		current = current->next;
 
 	if (!connected)
@@ -90,7 +90,7 @@
 	if (buffer != "") {
 		Parser *DATA = new Parser(this, buffer);
 
-		DATA->Parse (this);
+		DATA->Parse(this);
 
 		delete DATA;
 	}
--- a/sources/core.cpp	Mon Jan 21 01:17:06 2008 +0300
+++ b/sources/core.cpp	Thu Jan 24 19:38:34 2008 +0300
@@ -76,7 +76,7 @@
 {
 	servers = -1;
 
-	C = false;
+	C = NULL;
 
 	ifstream sysconfig(config, ios::in);
 
--- a/sources/engine.cpp	Mon Jan 21 01:17:06 2008 +0300
+++ b/sources/engine.cpp	Thu Jan 24 19:38:34 2008 +0300
@@ -38,15 +38,6 @@
 	return time (&t);
 }
 
-char *eTime(time_t t)
-{
-	char *nt = asctime(localtime(&t));
-
-	nt[strlen(nt) - 1] = 0;
-
-	return nt;
-}
-
 string getCountry(string c)
 {
 	if (c == "")
--- a/sources/headers/network.hpp	Mon Jan 21 01:17:06 2008 +0300
+++ b/sources/headers/network.hpp	Thu Jan 24 19:38:34 2008 +0300
@@ -43,11 +43,11 @@
 #endif
 	} address;
 
-	bool socketClose();
+	bool closeSocket();
 
-	bool hostResolve(pchar, address_type *, int inet = 0);
-	bool tcpActivate(int, pchar host = 0);
-	bool hostOpen();
+	bool openHost();
+	bool resolveHost(pchar, address_type *, int inet = 0);
+	bool activateTCP(int, pchar host = 0);
 
 	int canRead(int);
 	int canWrite(int);
--- a/sources/headers/parser.hpp	Mon Jan 21 01:17:06 2008 +0300
+++ b/sources/headers/parser.hpp	Thu Jan 24 19:38:34 2008 +0300
@@ -28,7 +28,6 @@
 {
 public:
 	Parser(Connection *, string);
-	~Parser();
 
 	void Parse(Connection *);
 
--- a/sources/network.cpp	Mon Jan 21 01:17:06 2008 +0300
+++ b/sources/network.cpp	Thu Jan 24 19:38:34 2008 +0300
@@ -19,7 +19,7 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-#include "cabal.hpp"	// required: network.hpp
+#include "cabal.hpp" // required: network.hpp
 
 Network::Network()
 {
@@ -28,7 +28,7 @@
 
 Network::~Network()
 {
-	socketClose();
+	closeSocket();
 
 	cout << "NETWORK: offline\n";
 }
@@ -88,10 +88,9 @@
 	return "";
 }
 
-bool Network::hostResolve(pchar name, struct address_type *a, int inet)
+bool Network::resolveHost(pchar name, struct address_type *a, int inet)
 {
 #ifdef ENABLE_IPV6
-
 	bool e = 0;
 
 	struct addrinfo hints, *res, *reso;
@@ -138,9 +137,7 @@
 	freeaddrinfo(reso);
 
 	return e;
-
 #else
-
 	if (inet && inet != AF_INET)
 		return 0;
 
@@ -157,11 +154,10 @@
 		memcpy(&a->sa.sin_addr.s_addr, he->h_addr_list[0], he->h_length);
 
 	return 1;
-
 #endif  // ENABLE_IPV6
 }
 
-bool Network::tcpActivate (int port, pchar host)
+bool Network::activateTCP(int port, pchar host)
 {
 	cs = socket(address.inet, SOCK_STREAM, IPPROTO_TCP);
 
@@ -171,7 +167,7 @@
 	if (host) {
 		struct address_type a;
 
-		if (!hostResolve(host, &a, address.inet)) {
+		if (!resolveHost(host, &a, address.inet)) {
 			close(cs);
 			return 0;
 		}
@@ -216,7 +212,7 @@
 	return 1;
 }
 
-bool Network::hostOpen()
+bool Network::openHost()
 {
 	int e;
 #ifdef ENABLE_IPV6
@@ -235,7 +231,7 @@
 		}
 
 		if (errno != EWOULDBLOCK && errno != EINPROGRESS && errno != EALREADY) {
-			socketClose();
+			closeSocket();
 
 			connecting = 0;
 
@@ -253,12 +249,12 @@
 	return 1;
 }
 
-bool Network::socketClose()
+bool Network::closeSocket()
 {
 	if (cs == -1)
 		return 0;
 
-	close (cs);
+	close(cs);
 
 	cs = -1;
 
--- a/sources/parser.cpp	Mon Jan 21 01:17:06 2008 +0300
+++ b/sources/parser.cpp	Thu Jan 24 19:38:34 2008 +0300
@@ -26,14 +26,64 @@
 	if (parsing.substr(0, 6) == "PING :") {
 		parsing[1] = 'O';
 		S->sendMsg(parsing);
-	} else
-		cout << "Processed line" << endl;
-}
+	} 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;
 
-Parser::~Parser()
-{
+			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");
 }