From 891bb6ca3ca2eacb880d82f429221595df5d132e Mon Sep 17 00:00:00 2001
From: Armin <armin.felder@osalliance.com>
Date: Tue, 26 Dec 2017 21:22:29 +0100
Subject: [PATCH] adapted dockerfile to also checkout pushercpp

---
 Dockerfile                 |  2 +-
 HandlerFactory.cpp         |  2 -
 main.cpp                   |  1 +
 models/ApplePushModel.cpp  | 92 ++++++++++++++++++++++++++------------
 models/ApplePushModel.h    |  9 ++++
 models/GooglePushModel.cpp | 70 +++++++++++++++++++++++------
 models/GooglePushModel.h   |  9 ++++
 utils.cpp                  | 20 ++++++---
 8 files changed, 154 insertions(+), 51 deletions(-)

diff --git a/Dockerfile b/Dockerfile
index ee8501b..4ca8996 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -63,7 +63,7 @@ RUN cd /proxygenSrc/proxygen/proxygen \
  
 RUN mkdir /pushGateway \
     && cd /pushGateway \
-    && git clone https://git.fairkom.net/chat/RocketChatMobilePushGateway.git \
+    && git clone --recursive https://git.fairkom.net/chat/RocketChatMobilePushGateway.git \
     && cd RocketChatMobilePushGateway \
     && cmake . \
     && make
diff --git a/HandlerFactory.cpp b/HandlerFactory.cpp
index 01337c9..f25e116 100644
--- a/HandlerFactory.cpp
+++ b/HandlerFactory.cpp
@@ -31,8 +31,6 @@ void HandlerFactory::onServerStop() noexcept{
 
 RequestHandler* HandlerFactory::onRequest(RequestHandler* requestHandler, HTTPMessage* httpMessage) noexcept{
     if(httpMessage) {
-        std::cout << "path " << httpMessage->getPath()<<std::endl;
-        std::cout<< "url "<<httpMessage->getURL()<<std::endl;
         std::vector<std::string> segments = utils::getSegments(httpMessage->getURL());
         if(segments.size()>2){
             if(segments[0] == "push"&&segments[2] == "send"){
diff --git a/main.cpp b/main.cpp
index e83bbf6..3992ef6 100644
--- a/main.cpp
+++ b/main.cpp
@@ -44,6 +44,7 @@ DEFINE_int32(threads, 0, "Number of threads to listen on. Numbers <= 0 "
 
 
 int main(int argc, char* argv[]) {
+
     gflags::ParseCommandLineFlags(&argc, &argv, true);
 
     google::InitGoogleLogging(argv[0]);
diff --git a/models/ApplePushModel.cpp b/models/ApplePushModel.cpp
index 73bf89d..7f6d4c4 100644
--- a/models/ApplePushModel.cpp
+++ b/models/ApplePushModel.cpp
@@ -20,59 +20,95 @@
 
 #include <jsoncpp/json/json.h>
 #include <iostream>
-#include <curl/curl.h>
 #include <fstream>
-#include <openssl/ssl.h>
+#include <algorithm>
 #include "ApplePushModel.h"
 
 
-ApplePushModel::ApplePushModel(const std::string &pJson):mPusher("credentials/apple/cred.pem") {
+ApplePushModel::ApplePushModel(const std::string &pJson):mPusher("./credentials/apple/cred.pem") {
+
+
     Json::Reader reader;
     Json::Value obj;
-    reader.parse(pJson, obj);
-    std::cout<<obj["token"].asString()<<std::endl;
-    if(obj.isMember("token") && obj.isMember("options")){
-        std::string token = obj["token"].asString();
-        Json::Value options = obj["options"];
-        if(options.isMember("from")&&options.isMember("badge")&&options.isMember("title")&&options.isMember("text")){
-            std::cout<<"valid json"<<std::endl;
-            mTitle = std::move(options["title"].asString());
-            mText = std::move(options["text"].asString());
-            mFrom = std::move(options["from"].asString());
+    Json::FastWriter fast;
+
+    if(pJson.length()) {
+        reader.parse(pJson, obj);
+
+        if (obj.isMember("token") && obj.isMember("options")) {
+
+            Json::Value options = obj["options"];
+
+            Json::Value apn = options["apn"];
+            std::string test(fast.write(options));
+            if (apn.isMember("text")) {
+               std::string temp = std::move(apn["text"].asString());
+                unsigned long index = 0;
+                while(true){
+                    index = temp.find('\n',index);
+                    if(index == std::string::npos){
+                        break;
+                    }
+                    temp.replace(index, 1, "\\r\\n");
+                    index += 4;
+                }
+                mApn = std::move(temp);
+
+
+            }
+            if (options.isMember("title")) {
+                mTitle = std::move(options["title"].asString());
+            }
+            if (options.isMember("text")) {
+                mText = std::move(options["text"].asString());
+            }
+            if (options.isMember("from")) {
+                mFrom = std::move(options["from"].asString());
+            }
+            if (options.isMember("badge")) {
+                mBadge = options["badge"].asInt();
+            }
+            if (options.isMember("payload")) {
+                mPayload = std::move(fast.write(options["payload"]));
+            }
+            if (options.isMember("topic")) {
+                mTopic = std::move(options["topic"].asString());
+            }
+            if (options.isMember("sound")) {
+                mSound = std::move(options["sound"].asString());
+            }
             mDeviceToken = std::move(obj["token"].asString());
-            mBadge = options["badge"].asInt();
+
         }
     }
+
 }
 
 bool ApplePushModel::sendMessage() {
 
+    std::vector<std::string> tokens;
+
     Json::FastWriter fast;
 
-    std::vector<std::string> tokens;
     tokens.push_back(mDeviceToken);
 
     PusherContent content;
 
-    Json::Value obj;
-    Json::Value msg;
-    msg["title"] = mTitle;
-    msg["body"] = mText;
-
-    std::string data = fast.write(msg);
-
+    if(mPayload.length()){
+        content.userData = "\"ejson\":"+mPayload+"";
+    }else{
+        content.userData = "";
+    }
 
     content.badge = mBadge;
-    content.content = mTitle;
-    content.userData = "\"ejson\":"+data;
+    content.content = mApn;
+    content.sound = mSound;
 
 
     mPusher.isSandBox = true;
     mPusher.pushNotification(content, tokens);
 
-    //TODO: shoud be fixed
-
-    std::string url{"https://gateway.sandbox.push.apple.com:2195"};
 
-    return false;
+    //TODO: shoud be fixed, pusherCPP does not give a return value
+    return true;
 }
diff --git a/models/ApplePushModel.h b/models/ApplePushModel.h
index 686f1a4..c695983 100644
--- a/models/ApplePushModel.h
+++ b/models/ApplePushModel.h
@@ -39,6 +39,15 @@ private:
     std::string mText;
     std::string mDeviceToken;
     std::string mFrom;
+    std::string mPayload;
+    std::string mSound;
+    std::string mApn;
+    std::string mGcm;
+    std::string mQuery;
+    bool mSent{false};
+    int mSendind{0};
+    std::string mTopic;
+
     int mBadge{0};
 };
 
diff --git a/models/GooglePushModel.cpp b/models/GooglePushModel.cpp
index dc91412..93b5319 100644
--- a/models/GooglePushModel.cpp
+++ b/models/GooglePushModel.cpp
@@ -45,18 +45,55 @@ void GooglePushModel::loadApiKey() {
 GooglePushModel::GooglePushModel(const std::string &pJson) {
     Json::Reader reader;
     Json::Value obj;
-    reader.parse(pJson, obj);
-    std::cout<<obj["token"].asString()<<std::endl;
-    if(obj.isMember("token") && obj.isMember("options")){
-        std::string token = obj["token"].asString();
-        Json::Value options = obj["options"];
-        if(options.isMember("from")&&options.isMember("badge")&&options.isMember("title")&&options.isMember("text")&&options.isMember("badge")){
-            std::cout<<"valid json"<<std::endl;
-            mTitle = std::move(options["title"].asString());
-            mText = std::move(options["text"].asString());
-            mFrom = std::move(options["from"].asString());
+    Json::FastWriter fast;
+
+    if(pJson.length()) {
+        reader.parse(pJson, obj);
+
+        if (obj.isMember("token") && obj.isMember("options")) {
+
+            Json::Value options = obj["options"];
+
+            Json::Value apn = options["gcm"];
+            std::string test(fast.write(options));
+            if (apn.isMember("text")) {
+                std::string temp = std::move(apn["text"].asString());
+                unsigned long index = 0;
+                while(true){
+                    index = temp.find('\n',index);
+                    if(index == std::string::npos){
+                        break;
+                    }
+                    temp.replace(index, 1, "\\r\\n");
+                    index += 4;
+                }
+                mApn = std::move(temp);
+
+
+            }
+            if (options.isMember("title")) {
+                mTitle = std::move(options["title"].asString());
+            }
+            if (options.isMember("text")) {
+                mText = std::move(options["text"].asString());
+            }
+            if (options.isMember("from")) {
+                mFrom = std::move(options["from"].asString());
+            }
+            if (options.isMember("badge")) {
+                mBadge = options["badge"].asInt();
+            }
+            if (options.isMember("payload")) {
+                mPayload = std::move(fast.write(options["payload"]));
+            }
+            if (options.isMember("topic")) {
+                mTopic = std::move(options["topic"].asString());
+            }
+            if (options.isMember("sound")) {
+                mSound = std::move(options["sound"].asString());
+            }
             mDeviceToken = std::move(obj["token"].asString());
-            mBadge = options["badge"].asInt();
+
         }
     }
 }
@@ -68,6 +105,9 @@ bool GooglePushModel::sendMessage() {
     Json::Value msg;
     msg["title"] = mTitle;
     msg["body"] = mText;
+    msg["message"] = mGcm;
+    msg["ejson"] = mPayload;
+    msg["msgcnt"] = mBadge;
 
     obj["to"] = mDeviceToken;
     obj["notification"] = msg;
@@ -84,7 +124,6 @@ bool GooglePushModel::sendMessage() {
 
         struct curl_slist *chunk = nullptr;
 
-        std::cout<<std::string("Authorization: key="+mApiKey)<<std::endl;
         chunk = curl_slist_append(chunk, std::string("Authorization: key="+mApiKey).c_str());
         chunk = curl_slist_append(chunk, "Content-Type: application/json");
 
@@ -98,12 +137,17 @@ bool GooglePushModel::sendMessage() {
 
 
         res = curl_easy_perform(curl);
+
         if(res != CURLE_OK){
             std::cerr<<"curl error: "<<curl_easy_strerror(res)<<std::endl;
         }else{
+            curl_easy_cleanup(curl);
+            curl_slist_free_all(chunk);
             return true;
         }
-    }
+        curl_easy_cleanup(curl);
+        curl_slist_free_all(chunk);
 
+    }
     return false;
 }
diff --git a/models/GooglePushModel.h b/models/GooglePushModel.h
index 760e327..3b3e61c 100644
--- a/models/GooglePushModel.h
+++ b/models/GooglePushModel.h
@@ -44,8 +44,17 @@ private:
     std::string mText;
     std::string mDeviceToken;
     std::string mFrom;
+    std::string mPayload;
+    std::string mSound;
+    std::string mApn;
+    std::string mGcm;
+    std::string mQuery;
+    bool mSent{false};
+    int mSendind{0};
+    std::string mTopic;
     int mBadge{0};
 
+
 };
 
 
diff --git a/utils.cpp b/utils.cpp
index f3aee94..dc1abcb 100644
--- a/utils.cpp
+++ b/utils.cpp
@@ -17,22 +17,28 @@
  * along with RocketChatMobilePushGateway. If not, see <http://www.gnu.org/licenses/>.                              *
  *                                                                                                                  *
  ********************************************************************************************************************/
+
 #include "utils.h"
 
+
 namespace utils{
     std::vector<std::string> getSegments(const std::string &pPath){
-        size_t current = 0;
-        size_t next = -1;
+        unsigned long current = 0;
+        long next = -1;
         std::vector<std::string> segments;
         do{
-            current = next +1;
-            next = pPath.find_first_of("/",current);
+            current = static_cast<size_t >(next + 1);
+            next = pPath.find_first_of('/',current);
+
             std::string segment = pPath.substr(current, next-current);
-            if(segment != "") {
-                segments.push_back(segment);
-                std::cout << segment << std::endl;
+            if(!segment.empty()) {
+                segments.push_back(std::move(segment));
             }
         }while(next != std::string::npos);
+
+
+
+
         return segments;
     }
 }
\ No newline at end of file
-- 
GitLab