void TS3::StartServerQueryClient() { auto serverAddress = GetRawConfigValue("Teamspeak.Server"); if (!serverAddress.empty()) _telnetClient = std::thread (&telnetClientThread, this, serverAddress); else LOG(INFO) << "No ts3server option in config, TS3 module disabled"; }
bool TS3::Init() { _nickname = GetRawConfigValue("Teamspeak.Name"); if (_nickname.empty()) _nickname = "Unseen\\svoice"; _channelID = easy_stoll(_botPtr->GetRawConfigValue("Teamspeak.Channel")); StartServerQueryClient(); return true; }
bool GithubWebhooks::InitLibeventServer() { auto portFromOptions = GetRawConfigValue("Github.Port"); std::uint16_t port = 5555; if (!portFromOptions.empty()) { try { port = std::stol(portFromOptions); } catch (std::exception &e) { LOG(WARNING) << "Invalid Github.Port in config.ini (" << e.what() << ")"; } } _httpServer = std::thread (&httpServerThread, this, port); return true; }
RSSWatcher::RSSWatcher(LemonBot *bot) : LemonHandler("rss", bot) { _feeds.init("rss"); auto updateRate = easy_stoll(GetRawConfigValue("RSS.UpdateSeconds")); if (updateRate <= 0) { LOG(WARNING) << "Invalid RSS update rate, defaulting to 1 hour"; updateRate = 60 * 60; } _updateSecondsMax = updateRate; UpdateFeeds(); _updateThread = std::thread(&UpdateThread, this); }
void TS3::telnetMessage(bufferevent *bev, void *parentPtr) { auto parent = static_cast<TS3*>(parentPtr); std::string s; char buf[1024]; int n; evbuffer *input = bufferevent_get_input(bev); while ((n = evbuffer_remove(input, buf, sizeof(buf))) > 0) s.append(buf, n); // trim CR LF if (s.size() > 2) s.erase(s.size() - 2); else LOG(ERROR) << "Received telnet line that is too short!"; // Pong messages clutter INFO log badly // LOG(INFO) << s; switch (parent->_sqState) { case TS3::State::NotConnected: // FIXME: very poor criteria for connection static const std::string welcome = "Welcome to the TeamSpeak 3 ServerQuery interface"; if (s.find(welcome) != s.npos) { LOG(INFO) << "Connected to ServerQuery interface"; parent->_sqState = TS3::State::ServerQueryConnected; evbuffer_add_printf(bufferevent_get_output(bev), "login %s %s\n", parent->GetRawConfigValue("Teamspeak.Login").c_str(), parent->GetRawConfigValue("Teamspeak.Password").c_str()); } break; case TS3::State::ServerQueryConnected: static const std::string okMsg = "error id=0 msg=ok"; if (beginsWith(s, okMsg)) { LOG(INFO) << "Authorized on TS3 server"; parent->_sqState = TS3::State::Authrozied; evbuffer_add_printf(bufferevent_get_output(bev), "use port=9987\n"); } break; case TS3::State::Authrozied: if (beginsWith(s, okMsg)) { LOG(INFO) << "Connected to Virtual Server"; parent->_sqState = TS3::State::VirtualServerConnected; evbuffer_add_printf(bufferevent_get_output(bev), "clientupdate client_nickname=%s\n", parent->_nickname.c_str()); } break; case TS3::State::VirtualServerConnected: if (beginsWith(s, okMsg)) { LOG(INFO) << "Nickname is set"; parent->_sqState = TS3::State::NickSet; evbuffer_add_printf(bufferevent_get_output(bev), "servernotifyregister event=server\n"); } break; case TS3::State::NickSet: if (beginsWith(s, okMsg)) { LOG(INFO) << "Subscribed to connects/disconnects"; parent->_sqState = TS3::State::Subscribed; evbuffer_add_printf(bufferevent_get_output(bev), "servernotifyregister event=textchannel id=%ld\n", parent->_channelID); } break; case TS3::State::Subscribed: if (beginsWith(s, "notifycliententerview")) { auto tokens = tokenize(s, ' '); try { parent->TS3Connected(tokens.at(4).substr(5), tokens.at(6).substr(16)); } catch (std::exception &e) { LOG(ERROR) << "Can't parse message: \"" << s << "\" | Exception: " << e.what(); } break; } if (beginsWith(s, "notifyclientleftview")) { auto tokens = tokenize(s, ' '); try { parent->TS3Disconnected(tokens.at(5).substr(5, tokens.at(5).size() - 5)); } catch (std::exception &e) { LOG(ERROR) << "Can't parse message: \"" << s << "\" | Exception: " << e.what(); } break; } if (beginsWith(s, "notifytextmessage")) { auto tokens = tokenize(s, ' '); try { auto nick = ReplaceTS3Spaces(tokens.at(4).substr(12)); auto message = ReplaceTS3Spaces(tokens.at(2).substr(4)); parent->TS3Message(nick, message); } catch (std::exception &e) { LOG(ERROR) << "Can't parse message: \"" << s << "\" | Exceptions: " << e.what(); } break; } break; } }