# macros mysql
MYSQL_CHECK_LOCAL_DOMAINS = SELECT DISTINCT domain_name FROM email_domain WHERE domain_name = '$domain'
MYSQL_CHECK_ISPOP = SELECT email_box.id FROM email_box,email_domain WHERE email_box.domain_name = email_domain.domain_name and email_box.local_part = '$local_part' and email_domain.domain_name = '$domain'
MYSQL_GET_MBOX = SELECT CONCAT(domain_name,'/',local_part) FROM email_box where local_part='$local_part' and domain_name='$domain'
MYSQL_CHECK_ALIAS = SELECT DISTINCT destination from email_alias where local_part='$local_part' and domain_name='$domain'
MYSQL_CHECK_VIRUS = SELECT DISTINCT antivirus from email_box where local_part='$local_part' and domain_name='$domain'
MYSQL_CHECK_SPAM = SELECT DISTINCT antispam from email_box where local_part='$local_part' and domain_name='$domain'
MYSQL_GET_AWAYMSG = SELECT DISTINCT CONCAT('--- Reponse automatique de $local_part@$domain ---\n\n',autorespond) from email_box where local_part='$local_part' and domain_name='$domain'
MYSQL_CHECK_AWAY = SELECT DISTINCT domain_name from email_box where local_part='$local_part' and domain_name='$domain' and autorespond != ''
MYSQL_CHECK_CATCHALL = SELECT DISTINCT catchall_alias from email_domain where domain_name='$domain'


######################################################################
#                    MAIN CONFIGURATION SETTINGS                     #
######################################################################

#primary_hostname = debian.webservices94.com 

domainlist local_domains = mysql;MYSQL_CHECK_LOCAL_DOMAINS
domainlist relay_to_domains = *
hostlist   relay_from_hosts = 127.0.0.1 : 192.168.0.103 : 192.168.0.101
#domainlist relay_from_domains = 
#hostlist   auth_relay_hosts = *

log_selector = +all

exim_user = vmail
exim_group = vmail

acl_smtp_rcpt = acl_check_rcpt
# You should not change that setting until you understand how ACLs work.

#qualify_domain = webservices94.com 
never_users = root

unknown_login = web

trusted_users = web
trusted_groups = web

host_lookup = *

# ident query - a etudier.
rfc1413_hosts = 
rfc1413_query_timeout = 0s

ignore_bounce_errors_after = 2d
timeout_frozen_after = 7d

local_interfaces = 0.0.0.0.25 : 127.0.0.1.10025

hide mysql_servers = /machine/exim/reverouge
#                     base  user   pass

smtp_banner = webservices94.com ESTMP

######################################################################
#                       ACL CONFIGURATION                            #
#         Specifies access control lists for incoming SMTP mail      #
######################################################################

begin acl

# This access control list is used for every RCPT command in an incoming
# SMTP message. The tests are run in order until the address is either
# accepted or denied.

acl_check_rcpt:

  # Accept if the source is local SMTP (i.e. not over TCP/IP). We do this by
  # testing for an empty sending host field.

  accept  hosts = :

  deny    domains       = !+local_domains
          local_parts   = ^[./|] : ^.*[@%!] : ^.*/\\.\\./

  accept  local_parts   = postmaster
          domains       = +local_domains
	  

  # Deny unless the sender address can be verified.

  require verify        = sender

  accept  domains       = +local_domains
          endpass
          verify        = recipient

  accept  domains       = +relay_to_domains
          endpass
          verify        = recipient

  accept  hosts         = +relay_from_hosts

  accept  authenticated = *

  deny    message       = relay not permitted



######################################################################
#                      ROUTERS CONFIGURATION                         #
#               Specifies how addresses are handled                  #
######################################################################
#     THE ORDER IN WHICH THE ROUTERS ARE DEFINED IS IMPORTANT!       #
# An address is passed to each router in turn until it is accepted.  #
######################################################################

begin routers

# This router routes addresses that are not in local domains by doing a DNS
# lookup on the domain name. Any domain that resolves to 0.0.0.0 or to a
# loopback interface address (127.0.0.0/8) is treated as if it had no DNS
# entry. Note that 0.0.0.0 is the same as 0.0.0.0/32, which is commonly treated
# as the local host inside the network stack. It is not 0.0.0.0/0, the default
# route. If the DNS lookup fails, no further routers are tried because of
# the no_more setting, and consequently the address is unrouteable.

dnslookup:
  driver = dnslookup
  domains = ! +local_domains
  transport = remote_smtp
  ignore_target_hosts = 0.0.0.0 : 127.0.0.0/8
  no_more


# The remaining routers handle addresses in the local domain(s).

#virtual_forward_domain:
#  driver = redirect
#  allow_defer
#  allow_fail
#  data

virtual_forward_alias:
  driver = redirect
  allow_defer
  allow_fail
  data = ${lookup mysql {MYSQL_CHECK_ALIAS}}
  forbid_pipe
  forbid_file

# antivirus
amavis:
  driver = accept
  condition = ${if or 	{							\
			{eq {$interface_port}{10025} }				\
			{eq {${lookup mysql {MYSQL_CHECK_VIRUS}}}{0} }		\
			{eq {$received_protocol}{spamcheck} }			\
			{eq {$sender_address}{} }				\
			}							\
		 {0}{1}								\
		}
  transport = amavis

# antispam
spamassassin:
  driver = accept
  condition = ${if or	{							\
			{eq {${lookup mysql {MYSQL_CHECK_SPAM}}}{0} }		\
			{eq {$received_protocol}{spamcheck} }			\
			{eq {$sender_address}{} }				\
			}							\
		{0}{1}								\
		}
  transport = spamcheck

# repondeur
autorespond:
  driver = accept
  condition = ${if or {{match {$h_To:}{$local_part@$domain}} \
    {match {$h_Cc:}{$local_part}}}{yes}{no}}
  domains = ${lookup mysql {MYSQL_CHECK_AWAY}{$value}}
  no_expn
  retry_use_local_part
  transport = autorespond
  unseen

# local delivery
virtual_localuser:
  driver = accept
  condition = ${if eq{}{${lookup mysql {MYSQL_CHECK_ISPOP}}}{no}{yes}}
  retry_use_local_part
  transport = virtual_localdelivery
  #cannot_route_message = Unknown user

# catchall
virtual_catchall:
  driver = redirect
  allow_defer
  allow_fail
  data = ${lookup mysql {MYSQL_CHECK_CATCHALL}}
  forbid_pipe
  forbid_file
  headers_add = X-Catchall: for $domain

######################################################################
#                      TRANSPORTS CONFIGURATION                      #
######################################################################
#                       ORDER DOES NOT MATTER                        #
#     Only one appropriate transport is called for each delivery.    #
######################################################################

# A transport is used only when referenced from a router that successfully
# handles an address.

begin transports


# This transport is used for delivering messages over SMTP connections.

remote_smtp:
  driver = smtp

virtual_localdelivery:
  driver = appendfile
  directory = /data0/vmail/${lookup mysql{MYSQL_GET_MBOX}{$value}fail}/Maildir
  directory_mode = 0700
  envelope_to_add
  group = vmail
  maildir_format
  mode = 0666
  return_path = $sender_address
  return_path_add
  user = vmail

address_pipe:
  driver = pipe
  return_output

address_file:
  driver = appendfile
  delivery_date_add
  envelope_to_add
  return_path_add

address_reply:
  driver = autoreply

amavis:
  driver = smtp
  hosts = 127.0.0.1
  port = 10024
  allow_localhost

spamcheck:
  driver = pipe
  command = /opt/exim/bin/exim -oMr spamcheck -bS
  use_bsmtp = true
  transport_filter = /usr/bin/spamc
  home_directory = /tmp
  current_directory = /tmp
  user = vmail
  group = vmail
  log_output = true
  return_fail_output = true
  return_path_add = false
  message_prefix =
  message_suffix =

autorespond:
  driver = autoreply
  from = AutoReply <drop@localhost.>
  subject = "${if def:h_Subject: {Re: $h_Subject:} {Repondeur automatique}}"
  text = ${lookup mysql{MYSQL_GET_AWAYMSG}{$value}}
  to = ${sender_address}
  user = vmail

######################################################################
#                      RETRY CONFIGURATION                           #
######################################################################

begin retry

# This single retry rule applies to all domains and all errors. It specifies
# retries every 15 minutes for 2 hours, then increasing retry intervals,
# starting at 1 hour and increasing each time by a factor of 1.5, up to 16
# hours, then retries every 6 hours until 4 days have passed since the first
# failed delivery.

# Domain               Error       Retries
# ------               -----       -------

*                      *           F,2h,15m; G,16h,1h,1.5; F,4d,6h



######################################################################
#                      REWRITE CONFIGURATION                         #
######################################################################

# There are no rewriting specifications in this default configuration file.

begin rewrite



######################################################################
#                   AUTHENTICATION CONFIGURATION                     #
######################################################################

# There are no authenticator specifications in this default configuration file.

begin authenticators



######################################################################
#                   CONFIGURATION FOR local_scan()                   #
######################################################################

# If you have built Exim to include a local_scan() function that contains
# tables for private options, you can define those options here. Remember to
# uncomment the "begin" line. It is commented by default because it provokes
# an error with Exim binaries that are not built with LOCAL_SCAN_HAS_OPTIONS
# set in the Local/Makefile.

# begin local_scan


# End of Exim configuration file
