comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:bafff9de2a76
1 /**
2 * parser.cpp (2007-05-13)
3 *
4 * -- CABAL -- irc commands parser
5 *
6 * Copyright (c) 2007 Vlad Glagolev <enqlave@gmail.com>
7 * All rights reserved.
8 *
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
11 * copyright notice and this permission notice appear in all copies.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
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
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 */
21
22 #include "cabal.h" // required: parser.h
23
24 Parser::Parser(Connection *S, string parsing)
25 :
26 alien(""),
27 alienid(""),
28 alienact(""),
29 alienloc(""),
30 aliencmd(""),
31 alienmsg ("")
32 {
33 if (parsing.substr(0, 6) == "PING :") {
34 string pong = parsing;
35 pong[1] = 'O';
36 S->msgSend(pong);
37 } else {
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 }
102
103 Parser::~Parser()
104 {
105 alien = alienid = alienact = alienloc = aliencmd = alienmsg = "";
106 }
107
108 void Parser::Parse (Connection *S)
109 {
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 }