diff options
author | Robert Pösel <robyer@seznam.cz> | 2015-06-01 20:23:44 +0000 |
---|---|---|
committer | Robert Pösel <robyer@seznam.cz> | 2015-06-01 20:23:44 +0000 |
commit | 086db7e6bed066d76771c96803c6699fe1044c14 (patch) | |
tree | b8c7267c972928bd6ba9c399dc01cffbbd03c34f | |
parent | cd60ff3b5bf31a99c2ccafcd404adf8a0452b0a4 (diff) |
MinecraftDynmap: Use new JSONNode syntax; rework TCHAR* to char*; version bump
git-svn-id: http://svn.miranda-ng.org/main/trunk@13963 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
-rw-r--r-- | protocols/MinecraftDynmap/src/chat.cpp | 61 | ||||
-rw-r--r-- | protocols/MinecraftDynmap/src/communication.cpp | 93 | ||||
-rw-r--r-- | protocols/MinecraftDynmap/src/proto.cpp | 2 | ||||
-rw-r--r-- | protocols/MinecraftDynmap/src/proto.h | 13 | ||||
-rw-r--r-- | protocols/MinecraftDynmap/src/utils.cpp | 11 | ||||
-rw-r--r-- | protocols/MinecraftDynmap/src/utils.h | 1 | ||||
-rw-r--r-- | protocols/MinecraftDynmap/src/version.h | 2 |
7 files changed, 85 insertions, 98 deletions
diff --git a/protocols/MinecraftDynmap/src/chat.cpp b/protocols/MinecraftDynmap/src/chat.cpp index cf40209b30..c4cd050f77 100644 --- a/protocols/MinecraftDynmap/src/chat.cpp +++ b/protocols/MinecraftDynmap/src/chat.cpp @@ -21,28 +21,31 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include "stdafx.h" -void MinecraftDynmapProto::UpdateChat(const TCHAR *name, const TCHAR *message, const time_t timestamp, bool addtolog) +void MinecraftDynmapProto::UpdateChat(const char *name, const char *message, const time_t timestamp, bool addtolog) { // replace % to %% to not interfere with chat color codes - std::tstring smessage = message; - utils::text::treplace_all(&smessage, _T("%"), _T("%%")); + std::string smessage = message; + utils::text::replace_all(&smessage, "%", "%%"); + + ptrT tmessage(mir_a2t(smessage.c_str())); + ptrT tname(mir_a2t(name)); GCDEST gcd = { m_szModuleName, m_tszUserName, GC_EVENT_MESSAGE }; GCEVENT gce = { sizeof(gce), &gcd }; gce.time = timestamp; - gce.ptszText = smessage.c_str(); + gce.ptszText = tmessage; - if (name == NULL) { + if (tname == NULL) { gcd.iType = GC_EVENT_INFORMATION; - name = TranslateT("Server"); + tname = mir_tstrdup(TranslateT("Server")); gce.bIsMe = false; } - else gce.bIsMe = !mir_tstrcmp(name, this->nick_); + else gce.bIsMe = (m_nick == name); if (addtolog) gce.dwFlags |= GCEF_ADDTOLOG; - gce.ptszNick = name; + gce.ptszNick = tname; gce.ptszUID = gce.ptszNick; CallServiceSync(MS_GC_EVENT,0,reinterpret_cast<LPARAM>(&gce)); } @@ -75,7 +78,7 @@ int MinecraftDynmapProto::OnChatEvent(WPARAM, LPARAM lParam) case GC_USER_LEAVE: case GC_SESSION_TERMINATE: - nick_ = NULL; + m_nick.clear(); SetStatus(ID_STATUS_OFFLINE); break; } @@ -83,19 +86,17 @@ int MinecraftDynmapProto::OnChatEvent(WPARAM, LPARAM lParam) return 0; } -void MinecraftDynmapProto::AddChatContact(const TCHAR *name) +void MinecraftDynmapProto::AddChatContact(const char *name) { + ptrT tname(mir_a2t(name)); + GCDEST gcd = { m_szModuleName, m_tszUserName, GC_EVENT_JOIN }; GCEVENT gce = { sizeof(gce), &gcd }; gce.time = DWORD(time(0)); gce.dwFlags = GCEF_ADDTOLOG; - gce.ptszNick = name; + gce.ptszNick = tname; gce.ptszUID = gce.ptszNick; - - if (name == NULL) - gce.bIsMe = false; - else - gce.bIsMe = mir_tstrcmp(name, this->nick_); + gce.bIsMe = (m_nick == name); if (gce.bIsMe) gce.ptszStatus = _T("Admin"); @@ -105,18 +106,17 @@ void MinecraftDynmapProto::AddChatContact(const TCHAR *name) CallServiceSync(MS_GC_EVENT,0,reinterpret_cast<LPARAM>(&gce)); } -void MinecraftDynmapProto::DeleteChatContact(const TCHAR *name) +void MinecraftDynmapProto::DeleteChatContact(const char *name) { + ptrT tname(mir_a2t(name)); + GCDEST gcd = { m_szModuleName, m_tszUserName, GC_EVENT_PART }; GCEVENT gce = { sizeof(gce), &gcd }; gce.dwFlags = GCEF_ADDTOLOG; - gce.ptszNick = name; + gce.ptszNick = tname; gce.ptszUID = gce.ptszNick; gce.time = DWORD(time(0)); - if (name == NULL) - gce.bIsMe = false; - else - gce.bIsMe = mir_tstrcmp(name, this->nick_); + gce.bIsMe = (m_nick == name); CallServiceSync(MS_GC_EVENT,0,reinterpret_cast<LPARAM>(&gce)); } @@ -153,12 +153,14 @@ INT_PTR MinecraftDynmapProto::OnJoinChat(WPARAM,LPARAM suppress) return 0; } -void MinecraftDynmapProto::SetTopic(const TCHAR *topic) +void MinecraftDynmapProto::SetTopic(const char *topic) { + ptrT ttopic(mir_a2t(topic)); + GCDEST gcd = { m_szModuleName, m_tszUserName, GC_EVENT_TOPIC }; GCEVENT gce = { sizeof(gce), &gcd }; gce.time = ::time(NULL); - gce.ptszText = topic; + gce.ptszText = ttopic; CallServiceSync(MS_GC_EVENT,0, reinterpret_cast<LPARAM>(&gce)); } @@ -184,14 +186,15 @@ void MinecraftDynmapProto::SetChatStatus(int status) if (status == ID_STATUS_ONLINE) { // Load actual name from database - nick_ = db_get_tsa(NULL, m_szModuleName, MINECRAFTDYNMAP_KEY_NAME); - if (nick_ == NULL) { - nick_ = mir_tstrdup(TranslateT("You")); - db_set_ts(NULL, m_szModuleName, MINECRAFTDYNMAP_KEY_NAME, nick_); + ptrA nick(db_get_sa(NULL, m_szModuleName, MINECRAFTDYNMAP_KEY_NAME)); + if (!nick) { + nick = mir_strdup(Translate("You")); + db_set_s(NULL, m_szModuleName, MINECRAFTDYNMAP_KEY_NAME, nick); } + m_nick = nick; // Add self contact - AddChatContact(nick_); + AddChatContact(m_nick.c_str()); CallServiceSync(MS_GC_EVENT,SESSION_INITDONE,reinterpret_cast<LPARAM>(&gce)); CallServiceSync(MS_GC_EVENT,SESSION_ONLINE, reinterpret_cast<LPARAM>(&gce)); diff --git a/protocols/MinecraftDynmap/src/communication.cpp b/protocols/MinecraftDynmap/src/communication.cpp index 4845d6899c..44dc4924b7 100644 --- a/protocols/MinecraftDynmap/src/communication.cpp +++ b/protocols/MinecraftDynmap/src/communication.cpp @@ -203,29 +203,29 @@ bool MinecraftDynmapProto::doSignOn() return handleError(__FUNCTION__, "Can't load configuration", true); } - JSONROOT root(resp.data.c_str()); - if (root == NULL) + JSONNode root = JSONNode::parse(resp.data.c_str()); + if (!root) return false; /* - JSONNODE *allowchat_ = json_get(root, "allowchat"); // boolean - JSONNODE *allowwebchat_ = json_get(root, "allowwebchat"); // boolean - JSONNODE *loggedin_ = json_get(root, "loggedin"); // boolean - JSONNODE *loginEnabled_ = json_get(root, "login-enabled"); // boolean - JSONNODE *loginRequired_ = json_get(root, "webchat-requires-login"); // boolean + const JSONNode &allowchat_ = root["allowchat"]; // boolean + const JSONNode &allowwebchat_ = root["allowwebchat"]; // boolean + const JSONNode &loggedin_ = root["loggedin"]; // boolean + const JSONNode &loginEnabled_ = root["login-enabled"]; // boolean + const JSONNode &loginRequired_ = root["webchat-requires-login"]; // boolean */ - JSONNODE *title_ = json_get(root, "title"); // name of server - JSONNODE *interval_ = json_get(root, "webchat-interval"); // limit in seconds for sending messages - JSONNODE *rate_ = json_get(root, "updaterate"); // probably update rate for events request + const JSONNode &title_ = root["title"]; // name of server + const JSONNode &interval_ = root["webchat-interval"]; // limit in seconds for sending messages + const JSONNode &rate_ = root["updaterate"]; // probably update rate for events request - if (title_ == NULL || interval_ == NULL || rate_ == NULL) { + if (!title_ || !interval_ || !rate_) { return handleError(__FUNCTION__, "No title, interval or rate in configuration", true); } - m_title = json_as_pstring(title_); - m_interval = json_as_int(interval_); - m_updateRate = json_as_int(rate_); + m_title = title_.as_string(); + m_interval = interval_.as_int(); + m_updateRate = rate_.as_int(); m_cookie.clear(); if (resp.headers.find("Set-Cookie") != resp.headers.end()) { @@ -257,42 +257,40 @@ bool MinecraftDynmapProto::doEvents() if (resp.code != HTTP_CODE_OK) return handleError(__FUNCTION__, "Response is not code 200"); - JSONROOT root(resp.data.c_str()); - if (root == NULL) + JSONNode root = JSONNode::parse(resp.data.c_str()); + if (!root) return handleError(__FUNCTION__, "Invalid JSON response"); - JSONNODE *timestamp_ = json_get(root, "timestamp"); - if (timestamp_ == NULL) + const JSONNode ×tamp_ = root["timestamp"]; + if (!timestamp_) return handleError(__FUNCTION__, "Received no timestamp node"); - m_timestamp = json_as_pstring(timestamp_); + m_timestamp = timestamp_.as_string(); - JSONNODE *updates_ = json_get(root, "updates"); - if (updates_ == NULL) + const JSONNode &updates_ = root["updates"]; + if (!updates_) return handleError(__FUNCTION__, "Received no updates node"); - for (unsigned int i = 0; i < json_size(updates_); i++) { - JSONNODE *it = json_at(updates_, i); - - JSONNODE *type_ = json_get(it, "type"); - if (type_ != NULL && json_as_pstring(type_) == "chat") { - JSONNODE *time_ = json_get(it, "timestamp"); - // JSONNODE *source_ = json_get(it, "source"); // e.g. "web" - JSONNODE *playerName_ = json_get(it, "playerName"); - JSONNODE *message_ = json_get(it, "message"); + for (auto it = updates_.begin(); it != updates_.end(); ++it) { + const JSONNode &type_ = (*it)["type"]; + if (type_ && type_.as_string() == "chat") { + const JSONNode &time_ = (*it)["timestamp"]; + // const JSONNode &source_ = (*it)["source"]; // e.g. "web" + const JSONNode &playerName_ = (*it)["playerName"]; + const JSONNode &message_ = (*it)["message"]; // TODO: there are also "channel" and "account" elements - if (time_ == NULL || playerName_ == NULL || message_ == NULL) { + if (!time_ || !playerName_ || !message_) { debugLog(_T("Error: No player name, time or text for message")); continue; } - time_t timestamp = utils::time::from_string(json_as_pstring(time_)); - ptrT name(json_as_string(playerName_)); - ptrT message(json_as_string(message_)); + time_t timestamp = utils::time::from_string(time_.as_string()); + std::string name = playerName_.as_string(); + std::string message = message_.as_string(); - debugLog(_T("Received message: [%d] %s -> %s"), timestamp, name, message); - UpdateChat(name, message, timestamp); + debugLog(_T("Received message: [%d] %s -> %s"), timestamp, name.c_str(), message.c_str()); + UpdateChat(name.c_str(), message.c_str(), timestamp); } } @@ -303,25 +301,24 @@ bool MinecraftDynmapProto::doSendMessage(const std::string &message_text) { handleEntry(__FUNCTION__); - std::string data = "{\"name\":\""; - data += this->nick_; - data += "\", \"message\" : \""; - data += message_text; - data += "\"}"; + JSONNode json(JSON_NODE); + json.push_back(JSONNode("name", m_nick.c_str())); + json.push_back(JSONNode("message", message_text.c_str())); + std::string data = json.write(); http::response resp = sendRequest(MINECRAFTDYNMAP_REQUEST_MESSAGE, &data); if (resp.code == HTTP_CODE_OK) { - JSONROOT root(resp.data.c_str()); - if (root != NULL) { - JSONNODE *error_ = json_get(root, "error"); - if (error_ != NULL) { - std::string error = json_as_pstring(error_); + JSONNode root = JSONNode::parse(resp.data.c_str()); + if (root) { + const JSONNode &error_ = root["error"]; + if (error_) { + std::string error = error_.as_string(); if (error == "none") { return handleSuccess(__FUNCTION__); } else if (error == "not-allowed") { - UpdateChat(NULL, TranslateT("Message was not sent. Probably you are sending them too fast or chat is disabled completely.")); + UpdateChat(NULL, Translate("Message was not sent. Probably you are sending them too fast or chat is disabled completely.")); } } } @@ -357,7 +354,7 @@ void MinecraftDynmapProto::SignOnWorker(void*) // Load server from database ptrA str(db_get_sa(NULL, m_szModuleName, MINECRAFTDYNMAP_KEY_SERVER)); - if ((str== NULL) || *str==NULL) { + if (!str || !str[0]) { MessageBox(NULL, TranslateT("Set server address to connect."), m_tszUserName, MB_OK); SetStatus(ID_STATUS_OFFLINE); return; diff --git a/protocols/MinecraftDynmap/src/proto.cpp b/protocols/MinecraftDynmap/src/proto.cpp index 3375654557..7400223e38 100644 --- a/protocols/MinecraftDynmap/src/proto.cpp +++ b/protocols/MinecraftDynmap/src/proto.cpp @@ -59,7 +59,7 @@ MinecraftDynmapProto::MinecraftDynmapProto(const char* proto_name, const TCHAR* this->hEventsConnection = NULL; // Client instantiation - this->nick_ = NULL; + this->m_nick = ""; this->error_count_ = 0; this->chatHandle_ = NULL; this->m_updateRate = 5000; // Some default update rate diff --git a/protocols/MinecraftDynmap/src/proto.h b/protocols/MinecraftDynmap/src/proto.h index 45cc6ab6fe..c86619c80a 100644 --- a/protocols/MinecraftDynmap/src/proto.h +++ b/protocols/MinecraftDynmap/src/proto.h @@ -69,14 +69,13 @@ public: void __cdecl SendMsgWorker(void*); // Chat handling - void AddChat(const TCHAR *id,const TCHAR *name); - void UpdateChat(const TCHAR *name, const TCHAR *message, const time_t timestamp = time(NULL), bool addtochat = true); - void SendChatMessage(std::string message); - void AddChatContact(const TCHAR *nick); - void DeleteChatContact(const TCHAR *name); + void AddChat(const char *id, const char *name); + void UpdateChat(const char *name, const char *message, const time_t timestamp = time(NULL), bool addtochat = true); + void AddChatContact(const char *nick); + void DeleteChatContact(const char *name); void SetChatStatus(int); void ClearChat(); - void SetTopic(const TCHAR *topic = NULL); + void SetTopic(const char *topic); MCONTACT GetChatHandle(); // Locks @@ -123,7 +122,7 @@ public: std::string doGetPage(int); // Configuration - ptrT nick_; + std::string m_nick; std::string m_cookie; std::string m_server; std::string m_title; diff --git a/protocols/MinecraftDynmap/src/utils.cpp b/protocols/MinecraftDynmap/src/utils.cpp index 15fea63aaf..ec01628899 100644 --- a/protocols/MinecraftDynmap/src/utils.cpp +++ b/protocols/MinecraftDynmap/src/utils.cpp @@ -49,17 +49,6 @@ void utils::text::replace_all(std::string* data, const std::string &from, const } } -void utils::text::treplace_all(std::tstring* data, const std::tstring &from, const std::tstring &to) -{ - std::tstring::size_type position = 0; - - while ((position = data->find(from, position)) != std::tstring::npos) - { - data->replace(position, from.size(), to); - position++; - } -} - std::string utils::text::special_expressions_decode(std::string data) { utils::text::replace_all(&data, "\\r", "\r"); diff --git a/protocols/MinecraftDynmap/src/utils.h b/protocols/MinecraftDynmap/src/utils.h index 29a62228ab..105a9b0e0c 100644 --- a/protocols/MinecraftDynmap/src/utils.h +++ b/protocols/MinecraftDynmap/src/utils.h @@ -52,7 +52,6 @@ namespace utils { void replace_first(std::string* data, const std::string &from, const std::string &to); void replace_all(std::string* data, const std::string &from, const std::string &to); - void treplace_all(std::tstring* data, const std::tstring &from, const std::tstring &to); std::string special_expressions_decode(std::string data); std::string slashu_to_utf8(const std::string &data); std::string trim(const std::string &data); diff --git a/protocols/MinecraftDynmap/src/version.h b/protocols/MinecraftDynmap/src/version.h index d60801e52b..95afe449d1 100644 --- a/protocols/MinecraftDynmap/src/version.h +++ b/protocols/MinecraftDynmap/src/version.h @@ -1,7 +1,7 @@ #define __MAJOR_VERSION 0 #define __MINOR_VERSION 0 #define __RELEASE_NUM 0 -#define __BUILD_NUM 1 +#define __BUILD_NUM 2 #include <stdver.h> |