diff --git a/Base/res/fortunes.json b/Base/res/fortunes.json new file mode 100644 index 0000000000..b834a29f62 --- /dev/null +++ b/Base/res/fortunes.json @@ -0,0 +1,82 @@ +[ + { + "quote": "add some quotes, problem solved?", + "author": "linusg", + "utc_time": 1596660175, + "url": "https://freenode.logbot.info/serenityos/20200805#c4651652", + "context": "About a different kind of quote, but that's good enough for me! :)" + }, + { + "quote": "BenW: i'm sure it's fine lol", + "author": "kling", + "utc_time": 1605364559, + "url": "https://freenode.logbot.info/serenityos/20201114#c5825428" + }, + { + "quote": "dammit fuzzer, what madman would write '?~x/'?", + "author": "CxByte", + "utc_time": 1606653374, + "url": "https://freenode.logbot.info/serenityos/20201129#c5999293", + "context": "Fuzzers are even worse than users." + }, + { + "quote": "I'd totally implement that", + "author": "nooga", + "utc_time": 1611335335, + "url": "https://freenode.logbot.info/serenityos/20210122#c6624263" + }, + { + "quote": "No need to dereference the nullptr!", + "author": "linusg", + "utc_time": 1612014120, + "url": "https://github.com/SerenityOS/serenity/commit/5b43419a636699d71752de7cec91f6eb35ed50b5" + }, + { + "quote": "We can't not have the quotes under VC, that's a sin", + "author": "CxByte", + "utc_time": 1612035713, + "url": "https://freenode.logbot.info/serenityos/20210130#c6726388", + "context": "A quote about putting quotes in VC, in VC." + }, + { + "quote": "\"We really should start a \\\"Quotes\\\" page.\" - BenW", + "author": "kling", + "utc_time": 1612900961, + "url": "https://freenode.logbot.info/serenityos/20210209#c6854420", + "context": "Apparently I said that once too often." + }, + { + "quote": "if your port uses textrels, it likely uses other things that we don't want near serenity", + "author": "thakis", + "utc_time": 1612900918, + "url": "https://freenode.logbot.info/serenityos/20210209#c6854410", + "context": "\"jk but only a little bit jk\"" + }, + { + "quote": "I'm afraid of where pulling that string will take me.", + "author": "boricj", + "utc_time": 1612954860, + "url": "https://freenode.logbot.info/serenityos/20210210#c6866141", + "context": "C++ templates will lead you down the rabbithole." + }, + { + "quote": "$commitdescription ($user opened: The build failed.)", + "author": "SerenityBot", + "utc_time": 1613906220, + "url": "https://freenode.logbot.info/serenityos/20210221#c6989891", + "context": "The IRC notifications are a little bit harsh sometimes, especially if they all seem to spell failure." + }, + { + "quote": "There cannot be more unused bits than the entirety of the input.", + "author": "CxByte", + "utc_time": 1615188240, + "url": "https://github.com/SerenityOS/serenity/pull/5692#issue-586526857" + }, + { + "quote": "Does your code contain unexpected integer overflow? of course it does! contact BenW to find out why!", + "author": "CxByte", + "utc_time": 1615228585, + "url": "https://freenode.logbot.info/serenityos/20210308#c7177736", + "context": "Overflow-correct code is deviously hard. https://github.com/SerenityOS/serenity/commit/183b2e71ba8d85293db493cab27b8adb4af54981" + } +] diff --git a/Userland/Utilities/fortune.cpp b/Userland/Utilities/fortune.cpp new file mode 100644 index 0000000000..cbd1ee3315 --- /dev/null +++ b/Userland/Utilities/fortune.cpp @@ -0,0 +1,156 @@ +/* + * Copyright (c) 2021, Ben Wiederhake + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +class Quote { +public: + static Optional try_parse(const JsonValue& value) + { + if (!value.is_object()) + return {}; + auto entry = value.as_object(); + Quote q; + if (!entry.has("quote") || !entry.has("author") || !entry.has("utc_time") || !entry.has("url")) + return {}; + // From here on, trust that it's probably fine. + q.m_quote = entry.get("quote").as_string(); + q.m_author = entry.get("author").as_string(); + // It is sometimes parsed as u32, sometimes as u64, depending on how large the number is. + q.m_utc_time = entry.get("utc_time").to_number(); + q.m_url = entry.get("url").as_string(); + if (entry.has("context")) + q.m_context = entry.get("context").as_string(); + + return q; + } + + const String& quote() const { return m_quote; } + const String& author() const { return m_author; } + const u64& utc_time() const { return m_utc_time; } + const String& url() const { return m_url; } + const Optional& context() const { return m_context; } + +private: + Quote() = default; + + String m_quote; + String m_author; + u64 m_utc_time; + String m_url; + Optional m_context; +}; + +static Vector parse_all(const JsonArray& array) +{ + Vector quotes; + for (int i = 0; i < array.size(); ++i) { + Optional q = Quote::try_parse(array[i]); + if (!q.has_value()) { + warnln("WARNING: Could not parse quote #{}!", i); + } else { + quotes.append(q.value()); + } + } + return quotes; +} + +int main(int argc, char** argv) +{ + if (pledge("stdio rpath", nullptr) < 0) { + perror("pledge"); + return 1; + } + + const char* path = "/res/fortunes.json"; + + Core::ArgsParser args_parser; + args_parser.set_general_help("Open a fortune cookie, recieve a free quote for the day!"); + args_parser.add_positional_argument(path, "Path to JSON file with quotes (/res/fortunes.json by default)", "path", Core::ArgsParser::Required::No); + args_parser.parse(argc, argv); + + auto file = Core::File::construct(path); + if (!file->open(Core::IODevice::ReadOnly)) { + warnln("Couldn't open {} for reading: {}", path, file->error_string()); + return 1; + } + + if (pledge("stdio", nullptr) < 0) { + perror("pledge"); + return 1; + } + if (unveil(nullptr, nullptr) < 0) { + perror("unveil"); + return 1; + } + + auto file_contents = file->read_all(); + auto json = JsonValue::from_string(file_contents); + if (!json.has_value()) { + warnln("Couldn't parse {} as JSON", path); + return 1; + } + if (!json->is_array()) { + warnln("{} does not contain an array of quotes", path); + return 1; + } + + const auto quotes = parse_all(json->as_array()); + if (quotes.is_empty()) { + warnln("{} does not contain any valid quotes", path); + return 1; + } + + u32 i = arc4random_uniform(quotes.size()); + const auto& chosen_quote = quotes[i]; + auto datetime = Core::DateTime::from_timestamp(chosen_quote.utc_time()); + + outln(); // Tasteful spacing + + out("\033]8;;{}\033\\", chosen_quote.url()); // Begin link + out("\033[34m({})\033[m", datetime.to_string()); // Datetime + out(" \033[34;1m<{}>\033[m", chosen_quote.author()); // Author + out(" \033[32m{}\033[m", chosen_quote.quote()); // Quote itself + out("\033]8;;\033\\"); // End link + outln(); + + if (chosen_quote.context().has_value()) + outln("\033[38;5;242m({})\033[m", chosen_quote.context().value()); // Some context + + outln(); // Tasteful spacing + + return 0; +}