Advertisement
LittleFox94

functions.sh - Mailbot

Feb 16th, 2014
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. DATABASE="/home/littlefox/bin/mailbot/developers"
  4. LOGFILE="/home/littlefox/bin/mailbot/mailbot.log"
  5.  
  6. function lockDatabase
  7. {
  8.     while true
  9.     do
  10.         if `mkdir "$DATABASE.lock" > /dev/null 2>&1`
  11.         then
  12.             if [ -e "$DATABASE.tmp" ]
  13.             then
  14.                 rm "$DATABASE.tmp"
  15.                 appendLog "Removed orphaned temporary database"
  16.             fi
  17.  
  18.             touch "$DATABASE.tmp"
  19.  
  20.             return
  21.         fi
  22.  
  23.         sleep 1s
  24.     done
  25. }
  26.  
  27. function unlockDatabase
  28. {
  29.     rm "$DATABASE"
  30.     mv "$DATABASE.tmp" "$DATABASE"
  31.  
  32.     chmod 777 "$DATABASE"
  33.  
  34.     rmdir "$DATABASE.lock"
  35. }
  36.  
  37. function appendLog
  38. {
  39.     echo "[`date`] $1" >> $LOGFILE
  40. }
  41.  
  42. function readDataset
  43. {
  44.     mail=$(echo "$1" | cut -d ":" -f 1)
  45.     nickname=$(echo "$1" | cut -d ":" -f 2)
  46.     expectedCount=$(echo "$1" | cut -d ":" -f 3)
  47.     voting=$(echo "$1" | cut -d ":" -f 4)
  48. }
  49.  
  50. function setExpectedMails
  51. {
  52.     lockDatabase
  53.  
  54.     developerMail="$1"
  55.     newExpectedCount="$2"
  56.  
  57.     while read line
  58.     do
  59.         if [ ! "_$(echo "$line" | cut -c 1)" = "_#" ]
  60.         then
  61.             readDataset "$line"
  62.  
  63.             if [ "_$mail" = "_$developerMail" ]
  64.             then
  65.                 expectedCount="$newExpectedCount"
  66.             fi
  67.  
  68.             # writing updated dataset
  69.             echo "$mail:$nickname:$expectedCount:$voting" >> "$DATABASE.tmp"
  70.         else
  71.             # Comments are just copied
  72.             echo "$line" >> "$DATABASE.tmp"
  73.         fi
  74.     done < "$DATABASE"
  75.  
  76.     unlockDatabase
  77. }
  78.  
  79. function setVoting
  80. {
  81.     lockDatabase
  82.  
  83.     developerMail="$1"
  84.     newVoting="$2"
  85.  
  86.     while read line
  87.     do
  88.         if [ ! "_$(echo "$line" | cut -c 1)" = "_#" ]
  89.         then
  90.             readDataset "$line"
  91.  
  92.             if [ "_$mail" = "_$developerMail" ]
  93.             then
  94.                 voting="$newVoting"
  95.             fi
  96.  
  97.             # write updated dataset
  98.             echo "$mail:$nickname:$expectedCount:$voting" >> "$DATABASE.tmp"
  99.         else
  100.             # Comments are just copied
  101.             echo "$line" >> "$DATABASE.tmp"
  102.         fi
  103.     done < "$DATABASE"
  104.  
  105.     unlockDatabase
  106. }
  107.  
  108. function getExpectedMails
  109. {
  110.     lockDatabase
  111.  
  112.     developerMail="$1"
  113.  
  114.     while read line
  115.     do
  116.         echo "$line" >> "$DATABASE.tmp"
  117.         if [ ! "_$(echo "$line" | cut -c 1)" = "_#" ]
  118.         then
  119.             readDataset "$line"
  120.  
  121.             if [ "_$mail" = "_$developerMail" ]
  122.             then
  123.                 echo "$expectedCount"
  124.             fi
  125.         fi
  126.     done < "$DATABASE"
  127.  
  128.     unlockDatabase
  129. }
  130.  
  131. function getVoting
  132. {
  133.     lockDatabase
  134.  
  135.     developerMail="$1"
  136.  
  137.     while read line
  138.     do
  139.         echo "$line" >> "$DATABASE.tmp"
  140.         if [ ! "_$(echo "$line" | cut -c 1)" = "_#" ]
  141.         then
  142.             readDataset "$line"
  143.  
  144.             if [ "_$mail" = "_$developerMail" ]
  145.             then
  146.                 echo "$voting"
  147.             fi
  148.         fi
  149.     done < "$DATABASE"
  150.  
  151.     unlockDatabase
  152. }
  153.  
  154. function getName
  155. {
  156.     lockDatabase
  157.  
  158.     developerMail="$1"
  159.  
  160.     while read line
  161.     do
  162.         echo "$line" >> "$DATABASE.tmp"
  163.         if [ ! "_$(echo "$line" | cut -c 1)" = "_#" ]
  164.         then
  165.             readDataset "$line"
  166.  
  167.             if [ "_$mail" = "_$developerMail" ]
  168.             then
  169.                 echo "$nickname"
  170.             fi
  171.         fi
  172.     done < "$DATABASE"
  173.  
  174.     unlockDatabase
  175. }
  176.  
  177. function getDevelopers
  178. {
  179.     lockDatabase
  180.  
  181.     while read line
  182.     do
  183.         echo "$line" >> "$DATABASE.tmp"
  184.         if [ ! "_$(echo "$line" | cut -c 1)" = "_#" ]
  185.         then
  186.             readDataset "$line"
  187.             echo "$mail"
  188.         fi
  189.     done < "$DATABASE"
  190.  
  191.     unlockDatabase
  192. }
  193.  
  194. function incExpectedMails
  195. {
  196.     setExpectedMails $1 $(expr $(getExpectedMails $1) + 1)
  197. }
  198.  
  199. function decExpectedMails
  200. {
  201.     setExpectedMails $1 $(expr $(getExpectedMails $1) - 1)
  202. }
  203.  
  204. function incVoting
  205. {
  206.     setVoting $1 $(expr $(getVoting $1) + 1)
  207. }
  208.  
  209. function decVoting
  210. {
  211.     setVoting $1 $(expr $(getVoting $1) - 1)
  212. }
  213.  
  214. function extraDecVoting
  215. {
  216.     setVoting $1 $(expr $(getVotign $1) - 10)
  217. }
  218.  
  219. function mailGetFrom
  220. {
  221.     echo "$1" | while read line
  222.     do
  223.         if [ "_$(echo "$line" | head -c 6)" = "_From: " ]
  224.         then
  225.             from=$(echo "$line" | cut -d " " -f 2-)
  226.  
  227.             if [ ! "_$(echo "$from" | grep "<")" = "_" ]
  228.             then
  229.                 from=$(echo "$from" | sed 's,.*<\([^ >]*\)>.*,\1,')
  230.             fi
  231.  
  232.             echo "$from"
  233.         fi
  234.     done
  235. }
  236.  
  237. function mailGetSubject
  238. {
  239.     echo "$1" | while read line
  240.     do
  241.         if [ "_$(echo "$line" | head -c 9)" = "_Subject: " ]
  242.         then
  243.             subject=$(echo "$line" | cut -d " " -f 2)
  244.             echo "$subject"
  245.         fi
  246.     done
  247. }
  248.  
  249. function sendNonExpected
  250. {
  251.     to="$1"
  252.     subject="RE: $2"
  253.     name=$(getName "$to")
  254.  
  255.     read -d '' text << EOF
  256. Hi $name,
  257.  
  258. I have got an e-mail from you which I didn't expect.
  259. Is this my fault or yours?
  260. If it is my fault please mail LittleFox at littlefox@fsfe.org - thank you!
  261.  
  262. Best regards,
  263. The most-annoying bot
  264. EOF
  265.     appendLog $(echo "$text" | mail -s "$subject" -a "From: Most-annoying bot (MAB) <fts-mailbot@lf-net.org>" "$to")
  266. }
  267.  
  268. function sendReminder
  269. {
  270.     to="$1"
  271.     name=$(getName "$to")
  272.  
  273.     decVoting "$to"
  274.     voting=$(getVoting "$to")
  275.  
  276.     read -d '' text << EOF
  277. Hi $name,
  278.  
  279. I didn't get a response from you since my last email.
  280. Sorry, but I had to downvote you, giving you a vote of $voting.
  281.  
  282. If you don't answer within 24 hours after the first mail you'll get a downvote of -10!
  283.  
  284. Best regards,
  285. The most-annoying bot
  286. EOF
  287.  
  288.     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"
  289. }
  290.  
  291. function sendWeeklyRequest
  292. {
  293.     to="$1"
  294.     name=$(getName "$to")
  295.  
  296.     incExpectedMails "$to"
  297.  
  298.     read -d '' text << EOF
  299. Hi $name,
  300.  
  301. it's that time of the week again - I would like to get a status-report from you.
  302. If you don't respond within an hour, I'll have to downvote you.
  303.  
  304. You can gain 1 point every week by not having me to send a reminder.
  305. 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.
  306.  
  307. The other team members will agree on a proper punishment when you have a bad voting >:]
  308.  
  309. Best regards,
  310. The most-annoying bot
  311. EOF
  312.  
  313.     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"
  314. }
  315.  
  316. function sendWeeklyDownvote
  317. {
  318.     to="$1"
  319.     name=$(getName "$to")
  320.  
  321.     extraDecVoting "$to"
  322.     voting=$(getVoting "$to")
  323.  
  324.     setExpectedCount "$to" "0"
  325.  
  326.     read -d '' text << EOF
  327. Hi $name,
  328.  
  329. I didn't get a response from you the last 24 hours.
  330. 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.
  331.  
  332. Don't make me angry and send me an response next week! >:[
  333. If you reach -200, I'll inform the whole mailinglist. You don't want this to happen, do you?
  334.  
  335. Best regards,
  336. The most-annoying bot
  337. EOF
  338.  
  339.     echo "$text" | mail -s "Extra-downvote, just for you!" -a "From: Most-annoying bot (MAB) <fts-mailbot@lf-net.org>" "$to"
  340. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement