Example #1
0
int main(int argc, char **argv) {
	vector<string> args;
	for(int i = 1; i < argc; ++i)
		args.push_back(argv[i]);

	Options opts{};
	for(auto &arg : args) {
		if (arg == "--teval") {
			teval(args);
			return 0;
		}
		if(arg == "--pprint") {
			for(auto &arg2 : args)
				if(!startsWith(arg2, "--"))
					prettyPrint(arg2);
			return 0;
		}
		if(arg == "--importGoogle") {
			importGoogleNGramData();
			return 0;
		}

		if(arg == "--import") {
			opts.import = true;
			cerr << "pbrane: import mode enabled" << endl;
		} else if(arg == "--debugSQL") {
			opts.debugSQL = true;
			cerr << "pbrane: debug sql enabled" << endl;
		} else if(arg == "--debugEventSystem") {
			opts.debugEventSystem = true;
			cerr << "pbrane: debug event system enabled" << endl;
		} else if(arg == "--debugFunctionBodies") {
			opts.debugFunctionBodies = true;
			cerr << "pbrane: debug function bodies enabled" << endl;
		} else if(arg == "--importLog") {
			opts.import = true;
			opts.importLog = true;
			cerr << "pbrane: import log enabled" << endl;
		} else {
			opts.seed = fromString<unsigned int>(argv[1]);
		}
	}

	if(args.empty()) {
		random_device randomDevice;
		opts.seed = randomDevice();
	}

	Database db{config::databaseFileName};
	Bot pbrane{db, opts, Clock{}, setupBot};

	// while there is more input coming
	while(!cin.eof() && !pbrane.done) {
		// read the current line of input
		string line;
		getline(cin, line);

		if(line.find_first_not_of(" \t\r\n") == string::npos)
			continue;

		string network;
		auto ts = Clock{}.now();

		if(!opts.importLog) {
			network = line.substr(0, line.find(" "));
			line = line.substr(line.find(" ") + 1);

			if(line.find_first_not_of(" \t\r\n") == string::npos)
				continue;
		} else {
			auto fs = util::split(line, "|");
			if(fs.size() < 3) {
				cerr << "unable to parse log line" << endl;
				cerr << "line: " << line << endl;
				return 32;
			}

			ts = util::fromString<sqlite_int64>(fs[0]);
			network = fs[1];
			line = util::join(fs.begin() + 2, fs.end(), " ");
		}

		Entry entry{ts, network, line};
		pbrane.journal.upsert(entry);

		auto fields = split(line);
		if(fields.empty()) continue;

		if(entry.type == EntryType::Text) {
			auto nick = entry.nick();
			auto message = entry.arguments;
			auto target = entry.where;

			if(target == pbrane.vars.get("bot.nick").toString())
				target = nick;

			// TODO: simplify construction?
			pbrane.vars.set("nick", nick);
			pbrane.vars.set("where", target);
			pbrane.vars.set("text", message);

			// check for a special hook
			bool wasHook = false;
			if(!opts.import) {
				for(auto h : hooks)
					if((*h)({ network, message, nick, target, pbrane })) {
						wasHook = true;
						break;
					}
			}

			if(wasHook)
				entry.etype = ExecuteType::Hook;
			// if the line is a ! command, run it
			else if(message[0] == '!' && message.length() > 1) {
				// it might be a !: to force intepretation line
				if(message.size() > 1 && message[1] == ':')
					pbrane.process(network, message.substr(2), nick, target);
				else
					pbrane.process(network, message, nick, target);
				entry.etype = ExecuteType::Function;
			} else if(message.substr(0, 2) == (string)"${" && message.back() == '}') {
				pbrane.process(network, message, nick, target);
				entry.etype = ExecuteType::Function;
			} else if(message.substr(0, 2) == (string)"::") {
				pbrane.process(network, message, nick, target);
				entry.etype = ExecuteType::Function;
			}
			// otherwise, run on text triggers
			else {
				entry.etype = ExecuteType::None;

				vector<Variable> results = pbrane.events.process(EventType::Text, pbrane.vm);
				if(results.size() == 1)
					pbrane.send(network, target, results.front().toString(), true);
			}
		}
		if(entry.type == EntryType::Join) {
			auto nick = entry.nick(), where = entry.where;

			// TODO: proper environment for triggers
			pbrane.vars.set("nick", nick);
			pbrane.vars.set("where", where);
			pbrane.vars.erase("text");

			auto results = pbrane.events.process(EventType::Join, pbrane.vm);
			if(results.size() == 1)
				pbrane.send(network, where, results.front().toString(), true);
		}
		if(entry.type == EntryType::Nick) {
			;// run nick triggers
		}
		if(entry.type == EntryType::Part || entry.type == EntryType::Quit) {
			;// run leave triggers
		}

		pbrane.journal.upsert(entry);
	}

	cerr << "pbrane: exited main loop" << endl;
	return 0;
}