Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##parser.cc
- #include <iostream>
- #include <string>
- using namespace std;
- /*
- #include "args.hpp"
- const int MESG_TYPE_NULL = 0;
- const int MESG_TYPE_USER = 1;
- const int MESG_TYPE_SERV = 2;
- class IRC
- {
- private:
- public:
- IRC() {};
- ~IRC() {};
- void getline(args* ref, string const line);
- };
- // interpret IRC protocol message
- void IRC::getline(args* ref, string const line)
- {
- // Types of messages:
- 1. :user@host COMMAND [params]
- 2. :user@host COMMAND :text goes here
- 3. :server.name.ext COMMAND param1 param2 .. paramN
- 4. :server.name.ext COMMAND :text goes here
- Notably identifable sections:
- Token 1 (user or server)
- Token 3 (parameter list | list of text sent to irc service)
- //
- string buffer, tempor, temp;
- int i=0, j=0, k=0, l=0;
- int endpos=0, start= 0, finis= 0;
- int next=0; args& message=(*ref);
- message.MesgType = MESG_TYPE_NULL;
- // Look for first next
- for (i=0; i < line.size() && line.at(i) != ' '; ++i)
- temp.push_back(line.at(i));
- for (i=0; i < temp.size(); ++i)
- {
- switch (temp.at(i))
- {
- // it's a nickname
- case '@':
- if (message.MesgType == MESG_TYPE_NULL) {
- message.MesgType = MESG_TYPE_USER;
- }
- break;
- // it's a server
- case '.':
- if (message.MesgType == MESG_TYPE_NULL) {
- message.MesgType = MESG_TYPE_SERV;
- }
- break;
- }
- }
- // From here on, we split into message types
- switch (message.MesgType)
- {
- case MESG_TYPE_USER:
- // loop message param list until we see ':'; ++i
- for (i=0; i < line.size() && line[i] != ':'; ++i)
- {
- if (line[i-1] == ' ') {
- start= i; finis= 0;
- }
- if (line[i+1] == ' ') {
- finis= i;
- buffer = line.substr(start, finis);
- }
- if (finis > 0) {
- cout << buffer << endl;
- buffer.clear();
- }
- // message.Params.push_back(temp.substr(start, finis));
- }
- *
- start = 0; finis = 0;
- for (i=l+1; temp[i-1] != ':' && i < temp.size(); ++i)
- {
- if (temp[i-1] == ' ' && i < temp.length())
- start = i;
- if (temp[i+1] == ' ' && i < temp.length()) {
- finis = i;
- message.Params.push_back(temp.substr(start, finis));
- }
- cout << temp.substr(start, finis) << endl;
- }
- for (i=l+1; temp[i-1] != ':' && i < temp.size(); ++i)
- start = i;
- for (i=start+1; i < temp.size(); ++i)
- message.Params.push_back(temp.substr(i, 1));
- *
- break;
- case MESG_TYPE_SERV:
- break;
- }
- }
- */
- char *irctok(string& line)
- {
- char *buffer = (char *)line.c_str();
- char array[255];
- int count = 0;
- if (buffer[0] == ':') {
- buffer++;
- }
- for (; *buffer; buffer++)
- {
- if (*buffer == ':')
- {
- array[count++] = buffer + 1;
- break;
- }
- else if (*buffer == ' ') { *buffer = 0; }
- else
- {
- array[count++] = buffer;
- if (count == size) { break; }
- for (; *buffer != ' '; buffer++) {
- if (!*buffer) { return count; }
- }
- *buffer = 0;
- }
- }
- return array;
- }
- int main()
- {
- string line(":robert_!me@r-butler.net OPER robert_ SomePaSSwORd :parameter");
- //IRC parser;
- //args message;
- //parser.getline(&message, line);
- char *parv = irctok(line);
- cout << "Src Nick: " << message[0] << endl
- << "Dst Nick: " << message[1] << endl
- << "Message : " << message[2] << endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment