v-add-user-notification 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/bin/bash
  2. # info: add user notification
  3. # options: USER TOPIC NOTICE [TYPE]
  4. #
  5. # The function adds user notification.
  6. #----------------------------------------------------------#
  7. # Variable&Function #
  8. #----------------------------------------------------------#
  9. # Argument definition
  10. user=$1
  11. topic=$(echo $2 |sed "s/'/%quote%/g")
  12. notice=$(echo $3 |sed "s/'/%quote%/g")
  13. type=$4
  14. # Includes
  15. source $VESTA/func/main.sh
  16. source $VESTA/conf/vesta.conf
  17. #----------------------------------------------------------#
  18. # Verifications #
  19. #----------------------------------------------------------#
  20. check_args '2' "$#" 'USER TOPIC NOTICE [TYPE]'
  21. is_format_valid 'user' 'topic' 'notice'
  22. is_object_valid 'user' 'USER' "$user"
  23. #----------------------------------------------------------#
  24. # Action #
  25. #----------------------------------------------------------#
  26. # Defining notification id
  27. if [ -e "$USER_DATA/notifications.conf" ]; then
  28. nid=$(grep "NID=" $USER_DATA/notifications.conf |cut -f 2 -d \')
  29. nid=$(echo "$nid" |sort -n |tail -n1)
  30. if [ ! -z "$nid" ]; then
  31. nid="$((nid +1))"
  32. else
  33. nid=1
  34. fi
  35. else
  36. nid=1
  37. fi
  38. # Generating timestamp
  39. time_n_date=$(date +'%T %F')
  40. time=$(echo "$time_n_date" |cut -f 1 -d \ )
  41. date=$(echo "$time_n_date" |cut -f 2 -d \ )
  42. # Concatenating string
  43. str="NID='$nid' TOPIC='$topic' NOTICE='$notice' TYPE='$type'"
  44. str="$str ACK='no' TIME='$time' DATE='$date'"
  45. # Adding to config
  46. echo "$str" >> $USER_DATA/notifications.conf
  47. # Changing permissions
  48. chmod 660 $USER_DATA/notifications.conf
  49. #----------------------------------------------------------#
  50. # Vesta #
  51. #----------------------------------------------------------#
  52. # Updating notification counter
  53. if [ -z "$(grep NOTIFICATIONS $USER_DATA/user.conf)" ]; then
  54. sed -i "s/^TIME/NOTIFICATIONS='yes'\nTIME/g" $USER_DATA/user.conf
  55. else
  56. update_user_value "$user" '$NOTIFICATIONS' "yes"
  57. fi
  58. exit