Skip to content

Instantly share code, notes, and snippets.

@TehPeGaSuS
Last active April 28, 2024 20:53
Show Gist options
  • Save TehPeGaSuS/e68c680ec1bb8e5e9a9aff919041ab1e to your computer and use it in GitHub Desktop.
Save TehPeGaSuS/e68c680ec1bb8e5e9a9aff919041ab1e to your computer and use it in GitHub Desktop.
Some useful stuff to use with The Lounge
#!/usr/bin/env bash
#------------------------------------------------------#
# This script was tested on Ubuntu 20.04 and newer #
# #
# This script cleans up the SQLite database, plaintext #
# files and uploaded files #
#------------------------------------------------------#
#-------------------------------------------------------------------------------------#
# ATTENTION #
# #
# The Lounge owns the sqlite DB. #
# It assumes it is the sole program that writes to the database and may not handle it #
# well if a different process is taking a write lock on the database. #
# Stop The Lounge whenever you mess with the DB. #
# #
# YOU HAVE BEEN WARNED! #
#-------------------------------------------------------------------------------------#
#------------------------------------------------------------------------#
# I'll assume that you're using systemd to manage The Lounge start/stop, #
# and that your unit file is called `thelounge.service` and you're #
# running it as a normal user #
# #
# Edit the paths/commands to fit your needs. #
#------------------------------------------------------------------------#
#-----------------------#
# TheLounge home folder #
#-----------------------#
theloungeHome="$HOME/.thelounge"
#-------------------------------------------------------#
# Max number of lines to be kept on plaintext log files #
#-------------------------------------------------------#
maxLinesLogs=1000
#-----------------------------------------------#
# Max number of days to keep on SQLite database #
#-----------------------------------------------#
maxSqliteDays=30
#-------------------------------------------------------------------------#
# Keep only chat messages? It removes messages like join, part, quit, etc #
# #
# 0 = no, 1 = yes #
#-------------------------------------------------------------------------#
keepOnlyMsg=0
#-----------------------------------------------------------#
# How many days to keep of uploaded files #
# #
# If you don't have file uploading enabled, set this to "0" #
#-----------------------------------------------------------#
maxUploadDays=7
#--------------------------------------------------------------------------------------------------------------#
# DON'T TOUCH ANYTHING BELOW UNLESS YOU KNOW WHAT YOU ARE DOING!!! #
# #
# If you touch the code below and then complain the script "suddenly stopped working" I'll touch you at night. #
#--------------------------------------------------------------------------------------------------------------#
#--------------------------------#
# Stopping TheLounge for cleanup #
#--------------------------------#
/usr/bin/systemctl --user stop thelounge.service
#----------------------------------------------------#
# Cleanup of the SQLite database and plaintext files #
#----------------------------------------------------#
if [[ "$keepOnlyMsg" == 1 ]]
then
# SQLite database cleanup
for filename in "$theloungeHome"/logs/*.sqlite3
do
sqlite3 "$filename" "delete from messages where type not in ('action', 'message', 'notice'); VACUUM;"
sqlite3 "$filename" "delete from messages where time < strftime('%s', datetime('now', '-$maxSqliteDays day'))*1000; VACUUM;"
done
# Plaintext files cleanup
find "$theloungeHome"/logs/ -name "*.log" -print0 | while read -r -d $'\0' file
do
sed -i '/^\[.*\] \*\*\*/d' "$file"
tail -n "$maxLinesLogs" "$file" > /tmp/file.tmp
mv /tmp/file.tmp "$file"
done
else
# SQLite database cleanup
for filename in "$theloungeHome"/logs/*.sqlite3
do
sqlite3 "$filename" "delete from messages where time < strftime('%s', datetime('now', '-$maxSqliteDays day'))*1000; VACUUM;"
done
# Plaintext files cleanup
find "$theloungeHome"/logs/ -name "*.log" -print0 | while read -r -d $'\0' file
do
tail -n "$maxLinesLogs" "$file" > /tmp/file.tmp
mv /tmp/file.tmp "$file"
done
fi
#---------------------------------------#
# Cleanup of uploaded files, if enabled #
#---------------------------------------#
if [ "$maxUploadDays" -ge 1 ]
then
find "$theloungeHome"/uploads/ -mindepth 1 -type f -mtime +"$maxUploadDays" -delete
find "$theloungeHome"/uploads/ -mindepth 1 -type d -empty -delete
else
return
fi
#------------------------------------------------#
# Restart TheLounge after all has been completed #
#------------------------------------------------#
/usr/bin/systemctl --user start thelounge.service
#!/usr/bin/env bash
#-------------------------------------------------------#
# This script was tested on Ubuntu 20.04 and newer #
# #
# It only cleans the SQLite database and uploaded files #
#-------------------------------------------------------#
#-----------------------------------------------------------------------------------#
# ATTENTION #
# #
# The Lounge owns the sqlite DBs. #
# It assumes it is the sole program that writes to the database and may #
# not handle it well if a different process is taking a write lock on the database. #
# Stop The Lounge whenever you mess with the DB. #
# #
# YOU HAVE BEEN WARNED! #
#-----------------------------------------------------------------------------------#
#--------------------------------------------------------------------------#
# I'll assume that you're using systemd to manage TheLounge start/stop, #
# and that your unit file is called `thelounge.service` and you're running #
# it as a regular user #
# #
# Edit the paths/commands to fit your needs. #
#--------------------------------------------------------------------------#
#-----------------------#
# TheLounge home folder #
#-----------------------#
theloungeHome="$HOME/.thelounge"
#-----------------------------------------------#
# Max number of days to keep on SQLite database #
#-----------------------------------------------#
maxSqliteDays=30
#------------------------------------------------------------------------------#
# Keep only chat messages? It removes only messages like join, part, quit, etc #
# #
# 0 = no, 1 = yes #
#------------------------------------------------------------------------------#
keepOnlyMsg=0
#-----------------------------------------------------------#
# How many days to keep of uploaded files (if enabled) #
# #
# If you don't have file uploading enabled, set this to "0" #
#-----------------------------------------------------------#
maxUploadDays=7
#--------------------------------------------------------------------------------------------------------------#
# DON'T TOUCH ANYTHING BELOW UNLESS YOU KNOW WHAT YOU ARE DOING!!! #
# #
# If you touch the code below and then complain the script "suddenly stopped working" I'll touch you at night. #
#--------------------------------------------------------------------------------------------------------------#
#--------------------------------#
# Stopping TheLounge for cleanup #
#--------------------------------#
/usr/bin/systemctl --user stop thelounge.service
#-------------------------#
# SQLite database cleanup #
#-------------------------#
if [[ "$keepOnlyMsg" == 1 ]]
then
for filename in "$theloungeHome"/logs/*.sqlite3
do
sqlite3 "$filename" "delete from messages where type not in ('action', 'message', 'notice'); VACUUM;"
sqlite3 "$filename" "delete from messages where time < strftime('%s', datetime('now', '-$maxSqliteDays day'))*1000; VACUUM;"
done
else
for filename in "$theloungeHome"/logs/*.sqlite3; do
sqlite3 "$filename" "delete from messages where time < strftime('%s', datetime('now', '-$maxSqliteDays day'))*1000; VACUUM;"
done
fi
#---------------------------------------#
# Cleanup of uploaded files, if enabled #
#---------------------------------------#
if [ "$maxUploadDays" -ge 1 ]
then
find "$theloungeHome"/uploads/ -mindepth 1 -type f -mtime +"$maxUploadDays" -delete
find "$theloungeHome"/uploads/ -mindepth 1 -type d -empty -delete
else
return
fi
###
# Restart TheLounge after all been completed
###
/usr/bin/systemctl --user start thelounge.service
#----------------------------------------------------------------#
# To be used when TheLounge is installed from source #
# #
# NOTE: Replace any occurrence of $USER with your shell username #
#----------------------------------------------------------------#
[Unit]
Description=The Lounge (IRC client)
[Service]
Type=simple
Environment=THELOUNGE_HOME=~/.thelounge
WorkingDirectory=/home/$USER/thelounge
ExecStart=/usr/bin/node index.js start
[Install]
WantedBy=default.target
#-------------------------------------------------#
# To be used when TheLounge is installed via yarn #
#-------------------------------------------------#
[Unit]
Description=The Lounge (IRC client)
[Service]
Type=simple
Environment=THELOUNGE_HOME=~/.thelounge
ExecStart=/usr/local/bin/thelounge start
[Install]
WantedBy=default.target
@bonelifer
Copy link

Thanks for the scripts I used chatGPT, to make them usable with linuxserver.io's docker install of TheLounge. It has a bash dialog based installer. Would of never thought to create this without finding your script, great work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment