Skip to content

Commit

Permalink
Merge branch 'regex-packet'
Browse files Browse the repository at this point in the history
  • Loading branch information
c650 committed Nov 13, 2017
2 parents 8df7762 + 602d4e2 commit c4f6638
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 52 deletions.
8 changes: 4 additions & 4 deletions src/IRCBot/include/packet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ namespace IRC {
/*
@param buf the buffer to parse into a Packet. (see the declared member variables)
*/
Packet(std::string buf) : Packet(buf, nullptr) {}
Packet(const std::string& buf) : Packet(buf, nullptr) {}

Packet(std::string buf, Server *o) : owner(o) {
Packet(const std::string& buf, Server *o) : owner(o) {
valid = _parse(buf);
}

Expand All @@ -76,7 +76,7 @@ namespace IRC {
@param msg the message to send.
*/
void reply(std::string msg) const;
void reply(const std::string& msg) const;

private:

Expand All @@ -85,7 +85,7 @@ namespace IRC {
@param buf the buffer to parse.
*/
bool _parse(std::string buf);
bool _parse(const std::string& buf);

/*
Determines type from string
Expand Down
2 changes: 1 addition & 1 deletion src/IRCBot/include/user.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace IRC {
A valid mask: "nick!realname@hostname"
*/
User(std::string mask);
User(const std::string& mask);

User(const std::string& n, const std::string& hn);
User(const std::string& n, const std::string& rn, const std::string& hn);
Expand Down
72 changes: 28 additions & 44 deletions src/IRCBot/packet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,78 +6,62 @@
#include "./include/server.hpp"
#include "./include/user.hpp"

#include <regex>

namespace IRC {

void Packet::reply(std::string msg) const {
void Packet::reply(const std::string& msg) const {
if (this->owner != nullptr)
this->owner->privmsg(this->channel, msg);
}

bool Packet::_parse(std::string buf) {
bool Packet::_parse(const std::string& buf) {

const static std::regex PACKET_REGEX{"\r?:(\\S+) (\\w+)(?:\\s+)?([^:\\s]+)? :?(.+)?\r?\n?"};

if (buf.find("!") == std::string::npos || buf.find("@") == std::string::npos) {
content = buf;
this->content = buf;
return false;
}

std::cout << "Analyzing : " << buf;

/* Get rid of \r\n */
buf.pop_back();
buf.pop_back();

size_t found = buf.find("!");
this->sender = buf.substr(1, found - 1); // gets user between : and !
buf = buf.erase(0, found+1);
std::smatch match;
if (!std::regex_search(buf, match, PACKET_REGEX)) {
std::cout << buf << " does not match.\n";
return false;
}

found = buf.find("@");
this->realname = buf.substr(0, found); // skip over ~
if (!this->realname.empty() && this->realname.front() == '~')
this->realname.erase(this->realname.begin());
buf = buf.erase(0, found+1);
try {
this->sender_user_object = User(match[1].str());

found = buf.find(" ");
this->hostname = buf.substr(0, found);
buf = buf.erase(0, found+1);
this->sender = this->sender_user_object.get_nick();
this->realname = this->sender_user_object.get_realname();
this->hostname = this->sender_user_object.get_hostname();
} catch (...) {
this->content = buf;
return false;
}

this->sender_user_object = User(this->sender, this->realname, this->hostname);
this->type = _read_type(match[2].str());

found = buf.find(" ");
this->type = _read_type(buf.substr(0, found));
buf = buf.erase(0, found+1);
this->channel = match[3].str();
this->content = match[4].str();

if (this->type == PacketType::JOIN || this->type == PacketType::PART) {
this->channel = buf.substr(this->type == PacketType::JOIN ? 1 : 0);
return true;
} else if (this->type == PacketType::NICK) {
this->content = buf.substr(1); // +1 for :
return true;
if (this->type == PacketType::JOIN || (this->type == PacketType::PART && this->channel.empty())) {
this->channel = match[4].str();
}

found = buf.find(" ");
this->channel = buf.substr(0, found);
buf = buf.erase(0, found+1);

if (this->channel.front() != '#')
this->channel = sender;

this->content = buf;

if ( !this->content.empty() && ( this->type == PacketType::PRIVMSG || this->type == PacketType::NOTICE))
this->content.erase(this->content.begin());

if (this->type == PacketType::NICK)
this->channel.erase(this->channel.begin()); // skip : in NICK messages

std::cout << "\tPacket successfully analyzed.\n";
std::cout << "\tsender: " << this->sender
std::cout << "\tPacket successfully analyzed.\n"
<< "\tsender: " << this->sender
<< "\n\trealname: " << this->realname
<< "\n\thostname: " << this->hostname
<< "\n\tchannel: " << this->channel
<< "\n\tcontent: " << this->content << '\n';

// << "\n\ttype: " << this->type

return true;
}

Expand Down
4 changes: 1 addition & 3 deletions src/IRCBot/user.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace IRC {

User::User(std::string mask) {
User::User(const std::string& mask) {

const static std::regex USER_REGEX{"([-\\w\\d]+)!([-\\w\\d]+)@([\\w\\d\\.]+)"};

Expand All @@ -25,8 +25,6 @@ namespace IRC {
realname = match[2];
hostname = match[3];

std::cout << nick << " " << realname << " " << hostname << "\n";

} catch (...) {
throw std::runtime_error(error_msg);
}
Expand Down
17 changes: 17 additions & 0 deletions src/tests/packet-regex.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <regex>
#include <iostream>

int main(void) {

const static std::regex PACKET_REGEX{"\r?:(\\S+) (\\w+)(\\s+[^:\\s]+)? :?(.+)?\r?\n?"};

std::smatch match;

std::string buf{":[email protected] PART #rickandmorty"};

std::cout << std::regex_search(buf, match, PACKET_REGEX) << "\n";

std::cout << match.str() << "\n";

return 0;
}

0 comments on commit c4f6638

Please sign in to comment.