forked from Wirlaburla/P3NG0
fixed attention str length crash, added group on/off, removed unknown messages from whispers
This commit is contained in:
parent
1873b660b9
commit
542c2c80c7
41
src/acfile.h
41
src/acfile.h
|
@ -58,6 +58,15 @@ public:
|
||||||
void setInt(std::string name, int value) {
|
void setInt(std::string name, int value) {
|
||||||
conf[name] = std::to_string(value);
|
conf[name] = std::to_string(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void write(const char* path) {
|
||||||
|
std::ofstream outfile(path);
|
||||||
|
printf("info: writing to file %s\n", path);
|
||||||
|
for (std::map<std::string, std::string>::iterator it = conf.begin(); it != conf.end(); ++it) {
|
||||||
|
outfile << it->first << "=" << it->second << std::endl;
|
||||||
|
}
|
||||||
|
outfile.close();
|
||||||
|
}
|
||||||
private:
|
private:
|
||||||
std::map<std::string, std::string> conf;
|
std::map<std::string, std::string> conf;
|
||||||
};
|
};
|
||||||
|
@ -104,6 +113,18 @@ public:
|
||||||
if (messages.find(group) == messages.end()) return {};
|
if (messages.find(group) == messages.end()) return {};
|
||||||
return messages[group];
|
return messages[group];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void write(const char* path) {
|
||||||
|
std::ofstream outfile(path);
|
||||||
|
printf("info: writing to file %s\n", path);
|
||||||
|
for (std::map<std::string, std::vector<std::string>>::iterator mi = messages.begin(); mi != messages.end(); ++mi) {
|
||||||
|
outfile << "[" << mi->first << "]" << std::endl;
|
||||||
|
for (auto vi = mi->second.begin(); vi < mi->second.end(); mi++) {
|
||||||
|
outfile << *vi << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
outfile.close();
|
||||||
|
}
|
||||||
private:
|
private:
|
||||||
std::map<std::string, std::vector<std::string>> messages;
|
std::map<std::string, std::vector<std::string>> messages;
|
||||||
std::random_device rd;
|
std::random_device rd;
|
||||||
|
@ -133,6 +154,26 @@ public:
|
||||||
std::vector<std::string> getLines() {
|
std::vector<std::string> getLines() {
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void addLine(std::string entry) {
|
||||||
|
list.push_back(entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
void delLine(std::string entry) {
|
||||||
|
std::vector<std::string>::iterator p = std::find(list.begin(), list.end(), entry);
|
||||||
|
if (p != list.end()) {
|
||||||
|
list.erase(p);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
void write(const char* path) {
|
||||||
|
std::ofstream outfile(path);
|
||||||
|
printf("info: writing to file %s\n", path);
|
||||||
|
for (std::string value : list) {
|
||||||
|
outfile << value << std::endl;
|
||||||
|
}
|
||||||
|
outfile.close();
|
||||||
|
}
|
||||||
private:
|
private:
|
||||||
std::vector<std::string> list;
|
std::vector<std::string> list;
|
||||||
};
|
};
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
#include "drone.h"
|
#include "drone.h"
|
||||||
#include "group.h"
|
#include "group.h"
|
||||||
#include "acfile.h"
|
#include "acfile.h"
|
||||||
|
#define BUFFERSIZE 4096
|
||||||
|
|
||||||
bool debug = false;
|
bool debug = false;
|
||||||
std::random_device rd;
|
std::random_device rd;
|
||||||
|
@ -25,9 +26,7 @@ ACFile* worldlist;
|
||||||
ACFile* replylist;
|
ACFile* replylist;
|
||||||
AMFile* messages;
|
AMFile* messages;
|
||||||
ALFile* filth;
|
ALFile* filth;
|
||||||
|
ALFile* nogroup;
|
||||||
#define BUFFERSIZE 4096
|
|
||||||
#define MAXGROUPS 64
|
|
||||||
|
|
||||||
bool autoOnline = false;
|
bool autoOnline = false;
|
||||||
bool roomOnline = false;
|
bool roomOnline = false;
|
||||||
|
|
32
src/main.cpp
32
src/main.cpp
|
@ -85,6 +85,7 @@ void loadConfig() {
|
||||||
replylist = new ACFile(mainConf->getValue("replyfile", "conf/replies.conf").c_str());
|
replylist = new ACFile(mainConf->getValue("replyfile", "conf/replies.conf").c_str());
|
||||||
messages = new AMFile(mainConf->getValue("messages", "conf/messages.list").c_str());
|
messages = new AMFile(mainConf->getValue("messages", "conf/messages.list").c_str());
|
||||||
filth = new ALFile(mainConf->getValue("filthlist", "conf/filth.list").c_str());
|
filth = new ALFile(mainConf->getValue("filthlist", "conf/filth.list").c_str());
|
||||||
|
nogroup = new ALFile(mainConf->getValue("groupblocklist", "conf/nogroup.list").c_str());
|
||||||
login_username = mainConf->getValue("username", "");
|
login_username = mainConf->getValue("username", "");
|
||||||
login_password = mainConf->getValue("password", "");
|
login_password = mainConf->getValue("password", "");
|
||||||
room = mainConf->getValue("room", "GroundZero#Reception<dimension-1>");
|
room = mainConf->getValue("room", "GroundZero#Reception<dimension-1>");
|
||||||
|
@ -555,6 +556,11 @@ bool isBlacklisted(std::string name) {
|
||||||
return std::find(blacklist.begin(), blacklist.end(), name) != blacklist.end();
|
return std::find(blacklist.begin(), blacklist.end(), name) != blacklist.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool canGroup(std::string name) {
|
||||||
|
std::vector<std::string> list = nogroup->getLines();
|
||||||
|
return std::find(list.begin(), list.end(), name) == list.end();
|
||||||
|
}
|
||||||
|
|
||||||
Group* findGroupOfMember(std::string name) {
|
Group* findGroupOfMember(std::string name) {
|
||||||
for (int i = 0; i < groups.size(); i++)
|
for (int i = 0; i < groups.size(); i++)
|
||||||
if (std::find(groups[i].members.begin(), groups[i].members.end(), name) != groups[i].members.end()) {
|
if (std::find(groups[i].members.begin(), groups[i].members.end(), name) != groups[i].members.end()) {
|
||||||
|
@ -646,6 +652,16 @@ bool handleGroups(char* buffer, std::string from, std::string message) {
|
||||||
} else if (args[1] == "leave" || args[1] == "invite") {
|
} else if (args[1] == "leave" || args[1] == "invite") {
|
||||||
sprintf(buffer, mainConf->getValue("nogrp_msg", "You are not in a group.").c_str());
|
sprintf(buffer, mainConf->getValue("nogrp_msg", "You are not in a group.").c_str());
|
||||||
return true;
|
return true;
|
||||||
|
} else if (args[1] == "on" && !canGroup(toLower(from))) {
|
||||||
|
nogroup->delLine(toLower(from));
|
||||||
|
nogroup->write(mainConf->getValue("groupblocklist", "conf/nogroup.list").c_str());
|
||||||
|
sprintf(buffer, mainConf->getValue("grpon_msg", "Your groups are now enabled.").c_str());
|
||||||
|
return true;
|
||||||
|
} else if (args[1] == "off" && canGroup(toLower(from))) {
|
||||||
|
nogroup->addLine(toLower(from));
|
||||||
|
nogroup->write(mainConf->getValue("groupblocklist", "conf/nogroup.list").c_str());
|
||||||
|
sprintf(buffer, mainConf->getValue("grpoff_msg", "Your groups are now disabled.").c_str());
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (args[1] == "create") {
|
if (args[1] == "create") {
|
||||||
|
@ -661,7 +677,7 @@ bool handleGroups(char* buffer, std::string from, std::string message) {
|
||||||
return true;
|
return true;
|
||||||
} else if (args[1] == "add" && args.size() > 2) {
|
} else if (args[1] == "add" && args.size() > 2) {
|
||||||
std::string newmem = toLower(args[2]);
|
std::string newmem = toLower(args[2]);
|
||||||
if (findGroupOfMember(newmem) == nullptr) {
|
if (findGroupOfMember(newmem) == nullptr & canGroup(newmem)) {
|
||||||
activeGroup->addMember(newmem);
|
activeGroup->addMember(newmem);
|
||||||
char *whisout = new char[255];
|
char *whisout = new char[255];
|
||||||
sprintf(whisout, mainConf->getValue("leftgrp_msg", "%s added you to the group.").c_str(), from.c_str());
|
sprintf(whisout, mainConf->getValue("leftgrp_msg", "%s added you to the group.").c_str(), from.c_str());
|
||||||
|
@ -713,7 +729,7 @@ bool handlePhrase(char* buffer, std::string from, std::string message) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void processText(int *sock, std::string username, std::string message) {
|
void processText(int *sock, std::string username, std::string message) {
|
||||||
if (debug) printf("debug: received text from %s: \"%s\"\n", username.c_str(), message.c_str());
|
printf("info: received text from %s: \"%s\"\n", username.c_str(), message.c_str());
|
||||||
if (isBlacklisted(toLower(username)) || username.compare(login_username) != 0) return;
|
if (isBlacklisted(toLower(username)) || username.compare(login_username) != 0) return;
|
||||||
char *msgout = new char[BUFFERSIZE];
|
char *msgout = new char[BUFFERSIZE];
|
||||||
// We'll make a lowercase version so we can work with it without worrying about cases.
|
// We'll make a lowercase version so we can work with it without worrying about cases.
|
||||||
|
@ -722,12 +738,16 @@ void processText(int *sock, std::string username, std::string message) {
|
||||||
// Someone has requested P3NG0s attention.
|
// Someone has requested P3NG0s attention.
|
||||||
// We'll accept some variations.
|
// We'll accept some variations.
|
||||||
if ((alen = vstrcontains(lowermsg, messages->getMessages("attention"))) > 0) {
|
if ((alen = vstrcontains(lowermsg, messages->getMessages("attention"))) > 0) {
|
||||||
|
if (message.length() > alen+1) {
|
||||||
// Strip out the attention. We got it.
|
// Strip out the attention. We got it.
|
||||||
if (handleCommand(msgout, username, message.substr(alen+1, message.length()))) {
|
if (handleCommand(msgout, username, message.substr(alen+1, message.length()))) {
|
||||||
printf("info: processed command\n");
|
printf("info: processed command\n");
|
||||||
} else if (handlePhrase(msgout, username, lowermsg.substr(alen+1, lowermsg.length()))) {
|
} else if (handlePhrase(msgout, username, lowermsg.substr(alen+1, lowermsg.length()))) {
|
||||||
printf("info: processed phrase\n");
|
printf("info: processed phrase\n");
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
sprintf(msgout, messages->getMessage("greets").c_str(), username.c_str());
|
||||||
|
}
|
||||||
sendChatMessage(sock, std::string(msgout));
|
sendChatMessage(sock, std::string(msgout));
|
||||||
} else if (lowermsg == "ping") {
|
} else if (lowermsg == "ping") {
|
||||||
sprintf(msgout, mainConf->getValue("pong_msg", "Pong!").c_str());
|
sprintf(msgout, mainConf->getValue("pong_msg", "Pong!").c_str());
|
||||||
|
@ -767,7 +787,7 @@ void processText(int *sock, std::string username, std::string message) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void processWhisper(int *sock, std::string username, std::string message) {
|
void processWhisper(int *sock, std::string username, std::string message) {
|
||||||
if (debug) printf("debug: received whisper from %s: \"%s\"\n", username.c_str(), message.c_str());
|
printf("info: received whisper from %s: \"%s\"\n", username.c_str(), message.c_str());
|
||||||
if (isBlacklisted(toLower(username))) return;
|
if (isBlacklisted(toLower(username))) return;
|
||||||
std::string lowermsg = toLower(message);
|
std::string lowermsg = toLower(message);
|
||||||
char *msgout = new char[BUFFERSIZE];
|
char *msgout = new char[BUFFERSIZE];
|
||||||
|
@ -783,8 +803,6 @@ void processWhisper(int *sock, std::string username, std::string message) {
|
||||||
Group* actG = findGroupOfMember(toLower(username));
|
Group* actG = findGroupOfMember(toLower(username));
|
||||||
if (actG != nullptr) {
|
if (actG != nullptr) {
|
||||||
relayGroupMessage(actG, sock, username, message);
|
relayGroupMessage(actG, sock, username, message);
|
||||||
} else {
|
|
||||||
sprintf(msgout, messages->getMessage("unknown").c_str());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sendWhisperMessage(sock, username, msgout);
|
sendWhisperMessage(sock, username, msgout);
|
||||||
|
@ -806,7 +824,7 @@ void sendChatMessage(int *sock, std::string msg) {
|
||||||
msgout[k++] = line[l];
|
msgout[k++] = line[l];
|
||||||
msgout[k] = 0;
|
msgout[k] = 0;
|
||||||
msgout[0] = k;
|
msgout[0] = k;
|
||||||
if(debug) printf("debug: sending text \"%s\"\n", line.c_str());
|
printf("info: sending text: \"%s\"\n", line.c_str());
|
||||||
unsigned char bufmsg[255];
|
unsigned char bufmsg[255];
|
||||||
for(int l = 0; l < sizeof(msgout); l++)
|
for(int l = 0; l < sizeof(msgout); l++)
|
||||||
bufmsg[l] = msgout[l];
|
bufmsg[l] = msgout[l];
|
||||||
|
@ -835,7 +853,7 @@ void sendWhisperMessage(int *sock, std::string to, std::string msg) {
|
||||||
msgout[k++] = line[l];
|
msgout[k++] = line[l];
|
||||||
msgout[k] = 0;
|
msgout[k] = 0;
|
||||||
msgout[0] = k;
|
msgout[0] = k;
|
||||||
if(debug) printf("debug: sending whisper to '%s' \"%s\"\n", to.c_str(), line.c_str());
|
printf("info: sending whisper to '%s': \"%s\"\n", to.c_str(), line.c_str());
|
||||||
qsend(sock, *(&msgout), true);
|
qsend(sock, *(&msgout), true);
|
||||||
memset(&msgout[0], 0, sizeof(msgout));
|
memset(&msgout[0], 0, sizeof(msgout));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user