Skip to content

Commit

Permalink
move Bot configurations to configuration file
Browse files Browse the repository at this point in the history
  • Loading branch information
c650 committed Dec 27, 2016
1 parent 28e806d commit fa28612
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 15 deletions.
16 changes: 16 additions & 0 deletions sample-config.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
{
"bot" : {
"nick":"bot",
"pass":"",
"admins": [
"YOU"
]
},

"servers" : {
"Freenode" : {
"address" : "irc.freenode.net",
"port" : 6667,
"with_ssl": false
}
},

"google_search" : {
"cx": "YOUR_CX_HERE",
"key": "YOUR_API_KEY",
Expand Down
6 changes: 5 additions & 1 deletion src/bot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ namespace IRC {
}
}

void Bot::add_server(const std::string& n , const std::string& a , const int& port, bool with_ssl) {
void Bot::add_server(std::string n , const std::string& a , const int& port, bool with_ssl) {
for (const Server* s : servers) {
if (s->get_name() == n)
n.append("_");
}
servers.push_back( new Server(n , a , port, with_ssl) );
}

Expand Down
12 changes: 6 additions & 6 deletions src/include/bot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ namespace IRC {

class Bot {

enum class RELATIONSHIP {
ADMIN,
IGNORED
};

struct Command {
std::string trigger, desc;
std::function<void(const Packet&)> func;
Expand All @@ -42,10 +37,15 @@ namespace IRC {

public:

enum class RELATIONSHIP {
ADMIN,
IGNORED
};

Bot(const std::string& n, const std::string& pass, const std::string& first_admin);
~Bot();

void add_server(const std::string& n , const std::string& a , const int& port, bool with_ssl = true);
void add_server(std::string n , const std::string& a , const int& port, bool with_ssl = true);
void connect_server(const std::string& server_name);
void connect_server(const std::vector<Server*>::iterator& it);

Expand Down
25 changes: 17 additions & 8 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,23 @@
#define DEFAULT_CONFIG_PATH "./config.json"
#endif

#ifndef WITH_SSL
#define WITH_SSL true
#endif

Babbler babbler("./customize/techno_babble.txt");

int main(void) {

nlohmann::json environment;
try {
std::fstream fs(DEFAULT_CONFIG_PATH, std::fstream::in);
fs >> environment;
fs.close();
} catch(...) {
std::cerr << "Couldn't open " << DEFAULT_CONFIG_PATH << '\n';
return 1;
}


/* Simple startup. Initialize a bot with a nick, password, and admin. */
IRC::Bot b("pinetree", "", "oaktree");
IRC::Bot b(environment["bot"]["nick"].get<std::string>(), environment["bot"]["pass"].get<std::string>() , environment["bot"]["admins"].at(0).get<std::string>());

/* And here are some actions/commands you can do! */
b.on_privmsg("@sayhi", [](const IRC::Packet& packet){
Expand Down Expand Up @@ -87,9 +94,11 @@ int main(void) {


/* Add a server and connect to it by name */
/* SSL is used by default. To disable it, pass `false` in place of WITH_SSL */
b.add_server("test","irc.0x00sec.org" , 6697, WITH_SSL);
b.connect_server("test");
/* SSL is used by default. To disable it, pass `false` in place of the with_ssl arg. */
for (auto& server : environment["servers"]) {
b.add_server( server["name"].get<std::string>(), server["address"].get<std::string>() , server["port"].get<int>(), server["with_ssl"].get<bool>());
b.connect_server( server["name"].get<std::string>() );
}

/* Listen on the server(s) for input! */
b.listen();
Expand Down

0 comments on commit fa28612

Please sign in to comment.