Loading

Paste Script

  1. #!/bin/bash
  2. # Sticky notes script made for https://paste.openttdcoop.org
  3. # Script made by ^Spike^ (spike <at that place> openttdcoop <dots> org)
  4. # For any further questions try joining our IRC @ OFTC (#openttdcoop)
  5.  
  6. # h: Help
  7. # l: Language
  8. # v: Visibility
  9. # p: Password
  10. # t: Title
  11. # d: Data
  12.  
  13. PASTEURL="https://paste.openttdcoop.org"
  14.  
  15. LANGUAGE="text"
  16. PRIVATE="false"
  17. TITLE=""
  18. PASSWORD=""
  19.  
  20. usage()
  21. {
  22.     echo "#openttdcoop Paste Script"
  23.     echo "Script is made to work with sticky-notes v1.8"
  24.     echo "It may work with earlier/later versions but no guarantees"
  25.     echo ""
  26.     echo "Usage:"
  27.     echo "$0 [-l <language>|-t <title>|-d <data>|-p <password>|-v]"
  28.     echo ""
  29.     echo "  -l <language>"
  30.     echo "          Can be any language which is supported by sticky-notes"
  31.     echo "                  Default: text"
  32.     echo "  -t <title>"
  33.     echo "          The title you would like to give to your paste"
  34.     echo "                  Default: none"
  35.     echo "  -d <data>"
  36.     echo "          The data you would like to publish to the paste"
  37.     echo "          You can also pipe data into this script. In that case"
  38.     echo "          there is no need to give the -d option"
  39.     echo "  -v"
  40.     echo "          Set your paste to private. This will make your paste"
  41.     echo "          only be accessable if the proper hash has been supplied"
  42.     echo "          with the paste ID"
  43.     echo "  -p <password>"
  44.     echo "          Set a password on the paste which will be needed to see it"
  45.     echo "          Can be seen as more secure then private"
  46. }
  47.  
  48. while getopts "hl:vt:p:t:d:" OPTION
  49. do
  50.     case $OPTION in
  51.         h)
  52.             usage
  53.             exit 1
  54.             ;;
  55.         l)
  56.             LANGUAGE=$OPTARG
  57.             ;;
  58.         v)
  59.             PRIVATE="true"
  60.             ;;
  61.         t)
  62.             TITLE=$OPTARG
  63.             ;;
  64.         d)
  65.             DATA=$OPTARG
  66.             ;;
  67.         p)
  68.             PASSWORD=$OPTARG
  69.             ;;
  70.         ?)
  71.             usage
  72.             exit;;
  73.     esac
  74. done
  75.  
  76. if [ -z $DATA ]
  77. then
  78.     DATA=$(cat)
  79.     if [ -z "$DATA" ]
  80.     then
  81.         usage
  82.         exit 1
  83.     fi
  84. fi
  85.  
  86. TEMPFILE=$(mktemp sticky-notes.XXXXXXXX)
  87.  
  88. echo $DATA > $TEMPFILE
  89.  
  90. DATA_RETURNED=""
  91.  
  92. if $PRIVATE
  93. then
  94.     DATA_RETURNED=$(curl -s -d data="$DATA" -d language="$LANGUAGE" -d title="$TITLE" -d private="$PRIVATE" -d password="$PASSWORD" $PASTEURL/api/xml/create)
  95. else
  96.     DATA_RETURNED=$(curl -s -d data="$DATA" -d language="$LANGUAGE" -d title="$TITLE" -d password="$PASSWORD" $PASTEURL/api/xml/create)
  97. fi
  98.  
  99.  
  100. PASTEID=$(echo $DATA_RETURNED | grep -oPm1 "(?<=<id>)[^<]+")
  101. PASTEHASH=$(echo $DATA_RETURNED | grep -oPm1 "(?<=<hash>)[^<]+")
  102.  
  103. if [ -z $PASTEID ]
  104. then
  105.     echo "Woops something has gone wrong pasting!"
  106.     echo "We made a backup copy of you paste in $TEMPFILE"
  107.     exit 1
  108. fi
  109.  
  110. if [ -z $PASTEHASH ]
  111. then
  112.     echo "Paste created at: $PASTEURL/$PASTEID"
  113.     rm $TEMPFILE
  114. else
  115.     echo "Paste created at: $PASTEURL/$PASTEID/$PASTEHASH"
  116.     rm $TEMPFILE
  117. fi

Comments