tazcraft annotate web/tazcraft.cgi @ rev 5

Quiet a few improvment to tazcraft and cgi interface
author Christophe Lincoln <pankso@slitaz.org>
date Sun Mar 19 22:29:36 2017 +0100 (2017-03-19)
parents d588912d23cd
children 4cfc84382409
rev   line source
pankso@4 1 #!/bin/sh
pankso@4 2 #
pankso@4 3 # tazcraft.cgi - A Minecraft server info/admin CGI interface
pankso@4 4 #
pankso@4 5 # Authors: Kayam Lincoln-Vazquez, Christophe Lincoln
pankso@4 6 # Copyright (C) 2017 SliTaz GNU/Linux - BSD License
pankso@4 7 #
pankso@4 8 . /usr/lib/slitaz/httphelper.sh
pankso@4 9 header
pankso@4 10
pankso@4 11 home="/home/minecraft"
pankso@4 12 server="$home/server"
pankso@4 13 config="$server/server.properties"
pankso@5 14 whitelist="${server}/whitelist.json"
pankso@4 15
pankso@5 16 # Get a key value from server config file
pankso@5 17 get_key() {
pankso@4 18 val=$(grep ^${1}= ${config} | cut -d "=" -f 2)
pankso@4 19 cat << EOT
pankso@4 20 <tr>
pankso@4 21 <td>$1</td>
pankso@4 22 <td>$val</td>
pankso@4 23 </tr>
pankso@4 24 EOT
pankso@4 25 }
pankso@4 26
pankso@5 27 # HTML footer
pankso@5 28 html_footer() {
pankso@5 29 cat << EOT
pankso@5 30 </div>
pankso@5 31 <footer style="text-align: center; margin: 10px; color: #888;">
pankso@5 32 &copy; $(date '+%Y') <a href="http://www.slitaz.org">SliTaz GNU/Linux</a>
pankso@5 33 </footer>
pankso@5 34 </body>
pankso@5 35 </htnl>
pankso@5 36 EOT
pankso@5 37 }
pankso@5 38
pankso@4 39 # HTML header
pankso@5 40 html_header() {
pankso@5 41 cat << EOT
pankso@4 42 <!DOCTYPE html>
pankso@4 43 <html>
pankso@4 44 <head>
pankso@4 45 <meta charset="utf-8" />
pankso@5 46 <title>Tazcraft - $1</title>
pankso@4 47 <link rel="stylesheet" href="http://play.slitaz.me/style.css" />
pankso@4 48 </head>
pankso@4 49 <body>
pankso@4 50 <header>
pankso@4 51 <div id="logo"></div>
pankso@4 52 <div id="network">
pankso@5 53 <a href="tazcraft.cgi">Tazcraft</a>
pankso@5 54 <a href="tazcraft.cgi?README">README</a>
pankso@5 55 <span style="color: #682368;">&hearts;</span>
pankso@4 56 <a href="http://play.slitaz.me">play.SliTaz.me</a>
pankso@4 57 </div>
pankso@4 58 <h1>Tazcraft Server</h1>
pankso@4 59 </header>
pankso@4 60 <div id="content">
pankso@5 61 EOT
pankso@5 62 }
pankso@4 63
pankso@5 64 #
pankso@5 65 # Handle GET commands
pankso@5 66 #
pankso@5 67
pankso@5 68 case " $(GET) " in
pankso@5 69
pankso@5 70 *\ README\ *)
pankso@5 71
pankso@5 72 if [ -f "../README" ]; then
pankso@5 73 README="../README"
pankso@5 74 else
pankso@5 75 README="/usr/share/doc/slitaz/tazcraft.txt"
pankso@5 76 fi
pankso@5 77
pankso@5 78 html_header "README"
pankso@5 79 echo "<h2>README</h2>"
pankso@5 80 echo "<pre>"
pankso@5 81 # Let have a few color in README
pankso@5 82 cat ${README} | sed \
pankso@5 83 -e "/^====.*/"d -e "/^----.*/"d \
pankso@5 84 -e s"#==\([^']*\)==#<h3 style='margin: 0;'>\1</h3>#"g \
pankso@5 85 -e s"#^ [\#|\$|\/]\([^']*\)#<span style='color: green;'>\0</span>#"g \
pankso@5 86 -e s"#http://\([^']*\).*#<a href='\0'>\0</a>#"g
pankso@5 87 echo "</pre>"
pankso@5 88 html_footer
pankso@5 89 exit 0 ;;
pankso@5 90
pankso@5 91 esac
pankso@5 92
pankso@5 93 #
pankso@5 94 # Home page with server information and configuration
pankso@5 95 #
pankso@5 96 html_header "Welcome"
pankso@5 97
pankso@5 98 uptime=$(ps | grep minecraft_server | grep -v grep | awk '{print $3}')
pankso@5 99 if [ ! "$uptime" ]; then
pankso@5 100 uptime="Not running"
pankso@5 101 fi
pankso@5 102
pankso@5 103 cat << EOT
pankso@4 104 <h2>Welcome to Tazcraft</h2>
pankso@5 105 <p>
pankso@5 106 A SliTaz GNU/Linux Minecraft server
pankso@5 107 - Uptime: <span style="color: green;">$uptime</span>
pankso@5 108 </p>
pankso@5 109
pankso@4 110 <pre>
pankso@5 111 <img src="minecraft.png" alt="*" />Server address: \
pankso@5 112 <span style="color: green; font-size: 20px;">play.slitaz.me</span>
pankso@4 113 </pre>
pankso@4 114
pankso@4 115 <h3>Configuration</h3>
pankso@4 116 <table>
pankso@4 117 <thead>
pankso@5 118 <td>$(gettext "Key")</td>
pankso@4 119 <td>$(gettext "Value")</td>
pankso@4 120 </thead>
pankso@4 121 EOT
pankso@4 122
pankso@5 123 for var in gamemode server-port level-name difficulty max-players \
pankso@5 124 allow-flight white-list
pankso@5 125 do
pankso@5 126 get_key ${var}
pankso@4 127 done
pankso@5 128 echo "</table>"
pankso@4 129
pankso@5 130 # White listed users names
pankso@5 131 if grep -q "white-list=true" ${config}; then
pankso@5 132 echo "<h3>White list</h3>"
pankso@5 133 echo "<pre>"
pankso@5 134 fgrep name ${whitelist} | cut -d '"' -f 4
pankso@5 135 echo "</pre>"
pankso@5 136 fi
pankso@5 137
pankso@5 138 html_footer
pankso@4 139 exit 0