From 17f13c7ac1b386279a5da43c0fef7c2dbc11ce8a Mon Sep 17 00:00:00 2001 From: Clinton Ebadi Date: Sun, 10 May 2020 17:19:34 -0400 Subject: [PATCH] Add query functions for versions string and release revision Requested by Dale Smith and long overdue. Version string is now also accessed through a getter/setter and is protected by a mutex in case any scheme threads set the version. --- documentation/bobot++.texinfo | 13 +++++++++++++ source/Bot.C | 21 ++++++++++++++++++++- source/Bot.H | 13 ++++++++++++- source/Commands.C | 2 +- source/Interp.C | 6 +++++- source/Parser.C | 2 +- source/ScriptCommands.C | 10 ++++++++++ source/ScriptCommands.H | 2 ++ 8 files changed, 64 insertions(+), 5 deletions(-) diff --git a/documentation/bobot++.texinfo b/documentation/bobot++.texinfo index eb9eadf..28c8361 100644 --- a/documentation/bobot++.texinfo +++ b/documentation/bobot++.texinfo @@ -1597,6 +1597,19 @@ These functions allow scripts to get various bits of information. @defun bot:getserverlist @end defun +@c (0, 0, 0) +@defun bot:getversion +Return the long bot version string (same as returned by a CTCP VERSION +request, e.g. ``bobotpp version 2.3.1.1-8cd2-dirty by +unknown_lamer@@FreeNode \n1.97 and below by +eb@@IRCNet ''). +@end defun + +@c (0, 0, 0) +@defun bot:getreleasrevision +Return the bot version only (e.g. ``2.3.1.1-8cd2''). +@end defun + @c (0, 0, 0) @defun bot:flush [ Flushes the socket to the server? ] diff --git a/source/Bot.C b/source/Bot.C index ed8aa50..868fbee 100644 --- a/source/Bot.C +++ b/source/Bot.C @@ -62,6 +62,8 @@ unsigned int Bot::MAX_MESSAGES = 2; unsigned int Bot::MAX_NICKLENGTH = 9; +const std::string Bot::release_revision = VERSION; + #define DEFAULT_NICKNAME "Bobot" #define DEFAULT_USERNAME "bobot" #define DEFAULT_IRCNAME "I'm a bobot++!" @@ -103,7 +105,8 @@ Bot::Bot(String filename, bool debug_on) receivedUserhostID(0), logFileName(DEFAULT_LOGFILENAME), logs_dir (DEFAULT_LOGDIR), - configFileName (filename) + configFileName (filename), + version_mutex(true) #ifdef USESCRIPTS ,scriptLogFileName(DEFAULT_SCRIPTLOGFILENAME), autoexecFileName(DEFAULT_AUTOEXECFILENAME) @@ -782,3 +785,19 @@ Bot::set_log_dir (String dir) closedir (temp); } } + +void +Bot::set_version (String new_version) +{ + BotLock setter_lock (version_mutex); + + versionString = new_version; +} + +String +Bot::get_version () const +{ + BotLock getter_lock (version_mutex); + + return versionString; +} diff --git a/source/Bot.H b/source/Bot.H index b57f2a8..017ff99 100644 --- a/source/Bot.H +++ b/source/Bot.H @@ -29,6 +29,7 @@ #include #include +#include "BotThreading.H" #include "String.H" #define VERSION_STRING PACKAGE " version " VERSION " by unknown_lamer@FreeNode \n1.97 and below by eb@IRCNet " @@ -61,7 +62,6 @@ public: String wantedNickName; String userName; String ircName; - String versionString; String userHost; String localIP; char commandChar; @@ -110,10 +110,18 @@ public: static const std::time_t PING_TIME = 90; static const std::time_t TIMEOUT = 120; + static const std::string release_revision; + private: String logFileName; String logs_dir; String configFileName; + String versionString; + + // mutexes (one per settable variable, although a global lock on bot + // state might not be unreasonable since setting is so rare) + mutable BotMutex version_mutex; + #ifdef USESCRIPTS String scriptLogFileName; String autoexecFileName; @@ -137,6 +145,9 @@ public: void set_log_dir (String); void set_log_file (String); + String get_version () const; + void set_version (String); + friend class Parser; friend class DCCParser; friend class Person; diff --git a/source/Commands.C b/source/Commands.C index b1e4f5f..6632dc9 100644 --- a/source/Commands.C +++ b/source/Commands.C @@ -795,7 +795,7 @@ Commands::SetVersion(Bot *bot, String str) if (str.length() == 0) return InvalidParameters; - bot->versionString = str; + bot->set_version (str); return Ok; } diff --git a/source/Interp.C b/source/Interp.C index 1a96cef..91d9e73 100644 --- a/source/Interp.C +++ b/source/Interp.C @@ -264,10 +264,14 @@ interp_init_helper (void* unused) bot_new_procedure ("bot:getnickname", (SCMFunc)ScriptCommands::getNickname, 0, 0, 0); + bot_new_procedure ("bot:getreleaserevision", + (SCMFunc)ScriptCommands::getReleaseRevision, 0, 0, 0); bot_new_procedure ("bot:getserver", (SCMFunc)ScriptCommands::getServer, 0, 0, 0); - bot_new_procedure ("bot:getserverlist", + bot_new_procedure ("bot:getserverlist", (SCMFunc)ScriptCommands::getServerList, 0, 0, 0); + bot_new_procedure ("bot:getversion", + (SCMFunc)ScriptCommands::getVersion, 0, 0, 0); bot_new_procedure ("bot:flush", (SCMFunc)ScriptCommands::flushQueue, 0, 0, 0); bot_new_procedure ("bot:flushport", (SCMFunc)ScriptCommands::flushPort, diff --git a/source/Parser.C b/source/Parser.C index de4d29d..50733bb 100644 --- a/source/Parser.C +++ b/source/Parser.C @@ -863,7 +863,7 @@ Parser::parseCTCP (ServerConnection * cnx, else if (command == "VERSION") { Commands::CTCPReply (cnx->bot, nick, "VERSION", - cnx->bot->versionString); + cnx->bot->get_version ()); } else if (command == "CLOCK") { diff --git a/source/ScriptCommands.C b/source/ScriptCommands.C index 502a9f8..691cda6 100644 --- a/source/ScriptCommands.C +++ b/source/ScriptCommands.C @@ -604,6 +604,16 @@ ScriptCommands::getServerList(void) return res; } +SCM ScriptCommands::getVersion(void) +{ + return Utils::str2scm (Interp::bot->get_version ()); +} + +SCM ScriptCommands::getReleaseRevision(void) +{ + return Utils::str2scm (Interp::bot->release_revision); +} + SCM ScriptCommands::flushQueue(void) { diff --git a/source/ScriptCommands.H b/source/ScriptCommands.H index 2c725e6..5e4cfc3 100644 --- a/source/ScriptCommands.H +++ b/source/ScriptCommands.H @@ -78,8 +78,10 @@ public: static SCM Whois(SCM); static SCM getNickname(void); + static SCM getReleaseRevision(void); static SCM getServer(void); static SCM getServerList(void); + static SCM getVersion(void); static SCM flushQueue(void); static SCM flushPort(void); static SCM random(SCM); -- 2.20.1