From b6be1cdc99e08c90f8f9bc32c9e9aa0c9686c4d5 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sat, 18 Oct 2014 16:55:26 -0400 Subject: [PATCH] Hooks - Factored out the code that determines if the JSON output from a hook script is actually JSON. --- src/Hooks.cpp | 47 ++++++++++++++++++++++++++++++++++++++++------- src/Hooks.h | 1 + 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/src/Hooks.cpp b/src/Hooks.cpp index dee6da11f..f848192e3 100644 --- a/src/Hooks.cpp +++ b/src/Hooks.cpp @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include @@ -133,7 +134,7 @@ void Hooks::onLaunch () { for (line = lines.begin (); line != lines.end (); ++line) { - if (line->length () && (*line)[0] == '{') + if (isJSON (*line)) { if (_debug >= 2) context.debug ("Hook output: " + *line); @@ -149,7 +150,7 @@ void Hooks::onLaunch () else { for (line = lines.begin (); line != lines.end (); ++line) - if (line->length () && (*line)[0] != '{') + if (! isJSON (*line)) context.error (*line); throw 0; // This is how hooks silently terminate processing. @@ -215,7 +216,7 @@ void Hooks::onExit () if (_debug >= 2) context.debug ("Hook output: " + *line); - if (line->length () && (*line)[0] != '{') + if (! isJSON (*line)) { if (status == 0) context.footnote (*line); @@ -280,7 +281,7 @@ void Hooks::onAdd (std::vector & changes) if (_debug >= 2) context.debug ("Hook output: " + *line); - if (line->length () && (*line)[0] == '{') + if (isJSON (*line)) changes.push_back (Task (*line)); else context.footnote (*line); @@ -289,7 +290,7 @@ void Hooks::onAdd (std::vector & changes) else { for (line = lines.begin (); line != lines.end (); ++line) - if (line->length () && (*line)[0] != '{') + if (! isJSON (*line)) context.error (*line); throw 0; // This is how hooks silently terminate processing. @@ -359,7 +360,7 @@ void Hooks::onModify (const Task& before, std::vector & changes) if (_debug >= 2) context.debug ("Hook output: " + *line); - if (line->length () && (*line)[0] == '{') + if (isJSON (*line)) changes.push_back (Task (*line)); else context.footnote (*line); @@ -368,7 +369,7 @@ void Hooks::onModify (const Task& before, std::vector & changes) else { for (line = lines.begin (); line != lines.end (); ++line) - if (line->length () && (*line)[0] != '{') + if (! isJSON (*line)) context.error (*line); throw 0; // This is how hooks silently terminate processing. @@ -403,3 +404,35 @@ std::vector Hooks::scripts (const std::string& event) } //////////////////////////////////////////////////////////////////////////////// +bool Hooks::isJSON (const std::string& input) const +{ + if (input.length () && + input[0] == '{' && + input[input.length () - 1] == '}') + { + try + { + Task maybe (input); + return true; + } + + catch (const std::string& e) + { + if (_debug >= 1) + context.debug ("Hook output looks like JSON, but is not a valid task."); + + if (_debug >= 2) + context.debug ("JSON parser error: " + e); + } + + catch (...) + { + if (_debug >= 1) + context.debug ("Hook output looks like JSON, but fails to parse."); + } + } + + return false; +} + +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/Hooks.h b/src/Hooks.h index ae0d9d27c..e0b713c3c 100644 --- a/src/Hooks.h +++ b/src/Hooks.h @@ -51,6 +51,7 @@ public: private: std::vector scripts (const std::string&); + bool isJSON (const std::string&) const; private: bool _enabled;