Add query functions for versions string and release revision
authorClinton Ebadi <clinton@unknownlamer.org>
Sun, 10 May 2020 21:19:34 +0000 (17:19 -0400)
committerClinton Ebadi <clinton@unknownlamer.org>
Sun, 10 May 2020 21:19:34 +0000 (17:19 -0400)
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
source/Bot.C
source/Bot.H
source/Commands.C
source/Interp.C
source/Parser.C
source/ScriptCommands.C
source/ScriptCommands.H

index eb9eadf..28c8361 100644 (file)
@@ -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 <clinton@@unknownlamer.org>\n1.97 and below by
+eb@@IRCNet <eb@@via.ecp.fr>'').
+@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? ]
index ed8aa50..868fbee 100644 (file)
@@ -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;
+}
index b57f2a8..017ff99 100644 (file)
@@ -29,6 +29,7 @@
 #include <string>
 #include <map>
 
+#include "BotThreading.H"
 #include "String.H"
 
 #define VERSION_STRING PACKAGE " version " VERSION " by unknown_lamer@FreeNode <clinton@unknownlamer.org>\n1.97 and below by eb@IRCNet <eb@via.ecp.fr>"
@@ -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;
index b1e4f5f..6632dc9 100644 (file)
@@ -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;
 }
 
index 1a96cef..91d9e73 100644 (file)
@@ -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, 
index de4d29d..50733bb 100644 (file)
@@ -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")
     {
index 502a9f8..691cda6 100644 (file)
@@ -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)
 {
index 2c725e6..5e4cfc3 100644 (file)
@@ -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);