| 1 | #!/usr/local/bin/perl | ||
| 2 | package axmud; | ||
| 3 | |||
| 4 | # Copyright (C) 2011-2024 A S Lewis | ||
| 5 | # | ||
| 6 | # This program is free software: you can redistribute it and/or modify it under the terms of the GNU | ||
| 7 | # General Public License as published by the Free Software Foundation, either version 3 of the | ||
| 8 | # License, or (at your option) any later version. | ||
| 9 | # | ||
| 10 | # This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without | ||
| 11 | # even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 12 | # General Public License for more details. | ||
| 13 | # | ||
| 14 | # You should have received a copy of the GNU General Public License along with this program. If not, | ||
| 15 | # see <http://www.gnu.org/licenses/>. | ||
| 16 | # | ||
| 17 | # | ||
| 18 | # baxmud.pl | ||
| 19 | # Axmud - a Multi-User Dungeon (MUD) client written in Perl5 / Gtk3 | ||
| 20 | # | ||
| 21 | # Most users should use the axmud.pl script. Visually-impaired users might prefer this script | ||
| 22 | # instead, as it automatically enables features for those users, such as built-in text-to-speech | ||
| 23 | # engine support | ||
| 24 | |||
| 25 | use strict; | ||
| 26 | #use diagnostics; | ||
| 27 | use warnings; | ||
| 28 | |||
| 29 | use Glib qw(TRUE FALSE); | ||
| 30 | |||
| 31 | # Minimum standards for Perl | ||
| 32 | require 5.008; | ||
| 33 | |||
| 34 | # Set global variables | ||
| 35 | use vars qw( | ||
| 36 | $SCRIPT $VERSION $DATE $NAME_SHORT $NAME_ARTICLE $BASIC_NAME $BASIC_ARTICLE $BASIC_VERSION | ||
| 37 | $AUTHORS $COPYRIGHT $URL $DESCRIP $NAME_FILE @COMPAT_FILE_LIST @COMPAT_DIR_LIST @COMPAT_EXT_LIST | ||
| 38 | $BLIND_MODE_FLAG $SAFE_MODE_FLAG $TEST_MODE_FLAG @TEST_MODE_LOGIN_LIST $TEST_MODE_CMD_FLAG | ||
| 39 | $TEST_TERM_MODE_FLAG $TEST_GLOB_MODE_FLAG $TEST_REGEX_FLAG $TEST_REGEX_ERROR | ||
| 40 | $TEST_PRE_CONFIG_FLAG $TEST_CTRL_SEQ_FLAG $TEST_MODEL_FLAG $TEST_MODEL_TIME $DEFAULT_ROOM | ||
| 41 | $DEFAULT_EXIT @LICENSE_LIST @CREDIT_LIST $NO_SSL_FLAG $TOP_DIR $SHARE_DIR $DEFAULT_DATA_DIR | ||
| 42 | $DATA_DIR $CLIENT | ||
| 43 | ); | ||
| 44 | |||
| 45 | $SCRIPT = 'Axmud'; # Name used in system messages | ||
| 46 | $VERSION = '2.0.002'; # Version number for this client | ||
| 47 | $DATE = '21 Jan 2024'; | ||
| 48 | $NAME_SHORT = 'axmud'; # Lower-case version of $SCRIPT; same as the package name above | ||
| 49 | $NAME_ARTICLE = 'an Axmud'; # Name with an article | ||
| 50 | $BASIC_NAME = 'Axbasic'; # Name of Axmud's built-in scripting library | ||
| 51 | $BASIC_ARTICLE = 'an Axbasic'; # Name with an article | ||
| 52 | $BASIC_VERSION = '1.005'; # Version number for the Axbasic library | ||
| 53 | $AUTHORS = 'A S Lewis'; | ||
| 54 | $COPYRIGHT = 'Copyright 2011-2024 A S Lewis'; | ||
| 55 | $URL = 'http://axmud.sourceforge.io/'; | ||
| 56 | $DESCRIP = 'A modern MUD client for MS Windows, Linux and *BSD'; | ||
| 57 | |||
| 58 | # Name used in headers of Axmud config/data files | ||
| 59 | $NAME_FILE = 'axmud'; | ||
| 60 | # Names used in all versions of Axmud, past and present. Firstly, a list of script names used in the | ||
| 61 | # headers to Axmud config/data files | ||
| 62 | @COMPAT_FILE_LIST = ('axmud', 'amud-client'); | ||
| 63 | # Secondly, a list of partial data directory names used in all versions of Axmud (the actual | ||
| 64 | # directory name adds '-data' to each string; see the setting of $DATA_DIR below) | ||
| 65 | @COMPAT_DIR_LIST = ('axmud', 'amud'); | ||
| 66 | # Thirdly, a list of file extensions for Axmud data files (but not config files) | ||
| 67 | @COMPAT_EXT_LIST = ('axm', 'amd'); | ||
| 68 | |||
| 69 | # Axmud blind mode: if this flag is TRUE, when Axmud starts the first time, it will do so with | ||
| 70 | # settings optimised for users with a visual impairment. Auto-disabled when test mode is enabled | ||
| 71 | $BLIND_MODE_FLAG = TRUE; | ||
| 72 | # Axmud safe mode: this flag is (briefly) set to TRUE whenever GA::Session->perlCmd tries to execute | ||
| 73 | # some arbitrary Perl code using the Safe module. In that situation, the error-trapping code below | ||
| 74 | # won't try to call GA::Client->writePerlError (which produces a load of extra errors) | ||
| 75 | $SAFE_MODE_FLAG = FALSE; | ||
| 76 | |||
| 77 | # Axmud test mode: if this flag is TRUE, when Axmud starts it automatically connects and logs in to | ||
| 78 | # the a world which is assumed to be running on your local system, without first opening the | ||
| 79 | # Connections window. Auto-disabled when blind mode is enabled | ||
| 80 | $TEST_MODE_FLAG = FALSE; | ||
| 81 | # If $TEST_MODE_FLAG is TRUE, the login details to use. Must be in the form | ||
| 82 | # (world_name, host, port, username, password, online_flag) | ||
| 83 | @TEST_MODE_LOGIN_LIST = (); | ||
| 84 | # If $TEST_MODE_FLAG is TRUE and this flag is also TRUE, GA::Session->start executes the ;test | ||
| 85 | # command as soon as the session starts | ||
| 86 | $TEST_MODE_CMD_FLAG = FALSE; | ||
| 87 | # If $TEST_TERM_MODE_FLAG is TRUE, all text received from the world (except out-of-bounds text) is | ||
| 88 | # written to the terminal, with non-printable characters like ESC written as <27> | ||
| 89 | $TEST_TERM_MODE_FLAG = FALSE; | ||
| 90 | # Glob test mode: In earlier Axmud versions, saving of data files failed (and Axmud crashed) because | ||
| 91 | # of infinite recursions with two Perl objects referencing each other. If TRUE, every save file | ||
| 92 | # operation (not including the config file) tests data for this problem, before saving it, writing | ||
| 93 | # the output to the terminal | ||
| 94 | $TEST_GLOB_MODE_FLAG = FALSE; | ||
| 95 | # Regex test mode: $TEST_REGEX_FLAG is set to TRUE by GA::Client->regexCheck, shortly before it | ||
| 96 | # tests a regex. If the regex is invalid, the Perl error/warning message is intercepted and stored | ||
| 97 | # in $TEST_REGEX_ERROR, so that GA::Client->regexCheck can detect it | ||
| 98 | $TEST_REGEX_FLAG = FALSE; | ||
| 99 | $TEST_REGEX_ERROR = undef; | ||
| 100 | # Pre-configured world test mode: When preparing for a release, the authors set this flag to TRUE to | ||
| 101 | # stop Axmud complaining about missing pre-configured worlds | ||
| 102 | $TEST_PRE_CONFIG_FLAG = FALSE; | ||
| 103 | # Simple telnet mode: If $TEST_CTRL_SEQ_FLAG is TRUE, VT100 control sequences (except colour/style | ||
| 104 | # sequences) are ignored (equivalent to GA::Client->useCtrlSeqFlag being FALSE) | ||
| 105 | $TEST_CTRL_SEQ_FLAG = FALSE; | ||
| 106 | # Automatic world model test mode: If $TEST_MODEL_FLAG is true, various parts of the code run a | ||
| 107 | # silent world model test from time to time, displaying output only if the test fails | ||
| 108 | $TEST_MODEL_FLAG = FALSE; | ||
| 109 | # The time of the last world model test (matches GA::Session->sessionTime) | ||
| 110 | $TEST_MODEL_TIME = 0; | ||
| 111 | |||
| 112 | # Default room object (GA::ModelObj::Room) and default exit object (GA::Obj::Exit) used to provide | ||
| 113 | # default values for IVs. (In order to reduce the size of the world model as much as possible, | ||
| 114 | # many IVs in room/exit objects don't actually exist in the blessed reference until they're given | ||
| 115 | # a non-default value) | ||
| 116 | $DEFAULT_ROOM = undef; | ||
| 117 | $DEFAULT_EXIT = undef; | ||
| 118 | |||
| 119 | # Licence and credits | ||
| 120 | @LICENSE_LIST = ( | ||
| 121 | 'This program is free software; you can redistribute it and/or modify it under', | ||
| 122 | 'the terms of the GNU General Public License as published by the Free Software', | ||
| 123 | 'Foundation; either version 3 of the License, or (at your option) any later', | ||
| 124 | 'version.', | ||
| 125 | ' ', | ||
| 126 | 'This program is distributed in the hope that it will be useful, but WITHOUT ANY', | ||
| 127 | 'WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A', | ||
| 128 | 'PARTICULAR PURPOSE. See the GNU General Public License for more details.', | ||
| 129 | ' ', | ||
| 130 | 'You should have received a copy of the GNU General Public License along with', | ||
| 131 | 'this program. If see <http://www.gnu.org/licenses/>', | ||
| 132 | ); | ||
| 133 | |||
| 134 | @CREDIT_LIST = ( | ||
| 135 | 'Axbasic based on Language::Basic by Amir Karger', | ||
| 136 | 'Binomial heap code copied (unmodified) from Heap::Binomial by John Macdonald', | ||
| 137 | 'Chat task based on Kildclient plugin by Eduardo M Kalinowski', | ||
| 138 | 'Pathfinding algorithms based on AI::Pathfinding::AStar by Aaron Dalton', | ||
| 139 | 'Roman numeral conversion based on Text::Roman by Stanislaw Pusep', | ||
| 140 | 'Simple list code based on Gtk3::SimpleList by Thierry Vignaud', | ||
| 141 | 'Telnet code based on Net::Telnet by Jay Rogers', | ||
| 142 | 'Window manager control code based on X11::WMCtrl by Gavin Brown', | ||
| 143 | 'Images/icons by Dave Stokes, www.fatcow.com and A S Lewis. License information', | ||
| 144 | ' and full attributions can be found in ../share/images/COPYING and', | ||
| 145 | ' ../share/icons/COPYING', | ||
| 146 | 'Sound by KevanGC, AirMan, AngryFlash, battlestar10, Brandondorf, Cam Martinez,', | ||
| 147 | ' Christopher, Conor, Daniel Simon, DrumM8, G-rant, Grant Evans, Grandpa,', | ||
| 148 | ' J Blow, J Bravo, Kevan, KevanGC, Lisa Redfern, Maximilien, Mike Koenig,', | ||
| 149 | ' Muska666, Pool Shot, PsychoBird, RA The Sun God, Ragdoll485, Samantha Enrico,', | ||
| 150 | ' Simon Craggs, Snore Man, Sonidor, Sound Explorer, Stephan, Sweeper, tamskp,', | ||
| 151 | ' Tim Fryer, Vladimir, Willem Hunt and Yannick Lemieux. License information and', | ||
| 152 | ' full attributions can be found in ../share/items/sounds/COPYING', | ||
| 153 | 'Documentation and help files by A S Lewis. Licence information can be found in', | ||
| 154 | ' ../share/docs/COPYING and ../share/help/COPYING', | ||
| 155 | ); | ||
| 156 | |||
| 157 | # External dependencies (Glib is commented out as it's already been used) | ||
| 158 | use Archive::Extract; | ||
| 159 | use Archive::Tar; | ||
| 160 | use Archive::Zip; | ||
| 161 | use Compress::Zlib; | ||
| 162 | use Encode qw(decode encode encodings find_encoding from_to); | ||
| 163 | use Fcntl qw(:flock); | ||
| 164 | use File::Basename; | ||
| 165 | use File::Copy qw(copy move); | ||
| 166 | use File::Copy::Recursive qw(dirmove); | ||
| 167 | use File::Fetch; | ||
| 168 | use File::Find; | ||
| 169 | use File::HomeDir qw(my_home); | ||
| 170 | use File::Path qw(remove_tree); | ||
| 171 | use File::ShareDir ':ALL'; | ||
| 172 | use File::ShareDir::Install; | ||
| 173 | #use Glib qw(TRUE FALSE); | ||
| 174 | use GooCanvas2; | ||
| 175 | use Gtk3 '-init'; | ||
| 176 | use HTTP::Tiny; | ||
| 177 | use IO::Socket::INET; | ||
| 178 | #use IO::Socket::INET6; | ||
| 179 | #use IO::Socket::SSL; | ||
| 180 | use IPC::Run qw(start); | ||
| 181 | use JSON; | ||
| 182 | use Math::Round; | ||
| 183 | use Math::Trig; | ||
| 184 | use Module::Load qw(load); | ||
| 185 | use Net::OpenSSH; | ||
| 186 | use POSIX qw(ceil floor); | ||
| 187 | use Regexp::IPv6 qw($IPv6_re); | ||
| 188 | use Safe; | ||
| 189 | use Scalar::Util qw(looks_like_number); | ||
| 190 | use Socket qw(AF_INET SOCK_STREAM inet_aton sockaddr_in); | ||
| 191 | use Symbol qw(qualify); | ||
| 192 | use Storable qw(lock_nstore lock_retrieve); | ||
| 193 | use Time::HiRes qw(gettimeofday usleep); | ||
| 194 | use Time::Piece; | ||
| 195 | |||
| 196 | # Net::SSLeay issues can cause inability to install IO::Socket::SSL on some systems. If it's not | ||
| 197 | # available, set a global flag so that GA::Session won't try to connect to a world with SSL | ||
| 198 | my $rc = eval { | ||
| 199 | require IO::Socket::SSL; | ||
| 200 | IO::Socket::SSL->import(); | ||
| 201 | 1; | ||
| 202 | }; | ||
| 203 | |||
| 204 | if ($rc) { | ||
| 205 | $NO_SSL_FLAG = TRUE; | ||
| 206 | } else { | ||
| 207 | $NO_SSL_FLAG = FALSE; | ||
| 208 | } | ||
| 209 | |||
| 210 | # As of v2.0, IO::Socket::INET6 can't be installed on MS Windows. Since the module is not | ||
| 211 | # referenced directly by Axmud code, we don't need to set a global flag | ||
| 212 | eval { | ||
| 213 | require IO::Socket::INET6; | ||
| 214 | IO::Socket::INET6->import(); | ||
| 215 | 1; | ||
| 216 | }; | ||
| 217 | |||
| 218 | # Internal dependencies | ||
| 219 | use Games::Axmud; | ||
| 220 | use Language::Axbasic; | ||
| 221 | use Language::Axbasic::Expression; # Due to way original Language::Basic was written, | ||
| 222 | use Language::Axbasic::Function; # quickest way to integrate it is to 'use' all the Axbasic | ||
| 223 | use Language::Axbasic::Statement; # source code files here | ||
| 224 | use Language::Axbasic::Subroutine; | ||
| 225 | use Language::Axbasic::Variable; | ||
| 226 | |||
| 227 | # Axmud's source file directory (folder) | ||
| 228 | $TOP_DIR = File::Basename::dirname(__FILE__); | ||
| 229 | # All files required after the Axmud script has been compiled are stored in /share | ||
| 230 | $SHARE_DIR = File::ShareDir::dist_dir('Games-Axmud'); | ||
| 231 | # Axmud's data directory. Axmud creates any data files from scratch if they don't already exist | ||
| 232 | # (Use literal backwards slashes on MS Windows so that commands like ';listdirectory' show what the | ||
| 233 | # use is expecting to see) | ||
| 234 | if ($^O eq 'MSWin32') { | ||
| 235 | $DEFAULT_DATA_DIR = File::HomeDir->my_home . '\\' . $NAME_SHORT . '-data'; | ||
| 236 | } else { | ||
| 237 | $DEFAULT_DATA_DIR = File::HomeDir->my_home . '/' . $NAME_SHORT . '-data'; | ||
| 238 | } | ||
| 239 | # If a file 'datadir.cfg' exists and contains (in its first line) a directory, use that as the | ||
| 240 | # data directory instead of using the default location | ||
| 241 | $DATA_DIR = $DEFAULT_DATA_DIR; | ||
| 242 | if (-e $TOP_DIR . '/datadir.cfg') { | ||
| 243 | |||
| 244 | my ($fileHandle, $firstLine); | ||
| 245 | |||
| 246 | if (open $fileHandle, '<', $TOP_DIR . '/datadir.cfg') { | ||
| 247 | |||
| 248 | $firstLine = <$fileHandle>; | ||
| 249 | close $fileHandle; | ||
| 250 | } | ||
| 251 | |||
| 252 | if (defined $firstLine) { | ||
| 253 | |||
| 254 | chomp $firstLine; | ||
| 255 | $DATA_DIR = $firstLine; | ||
| 256 | } | ||
| 257 | } | ||
| 258 | |||
| 259 | # Put paths to plugins (all of them Perl modules) into @INC | ||
| 260 | push (@INC, | ||
| 261 | $SHARE_DIR . '/plugins', | ||
| 262 | $SHARE_DIR . '/private', | ||
| 263 | ); | ||
| 264 | |||
| 265 | # Standard Perl error/warning trapping | ||
| 266 | $SIG{__DIE__} = sub { | ||
| 267 | |||
| 268 | if ($CLIENT) { | ||
| 269 | |||
| 270 | my $engines = join('|', @{$CLIENT->{constTTSList}}); | ||
| 271 | |||
| 272 | # Errors generated by GA::Session->perlCmd cause a chain of errors, because the Perl Safe | ||
| 273 | # module uses its own namespace and can't call GA::Client->writePerlWarning | ||
| 274 | # Workaround is to use a global flag and to generate our own error message if it's set | ||
| 275 | if ($TEST_REGEX_FLAG) { | ||
| 276 | |||
| 277 | # Regex test initiated by GA::Client->regexCheck | ||
| 278 | $TEST_REGEX_ERROR = $_[0]; | ||
| 279 | |||
| 280 | } elsif ( | ||
| 281 | # Catch the exceptions generated by GA::Client->ipv4Get | ||
| 282 | ! ($_[0] =~ m/Could not connect to.*Name or service not known/) | ||
| 283 | && ! ($_[0] =~ m/Timed out while waiting for socket to become ready for reading/) | ||
| 284 | && ! ($_[0] =~ m/Could not connect.*Network is unreachable/) | ||
| 285 | && ! ($_[0] =~ m/SSL connection failed for.*SSL wants a read first/) | ||
| 286 | # Catch the exceptions generated by TTS engines not installed on a Linux system | ||
| 287 | # (on MS Windows, the location of the executable is predictable, so GA::Client code | ||
| 288 | # checks for it explicitly) | ||
| 289 | && ! ($_[0] =~ m/Command \'($engines)\' not found in/) | ||
| 290 | ) { | ||
| 291 | # We don't know which GA::Session caused the Perl error, but we can leave Axmud in a | ||
| 292 | # (more or less) functional state by halting all client loops and session loops; the | ||
| 293 | # user can restart them, when ready, with the ';restart' command | ||
| 294 | $CLIENT->writePerlError(@_); | ||
| 295 | |||
| 296 | if ($CLIENT->sessionHash && ! $CLIENT->suspendSessionLoopFlag) { | ||
| 297 | |||
| 298 | $CLIENT->haltSessionLoops(); | ||
| 299 | } | ||
| 300 | } | ||
| 301 | |||
| 302 | } else { | ||
| 303 | |||
| 304 | # (If the GA::Client object doesn't exist yet, better to die() than to carry on) | ||
| 305 | die(@_); | ||
| 306 | } | ||
| 307 | }; | ||
| 308 | |||
| 309 | $SIG{__WARN__} = sub { | ||
| 310 | |||
| 311 | if ($TEST_REGEX_FLAG) { | ||
| 312 | |||
| 313 | # Regex test initiated by GA::Client->regexCheck | ||
| 314 | $TEST_REGEX_ERROR = $_[0]; | ||
| 315 | |||
| 316 | } elsif ( | ||
| 317 | # Warnings seen only on MS Windows | ||
| 318 | ! ($_[0] =~ m/attempt to override closure\-\>va_marshal/) | ||
| 319 | # Git #10/11: intercept Glib spam in Axmud's main window | ||
| 320 | && ! ($_[0] =~ m/GLib-GObject-WARNING/) | ||
| 321 | && ! ($_[0] =~ m/GLib-GObject-CRITICAL/) | ||
| 322 | ) { | ||
| 323 | if ($CLIENT && ! $SAFE_MODE_FLAG) { | ||
| 324 | $CLIENT->writePerlWarning(@_); | ||
| 325 | } else { | ||
| 326 | warn(@_); | ||
| 327 | } | ||
| 328 | } | ||
| 329 | }; | ||
| 330 | |||
| 331 | # Create the main GA::Client object | ||
| 332 | $CLIENT = Games::Axmud::Client->new(); | ||
| 333 | # Start the client. If this fails, terminate the script | ||
| 334 | if (! $CLIENT || ! $CLIENT->start()) { | ||
| 335 | |||
| 336 | exit 1; | ||
| 337 | } | ||
| 338 | |||
| 339 | # Start Gtk3's main loop | ||
| 340 | Gtk3->main(); | ||
| 341 | |||
| 342 | END { | ||
| 343 | |||
| 344 | # Stop the client - unless the GA::Client->stop() function has already been called | ||
| 345 | # (the only other exit() call is in GA::Client->stop() ) | ||
| 346 | if (! $CLIENT->shutdownFlag) { | ||
| 347 | |||
| 348 | $CLIENT->stop(); | ||
| 349 | } | ||
| 350 | } | ||
| 351 | |||
| 352 | # Package must return a true value | ||
| 353 | 1; |