#!/usr/bin/perl ## (Change the top line to reflect the location of perl on your system) #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# ## cmail $Revision: 2.10 $: a tool to aid playing chess by email ## Copyright (C) 1993 Free Software Foundation, Inc. ## ## cmail is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## cmail is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with cmail; if not, write to the Free Software ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. ## ## Modified: $Date: 1994/05/31 18:35:21 $ ## Revision: $Revision: 2.10 $ ## Email: welsh@epcc.ed.ac.uk ## Snailmail: Evan Welsh ## Edinburgh Parallel Computing Centre ## JCMB, Kings Buildings ## The University of Edinburgh ## Edinburgh EH9 3JZ ## Scotland #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# ## Print verbose diagnostics for debugging sub debug { if ($DEBUG) { $old = select ; ## Remember the selected output select(logfile) ; ## Diagnostics go to logfile $| = 1 ; ## Keep it flushed for ($i = 0 ; $i <= $#_ ; $i ++) { printf($_[$i]) ; ## Print one or more diagnostic messages } select($old) ; ## Re-select the old output } } #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# ## Create a directory for storing games in if it doesn't already exist sub need_chess_dir { ## ################################################################# ## ## Check for existence of the named chess directory ## ################################################################# ## if (! (-d "$CHESSDIR")) { ## ############################################################# ## ## Ask user for confirmation if attached to tty ## ############################################################# ## if (-t) { $old = select ; ## Remember selected output select(stdout) ; ## Write to standard output $| = 1 ; ## Keep it flushed printf("Chess directory <$CHESSDIR> does not exist.") ; printf(" Create it? [y/q]: "); $_ = ; ## Read response from tty if (/^[qQ].*/) { die "Bye!\n" ; ## Quit if q selected } select($old) ; ## Re-select the old output } ## ############################################################# ## ## Create a chess directory or die ## ############################################################# ## die "cmail: Can't create directory: $CHESSDIR\n" unless mkdir("$CHESSDIR", 511) ; printf("Created chess directory <$CHESSDIR>.\n") ; printf("You can move it but remember to set the CMAIL_DIR") ; printf(" environment variable.\n") ; } ## ################################################################# ## ## Change to the $CHESSDIR directory whether newly created or not ## ################################################################# ## chdir "$CHESSDIR" ; } #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# ## Parse command-line arguments sub parse_flags { ## ################################################################# ## ## Set up defaults from the environment or from hard-wired constants ## ################################################################# ## $SHOWC = 0 ; $SHOWW = 0 ; $LOGFILE = "&STDERR" ; $MAILPROG = $ENV{'CMAIL_MAILPROG'} ; $MAILPROG = "/usr/ucb/Mail" if ( (-x "/usr/ucb/Mail") && (! $MAILPROG)) ; $MAILPROG = "/usr/ucb/mail" if ( (-x "/usr/ucb/mail") && (! $MAILPROG)) ; $MAILPROG = "Mail" unless ($MAILPROG) ; printf("\$CMAIL_DIR now obsolete. Use \$CHESSDIR instead.\n") if ($ENV{'CMAIL_DIR'}) ; $CHESSDIR = $ENV{'CHESSDIR'} ; $HOMEDIR = $ENV{'HOME'} ; $CHESSDIR = "$HOMEDIR/Chess" unless ($CHESSDIR) ; $CHESSDIR = "~/Chess" unless ($HOMEDIR) ; $ALIAS_FILE = $ENV{'CMAIL_ALIASES'} ; $ALIAS_FILE = ".cmailaliases" unless ($ALIAS_FILE) ; $GAMES_FILE = $ENV{'CMAIL_GAMES'} ; $GAMES_FILE = ".cmailgames" unless ($GAMES_FILE) ; $TIME_DELAY = $ENV{'CMAIL_TIME_DELAY'} ; $TIME_DELAY = 0 unless ($TIME_DELAY) ; &get_pw_name() ; $ME = $PW_NAME ; $ME = $ENV{'LOGNAME'} unless ($ME) ; $PGN_EVENT = "Email correspondence game" ; $PGN_SITE = "NET"; $PGN_ROUND = "-"; $PGN_WHITE = "?"; $PGN_BLACK = "?"; $PGN_WHITENA = "?"; $PGN_BLACKNA = "?"; $PGN_MODE = "EM"; $SEND_MAIL = 1 ; $LOAD_XBOARD = 1 ; $REUSE = 1 ; $TD_FLAGS = " -td $TIME_DELAY" ; $NCP_FLAGS = " -ncp" ; ## ################################################################# ## ## Define the usage string ## ################################################################# ## $USAGE = ("cmail [-h] [-c] [-w] [-[x]v] [-[x]mail] [-[x]xboard] [-[x]reuse] [-remail] [-game ] [-opp ] [-addr
] [-dir ] [-gamesFile ] [-aliasesFile ] [-logFile ] [-event ] [-site ] [-round ] [-mode ] [-(white|black) ] [-(white|black)na ] [-mailprog ]") ; ## ################################################################# ## ## Overwrite defaults if specified on the command-line ## ################################################################# ## $UNREC_ARGS = "" ; while ($ARGV = shift) { $UNREC = 0 if ($ARGV =~ /^-/) ; if ("$ARGV" eq "-h") {die("Usage: $USAGE\n") ;} elsif ("$ARGV" eq "-c") {$SHOWC = 1 ;} elsif ("$ARGV" eq "-w") {$SHOWW = 1 ;} elsif ("$ARGV" eq "-v") {$DEBUG = 1 ;} elsif ("$ARGV" eq "-xv") {$DEBUG = 0 ; $QUIET = 1 ;} elsif ("$ARGV" eq "-mail") {$SEND_MAIL = 1 ;} elsif ("$ARGV" eq "-xmail") {$SEND_MAIL = 0 ;} elsif ("$ARGV" eq "-xboard") {$LOAD_XBOARD = 1 ;} elsif ("$ARGV" eq "-xxboard") {$LOAD_XBOARD = 0 ;} elsif ("$ARGV" eq "-reuse") {$REUSE = 1 ;} elsif ("$ARGV" eq "-xreuse") {$REUSE = 0 ;} elsif ("$ARGV" eq "-remail") {$LOAD_XBOARD = 0 ; $SEND_MAIL = 1 ;} elsif ("$ARGV" eq "-game") {$PGN_GAME = shift ;} elsif ("$ARGV" eq "-opp") {$OPP_NAME = shift ;} elsif ("$ARGV" eq "-addr") {$OPP_ADDRESS = shift ;} elsif ("$ARGV" eq "-dir") {$CHESSDIR = shift ;} elsif ("$ARGV" eq "-mailprog") {$MAILPROG = shift ;} elsif ("$ARGV" eq "-gamesFile") {$GAMES_FILE = shift ;} elsif ("$ARGV" eq "-aliasesFile") {$ALIAS_FILE = shift ;} elsif ("$ARGV" eq "-logFile") {$LOGFILE = shift ;} elsif ("$ARGV" =~ /^-(td|timeDelay)$/) {$TD_FLAGS = ( " $ARGV " . shift) ;} elsif ("$ARGV" =~ /^-noChessComputer$/) {$NCP_FLAGS = ( " $ARGV " . shift) ;} elsif ("$ARGV" =~ /^-[x]?ncp$/) {$NCP_FLAGS = " $ARGV" ;} elsif ("$ARGV" eq "-event") {$PGN_EVENT = shift ;} elsif ("$ARGV" eq "-site") {$PGN_SITE = shift ;} elsif ("$ARGV" eq "-round") {$PGN_ROUND = shift ;} elsif ("$ARGV" eq "-white") {$PGN_WHITE = shift ;} elsif ("$ARGV" eq "-black") {$PGN_BLACK = shift ;} elsif ("$ARGV" eq "-whitena") {$PGN_WHITENA = shift ;} elsif ("$ARGV" eq "-blackna") {$PGN_BLACKNA = shift ;} elsif ("$ARGV" eq "-mode") {$PGN_MODE = shift ;} elsif ("$ARGV" =~ /^-[x]?oldsave$/) {$SAVE_FLAGS = " $ARGV" ;} elsif ("$ARGV" eq "-oldSaveStyle"){$SAVE_FLAGS = ( " $ARGV " . shift) ;} elsif ("$ARGV" =~ /^-/ || $UNREC) { $UNREC_ARGS .= " " . $ARGV ; $UNREC = 1 ; } else { die("cmail: Unrecognised flag \"$ARGV\"\nUsage: $USAGE\n") ; } } $ENV{'CMAIL_MAILPROG'} = $MAILPROG ; ## Propagate to cmail grandchild $OLDSAVE = ( $SAVE_FLAGS =~ /^ (-oldsave|-oldSaveStyle [Tt][Rr][Uu][Ee])$/) ; } #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# ## Initialisation of variables and environment sub showGPL { ## ################################################################# ## ## Show copyright notice ## ################################################################# ## while() { last if(/^{END OF GPL COPYRIGHT}$/) ; $_ =~ s/\$Revision[:] (.*) \$/$1/ ; print ; } ## ################################################################# ## ## Show conditions if requested ## ################################################################# ## while() { last if(/^{END OF GPL CONDITIONS}$/) ; if($SHOWW) { print ; } } ## ################################################################# ## ## Show warranty if requested ## ################################################################# ## if ($SHOWC) { if($SHOWW) { printf("\n") ; } while() { print ; } } if ($SHOWC || $SHOWW) { exit ; ## Abort if showed conditions or warranty } } #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# ## Initialisation of variables and environment sub initialise { $FLIP = "false" ; ## The board is not flipped by default &parse_flags(@ARGV) ; ## Parse command-line arguments &showGPL unless $QUIET ; open(tty, "< /dev/tty") ; ## Open tty for reading &need_chess_dir() ; ## Check for the existence of CHESSDIR if ($DEBUG) { open (logfile, ">$LOGFILE") ; ## Default is STDERR } &debug("Called \n") ; } #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# ## Prompt for a game name, if use a default sub prompt_for_game_name { &debug("Called \n") ; $old = select ; ## Remember the selected output select(stdout); ## Diagnostics go to logfile $| = 1 ; ## Keep it flushed printf("Game name [ to use default]: ") ; die "cmail: tty not open\n" unless (-t) ; =~ /(.*)/ ; ## Read line from tty $PGN_GAME = "$1" ; ## Assign to game name select($old) ; ## Re-select the old output } #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# ## Get opponent's address sub find_opp_address_in_game_file { &debug("Called game name", " <$PGN_GAME>\n") ; if (! ("" eq "$PGN_GAME")) { ## Can't find it without the game name ## ############################################################# ## ## Open the game file and read all the entries ## ############################################################# ## if (open(GAMES_IN, "<$GAMES_FILE")) { while() { if (/^<$PGN_GAME>\s*(.*)\s*$/) { ## Match! $OPP_ADDRESS = $1 ; ## Found opponent's address } } if ("" eq $OPP_ADDRESS) { $SAVE_ADDRESS = 1 ; ## Save the address when it is found } } else { ## Game file doesn't exist yet $SAVE_ADDRESS = 1 ; ## Save the address when it is found } } } #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# ## Get game name from game file sub find_game_in_game_file { &debug("Called opp address", " <$OPP_ADDRESS>\n") ; if ("" ne "$OPP_ADDRESS") { ## Can't find it without the opp address ## ############################################################# ## ## Open the game file and read all the entries ## ############################################################# ## if (open(GAMES_IN, "< $GAMES_FILE")) { while() { if ($_ =~ /^<(.*)>\s*$OPP_ADDRESS\s*$/) { $PGN_GAME = $1 ; ## Match! } } } else { $SAVE_ADDRESS = 1 ; ## Save the address when it is found $PGN_GAME = "$ME-VS-$OPP_NAME" ; ## Construct default name } } ## ################################################################# ## ## Failed to find the game name so construct a default from players ## ################################################################# ## if ("" eq $PGN_GAME) { $SAVE_ADDRESS = 1 ; ## Save the address when it is found $PGN_GAME = "$ME-VS-$OPP_NAME" ; ## Construct default name } } #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# ## Prompt for opponent's address sub prompt_for_opp_address { &debug("Called \n") ; ## ################################################################# ## ## Prompt for opponent's email address ## ################################################################# ## printf("Opponent's email address: ") ; die "cmail: tty not open\n" unless (-t) ; =~ /(.*)/ ; $OPP_ADDRESS = $1 ; ## ################################################################# ## ## Use name as default if still blank ## ################################################################# ## if ("" eq $OPP_ADDRESS) { $OPP_ADDRESS = $OPP_NAME ; } ## ################################################################# ## ## Add the alias to the alias file ## ################################################################# ## if ($ADD_ALIAS == 1) { printf("Adding alias <$OPP_NAME = $OPP_ADDRESS>.\n") ; die "cmail: Can't open file: $ALIAS_FILE\n" unless open(ALIASES_OUT, ">> $ALIAS_FILE") ; select(ALIASES_OUT) ; printf("$OPP_NAME\t=\t$OPP_ADDRESS\n") ; close(ALIASES_OUT) ; select(stdout) ; } } #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# ## Prompt for opponent's name sub prompt_for_opp_name { &debug("Called \n") ; $old = select ; ## Remember the selected output select(stdout) ; ## Write to stdout $| = 1 ; ## Keep it flushed printf("Opponent's name: ") ; die "cmail: tty not open\n" unless (-t) ; ## Check tty is open =~ /(.*)/ ; ## Read line from tty $OPP_NAME = $1 ; ## Match! die "cmail: I can't proceed without the opponent's name.\n" unless ($OPP_NAME) ; select($old) ; ## Re-select the old output } #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# ## Search for opponent's name in alias file if it exists sub find_opp_address_in_alias_file { &debug("Called opp name <$OPP_NAME>\n") ; ## ################################################################# ## ## Check the alias file exists ## ################################################################# ## if (-f "$ALIAS_FILE") { ## ############################################################# ## ## Open alias file and read all the entries ## ############################################################# ## die "cmail: Can't open file: $ALIAS_FILE\n" unless open(ALIASES_IN, "< $ALIAS_FILE") ; while () { if (/$OPP_NAME\s*=\s*(.*)/) { $OPP_ADDRESS = $1 ; ## Match! } } ## ############################################################# ## ## Check if we should save the new address in the alias file ## ############################################################# ## if ("" eq $OPP_ADDRESS) { $ADD_ALIAS = 1 ; ## Remember to add new alias to alias file } close(ALIASES_IN) ; ## Close alias in file } else { $ADD_ALIAS = 1 ; ## Remember to add new alias to alias file } } #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# ## Add this game to the list of games in the game file sub add_game_to_game_file { &debug("Called \n") ; ## ################################################################# ## ## Append address to game file ## ################################################################# ## if ($SAVE_ADDRESS) { die "cmail: Can't open file: $GAMES_FILE\n" ## Open the file or die unless open(GAMES_OUT, ">> $GAMES_FILE") ; select(GAMES_OUT) ; ## Write to games file printf("<$PGN_GAME> $OPP_ADDRESS\n") ; ## Make the entry close(GAMES_OUT) ; ## Close games file } select(STDOUT) ; ## Re-select STDOUT } #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-# #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# ## Load the game sub play_game { &debug("Called \n") ; $| = 1 ; ## Start flushing output buffer ## ################################################################# ## ## Load xboard unless inhibited by command-line arguments ## ################################################################# ## if ($LOAD_XBOARD) { ## ############################################################# ## ## Remove output files from previous run ## ############################################################# ## unlink("$PGN_GAME.pos.out") ; unlink("$PGN_GAME.game.out") ; ## ############################################################# ## ## Invoke xboard with loads of flags ## ############################################################# ## $LOG_FILE = "$PGN_GAME.log" ; $XBOARD_ARGS = ( " -cmail $PGN_GAME" . " -flipView $FLIP" . "$SAVE_FLAGS" . "$TD_FLAGS" . "$NCP_FLAGS" . "$UNREC_ARGS") ; &debug("Invoking xboard with args:$XBOARD_ARGS\n") ; $PID_FILE = "$PGN_GAME.pid" ; if ( (! $REUSE) || (! ( (-f $PID_FILE) && ($XBOARD_PID = `cat $PID_FILE`) && ("$XBOARD_PID" =~ /^\d+$/) && (kill "SIGUSR1", $XBOARD_PID)))) { printf("Loading xboard for game <$PGN_GAME>...") ; system( "{ ({ xboard $XBOARD_ARGS & } ;" . " echo \$! > $PID_FILE ;" . " wait ;" . " rm $PID_FILE) & } >$LOG_FILE 2>&1") ; printf( "done.\n" . "If nothing happens look for an error message in\n" . "$CHESSDIR/$LOG_FILE.\n") ; } else { printf("Revived existing xboard for game <$PGN_GAME>.\n" . "If nothing happens" . " remove $CHESSDIR/$PID_FILE and try again.\n") ; } exit 0 ; } } #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# ## Find the game the user wants by any means possible sub find_game { &debug("Called \n") ; ## ################################################################# ## ## Ask user for a game name if not already known ## ################################################################# ## if ("" eq "$PGN_GAME") { &prompt_for_game_name() ; } ## ################################################################# ## ## Find out opponent's details ## ################################################################# ## if ("" eq "$PGN_GAME") { if ("" eq "$OPP_NAME") { &prompt_for_opp_name() ; ## Ask user for opponent's name } &find_opp_address_in_alias_file() ; ## Find opp address in aliases &find_game_in_game_file() ; ## Find game name in game file } else { &find_opp_address_in_game_file() ; ## Find opp address in game file if ("" eq "$OPP_ADDRESS") { $OPP_ADDRESS = $RETURN_ADDRESS ; ## Use return address instead &debug( "Using return address <$OPP_ADDRESS>" . " for opponent address\n") ; } if (("" eq "$OPP_ADDRESS") && ("" eq "$OPP_NAME")) { &prompt_for_opp_name() ; ## Ask user for opponent's name } } ## ################################################################# ## ## Finally ask user for missing details as a last resort ## ################################################################# ## if ("" eq "$OPP_ADDRESS") { &find_opp_address_in_alias_file() ; ## Find opp address in aliases if ("" eq "$OPP_ADDRESS") { &prompt_for_opp_address() ; ## Ask user for opponent's address } } ## ################################################################# ## ## If no $PGN_GAME.game.in file, assume we're starting a new game ## ################################################################# ## if (! (-f "$PGN_GAME.game.in")) { &start_new_game() ; } ## ################################################################# ## ## Give up if we haven't got anywhere to send a move to ## ################################################################# ## if ("" eq "$OPP_ADDRESS") { die "cmail: Can't proceed without opponent's email address.\n" ; } } #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# ## Get the date sub get_date { $the_time = time ; ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime($the_time) ; $mon ++ ; if ($mon < 10) { $mon = "0$mon" ; } if ($year < 94) { $century = 20 ; } else { $century = 19 ; } "$century$year.$mon.$mday" ; } #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# ## Start a new game sub start_new_game { printf("Starting a new game.\n") ; $TO_PLAY = "white" ; $MOVENUM = 0 ; ## ################################################################# ## ## Create an empty game file ## ################################################################# ## open(GAMEFILE, "> $PGN_GAME.game.in") ; select(GAMEFILE) ; if ($OLDSAVE) { printf("# xboard game file\n\n algebraic\n") ; } else { ## New PGN format &get_pw_name() ; &get_pw_gcos() ; $PGN_WHITE = $PW_GCOS ; $PGN_WHITE = "?" unless ($PGN_WHITE) ; $PGN_BLACK = "?" unless ($PGN_BLACK) ; $PGN_WHITENA = "$PW_NAME@" . `domainname` if ($PGN_WHITENA eq "?") ; $PGN_WHITENA =~ s/\n// ; $PGN_WHITENA = "?" if ($PGN_WHITENA =~ /(^@)|(@$)/) ; $PGN_BLACKNA = $OPP_ADDRESS if ($PGN_BLACKNA eq "?"); $PGN_BLACKNA = "?" unless ($PGN_BLACKNA) ; $PGN_DATE = &get_date() ; $PGN_DATE = "?" unless ($PGN_DATE) ; printf("[Event \"$PGN_EVENT\"] [Site \"$PGN_SITE\"] [Date \"$PGN_DATE\"] [Round \"$PGN_ROUND\"] [White \"$PGN_WHITE\"] [Black \"$PGN_BLACK\"] [Result \"*\"] [WhiteNA \"$PGN_WHITENA\"] [BlackNA \"$PGN_BLACKNA\"] [Mode \"$PGN_MODE\"] [CmailGameName \"$PGN_GAME\"] * ") ; } close(GAMEFILE) ; } #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# ## Get the password file gcos (full name) entry sub get_pw_entry { ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($<); ($name, $gcos) ; } #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# ## Get password file gcos (full name) entry sub get_pw_gcos { if (! $PW_GCOS) { ($dummy, $PW_GCOS) = &get_pw_entry() ; $PW_GCOS =~ s/^\s*([^,()]+[^ ,()])[ ]*[,()].*$/$1/; if ($PW_GCOS =~ /^([^,()]+)\s+([^\s,()]+)$/) { ## Multi-word name $PW_GCOS = $2 . ", " . $1 ; } elsif ($PW_GCOS !~ /^([^\s,()]+)/) { ## No sensible gcos entry $PW_GCOS = "" ; } ## Else leave it as one word &debug("PW full name is <$PW_GCOS>\n"); } } #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# ## Get password file user name entry sub get_pw_name { if (! $PW_NAME) { ($PW_NAME, $dummy) = &get_pw_entry() ; &debug("PW name is $PW_NAME\n"); } } #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# ## Analyse the email message sub analyse_email_message { printf("Processing game message...") ; ## ################################################################# ## ## Parse the mail message ## ################################################################# ## while() { if (/^From:?.*[<\s]([^<\s]+@[^>\s]+)([\s>]|$)/) { $RETURN_ADDRESS = $1 ; ## Default for opponent's email address &debug("Found opponent's email address <$RETURN_ADDRESS>\n") ; } elsif(/^Subject:\s*cmail\s*(\d+)[.]\s+(.*)\s*<(.*)>$/) { &debug("Move is $1 according to subject line\n") ; $PGN_GAME = $3 ; &debug("Found game name <$PGN_GAME>\n") ; if (($1 eq 1) && ($2 =~ /^[^.]/)) { $SAVE_ADDRESS = 1 ; ## Save address if on black's first move } } elsif(/xboard position file/) { &debug("Found xboard position file\n") ; ## Don't need to read $SAVE_FLAGS = " -oldsave" ; ## Old save-style format $OLDSAVE = 1 ; } elsif(/^(black|white) to play$/) { $TO_PLAY = $1 ; ## Whose move is it (colour)? if ($TO_PLAY eq "black") { $FLIP = "true" ; ## Flip xboard board if black to play } &debug("Found to play <$TO_PLAY>, flip is <$FLIP>\n") ; } elsif( /xboard game file/ ## Old xboard format || ( (! ($IN_FILE || $STORING)) && /^(\[.*\]|\{.*)/)) { ## PGN format ## ######################################################### ## ## Open game file for copying the rest of the mail message into ## ######################################################### ## if ($PGN_GAME) { $IN_FILE = 1 ; die "cmail: Can't open file: $PGN_GAME.game.in\n" unless open(GAMEFILE, "> $PGN_GAME.game.in") ; select(GAMEFILE) ; } else { $STORING = 1 ; } &debug("Found xboard game file\n") ; } elsif((/^\[Black "[?]"\]/) && ($SAVE_ADDRESS)) { &get_pw_gcos() ; $PW_GCOS = "$RETURN_ADDRESS" unless ($PW_GCOS) ; $PW_GCOS = "?" unless ($PW_GCOS) ; $_ = "[Black \"$PW_GCOS\"]\n" ; &debug("Changed Black tag to be $_") ; } elsif( (/^\[WhiteNA "[?]"\]/) && (($SAVE_ADDRESS) && ($RETURN_ADDRESS))) { $_ = "[WhiteNA \"$RETURN_ADDRESS\"]\n" ; &debug("Changed WhiteNA tag to be $_") ; } elsif( (/^\[WhiteNA "(.*)"\]/) && ("$PGN_WHITENA" eq "?")) { $PGN_WHITENA = $1 ; &debug("Found WhiteNA tag to be \"$PGN_WHITENA\"\n") ; } elsif( (/^\[BlackNA "(.*)"\]/) && ("$PGN_BLACKNA" eq "?")) { $PGN_BLACKNA = $1 ; &debug("Found BlackNA tag to be \"$PGN_BLACKNA\"\n") ; } elsif(/^\[CmailGameName "(.*)"\]/) { $PGN_GAME = "$1" ; &debug("Found cmail game name from PGN tag \"$PGN_GAME\"\n") ; if ($STORING) { $IN_FILE = 1 ; $STORING = 0 ; die "cmail: Can't open file: $PGN_GAME.game.in\n" unless open(GAMEFILE, "> $PGN_GAME.game.in") ; select(GAMEFILE) ; print "$STORE" ; } } elsif (/^(.*[^\d]+|)\d+[.]\s*([^\s*]*\s+|)[^\s.*]+(\s*\d+[.]\s*|)[\s*]*$/) { if ($2) { $TO_PLAY = "white" ; } else { $TO_PLAY = "black" ; } &debug("$TO_PLAY to play\n") ; } if ($IN_FILE) { &debug("$_") ; print ; ## Copy end of mail message into game file } if ($STORING) { &debug("$_") ; $STORE .= $_ ; ## Add line to store } } die "cmail: Couldn't find cmail game name!\n" if ($STORING) ; if (! $RETURN_ADDRESS) { if ($TO_PLAY eq "black") { $RETURN_ADDRESS = $PGN_WHITENA unless ("$PGN_WHITENA" eq "?") ; } else { $RETURN_ADDRESS = $PGN_BLACKNA unless ("$PGN_BLACKNA" eq "?") ; } } close(GAMEFILE) ; select(STDOUT) ; printf("done.\n") ; ## ################################################################# ## ## Check that we have enough info about the players to continue ## ################################################################# ## &find_game() ; } #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# ## Analyse output files and send the move sub send_move { ## ################################################################# ## ## Process the game.out and pos.out (if there is one) files ## ################################################################# ## die "cmail: Can't open file: $PGN_GAME.out\n" unless open(out, "> $PGN_GAME.out") ; select(out) ; ## ################################################################# ## ## Cat the pos file into the .out file if there is one (old-style) ## ################################################################# ## if (open(pos, "< $PGN_GAME.pos.out")) { printf("cmail game <$PGN_GAME>\n") ; ## Old-style format while() { &debug("$_") ; ## ######################################################### ## ## We probably don't know who it is to play if we bypassed ## loading xboard (for remail) so derive it from the ## "to play" line in the pos.out file ## ######################################################### ## if (/^(black|white) to play$/) { if ($1 eq "black") { $TO_PLAY = "white" ; ## Change black to white } else { $TO_PLAY = "black" ; ## Change white to black } &debug("Found <$1> to play in", " <$PGN_GAME.pos.out>,", " reversed for remailing <$TO_PLAY>\n") ; } print ; ## Write the line to the .out file } close(pos) ; ## Close the pos.out file } ## ################################################################# ## ## Cat the game file into the .out file. There must be one! ## ################################################################# ## if (open(game, "< $PGN_GAME.game.out")) { ## ############################################################# ## ## Cat the .game.out file into the .out file, remembering ## the move number of the last line ## ############################################################# ## while() { &debug("$_") ; if ($_ =~ /^(.*[^\d]+|)(\d+)[.]+\s*[^\s.]*\s+(\S+)\s*$/) { $MOVENUM = $2 ; $MOVE = $3 ; } ## ######################################################### ## ## We probably don't know who it is to play if we bypassed ## loading xboard (for remail) and there is no position ## file (new-style), so derive it from the "to play" line ## in the game.out file ## ######################################################### ## if (/^(black|white) to play$/) { if ($1 eq "black") { $TO_PLAY = "white" ; ## Change black to white } else { $TO_PLAY = "black" ; ## Change white to black } &debug("Found <$1> to play in", " <$PGN_GAME.game.out>,", " reversed for remailing <$TO_PLAY>\n") ; } print ; } close(game) ; ## Close the game.out file close(out) ; ## Close the .out file select(STDOUT) ; ## Write to the standard output ## ############################################################# ## ## If it's black to play, the subject line of the outgoing ## mail message contains dots where the white move would ## normally be ## ############################################################# ## if ($TO_PLAY eq "black") { $DOTS = " ...." ; } else { $DOTS = "" ; } ## ############################################################# ## ## Send the mail message to opponent's address unless bypassed ## ############################################################# ## if ($SEND_MAIL) { $| = 1 ; ## Keep it flushed print "" ; $RES = system( "$MAILPROG" . " -s \"cmail $MOVENUM.$DOTS $MOVE <$PGN_GAME>\"" . " $OPP_ADDRESS < $PGN_GAME.out") ; if ($RES / 256) { printf( "Failed to send email message.\n") ; } else { printf( "Emailed move to <$OPP_ADDRESS>: $MOVENUM.$DOTS" . " $MOVE <$PGN_GAME>\n") ; } } else { printf("Email not sent (as requested).\n") ; printf("Would have emailed move to <$OPP_ADDRESS>:") ; printf(" $MOVENUM.$DOTS $MOVE <$PGN_GAME>\n") ; } } else { printf("I can't find the game file.") ; printf(" Did you save the game?\n") ; printf("Email not sent\n") ; } } #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# ## Main body &initialise() ; ## Initialise variables etc. if (-t) { ## No input directed (invoked from a shell rather than a mailer) &debug("Interactive!\n") ; &find_game() ; ## Get the necessary info about the game } else { &debug("Piping!\n") ; &analyse_email_message() ; ## Analyse the mail message } &add_game_to_game_file() ; ## Create an entry in user's list of games &play_game() ; ## Load the game &send_move() ; ## Analyse output files and send the move close(tty) ; ## Tidy up if ($DEBUG) { close (logfile) ; ## Tidy up } #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=# __END__ cmail $Revision: 2.10 $, Copyright (C) 1993 Free Software Foundation, Inc. cmail comes with ABSOLUTELY NO WARRANTY; for details type `cmail -w'. cmail is free software, and you are welcome to redistribute it under certain conditions; type `cmail -c' for details. {END OF GPL COPYRIGHT} GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. {END OF GPL CONDITIONS} GNU GENERAL PUBLIC LICENSE NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.