summaryrefslogtreecommitdiffstats
path: root/doc/scriptexamples/mail.kvs
blob: 94fb70e234967c50b2f7e487c06250f0a2974373 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# This is a really basic mail sender script
# It uses exec and a telnet slave process
# You will probably need to hack it a bit since
# most mail servers will simply refuse to
# send so spam-looking mail :D

# Ah... remember that spamming creates bad karma.


# exec a telnet slave.
# Since we want to make sure that it does not run for too long,
# we give it 60 seconds to complete the operations

exec -x -k=60000 -t -e ("telnet my.mail.server.com 25")
{
	# We keep the state in the %:state extended scope variable

	if($0 == "started")
	{
		# This is the first event triggered
		# Initialize our variables...
		echo [process started]
		%:state = 0
		# This is the source address as specified in the MAIL command
		%:from = "<me@me.com>"
		%:rcpt = "<me@me.com>"
		# This is the From field
		%:sender = "\"Me\" <me@me.com>"
		%:receiver = "\"Me\" <me@me.com>"
		%:body = "Hello, this is a test mesage for the small KVIrc mail script$cr$lf"
		%:body .= "I hope that you have fun writing in kvs :)))"
		%:subject = "This is a test subject"
		# Returning an empty string does not write to stdin
		return
	}
	
	if($0 == "stderr")
	{
		# This is our stderr handler.. just in case something goes wrong
		echo "[stderr] $1"
		return
	}
	
	if($0 == "terminated")
	{
		echo "[process terminated]"
		return
	}

	# Ok.. this must be a stdout data event
	# Echo the line to the user
	echo "[stdout] $1"

	# And make our little state machine work
	switch(%:state)
	{
		case(0):
		{
			# We're waiting for 220 (ready)
			if($str.match("220*",$1))
			{
				# Send the HELO and go to next state
				%:state++
				echo "Sending HELO..."
				return "HELO myhostname$cr$lf";
			}
		}
		break
		case(1):
		{
			# Waiting for 250 (after the HELO)
			if($str.match("250*",$1))
			{
				# Send the MAIL command
				%:state++
				echo "Sending MAIL..."
				return "MAIL From: %:from$cr$lf"
			} else {
				# The server replied with something that is not a 250...
				# Fail :/
				echo "HELO command not accepted: $1"
				halt
			}
		}
		break;
		case(2):
		{
			# Waiting for another 250 (MAIL accepted)
			if($str.match("250*",$1))
			{
				# ...
				%:state++
				echo "Sending RCPT..."
				return "RCPT To: %:rcpt$cr$lf"
			} else {
				echo "MAIL command not accepted: $1"
				halt
			}
		}
		break;
		case(3):
		{
			# Waiting for another 250 (RCPT accepted)
			if($str.match("250*",$1))
			{
				%:state++
				echo "Sending DATA..."
				return "DATA$cr$lf"
			} else {
				echo "RCPT not accepted: $1"
				halt
			}
		}
		break;
		case(4):
		{
			# Waiting for 354 (ok, go on)
			if($str.match("354*",$1))
			{
				# You will probably need to hack in the Date: field :)
				%:state++
				echo "Sending body..."
				%x = "From: %:sender$cr$lf"
				%x .= "To: %:receiver$cr$lf"
				%x .= "Subject: %:subject$cr$lf"
				%x .= "Reply-To: %:sender$cr$lf"
				%x .= "Date: Thu, 8 Apr 2004 05:28:01 +0200$cr$lf"
				%x .= "X-Mailer: KVIrc funky KVS script$cr$lf"
				%x .= "$cr$lf"
				%x .= "%:body$cr$lf$cr$lf"
				%x .= ".$cr$lf"
				return %x
			} else {
				echo "DATA not accepted: $1"
				halt
			}
		}
		break;
		case(5):
		{
			# We don't wait anymore :)
			%:state++
			echo "Sending QUIT..."
			return "QUIT$cr$lf"
		}
		break;
		default:
		{
			# Usually the mail server closes the connection
			%:state++
			if(%:state > 10)
			{
				# But if it does not in few messages
				# Then force the process to die
				halt
			}
		}
	}
}