#!/bin/sh # tmww plugin: versionlog # whatis: try determine player's client version using online list and client versions log # conflicts: log (versionlog logs also online/offline users) # depends: - # recommends: - # this file is part of tmww - the mana world watcher # willee, 2012-2014 # GPL v3 # check if not run as plugin if [ "$TMWW_PLUGINS" != "yes" ] ; then echo >&2 "This script is tmww plugin and rely heavily on it's facilities." exit 1 fi help_versionlog() { cat << EOF versionlog -- try determine player's client version using online list and client versions log. versionlog generates extended version of "log" plugin so there's no reason to run both. IMPORTANT: run "versionlog" action only after you performed "fetch" Recommended execution interval equals to max between online list update time or versions log file. Recommended way is something like: $ watch -pn 20 tmww versionlogconf Command line options: -- (no commands) EOF } [ "$TMWW_PLUGINHELP" = "yes" ] && help_versionlog && return 0 [ "$TMWW_PLUGINEXPORT" = "yes" ] && return 0 TMWW_LOGLOCK="${TMWW_LOCK}/tmww-log-${servername}" set_log_lock() { check_lock "log" "${TMWW_LOGLOCK}" 35 } unset_log_lock() { rmdir "${TMWW_LOGLOCK}" 2>/dev/null } # check if nothing to do processversions='' [ -s "${list_logon}" ] && processversions="yes" [ "${processversions}" != "yes" ] && [ ! -s "${list_logoff}" ] && return 0 set_log_lock # prepare report file TMWW_VERSIONREPORT="${TMWW_VERSIONREPORT:-${HOME}/log/tmww}" log_path="${TMWW_VERSIONREPORT}/${servername}/$(date -u +%Y-%m)" check_dir "$log_path" log_file="$log_path/$(date -u +%d).yml" # prepare work files TMWW_VERSIONCACHE="${TMWW_VERSIONCACHE:-${HOME}/log/versions}/${servername}" check_dir "${TMWW_VERSIONCACHE}" ver_work="${TMWW_VERSIONCACHE}/active.txt" ver_old="${TMWW_VERSIONCACHE}/$(date -u +%Y-%m-%d).txt" # prepare versions url LINK="${TMWW_VERSIONURLBASE:-http://updates.themanaworld.org/updates/versions/}$(date -u +%Y-%m-%d).txt" # "standart" log part exec 3>&1 1>>${log_file} echo "- time: \"${servertime}\"" if [ -s "${list_logon}" ]; then echo " logon: [ $( make_qcsv < "${list_logon}" ) ]"; fi if [ -s "${list_logoff}" ]; then echo " logoff: [ $( make_qcsv < "${list_logoff}" ) ]"; fi exec 1>&3 3>&- # ----- this part will be skipped if noone to detect if [ -s "${list_logon}" ]; then logon_clients=$( cat "$list_logon" ) logon_count=$( wc -l < "$list_logon" ) # versions echo >&2 ${hlon}tmww plugin: version2${hloff} echo >&2 ${hlon}Fetching!${hloff} $FETCH $LINK > "$ver_work" [ $? != 0 ] && { rm -f "$ver_work" ; echo >&2 "Failed to retrieve versions file. Aborting." ; return 1 ; } if [ ! -f "$ver_old" ]; then mv $ver_work $ver_old echo >&2 "Aborting due to dry run." return 0 fi new_clients=$( sed "1,$( wc -l < ${ver_old} )d" ${ver_work} | sed 's/\\/\\\\/g;s/"/\\"/g' ) new_count=$( printf "%s\n" "$new_clients" | wc -l ) [ $logon_count -eq 1 ] && [ -n "$new_clients" ] && [ $new_count -eq 1 ] && echo >&2 ${hlon}Player detected:${hloff} ${logon_clients} fi # ----- proceeding with log exec 3>&1 1>>${log_file} if [ -n "$new_clients" ]; then if [ $logon_count -eq 1 ] && [ $new_count -eq 1 ]; then new_servertime="${new_clients%%]*}" new_servertime="${new_servertime#*"["}" new_clients="${new_clients#*] }" echo " detected: [ \"$new_servertime\", \"${logon_clients}\", \"${new_clients}\" ]" else if [ "$TMWW_VERSIONSUMMARY" = "yes" ]; then echo " versions: " printf "%s\n" "${new_clients}" | cut -c 9- | sort | uniq -c | sort -rn | awk -- ' { printf " - [ \"%s\"",$1; sub(" *[^ ]* *",""); printf ", \"%s\" ]\n",$0 }' else echo " clients: " printf "%s\n" "${new_clients}" | awk -- ' { sub("\\[",""); sub("] ","\",\""); printf " - [ \"%s\" ]\n",$0 }' fi fi fi exec 1>&3 3>&- # if we worked with versions file - move current to old [ "${processversions}" = "yes" ] && mv -f "$ver_work" "$ver_old" unset_log_lock return 0