comparison sources/parser.cpp @ 2:19227b0b7cc1

.h => .hpp for the headers added year 2008 to copyright notes renamed some functions for better meaning prepared parser module for rewriting added showHelp() function some code clean up
author Vlad Glagolev <enqlave@gmail.com>
date Mon, 21 Jan 2008 01:14:10 +0300
parents bafff9de2a76
children a7051ac7118b
comparison
equal deleted inserted replaced
1:08d8cdf4fe7f 2:19227b0b7cc1
1 /** 1 /**
2 * parser.cpp (2007-05-13) 2 * parser.cpp (2007-05-13)
3 * 3 *
4 * -- CABAL -- irc commands parser 4 * -- CABAL -- irc commands parser
5 * 5 *
6 * Copyright (c) 2007 Vlad Glagolev <enqlave@gmail.com> 6 * Copyright (c) 2007-2008 Vlad Glagolev <enqlave@gmail.com>
7 * All rights reserved. 7 * All rights reserved.
8 * 8 *
9 * Permission to use, copy, modify, and distribute this software for any 9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above 10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies. 11 * copyright notice and this permission notice appear in all copies.
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 */ 20 */
21 21
22 #include "cabal.h" // required: parser.h 22 #include "cabal.hpp" // required: parser.hpp
23 23
24 Parser::Parser(Connection *S, string parsing) 24 Parser::Parser(Connection *S, string parsing)
25 :
26 alien(""),
27 alienid(""),
28 alienact(""),
29 alienloc(""),
30 aliencmd(""),
31 alienmsg ("")
32 { 25 {
33 if (parsing.substr(0, 6) == "PING :") { 26 if (parsing.substr(0, 6) == "PING :") {
34 string pong = parsing; 27 parsing[1] = 'O';
35 pong[1] = 'O'; 28 S->sendMsg(parsing);
36 S->msgSend(pong); 29 } else
37 } else { 30 cout << "Processed line" << endl;
38 char *query = new char[parsing.size() + 1];
39
40 int e = 0;
41
42 while ((query[e] = parsing[e]))
43 e++;
44
45 char *who = new char[e];
46 char *what = new char[e];
47 char *where = new char[e];
48 char *message = new char[e];
49
50 if (sscanf (query, "%s %s %s", who, what, where) == 3) {
51 alienact = what;
52
53 if ((alienid = strchr (who, '!'))) {
54 alienid++, alien = strtok (who, "!");
55
56 if (*where == ':')
57 alienloc = where + 1;
58 else {
59 alienloc = where;
60
61 if ((aliencmd = strchr (strstr (query, alienloc), ' ')) \
62 && *++aliencmd == ':') {
63 if (!strcmp (alienloc, S->nick))
64 alienloc = alien;
65
66 if (*++aliencmd == CABAL->commander) {
67 if ((alienmsg = strchr (aliencmd, ' ')))
68 alienmsg++;
69 else
70 alienmsg = "";
71
72 aliencmd = strtok (aliencmd, " "), aliencmd++;
73 }
74 else
75 alienmsg = aliencmd, aliencmd = "";
76 }
77 else
78 alienmsg = strtok (aliencmd, " "), aliencmd = "";
79 }
80 }
81 else
82 alienid = "";
83 /* DEBUG
84 cout
85 << "-----------------------------------\n" \
86 << "Alien: \t" << alien << endl \
87 << "Alien ID:\t" << alienid << endl \
88 << "Alien action:\t" << alienact << endl \
89 << "Alien location:\t" << alienloc << endl \
90 << "Alien command:\t" << aliencmd << endl \
91 << "Alien message:\t" << alienmsg << endl \
92 << "-----------------------------------\n";
93 */ }
94
95 delete[] who;
96 delete[] what;
97 delete[] where;
98 delete[] message;
99 delete[] query;
100 }
101 } 31 }
102 32
103 Parser::~Parser() 33 Parser::~Parser()
104 { 34 {
105 alien = alienid = alienact = alienloc = aliencmd = alienmsg = "";
106 } 35 }
107 36
108 void Parser::Parse (Connection *S) 37 void Parser::Parse(Connection *S)
109 { 38 {
110 if (!strcmp (aliencmd, "die") && !strcmp (alienid, "stealth@ru.raver"))
111 shut ("Requested shutdown");
112 else if (!strcmp (aliencmd, "hi"))
113 S->msgSend ("PRIVMSG " + (string)alienloc + " :" + "Hello, " + \
114 (string)alien + " (" + (string)alienid + ")");
115 else if (!strcmp (aliencmd, "nick"))
116 S->ircNick (alienmsg);
117 else if (!strcmp (aliencmd, "away"))
118 S->ircAway (alienmsg);
119 else if (!strcmp (aliencmd, "join"))
120 S->ircJoin (alienmsg, "");
121 else if (!strcmp (aliencmd, "part"))
122 S->ircPart (alienmsg);
123 else if (!strcmp (aliencmd, "cycle") && strcmp (alienloc, S->nick))
124 S->ircCycle (alienloc);
125 else if (!strcmp (alienact, "KICK") && !strcmp (alienmsg, S->nick))
126 S->ircJoin (alienloc, "");
127 } 39 }