This file is indexed.

/usr/games/zec is in zec 0.12-5.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
#!/bin/zsh -i

# zec: Z-Shell Empire Client
#   Copyright (C) 2001, 2002, 2003, 2004, 2005  Clint Adams

# This program 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.

# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA

ZEC_VERSION="0.12"

cat <<EOF;
  zec version ${ZEC_VERSION},
  Copyright (C) 2001, 2002, 2003, 2004, 2005, 2008  Clint Adams
  zec comes with ABSOLUTELY NO WARRANTY.
  This is free software, and you are welcome to redistribute it
  under certain conditions.

EOF

zmodload -i zsh/parameter || exit 244
zmodload -i zsh/net/tcp || exit 245
zmodload -i zsh/zselect || exit 246

autoload -U tcp_open
TCP_SILENT=yes
TCP_PROMPT=

setopt extendedglob


# send to empire server
zec_send() {
	tcp_send -s empire_server -- "$@"
}

# read from empire server
zec_readline() {
	if tcp_read -t 0.5 -s empire_server 
	then
		RET="$TCP_LINE"
	else
		return 1
	fi
}

# connect to empire server
# usage: zelogin country password hostname_or_ip tcpportnum

zelogin() {

	tcp_open $3 $4 empire_server || return 1
	zec_readline
	[[ "$RET" == "2 Empire server ready" ]] || return 2
	zec_send "user $LOGNAME"
	zec_readline
	[[ "$RET" == "0 hello $LOGNAME" ]] || return 3
	zec_send "client zec $ZEC_VERSION"
	zec_readline
	[[ "$RET" == "0 talking to zec $ZEC_VERSION" ]] || return 4
	if [[ "$LANG" == (#i)*UTF-#8 ]]; then
		zec_send "options utf-8"
		zec_readline
		[[ "$RET" == "0 Accepted" ]] || print "UTF-8 not supported by server."
	fi
	zec_send "coun $1"
	zec_readline
	[[ "$RET" == "0 country name $1" ]] || return 5
	zec_send "pass $2"
	zec_readline
	[[ "$RET" == "0 password ok" ]] || return 6
	zec_send "play"
	zec_readline
	case "$RET" in
	  ("2 2")
	    # success
	    ;;
	  ("3 country in use by "(#b)(*))
	    print "That country is in use by $match[1]"
	    if [[ -n "$zec_force_disconnect" ]];
	    then
	      print -n "Killing that session..."
	      zec_send "kill"
	      zec_readline
	      if [[ "$RET" == "3 closed socket of offending job" ]];
	      then
		print " succeeded."
		exit
	      else
	        print " failed... $RET"
	        return 7
	      fi
	    else
	      print "If you wish to kill that session, run zec -K."
	    fi
	    ;;
	  (*)
	    return 8
	    ;;
	esac

	return 0
}

zelogout() {
	zec_send "quit"
	zec_readline
	[[ "$RET" == "3 so long" ]] || return 9
}

zec_alias_parse() {
	local cmd="$1"

	[[ -n "${zec_aliases[$cmd]}" ]] || return 10

	cookedline="${line/$cmd/$zec_aliases[$cmd]}"
}

zec_tool_history() {
	local i

	for i in "${(onk)history[@]}"
	do
		print "$i: ${history[$i]}"
	done
}

zec_tool_tmp() {

	local tmpfilenam="${TMPDIR:-/tmp}/zectmp$$"
	local sects types

	if [[ -z "$1" ]]
	then
		sects="*"
		types="jk"
	else
		if [[ "$1" == [[:alpha:]%]## ]]
		then
			sects="*"
			types="$1"
		else
			sects="$1"
			shift
			if [[ -z "$1" ]]
			then
				types="jk"
			else
				if [[ "$1" == \?* ]]
				then
					sects += "$1"
					shift
					if [[ -z "$1" ]]
					then
						types="jk"
					else
						types="$1"
					fi
				else
					types="$1"
				fi
			fi
		fi
	fi

	zec_send "prod $sects >$tmpfilenam"
	zec_parseresponse

	if [[ -n "${types//[jkdilt%]/}" ]];
	then
		print "Invalid sector types in ${types}."
		return 1
	fi

	for i in ${(M)${(f)"$(<$tmpfilenam)"}:#[[:space:]]#-#[0-9]##,-#[0-9]##[[:space:]]##[${types}]*}
	do
		case "$i" in
			(* [jk] *)
			zec_send "th i ${${(z)i}[1]} ${${(z)i}[9]/i(#e)/}"
			zec_parseresponse
			;;

			(* d *)
			zec_send "th o ${${(z)i}[1]} ${${(z)i}[11]/o(#e)/}"
			zec_parseresponse
			zec_send "th l ${${(z)i}[1]} ${${(z)i}[12]/l(#e)/}"
			zec_parseresponse
			zec_send "th h ${${(z)i}[1]} ${${(z)i}[13]/h(#e)/}"
			zec_parseresponse
			;;

			(* i *)

			zec_send "th l ${${(z)i}[1]} ${${(z)i}[10]/l(#e)/}"
			zec_parseresponse
			zec_send "th h ${${(z)i}[1]} ${${(z)i}[11]/h(#e)/}"
			zec_parseresponse
			;;

			(* l *)
			zec_send "th l ${${(z)i}[1]} ${${(z)i}[9]/l(#e)/}"
			zec_parseresponse
			;;

			(* t *)
			zec_send "th d ${${(z)i}[1]} ${${(z)i}[11]/d(#e)/}"
			zec_send "th o ${${(z)i}[1]} ${${(z)i}[12]/o(#e)/}"
			zec_send "th l ${${(z)i}[1]} ${${(z)i}[13]/l(#e)/}"
			zec_parseresponse
			;;

			(* % *)
			zec_send "th o ${${(z)i}[1]} ${${(z)i}[9]/o(#e)/}"
			zec_parseresponse
		esac

	done


	rm ${tmpfilenam}
}

zec_tool_alias() {
	local i

	for i in "${(onk)zec_aliases[@]}"
	do
		print "$i: ${zec_aliases[$i]}"
	done
}

zec_tools_parse() {
	local cmd="$1"

	[[ -n "${zec_tools[(r)$cmd]}" ]] || return 10

	shift
	zec_tool_$cmd "$@"
}

zec_subcommand() {
	local vpmpt line

	vpmpt="-p $subpmpt ${1/\%/%%}> "

	vared -h "$vpmpt" line
	[[ -z "$line" ]] || print -s "$line"
	zec_send "$line"
}

unset outgo

redir_output() {
	outgo=${1}
	[[ "$outgo" != \>* ]] && exit 211
	eval exec 8$outgo
	outgo="-u8"
}

pipe_output() {
	[[ "$@" != \|* ]] && exit 212
	outgo=("${(z)@/| #/}")
	eval exec 8\>\>\(${outgo}\)
	outgo="-u8"
}

restore_output() {
	[[ "$outgo" == "-u8" ]] && exec 8>&-
	unset outgo
}

print_output() {
	print -r $outgo -- "${(Q)1}"
}

zec_parseresponse() {

	while zec_readline
	do

	case "$RET" in
	'1'(#b)(*)) print_output "${(q)${match[1]/ /}}"
		;;
	3*) return 1
		;;
	'4'(#b)(*)) zec_subcommand "$match[1]"
		;;
	6*) restore_output
		;;
	'8 '(#b)(*)) redir_output "$match[1]"
		;;
	'9 '(#b)(*)) pipe_output "$match[1]"
		;;
	'd'(#b)(*)) print_output "${match[1]/ /}"  # flash
		;;
	'e'(#b)(*)) print_output "INFORM (slight confusion): ${match[1]/ /}"  # inform
		;;
	*) print "OH, NO!  I AM CONFUSED BY THIS LINE!"
		print "$RET"
		zelogout
		;;
	esac

	done
}

zec_command() {
	local line

	[[ "$RET" == '6 '(#b)([0-9]##)' '([0-9]##) ]] || return 9
	vpmpt="-p $pmpt $match[1]:$match[2]%# "

	vared -he "$vpmpt" line || zelogout
	[[ -z "$line" ]] || print -s "$line"
	if zec_alias_parse "${(z)line}"
	then
		if zec_tools_parse "${(z)cookedline}"
		then
			zec_send ""
		else
			zec_send "$cookedline"
		fi
	elif zec_tools_parse "${(z)line}"
	then
		zec_send ""
	else
		zec_send "$line"
	fi
}

parse_config() {
	local CONFIGLINE
	local i=1

	while read CONFIGLINE
	do
		case $CONFIGLINE in
		(addgame*)
			games[i]="${CONFIGLINE#addgame }"
			(( i++ ))
			;;
		'alias '(#b)([^ ]##)' '(*))
			zec_aliases[${match[1]}]="$match[2]"
			;;
		(\#*) # ignore comments
			;;
		(*)
			print "Unknown config line: $CONFIGLINE"
			;;
		esac

	done <$1
}

zec_tool_foreach()
{

	if [[ $# -lt 2 ]]; then
		print "usage: foreach <sects> <command>"
		return 1
	fi

	local tmpfilenam="${TMPDIR:-/tmp}/zecforeach$$"
	local sects sect

	sects="$1"
	shift
	if [[ "$1" == \?* ]];
	then
		sects+=" $1"
		shift
	fi

	zec_send "dump $sects >$tmpfilenam"
	zec_parseresponse
	for i in ${(M)${(f)"$(<$tmpfilenam)"}:##[0-9-]##[[:space:]]##[0-9-]##[[:space:]]*}
	do
		sect=${${i/ /,}%% *}
		zec_send "${(e)*}"
		zec_parseresponse
	done

	rm ${tmpfilenam}
}

# setfood <sects> <num of updates>
zec_tool_setfood()
{
	local tmpfilenam="${TMPDIR:-/tmp}/zecsetfood$$"
	local optionstate=NONE sects
	local -A gameoptions
	integer etus=0 thresh curthresh num=1
	float babyeatrate=0.0 eatrate=0.0

	if [[ $# -lt 1 ]]; then
		sects="*"
	else
		sects="$1"
		shift
		if [[ "$1" == \?* ]];
		then
			sects+=" $1"
			shift
		fi
		if [[ $# -eq 1 ]]; then
			num=$1
		else
			print "setfood: Too many arguments"
			return 1
		fi
	fi

	zec_send "version >$tmpfilenam"
	zec_parseresponse
	for i in ${(f)"$(<$tmpfilenam)"}
	do
		case "$i" in
			(An update consists of (#b)([[:digit:]]##) empire time units.)
			etus="$match[1]"
			;;
			((#b)([[:digit:]]##) babies eat ([[:digit:].]##) units of food becoming adults.)
			babyeatrate=$(( match[2] / match[1] ))
			;;
			(In one time unit, (#b)([[:digit:]]##) people eat ([[:digit:].]##) units of food.)
			eatrate=$(( match[2] / match[1] ))
			;;
			((#b)([[:digit:]]##) civilians will give birth to ([[:digit:].]##) babies per etu.)
			civbirthrate=$(( match[2] / match[1] ))
			;;
			((#b)([[:digit:]]##) uncompensated workers will give birth to ([[:digit:].]##) babies.)
			uwbirthrate=$(( match[2] / match[1] ))
			;;
			(Options enabled in this game:)
			optionstate=enabled
			;;
			(Options disabled in this game:)
			optionstate=disabled
			;;
			(*BIG_CITY*)
			[[ optionstate == "enabled" ]] && gameoptions[BIG_CITY]=enabled
			;;
			(*NOFOOD*)
			[[ optionstate == "enabled" ]] && gameoptions[NOFOOD]=enabled
			;;
		esac
	done

	rm ${tmpfilenam}

	if [[ gameoptions[NOFOOD] == "enabled" ]];
	then
		print "setfood: food is not required in this game"
		return 1
	fi

	zec_send "dump * >$tmpfilenam"
	zec_parseresponse

	colheads=( ${(z)${(M)${(f)"$(<$tmpfilenam)"}:#*coast*}} )
	for i in ${(M)${(f)"$(<$tmpfilenam)"}:#[[:digit:]-]## [[:digit:]-]## *}
	do
	z=(${(z)i})
	civ=$z[$colheads[(i)civ]]
	mil=$z[$colheads[(i)mil]]
	uw=$z[$colheads[(i)uw]]
	curthresh=$z[$colheads[(i)f_dist]]
	(( thresh = int(ceil(num * etus * eatrate * (civ + uw + mil ) + 2 * babyeatrate * (civ * civbirthrate + uw * uwbirthrate))) ))
	if [[ $thresh -gt $curthresh ]]; then
		print "th f $z[1],$z[2] $thresh"
		zec_send "th f $z[1],$z[2] $thresh"
		zec_parseresponse
	fi
	done
}

# prodflow <sects>
zec_tool_prodflow()
{
	local tmpfilenam="${TMPDIR:-/tmp}/zecprodflow$$"
	local sectortype sectormake prodtype uses
	typeset -A produced used
	local u types

	if [[ $# -lt 1 ]]; then
		sects="*"
	else
		sects="$1"
	fi

	zec_send "prod $sects >$tmpfilenam"
	zec_parseresponse
	for i in ${(f)"$(<$tmpfilenam)"}
	do
		case "$i" in
			([ ]##[0-9-]##,[0-9-]## ##(#b)([^ ]) ##[0-9]##\% ##[0-9]## ##([0-9.]##[a-z]#) ##[0-9.]## ##\$[0-9]## (*) [0-9.]##)
			sectortype="$match[1]"
			sectormake="$match[2]"
			usage="$match[3]"
			[[ -n "$ZEC_DEBUG" ]] && print "$sectortype makes $sectormake"
			if [[ ${sectormake[-1]} == [a-z] ]]; then
			  prodtype=${sectormake[-1]}
			else
			  case "$sectortype" in
			    (t)
			    prodtype="tech"
			    ;;
			    (l)
			    prodtype="edu"
			    ;;
			    (p)
			    prodtype="happiness"
			    ;;
			    (*)
			    prodtype="unknown"
			    ;;
			  esac
			fi

			(( produced[$prodtype] += ${sectormake%[a-z]} ))

			uses=(${=usage})
			for u in ${uses[1,$#uses/2]}
			do
			  prodtype=${u[-1]}
			  (( used[$prodtype] += ${u%[a-z]} ))
			done
			;;
			(*)
			# silently throw away
			;;
		esac
	done

	rm ${tmpfilenam}

	printf "\nTO BE PRODUCED/USED\n"

	types=(${(k)produced} ${(k)used})
	typeset -U types

	for prodtype in ${(o)types}
	do
	  case "$prodtype" in
	      (tech)
	        printf "%-10s: %4.2f\n" "$prodtype" "${produced[$prodtype]}"
	      ;;
	      (happiness|edu)
	        printf "%-10s: %d\n" "$prodtype" "${produced[$prodtype]}"
	      ;;
	      (*)
	        printf "%-10s: %6d - %6d = %6d\n" "$prodtype" "${produced[$prodtype]}" "${used[$prodtype]}" "$(( 0$produced[$prodtype] - 0${used[$prodtype]} ))"
	      ;;
	  esac
	done


}


twitter() {
	local g

	while getopts :K opt
	do
	  case "$opt" in
	  (K)
	    zec_force_disconnect=true
	    ;;
	  (*)
	    return 66
	    ;;
	  esac
	done

	[[ -r ~/.zecrc ]] && parse_config ~/.zecrc

	case $#games in
	0)
		;;
	1) set -- ${(z)games[1]}
	shift
		;;
	*) for i in $games; do g=(${(z)i}); printf "%-20s %20s:%s\n" ${g[1]} ${g[4]} ${g[5]}; done
		print "FIXME: should give a menu here"
		set -- ${(z)games[1]}
	shift
		;;
	esac

	if [[ -z "$1" ]] || [[ -z "$2" ]] || [[ -z "$3" ]] || [[ -z "$4" ]];
	then
		print "usage: $SCRIPTNAME country password host port"
		print "or"
		return 2
	fi

	if zelogin $1 $2 $3 $4
	then
		zec_parseresponse
		# set up history
		local zec_savedir="${5/(#s)\~/$HOME}"
		[[ -n "$5" ]] && [[ -d "${zec_savedir}" ]] &&
			history -ap "${zec_savedir}"/history || history -ap ${TMPDIR:-/tmp}/zec_history.$$

		while zec_command
		do
			zec_parseresponse
		done
	else
		print "Boo, you suck.  $?"
		return 2
	fi

}

[[ -n "$ZEC_DEBUG" ]] && unset TCP_SILENT
typeset -A zec_aliases
zec_aliases=(exit quit jack "tmp jk")
typeset -a zec_tools
zec_tools=(history tmp alias foreach setfood prodflow)

SCRIPTNAME="$0"

twitter "$@"