\doxysection{milter-\/nomix.\+py}

\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{1 }
\DoxyCodeLine{4 }
\DoxyCodeLine{5 \textcolor{keyword}{from} \_\_future\_\_ \textcolor{keyword}{import} print\_function}
\DoxyCodeLine{6 \textcolor{keyword}{import} Milter}
\DoxyCodeLine{7 \textcolor{keyword}{import} time}
\DoxyCodeLine{8 \textcolor{keyword}{import} sys}
\DoxyCodeLine{9 \textcolor{keyword}{from} Milter.utils \textcolor{keyword}{import} parse\_addr}
\DoxyCodeLine{10 }
\DoxyCodeLine{11 internal\_tlds = [\textcolor{stringliteral}{"{}corp"{}}, \textcolor{stringliteral}{"{}personal"{}}]}
\DoxyCodeLine{12 }
\DoxyCodeLine{13 }
\DoxyCodeLine{15 \textcolor{keyword}{def }is\_internal(hostname):}
\DoxyCodeLine{16     components = hostname.split(\textcolor{stringliteral}{"{}."{}})}
\DoxyCodeLine{17     \textcolor{keywordflow}{return} components.pop() \textcolor{keywordflow}{in} internal\_tlds}
\DoxyCodeLine{18 }
\DoxyCodeLine{19 \textcolor{comment}{\# Determine if internal and external hosts are mixed based on a list}}
\DoxyCodeLine{20 \textcolor{comment}{\# of hostnames}}
\DoxyCodeLine{21 \textcolor{keyword}{def }are\_mixed(hostnames):}
\DoxyCodeLine{22     hostnames\_mapped = map(is\_internal, hostnames)}
\DoxyCodeLine{23 }
\DoxyCodeLine{24     \textcolor{comment}{\# Num internals}}
\DoxyCodeLine{25     num\_internal\_hosts = hostnames\_mapped.count(\textcolor{keyword}{True})}
\DoxyCodeLine{26 }
\DoxyCodeLine{27     \textcolor{comment}{\# Num externals}}
\DoxyCodeLine{28     num\_external\_hosts = hostnames\_mapped.count(\textcolor{keyword}{False})}
\DoxyCodeLine{29 }
\DoxyCodeLine{30     \textcolor{keywordflow}{return} num\_external\_hosts >= 1 \textcolor{keywordflow}{and} num\_internal\_hosts >= 1}
\DoxyCodeLine{31 }
\DoxyCodeLine{32 \textcolor{keyword}{class }NoMixMilter(Milter.Base):}
\DoxyCodeLine{33 }
\DoxyCodeLine{34   \textcolor{keyword}{def }\_\_init\_\_(self):  \textcolor{comment}{\# A new instance with each new connection.}}
\DoxyCodeLine{35     self.id = Milter.uniqueID()  \textcolor{comment}{\# Integer incremented with each call.}}
\DoxyCodeLine{36 }
\DoxyCodeLine{37 }
\DoxyCodeLine{38   }
\DoxyCodeLine{39   \textcolor{preprocessor}{@Milter.noreply}}
\DoxyCodeLine{40   \textcolor{keyword}{def }envfrom(self, mailfrom, *str):}
\DoxyCodeLine{41     self.mailfrom = mailfrom}
\DoxyCodeLine{42     self.domains = []}
\DoxyCodeLine{43     t = parse\_addr(mailfrom)}
\DoxyCodeLine{44     \textcolor{keywordflow}{if} len(t) > 1:}
\DoxyCodeLine{45       self.domains.append(t[1])}
\DoxyCodeLine{46     \textcolor{keywordflow}{else}:}
\DoxyCodeLine{47       self.domains.append(\textcolor{stringliteral}{'local'})}
\DoxyCodeLine{48     self.internal = \textcolor{keyword}{False}}
\DoxyCodeLine{49     \textcolor{keywordflow}{return} Milter.CONTINUE}
\DoxyCodeLine{50 }
\DoxyCodeLine{51   }
\DoxyCodeLine{52   \textcolor{keyword}{def }envrcpt(self, to, *str):}
\DoxyCodeLine{53     self.R.append(to)}
\DoxyCodeLine{54     t = parse\_addr(to)}
\DoxyCodeLine{55     \textcolor{keywordflow}{if} len(t) > 1:}
\DoxyCodeLine{56       self.domains.append(t[1])}
\DoxyCodeLine{57     \textcolor{keywordflow}{else}:}
\DoxyCodeLine{58       self.domains.append(\textcolor{stringliteral}{'local'})}
\DoxyCodeLine{59 }
\DoxyCodeLine{60     \textcolor{keywordflow}{if} are\_mixed(self.domains):}
\DoxyCodeLine{61       \textcolor{comment}{\# FIXME: log recipients collected in self.mailfrom and self.R}}
\DoxyCodeLine{62       self.setreply(\textcolor{stringliteral}{'550'},\textcolor{stringliteral}{'5.7.1'},\textcolor{stringliteral}{'Mixing internal and external TLDs'})}
\DoxyCodeLine{63       \textcolor{keywordflow}{return} Milter.REJECT}
\DoxyCodeLine{64         }
\DoxyCodeLine{65     \textcolor{keywordflow}{return} Milter.CONTINUE}
\DoxyCodeLine{66     }
\DoxyCodeLine{67 \textcolor{keyword}{def }main():}
\DoxyCodeLine{68   socketname = \textcolor{stringliteral}{"{}/var/run/nomixsock"{}}}
\DoxyCodeLine{69   timeout = 600}
\DoxyCodeLine{70   \textcolor{comment}{\# Register to have the Milter factory create instances of your class:}}
\DoxyCodeLine{71   Milter.factory = NoMixMilter}
\DoxyCodeLine{72   print(\textcolor{stringliteral}{"{}\%s milter startup"{}} \% time.strftime(\textcolor{stringliteral}{'\%Y\%b\%d \%H:\%M:\%S'}))}
\DoxyCodeLine{73   sys.stdout.flush()}
\DoxyCodeLine{74   Milter.runmilter(\textcolor{stringliteral}{"{}nomixfilter"{}},socketname,timeout)}
\DoxyCodeLine{75   logq.put(\textcolor{keywordtype}{None})}
\DoxyCodeLine{76   bt.join()}
\DoxyCodeLine{77   print(\textcolor{stringliteral}{"{}\%s nomix milter shutdown"{}} \% time.strftime(\textcolor{stringliteral}{'\%Y\%b\%d \%H:\%M:\%S'}))}
\DoxyCodeLine{78 }
\DoxyCodeLine{79 \textcolor{keywordflow}{if} \_\_name\_\_ == \textcolor{stringliteral}{"{}\_\_main\_\_"{}}:}
\DoxyCodeLine{80   main()}

\end{DoxyCodeInclude}
 