freedombone-mesh-blog 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Blogging functions for mesh clients
  12. #
  13. # License
  14. # =======
  15. #
  16. # This program is free software: you can redistribute it and/or modify
  17. # it under the terms of the GNU Affero General Public License as published by
  18. # the Free Software Foundation, either version 3 of the License, or
  19. # (at your option) any later version.
  20. #
  21. # This program is distributed in the hope that it will be useful,
  22. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. # GNU Affero General Public License for more details.
  25. #
  26. # You should have received a copy of the GNU Affero General Public License
  27. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. PROJECT_NAME='freedombone'
  29. export TEXTDOMAIN=${PROJECT_NAME}-mesh-blog
  30. export TEXTDOMAINDIR="/usr/share/locale"
  31. IPFS_PATH=/usr/bin
  32. IPFS_COMMAND=$IPFS_PATH/ipfs
  33. IPFS_PUBLIC=/home/$USER/.ipfs-public
  34. BLOG_PATH=~/CreateBlog
  35. BLOG_CONTENT_PATH=$BLOG_PATH/content
  36. CURRENT_BLOG_INDEX=/home/$USER/.blog-index
  37. BLOG_EDITOR='pluma'
  38. DEFAULT_BLOG_TITLE=$"Freedombone Blog"
  39. function remove_bad_blog_links {
  40. find ./ -type f -name "*.css" -exec sed -i -e '/googleapi/d' {} \;
  41. find ./ -type f -name "*.scss" -exec sed -i -e '/googleapi/d' {} \;
  42. find ./ -type f -name "*.html" -exec sed -i -e '/googleapi/d' {} \;
  43. find ./ -type f -name "*.css" -exec sed -i -e '/bootstrapcdn/d' {} \;
  44. find ./ -type f -name "*.scss" -exec sed -i -e '/bootstrapcdn/d' {} \;
  45. find ./ -type f -name "*.html" -exec sed -i -e '/bootstrapcdn/d' {} \;
  46. }
  47. function ipfs_publish {
  48. DIR_TO_CHECK=/home/$USER/Public
  49. if [ ! -d "$DIR_TO_CHECK" ]; then
  50. return
  51. fi
  52. echo ''
  53. echo $'Publishing to IPFS. This may take some time...'
  54. OLD_STAT_FILE=/home/$USER/.old_stat.txt
  55. NEW_STAT=$(stat -t "$DIR_TO_CHECK")
  56. $IPFS_COMMAND add -rq "/home/$USER/Public" | tail -n 1 > "$IPFS_PUBLIC"
  57. echo "$NEW_STAT" > "$OLD_STAT_FILE"
  58. if [ -f "$IPFS_PUBLIC" ]; then
  59. IPFS_PUBLIC_ID=$(cat "$IPFS_PUBLIC")
  60. $IPFS_COMMAND name publish "/ipfs/$IPFS_PUBLIC_ID"
  61. fi
  62. }
  63. function regenerate_blog {
  64. clear
  65. echo ''
  66. echo $'Regenerating blog...'
  67. cd $BLOG_PATH || exit 246872648
  68. if grep -q "SITENAME=u'${DEFAULT_BLOG_TITLE}'" $BLOG_PATH/pelicanconf.py; then
  69. TOX_NICK=$(toxid --showuser)
  70. BLOG_TITLE=$"${TOX_NICK}'s Blog"
  71. sed -i "s|SITENAME=.*|SITENAME=u\"${BLOG_TITLE}\"|g" $BLOG_PATH/pelicanconf.py
  72. fi
  73. make html
  74. cd $BLOG_PATH || exit 23682468
  75. remove_bad_blog_links
  76. ipfs_publish
  77. }
  78. function view_blog {
  79. freedombone-mesh-visit-site '/Blog'
  80. exit 0
  81. }
  82. function new_blog {
  83. DATESTR=$(date "+%Y-%m-%d %H:%M:%S")
  84. { echo $'Title: Blog Post Title';
  85. echo $"Date: ${DATESTR}";
  86. echo $"Author: $(toxid --showuser)";
  87. echo $'Category: default';
  88. echo $'Tags: blog, tag';
  89. echo '';
  90. echo $'Add your text here';
  91. echo '';
  92. echo -n $'To include an image copy it into the ~/CreateBlog/content/images directory, ';
  93. echo $'then link to it with:';
  94. echo '';
  95. echo $'![My image]({filename}images/myimage.jpg)';
  96. echo ''; } > ~/.new-blog-entry
  97. $BLOG_EDITOR ~/.new-blog-entry
  98. if grep -q $"Add your text here" ~/.new-blog-entry; then
  99. return
  100. fi
  101. if grep -q $"Blog Post Title" ~/.new-blog-entry; then
  102. return
  103. fi
  104. if [ ! -f "$CURRENT_BLOG_INDEX" ]; then
  105. echo '0' > "$CURRENT_BLOG_INDEX"
  106. fi
  107. # move to the content directory
  108. CURRENT_INDEX=$(cat "$CURRENT_BLOG_INDEX")
  109. # shellcheck disable=SC2086
  110. mv ~/.new-blog-entry $BLOG_CONTENT_PATH/${CURRENT_INDEX}_post.md
  111. # increment the index
  112. CURRENT_INDEX=$((CURRENT_INDEX + 1))
  113. echo "$CURRENT_INDEX" > "$CURRENT_BLOG_INDEX"
  114. regenerate_blog
  115. }
  116. function edit_blog {
  117. if [ ! -f "$CURRENT_BLOG_INDEX" ]; then
  118. return
  119. fi
  120. CURRENT_INDEX=$(cat "$CURRENT_BLOG_INDEX")
  121. PREVIOUS_INDEX=$((CURRENT_INDEX - 1))
  122. LAST_BLOG_ENTRY=$BLOG_CONTENT_PATH/${PREVIOUS_INDEX}_post.md
  123. if [ ! -f $LAST_BLOG_ENTRY ]; then
  124. return
  125. fi
  126. $BLOG_EDITOR $LAST_BLOG_ENTRY
  127. regenerate_blog
  128. }
  129. function delete_blog {
  130. if [ ! -f "$CURRENT_BLOG_INDEX" ]; then
  131. return
  132. fi
  133. CURRENT_INDEX=$(cat "$CURRENT_BLOG_INDEX")
  134. PREVIOUS_INDEX=$((CURRENT_INDEX - 1))
  135. LAST_BLOG_ENTRY=$BLOG_CONTENT_PATH/${PREVIOUS_INDEX}_post.md
  136. if [ ! -f $LAST_BLOG_ENTRY ]; then
  137. return
  138. fi
  139. if ! zenity --question --title=$'Delete the previous blog entry' --text=$"\\nAre you sure that you wish to delete the previous blog entry?" --ok-label=No --cancel-label=Yes --width=300; then
  140. rm $LAST_BLOG_ENTRY
  141. if [ "$CURRENT_INDEX" -gt 0 ]; then
  142. CURRENT_INDEX=$PREVIOUS_INDEX
  143. echo "$CURRENT_INDEX" > "$CURRENT_BLOG_INDEX"
  144. else
  145. rm -f "$CURRENT_BLOG_INDEX"
  146. fi
  147. regenerate_blog
  148. fi
  149. }
  150. function change_theme {
  151. THEMES=()
  152. for d in $BLOG_PATH/themes/*/ ; do
  153. THEME_NAME=$(echo "$d" | awk -F '/' '{print $6}')
  154. THEMES+=("$THEME_NAME")
  155. done
  156. n=1
  157. curr_theme_index=
  158. if [ -f "/home/$USER/.blog-theme-index" ]; then
  159. curr_theme_index=$(cat "/home/$USER/.blog-theme-index")
  160. fi
  161. if [ -f /tmp/.blog-themes ]; then
  162. rm /tmp/.blog-themes
  163. fi
  164. # shellcheck disable=SC2068
  165. for a in ${THEMES[@]}
  166. do
  167. echo "$n $a" >> /tmp/.blog-themes
  168. n=$((n+1))
  169. done
  170. CHOSEN_THEME_INDEX=$(
  171. # shellcheck disable=SC2002
  172. cat /tmp/.blog-themes | \
  173. awk -F ' ' '{
  174. for(i=1;i<=NF;i++){
  175. print $i;
  176. }
  177. }' | \
  178. zenity --list \
  179. --title=$'Select Blog Theme' \
  180. --column=$'Index' --column=$'Theme' \
  181. --print-column=1 --hide-column=1 --width=300 --height=400)
  182. rm /tmp/.blog-themes
  183. if [ ! "$CHOSEN_THEME_INDEX" ]; then
  184. exit 1
  185. fi
  186. echo "$CHOSEN_THEME_INDEX" > "/home/$USER/.blog-theme-index"
  187. CHOSEN_THEME_INDEX=$((CHOSEN_THEME_INDEX - 1))
  188. CHOSEN_THEME=${THEMES[$CHOSEN_THEME_INDEX]}
  189. cd "$BLOG_PATH/themes/$CHOSEN_THEME" || exit 346746824
  190. remove_bad_blog_links
  191. if grep -q "THEME=" $BLOG_PATH/pelicanconf.py; then
  192. sed -i "s|THEME=.*|THEME='themes/${CHOSEN_THEME}'|g" $BLOG_PATH/pelicanconf.py
  193. else
  194. echo "THEME='themes/${CHOSEN_THEME}'" >> $BLOG_PATH/pelicanconf.py
  195. fi
  196. regenerate_blog
  197. }
  198. function menu_blog {
  199. data=$(zenity --list 1 $"View a blog" 2 $"New blog entry" 3 $"Edit the previous blog entry" 4 $"Delete the previous blog entry" 5 $"Change theme" --column="id" --title $"Blogging" --column=$"Choose an operation:" --hide-column=1 --print-column=1 --height=250)
  200. sel=$?
  201. case $sel in
  202. 1) exit 1;;
  203. 255) exit 1;;
  204. esac
  205. case $data in
  206. 1) view_blog;;
  207. 2) new_blog;;
  208. 3) edit_blog;;
  209. 4) delete_blog;;
  210. 5) change_theme;;
  211. esac
  212. }
  213. menu_blog
  214. exit 0