Add mailid_to_sender script
Benjamin Renard

Benjamin Renard commited on 2016-01-20 17:54:57
Showing 1 changed files, with 98 additions and 0 deletions.

... ...
@@ -0,0 +1,98 @@
1
+#!/bin/bash
2
+
3
+MAILID=$1
4
+[ -z "$MAILID" ] && echo "Usage : $0 [mailid]" && exit 1
5
+
6
+DEBUG=0
7
+[ "$2" == "-d" ] && DEBUG=1
8
+
9
+MAX_LOG=9
10
+
11
+HOSTNAME=$( hostname -f )
12
+
13
+function search_sender () {
14
+	MAILID=$1
15
+	MAILLOG=$2
16
+	[ ! -f "$MAILLOG" ] && MAILLOG=$MAILLOG.gz
17
+	[ -f "$MAILLOG" ] && zgrep $MAILID $MAILLOG|grep 'from='|sed 's/^.*\: from=<\([^>]*\)>.*$/\1/'|head -n 1
18
+}
19
+
20
+function search_sender_from_mailid() {
21
+	MAILID=$1
22
+	[ $DEBUG -eq 1 ] && echo "Search sender from Mail ID $MAILID ..."
23
+	res=$( search_sender $MAILID /var/log/mail.log )
24
+	if [ -n "$res" ]
25
+	then
26
+		echo "$res"
27
+		exit 0
28
+	else
29
+		[ $DEBUG -eq 1 ] && echo "Not found from log file /var/log/mail.log. Try with archive (Max : $MAX_LOG)"
30
+		for i in $( seq 1 $MAX_LOG )
31
+		do
32
+			res=$( search_sender $MAILID /var/log/mail.log.$i )
33
+			if [ -n "$res" ]
34
+			then
35
+				[ $DEBUG -eq 1 ] && echo "Found from log file /var/log/mail.log.$i"
36
+				echo "$res"
37
+				exit 0
38
+			else
39
+				[ $DEBUG -eq 1 ] && echo "Not found from log file /var/log/mail.log.$i"
40
+			fi
41
+		done
42
+		[ $DEBUG -eq 1 ] && echo "Not found in archive (Max : $MAX_LOG)"
43
+	fi
44
+}
45
+
46
+if [ $( mailq|egrep -c ^$MAILID ) -ne 0 ]
47
+then
48
+	[ $DEBUG -eq 1 ] && echo "Mail $MAILID still in mailq. Try to detect original mail ID ..."
49
+	ORIGMAILID=$( postcat -q "$MAILID" |grep "by $HOSTNAME"|tail -n 1|sed 's/^.*by .* with.* id \([A-Z0-9]*\).*$/\1/' )
50
+	if [ -n "$ORIGMAILID" ]
51
+	then
52
+		if [ "$ORIGMAILID" != "$MAILID" ]
53
+		then
54
+			[ $DEBUG -eq 1 ] && echo "Original mail ID : $ORIGMAILID"
55
+			search_sender_from_mailid "$ORIGMAILID"
56
+			[ $DEBUG -eq 1 ] && echo "Sender not found from original mail ID"
57
+			exit 1
58
+		else
59
+			[ $DEBUG -eq 1 ] && echo "Mail ID and original mail ID are equals"
60
+		fi
61
+	else
62
+		[ $DEBUG -eq 1 ] && echo "Fail to detect original mail ID. Continue with mail ID in parameter"
63
+	fi
64
+fi
65
+
66
+search_sender_from_mailid "$MAILID"
67
+[ -n "$ORIGMAILID" ] && echo "Sender not found" && exit 1
68
+
69
+[ $DEBUG -eq 1 ] && echo "Sender not found. Try to detect original mail ID with message ID in log"
70
+
71
+MSGID=$( mailid_to_messageid "$MAILID" )
72
+
73
+if [ -z "$MSGID" ]
74
+then
75
+	[ $DEBUG -eq 1 ] && echo "Message ID not found from log."
76
+	exit 1
77
+fi
78
+
79
+[ $DEBUG -eq 1 ] && echo "Message ID found : $MSGID. Try to detect original mail ID with this message ID"
80
+
81
+ORIGMAILID=$( messageid_to_original_mailid "$MSGID" )
82
+
83
+if [ -z "$ORIGMAILID" ]
84
+then
85
+	[ $DEBUG -eq 1 ] && echo "Original mail ID not found from message ID"
86
+	exit 1
87
+fi
88
+
89
+if [ "$ORIGMAILID" == "$MAILID" ]
90
+then
91
+	[ $DEBUG -eq 1 ] && echo "Original mail ID is same as mail ID in parameter"
92
+	exit 1
93
+fi
94
+
95
+search_sender_from_mailid "$ORIGMAILID"
96
+[ $DEBUG -eq 1 ] && echo "Sender not found from original mail ID"
97
+
98
+exit 1
0 99