freedombone-mesh-blog 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 ipfs_publish {
  40. DIR_TO_CHECK=/home/$USER/Public
  41. if [ ! -d $DIR_TO_CHECK ]; then
  42. return
  43. fi
  44. echo ''
  45. echo $'Publishing to IPFS. This may take some time...'
  46. OLD_STAT_FILE=/home/$USER/.old_stat.txt
  47. NEW_STAT=$(stat -t $DIR_TO_CHECK)
  48. echo $($IPFS_COMMAND add -rq /home/$USER/Public | tail -n 1) > $IPFS_PUBLIC
  49. echo "$NEW_STAT" > $OLD_STAT_FILE
  50. if [ -f $IPFS_PUBLIC ]; then
  51. IPFS_PUBLIC_ID=$(cat $IPFS_PUBLIC)
  52. $IPFS_COMMAND name publish /ipfs/$IPFS_PUBLIC_ID
  53. fi
  54. }
  55. function regenerate_blog {
  56. clear
  57. echo ''
  58. echo $'Regenerating blog...'
  59. cd $BLOG_PATH
  60. if grep -q "SITENAME=u'${DEFAULT_BLOG_TITLE}'" $BLOG_PATH/pelicanconf.py; then
  61. TOX_NICK=$(toxid --showuser)
  62. BLOG_TITLE=$"${TOX_NICK}'s Blog"
  63. sed -i "s|SITENAME=.*|SITENAME=u\"${BLOG_TITLE}\"|g" $BLOG_PATH/pelicanconf.py
  64. fi
  65. make html
  66. ipfs_publish
  67. }
  68. function view_blog {
  69. freedombone-mesh-visit-site '/Blog'
  70. exit 0
  71. }
  72. function new_blog {
  73. DATESTR=$(date "+%Y-%m-%d %H:%M:%S")
  74. echo $'Title: Blog Post Title' > ~/.new-blog-entry
  75. echo $"Date: ${DATESTR}" >> ~/.new-blog-entry
  76. echo $"Author: $(toxid --showuser)" >> ~/.new-blog-entry
  77. echo $'Category: default' >> ~/.new-blog-entry
  78. echo $'Tags: blog, tag' >> ~/.new-blog-entry
  79. echo '' >> ~/.new-blog-entry
  80. echo $'Add your text here' >> ~/.new-blog-entry
  81. echo '' >> ~/.new-blog-entry
  82. $BLOG_EDITOR ~/.new-blog-entry
  83. if grep -q $"Add your text here" ~/.new-blog-entry; then
  84. return
  85. fi
  86. if grep -q $"Blog Post Title" ~/.new-blog-entry; then
  87. return
  88. fi
  89. if [ ! -f $CURRENT_BLOG_INDEX ]; then
  90. echo '0' > $CURRENT_BLOG_INDEX
  91. fi
  92. # move to the content directory
  93. CURRENT_INDEX=$(cat $CURRENT_BLOG_INDEX)
  94. mv ~/.new-blog-entry $BLOG_CONTENT_PATH/${CURRENT_INDEX}_post.md
  95. # increment the index
  96. CURRENT_INDEX=$((CURRENT_INDEX + 1))
  97. echo "$CURRENT_INDEX" > $CURRENT_BLOG_INDEX
  98. regenerate_blog
  99. }
  100. function edit_blog {
  101. if [ ! -f $CURRENT_BLOG_INDEX ]; then
  102. return
  103. fi
  104. CURRENT_INDEX=$(cat $CURRENT_BLOG_INDEX)
  105. PREVIOUS_INDEX=$((CURRENT_INDEX - 1))
  106. LAST_BLOG_ENTRY=$BLOG_CONTENT_PATH/${PREVIOUS_INDEX}_post.md
  107. if [ ! -f $LAST_BLOG_ENTRY ]; then
  108. return
  109. fi
  110. $BLOG_EDITOR $LAST_BLOG_ENTRY
  111. regenerate_blog
  112. }
  113. function delete_blog {
  114. if [ ! -f $CURRENT_BLOG_INDEX ]; then
  115. return
  116. fi
  117. CURRENT_INDEX=$(cat $CURRENT_BLOG_INDEX)
  118. PREVIOUS_INDEX=$((CURRENT_INDEX - 1))
  119. LAST_BLOG_ENTRY=$BLOG_CONTENT_PATH/${PREVIOUS_INDEX}_post.md
  120. if [ ! -f $LAST_BLOG_ENTRY ]; then
  121. return
  122. fi
  123. dialog --title $"Delete the previous blog entry" \
  124. --backtitle $"Freedombone Mesh" \
  125. --defaultno \
  126. --yesno $"\nAre you sure that you wish to delete the previous blog entry?" 8 60
  127. sel=$?
  128. case $sel in
  129. 0) rm $LAST_BLOG_ENTRY
  130. if [ $CURRENT_INDEX -gt 0 ]; then
  131. CURRENT_INDEX=$PREVIOUS_INDEX
  132. echo "$CURRENT_INDEX" > $CURRENT_BLOG_INDEX
  133. else
  134. rm -f $CURRENT_BLOG_INDEX
  135. fi
  136. regenerate_blog
  137. ;;
  138. esac
  139. }
  140. function change_theme {
  141. THEMES=()
  142. for d in $BLOG_PATH/themes/*/ ; do
  143. THEME_NAME=$(echo "$d" | awk -F '/' '{print $6}')
  144. THEMES+=("$THEME_NAME")
  145. done
  146. themelist=""
  147. n=1
  148. theme_index=0
  149. curr_theme_index=
  150. if [ -f /home/$USER/.blog-theme-index ]; then
  151. curr_theme_index=$(cat /home/$USER/.blog-theme-index)
  152. fi
  153. for a in "${THEMES[@]}"
  154. do
  155. is_selected='off'
  156. if [ $curr_theme_index ]; then
  157. if [ $n -eq $curr_theme_index ]; then
  158. is_selected='on'
  159. fi
  160. else
  161. if [[ "$a" == 'Just-Read' ]]; then
  162. is_selected='on'
  163. fi
  164. fi
  165. themelist="$themelist $n $a $is_selected"
  166. n=$[n+1]
  167. theme_index=$[theme_index+1]
  168. done
  169. data=$(tempfile 2>/dev/null)
  170. trap "rm -f $data" 0 1 2 5 15
  171. dialog --backtitle $"Freedombone Mesh" \
  172. --title $"Select Blog Theme" \
  173. --radiolist $'Choose:' \
  174. 80 40 20 $themelist 2> $data
  175. sel=$?
  176. case $sel in
  177. 1) exit 1;;
  178. 255) exit 1;;
  179. esac
  180. CHOSEN_THEME_INDEX=$(cat $data)
  181. echo "$CHOSEN_THEME_INDEX" > /home/$USER/.blog-theme-index
  182. CHOSEN_THEME_INDEX=$((CHOSEN_THEME_INDEX - 1))
  183. CHOSEN_THEME=${THEMES[$CHOSEN_THEME_INDEX]}
  184. if grep -q "THEME=" $BLOG_PATH/pelicanconf.py; then
  185. sed -i "s|THEME=.*|THEME='themes/${CHOSEN_THEME}'|g" $BLOG_PATH/pelicanconf.py
  186. else
  187. echo "THEME='themes/${CHOSEN_THEME}'" >> $BLOG_PATH/pelicanconf.py
  188. fi
  189. regenerate_blog
  190. }
  191. function menu_blog {
  192. data=$(tempfile 2>/dev/null)
  193. trap "rm -f $data" 0 1 2 5 15
  194. dialog --backtitle $"Freedombone Mesh" \
  195. --title $"Blogging" \
  196. --radiolist $"Choose an operation:" 19 50 12 \
  197. 1 $"View a blog" on \
  198. 2 $"New blog entry" off \
  199. 3 $"Edit the previous blog entry" off \
  200. 4 $"Delete the previous blog entry" off \
  201. 5 $"Change theme" off \
  202. 6 $"Exit" off 2> $data
  203. sel=$?
  204. case $sel in
  205. 1) exit 1;;
  206. 255) exit 1;;
  207. esac
  208. case $(cat $data) in
  209. 1) view_blog;;
  210. 2) new_blog;;
  211. 3) edit_blog;;
  212. 4) delete_blog;;
  213. 5) change_theme;;
  214. 6) break;;
  215. esac
  216. }
  217. menu_blog
  218. exit 0