Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- DATABASE="/home/littlefox/bin/mailbot/developers"
- LOGFILE="/home/littlefox/bin/mailbot/mailbot.log"
- function lockDatabase
- {
- while true
- do
- if `mkdir "$DATABASE.lock" > /dev/null 2>&1`
- then
- if [ -e "$DATABASE.tmp" ]
- then
- rm "$DATABASE.tmp"
- appendLog "Removed orphaned temporary database"
- fi
- touch "$DATABASE.tmp"
- return
- fi
- sleep 1s
- done
- }
- function unlockDatabase
- {
- rm "$DATABASE"
- mv "$DATABASE.tmp" "$DATABASE"
- chmod 777 "$DATABASE"
- rmdir "$DATABASE.lock"
- }
- function appendLog
- {
- echo "[`date`] $1" >> $LOGFILE
- }
- function readDataset
- {
- mail=$(echo "$1" | cut -d ":" -f 1)
- nickname=$(echo "$1" | cut -d ":" -f 2)
- expectedCount=$(echo "$1" | cut -d ":" -f 3)
- voting=$(echo "$1" | cut -d ":" -f 4)
- }
- function setExpectedMails
- {
- lockDatabase
- developerMail="$1"
- newExpectedCount="$2"
- while read line
- do
- if [ ! "_$(echo "$line" | cut -c 1)" = "_#" ]
- then
- readDataset "$line"
- if [ "_$mail" = "_$developerMail" ]
- then
- expectedCount="$newExpectedCount"
- fi
- # writing updated dataset
- echo "$mail:$nickname:$expectedCount:$voting" >> "$DATABASE.tmp"
- else
- # Comments are just copied
- echo "$line" >> "$DATABASE.tmp"
- fi
- done < "$DATABASE"
- unlockDatabase
- }
- function setVoting
- {
- lockDatabase
- developerMail="$1"
- newVoting="$2"
- while read line
- do
- if [ ! "_$(echo "$line" | cut -c 1)" = "_#" ]
- then
- readDataset "$line"
- if [ "_$mail" = "_$developerMail" ]
- then
- voting="$newVoting"
- fi
- # write updated dataset
- echo "$mail:$nickname:$expectedCount:$voting" >> "$DATABASE.tmp"
- else
- # Comments are just copied
- echo "$line" >> "$DATABASE.tmp"
- fi
- done < "$DATABASE"
- unlockDatabase
- }
- function getExpectedMails
- {
- lockDatabase
- developerMail="$1"
- while read line
- do
- echo "$line" >> "$DATABASE.tmp"
- if [ ! "_$(echo "$line" | cut -c 1)" = "_#" ]
- then
- readDataset "$line"
- if [ "_$mail" = "_$developerMail" ]
- then
- echo "$expectedCount"
- fi
- fi
- done < "$DATABASE"
- unlockDatabase
- }
- function getVoting
- {
- lockDatabase
- developerMail="$1"
- while read line
- do
- echo "$line" >> "$DATABASE.tmp"
- if [ ! "_$(echo "$line" | cut -c 1)" = "_#" ]
- then
- readDataset "$line"
- if [ "_$mail" = "_$developerMail" ]
- then
- echo "$voting"
- fi
- fi
- done < "$DATABASE"
- unlockDatabase
- }
- function getName
- {
- lockDatabase
- developerMail="$1"
- while read line
- do
- echo "$line" >> "$DATABASE.tmp"
- if [ ! "_$(echo "$line" | cut -c 1)" = "_#" ]
- then
- readDataset "$line"
- if [ "_$mail" = "_$developerMail" ]
- then
- echo "$nickname"
- fi
- fi
- done < "$DATABASE"
- unlockDatabase
- }
- function getDevelopers
- {
- lockDatabase
- while read line
- do
- echo "$line" >> "$DATABASE.tmp"
- if [ ! "_$(echo "$line" | cut -c 1)" = "_#" ]
- then
- readDataset "$line"
- echo "$mail"
- fi
- done < "$DATABASE"
- unlockDatabase
- }
- function incExpectedMails
- {
- setExpectedMails $1 $(expr $(getExpectedMails $1) + 1)
- }
- function decExpectedMails
- {
- setExpectedMails $1 $(expr $(getExpectedMails $1) - 1)
- }
- function incVoting
- {
- setVoting $1 $(expr $(getVoting $1) + 1)
- }
- function decVoting
- {
- setVoting $1 $(expr $(getVoting $1) - 1)
- }
- function extraDecVoting
- {
- setVoting $1 $(expr $(getVotign $1) - 10)
- }
- function mailGetFrom
- {
- echo "$1" | while read line
- do
- if [ "_$(echo "$line" | head -c 6)" = "_From: " ]
- then
- from=$(echo "$line" | cut -d " " -f 2-)
- if [ ! "_$(echo "$from" | grep "<")" = "_" ]
- then
- from=$(echo "$from" | sed 's,.*<\([^ >]*\)>.*,\1,')
- fi
- echo "$from"
- fi
- done
- }
- function mailGetSubject
- {
- echo "$1" | while read line
- do
- if [ "_$(echo "$line" | head -c 9)" = "_Subject: " ]
- then
- subject=$(echo "$line" | cut -d " " -f 2)
- echo "$subject"
- fi
- done
- }
- function sendNonExpected
- {
- to="$1"
- subject="RE: $2"
- name=$(getName "$to")
- read -d '' text << EOF
- Hi $name,
- I have got an e-mail from you which I didn't expect.
- Is this my fault or yours?
- If it is my fault please mail LittleFox at littlefox@fsfe.org - thank you!
- Best regards,
- The most-annoying bot
- EOF
- appendLog $(echo "$text" | mail -s "$subject" -a "From: Most-annoying bot (MAB) <fts-mailbot@lf-net.org>" "$to")
- }
- function sendReminder
- {
- to="$1"
- name=$(getName "$to")
- decVoting "$to"
- voting=$(getVoting "$to")
- read -d '' text << EOF
- Hi $name,
- I didn't get a response from you since my last email.
- Sorry, but I had to downvote you, giving you a vote of $voting.
- If you don't answer within 24 hours after the first mail you'll get a downvote of -10!
- Best regards,
- The most-annoying bot
- EOF
- echo "$text" | mail -s "No response" -a "From: Most-annoying bot (MAB) <fts-mailbot@lf-net.org>" -a "Reply-to: fts-mailbot@lf-net.org, fts-devel@lf-net.org" "$to"
- }
- function sendWeeklyRequest
- {
- to="$1"
- name=$(getName "$to")
- incExpectedMails "$to"
- read -d '' text << EOF
- Hi $name,
- it's that time of the week again - I would like to get a status-report from you.
- If you don't respond within an hour, I'll have to downvote you.
- You can gain 1 point every week by not having me to send a reminder.
- You'll lose a point for every reminder I send. If you don't respond within 24 hours, you'll lose 10 points + one for each reminder, giving -34 points.
- The other team members will agree on a proper punishment when you have a bad voting >:]
- Best regards,
- The most-annoying bot
- EOF
- echo "$text" | mail -s "Status-reports please" -a "From: Most-annoying bot (MAB) <fts-mailbot@lf-net.org>" -a "Reply-to: fts-mailbot@lf-net.org, fts-devel@lf-net.org" "$to"
- }
- function sendWeeklyDownvote
- {
- to="$1"
- name=$(getName "$to")
- extraDecVoting "$to"
- voting=$(getVoting "$to")
- setExpectedCount "$to" "0"
- read -d '' text << EOF
- Hi $name,
- I didn't get a response from you the last 24 hours.
- I gave you a downvote of -10 in addition to the -1 for every reminder, giving you -34 this week for a total of now $voting points.
- Don't make me angry and send me an response next week! >:[
- If you reach -200, I'll inform the whole mailinglist. You don't want this to happen, do you?
- Best regards,
- The most-annoying bot
- EOF
- echo "$text" | mail -s "Extra-downvote, just for you!" -a "From: Most-annoying bot (MAB) <fts-mailbot@lf-net.org>" "$to"
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement