Debian Codegraph @

mailbox_grep.sh

raw symbols SCIP
1 #!/bin/bash
2 # Script by Francisco Lobo,
3 #+ and slightly modified and commented by ABS Guide author.
4 # Used in ABS Guide with permission. (Thank you!)
5
6 # This script will not run under Bash versions -lt 3.0.
7
8
9 E_MISSING_ARG=87
10 if [ -z "$1" ]
11 then
12 echo "Usage: $0 mailbox-file"
13 exit $E_MISSING_ARG
14 fi
15
16 mbox_grep() # Parse mailbox file.
17 {
18 declare -i body=0 match=0
19 declare -a date sender
20 declare mail header value
21
22
23 while IFS= read -r mail
24 # ^^^^ Reset $IFS.
25 # Otherwise "read" will strip leading & trailing space from its input.
26
27 do
28 if [[ $mail =~ ^From ]] # Match "From" field in message.
29 then
30 (( body = 0 )) # "Zero out" variables.
31 (( match = 0 ))
32 unset date
33
34 elif (( body ))
35 then
36 (( match ))
37 # echo "$mail"
38 # Uncomment above line if you want entire body
39 #+ of message to display.
40
41 elif [[ $mail ]]; then
42 IFS=: read -r header value <<< "$mail"
43 # ^^^ "here string"
44
45 case "$header" in
46 [Ff][Rr][Oo][Mm] ) [[ $value =~ "$2" ]] && (( match++ )) ;;
47 # Match "From" line.
48 [Dd][Aa][Tt][Ee] ) read -r -a date <<< "$value" ;;
49 # ^^^
50 # Match "Date" line.
51 [Rr][Ee][Cc][Ee][Ii][Vv][Ee][Dd] ) read -r -a sender <<< "$value" ;;
52 # ^^^
53 # Match IP Address (may be spoofed).
54 esac
55
56 else
57 (( body++ ))
58 (( match )) &&
59 echo "MESSAGE ${date:+of: ${date[*]} }"
60 # Entire $date array ^
61 echo "IP address of sender: ${sender[1]}"
62 # Second field of "Received" line ^
63
64 fi
65
66
67 done < "$1" # Redirect stdout of file into loop.
68 }
69
70
71 mbox_grep "$1" # Send mailbox file to function.
72
73 exit $?
74
75 # Exercises:
76 # ---------
77 # 1) Break the single function, above, into multiple functions,
78 #+ for the sake of readability.
79 # 2) Add additional parsing to the script, checking for various keywords.
80
81
82
83 $ mailbox_grep.sh scam_mail
84 MESSAGE of Thu, 5 Jan 2006 08:00:56 -0500 (EST)
85 IP address of sender: 196.3.62.4