Skip to content

Instantly share code, notes, and snippets.

@coderofsalvation
Created May 24, 2012 07:35
Show Gist options
  • Save coderofsalvation/2780042 to your computer and use it in GitHub Desktop.
Save coderofsalvation/2780042 to your computer and use it in GitHub Desktop.
playterm - a console utility to upload/record playterm.org recordings (wrapper around ttyrec)
#!/bin/bash
#
# Author: Coder of Salvation /Leon van Kammen
# Website: www.playterm.org
# Date: 2012-05-15
# Function: console frontend to playterm.org (wrapper around ttyrec)
#
# Copyright (C) 2012, Coder of Salvation / Leon van Kammen
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
###
# Variables
###
SELF_ABS=`readlink -f $0`
# Absolute path this script is in, thus /home/user/bin
SELF_PATH=`dirname $SELF_ABS`
SELF=$( basename $0 )
mydir=$(pwd)
C_NORMAL="\E[0;37;40m"
C_BOLD="\E[1;37;40m"
C_BRIGHT="\E[1;33;40m"
errmsg_manual="please install ttyrec manually.."
deps_playterm=('cut' 'printf' 'sed' 'read' 'grep')
deps_install=('gcc' 'make' 'cut' 'patch' 'wget')
###
# Patches
###
patch_Makefile_out_of_ptys='--- Makefile 2006-06-11 17:52:50.000000000 +0200
+++ ../ttyrec-1.0.8.new/Makefile 2012-05-23 16:33:39.417434479 +0200
@@ -1,5 +1,5 @@
CC = gcc
-CFLAGS = -O2
+CFLAGS = -O2 -DHAVE_openpty -I/usr/include/libbsd
VERSION = 1.0.8
TARGET = ttyrec ttyplay ttytime
@@ -10,13 +10,13 @@
all: $(TARGET)
ttyrec: ttyrec.o io.o
- $(CC) $(CFLAGS) -o ttyrec ttyrec.o io.o
+ $(CC) $(CFLAGS) -o ttyrec ttyrec.o io.o -lutil
ttyplay: ttyplay.o io.o
- $(CC) $(CFLAGS) -o ttyplay ttyplay.o io.o
+ $(CC) $(CFLAGS) -o ttyplay ttyplay.o io.o -lutil
ttytime: ttytime.o io.o
- $(CC) $(CFLAGS) -o ttytime ttytime.o io.o
+ $(CC) $(CFLAGS) -o ttytime ttytime.o io.o -lutil
clean:
rm -f *.o $(TARGET) ttyrecord *~
'
###
# private functions
###
_check_ttyrec(){
_notify "checking for ttyrec.. " 1
ttyrec=$(which ttyrec 2> /dev/null)
if [[ $? != 0 ]]; then
printf "$C_BRIGHT[!error]\n"
read -p "[!] could not find ttyrec..wanna try download/compile it automatically? (y/n) $C_NORMAL" installit
if [ $installit == "y" ]; then install; fi
exit
fi
printf "$C_BOLD[OK]$C_NORMAL\n"
}
_check_dependancies(){
_check_ttyrec
apps="$1"
for app in $apps; do
echo " which $app"
ok=$(which $app)
if [[ $? != 0 ]]; then _notify "it seems $app is not installed..please do so"; exit; fi
done
}
_check_libbsd(){
_notify "checking for libbsd-[development] library.. " 1
libbsd=$(find /usr/include -name "*libutil.h*")
if [[ $? != 0 ]]; then printf "[error!]\n"; echo "[!] please install this missing item first..exiting"; exit; fi
printf "$C_BOLD[OK]$C_NORMAL\n"
}
_install_ttyrec(){
cp ttyrec /usr/bin/.
if [[ $? != 0 ]]; then
read -p "could not install ttyrec to /usr/bin..please specify dir: " dir
cp ttyrec ttyplay ttytime "$dir"
if [[ $? != 0 ]]; then return 1; fi
cp $0 "$dir"
fi
return 0
}
_notify(){
printf "$C_BRIGHT[ ] $1$C_NORMAL"
if [[ ${#2} < 1 ]]; then printf "\n"; fi
}
_unpack(){
pkgtar="$1"
pkgdir="$2"
errmsg="$3"
if [ -f "$pkgtar" ]; then
cd /tmp
_notify "unpacking sourcecode $pkgtar"
tar -xvf "$pkgtar" | while read line; do echo "[C] $line"; done
cd "$pkgdir"
else echo "$errmsg"; fi
}
_compile(){
_notify "compiling.."
pkgdir="$1"
make clean; make | while read line; do echo "[C] $line"; done
if [[ ! -f "ttyrec" ]]; then echo "[ ] something went wrong";
else
_notify "compiling done..testing ttyrec"
_test_ttyrec
_detect_path "stderr.txt"
if [[ $? == 0 ]]; then return 0; fi
fi
return 1
}
_test_ttyrec(){
trap 'printf "$C_BRIGHT[!] ttyrec failed :($C_NORMAL\n"' INT TERM EXIT
(./ttyrec test.ttyrec 1> stdout.txt 2> stderr.txt &)
sleep 1s
pgrep ttyrec | while read pid; do kill -9 $pid; done
trap - INT TERM EXIT
}
_test_executable(){
ttyrec=$(which ttyrec)
if [[ $? != 0 ]]; then printf "$C_BRIGHT[!] ttyrec cannot be found using your \$PATH variable..exiting..$C_BOLD"; exit; fi
}
_detect_path(){
errlog="$1"
problem_outofpty=$(cat "$errlog" | grep "Out of pty")
if [[ ${#problem_outofpty} > 0 ]]; then
printf "$C_BRIGHT[!] found 'Out of pty'-problem..patching$C_NORMAL\n";
printf "%s" "$patch_Makefile_out_of_ptys" > patch.diff
patch -p0 -i patch.diff | while read line; do echo "[C] $line"; done
if [[ $? != 0 ]]; then echo "[!] patching went wrong..$errmsg_manual"; exit;
else _compile; return 1; fi
fi
return 0
}
###
# public functions
###
# install: installs playterm/ttyrec
install(){
pkg='http://0xcc.net/ttyrec/ttyrec-1.0.8.tar.gz'
pkgtar=$(basename "$pkg")
pkgdir=$(echo "$pkgtar" | sed 's/\.tar.*//g')
errmsg="[!] could not download ttyrec..$errmsg_manual"
_check_dependancies $deps_install
if [ ! -f "/tmp/$pkgtar" ]; then
_notify "downloading sourcecode $pkgtar"
wget "$pkg" -O "/tmp/$pkgtar"
if [[ $? != 0 ]]; then echo "$errmsg"; exit; fi
fi
_unpack "/tmp/$pkgtar" "$pkgdir" "$errmsg"
_compile "$pkgdir"
_notify "btw. LEAVE BRITNEY ALONE!! :P"
if [[ $? == 0 ]]; then
echo "[ ] it worked! copying.."
_install_ttyrec && _test_executable
fi
_notify "installation done"
cd "$mydir"
}
# record <yourfile.ttyrec>: starts recording
record(){
outfile="$1"
if [[ ${#outfile} < 1 ]]; then usage; exit; fi
_notify "recording started"
echo "[ ] type 'exit' to stop recording"
ttyrec "$outfile"
echo ¨¨; _notify "done..created $C_NORMAL'"$(pwd)"'/$outfile'\n"
}
# push <yourfile.ttyrec>: pushes recording to server
push(){
_notify "push not implemented yet!"; exit
}
_usage(){
echo ""; grep "^[^_].\+(){$" $0 | grep -v manual | while read line; do
local func=$(echo "$line" | sed "s/(){//g")
local comment=$(grep -C0 -A0 -B1 "$func(){" $0 | sed 'N;s/\n.*//g;' )
local cmd=$(echo "$comment" | cut -f1 -d":" )
local info=$(echo "$comment" | cut -f2 -d":" )
printf " $C_BOLD$SELF $C_BRIGHT%-27s $C_NORMAL<-%s\n" "$cmd" "$info" | sed "s/\# //g"
done; printf "\n beta: pleaz send contributions to [email protected]\n\n";
}
manual(){
if [ "$1" != "--manual" ]; then return 0; fi
local tempfile="/tmp/foo.1"
hash pod2man 2>&-
if [ $? == 1 ]; then echo "please install perl"; exit; fi
SCRIPT=`readlink -f $0`
echo "(please wait..)"
pod2man $SCRIPT > "$tempfile"
clear
if [ -f "$tempfile" ]; then man "$tempfile"; rm "$tempfile"; fi
exit 0
}
manual "$1" && test $# -lt 1 && _usage && exit 65
_check_dependancies $deps_playterm && "$@"
exit 0
<<=cut
=head1 NAME
playterm - a console utility to upload/record playterm recordings
=head1 SYNOPSIS
PLAYTERM.ORG is intended to raise the skills of terminal CLI users.
PLAYTERM.ORG is to share skills and inspire others.
PLAYTERM.ORG wants to push forward a new way of education.
=head1 DESCRIPTION
PLAYTERM.ORG can record/replay your terminalsessions online, by providing embedcodes which you can put onto your blog, article or website
=head1 LICENSE
GNU AGPL v3 License (so everybody can enjoy it)
=head1 AUTHOR
Coder of Salvation
=head1 CONTRIBUTORS
(none yet)
=cut
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment