aboutsummaryrefslogtreecommitdiff
path: root/mflib/callout.mf4
blob: 701c7f3e25d3e633cc94c2ec8f3503383d023894 (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* Callout functions                                    -*- mfl -*-
   Copyright (C) 2010-2011, 2015-2017 Sergey Poznyakoff

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */

module 'callout'.

require status

#pragma regex push +extended

dclex e_callout_proto

m4_dnl The following two variables are defined in callout.bi
m4_dnl Perhaps someday they will be defined here.
m4_dnl precious string ehlo_domain
m4_dnl precious string mailfrom_address

string last_poll_host
string last_poll_greeting
string last_poll_helo
string last_poll_sent
string last_poll_recv
number cache_used

static func setvar(number fd, string name, string value)
do
  write(fd, "SET %name=%value\r\n")
  set reply getline(fd)
  if not reply matches 'OK.*'
    throw e_callout_proto reply
  fi
done

func callout_open(string url)
  returns number
do
  number fd open("@ %url")
  fd_set_delimiter(fd,"\r\n")
  string reply getline(fd)
  if not reply matches 'OK.*'
    throw e_callout_proto reply
  fi

  string sid ""
  if milter_client_address == ""
    set sid "local:"
  else
    set sid "%milter_client_address:"
  fi
  if macro_defined("i")
    set sid sid . getmacro("i")
  fi
  write(fd, "SID %sid\r\n")
  set reply getline(fd)
  if not reply matches 'OK.*'
    throw e_callout_proto reply
  fi
    
  m4_ifelse(`
  # Set command is not yet supported by the server.
  if ehlo_domain
    setvar(fd, "EHLO", ehlo_domain)
  fi
  if mailfrom_address
    setvar(fd, "MAILFROM", mailfrom_address)
  fi')
  return fd
done

func callout_close(number fd)
do
  write(fd, "QUIT\r\n")
  set reply getline(fd)
  close(fd)
done

func callout_do(number fd, string email; string rest)
  returns number
do
  string reply ""

  write(fd, 'VRFY "'.escape(email).'"')
  if defined(rest)
    write(fd, ' '.rest)
  fi
  write(fd, "\r\n")
  set reply getline(fd)
  if not reply matches 'OK.*'
    throw e_callout_proto reply
  fi

  write(fd, "RUN\r\n")
  set cache_used 1
  loop for set reply getline(fd),
       while index(reply, "*") == 0,
       set reply getline(fd)
  do
    if reply matches '\* ([0-9]+) ([A-Z]+) (.*)'
      set cache_used 0
      switch \2
      do
      case "INIT":
	set last_poll_host \3
      case "GRTNG":
	set last_poll_greeting \3
      case "HELO":
        set last_poll_helo \3
      case "SENT":
	set last_poll_sent \3
      case "RECV":
	set last_poll_recv \3
      done
    fi
  done

  if reply matches "OK ([0-9]+)=(.*)"
    switch \2
    do
    case "success":
      pass
    case "not_found":
      return e_not_found
    case "failure":
      return e_failure
    case "temp_failure" or "timeout":
      return e_temp_failure
    done
  else
    return e_not_found
  fi
  return 0
done

#pragma provide-callout
precious string callout_server_url
precious number __callout_fd -1

begin
do
  if callout_server_url == ""
    set callout_server_url default_callout_server_url()
  fi
done

func __callout_open_default()
  returns number
do
  if __callout_fd == -1
    set __callout_fd callout_open(callout_server_url)
  fi
  return __callout_fd
done

func callout(string email)
  returns number
do
  return callout_do(__callout_open_default(), email)
done

  
  
  
    
  
	  

Return to:

Send suggestions and report system problems to the System administrator.