\doxysection{milter-\/nomix.\+py}
\label{milter-nomix_8py-example}
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{00001\ }
\DoxyCodeLine{00004\ }
\DoxyCodeLine{00005\ \textcolor{keyword}{from}\ \_\_future\_\_\ \textcolor{keyword}{import}\ print\_function}
\DoxyCodeLine{00006\ \textcolor{keyword}{import}\ Milter}
\DoxyCodeLine{00007\ \textcolor{keyword}{import}\ time}
\DoxyCodeLine{00008\ \textcolor{keyword}{import}\ sys}
\DoxyCodeLine{00009\ \textcolor{keyword}{from}\ Milter.utils\ \textcolor{keyword}{import}\ parse\_addr}
\DoxyCodeLine{00010\ }
\DoxyCodeLine{00011\ internal\_tlds\ =\ [\textcolor{stringliteral}{"{}corp"{}},\ \textcolor{stringliteral}{"{}personal"{}}]}
\DoxyCodeLine{00012\ }
\DoxyCodeLine{00013\ }
\DoxyCodeLine{00015\ \textcolor{keyword}{def\ }is\_internal(hostname):}
\DoxyCodeLine{00016\ \ \ \ \ components\ =\ hostname.split(\textcolor{stringliteral}{"{}."{}})}
\DoxyCodeLine{00017\ \ \ \ \ \textcolor{keywordflow}{return}\ components.pop()\ \textcolor{keywordflow}{in}\ internal\_tlds}
\DoxyCodeLine{00018\ }
\DoxyCodeLine{00019\ \textcolor{comment}{\#\ Determine\ if\ internal\ and\ external\ hosts\ are\ mixed\ based\ on\ a\ list}}
\DoxyCodeLine{00020\ \textcolor{comment}{\#\ of\ hostnames}}
\DoxyCodeLine{00021\ \textcolor{keyword}{def\ }are\_mixed(hostnames):}
\DoxyCodeLine{00022\ \ \ \ \ hostnames\_mapped\ =\ map(is\_internal,\ hostnames)}
\DoxyCodeLine{00023\ }
\DoxyCodeLine{00024\ \ \ \ \ \textcolor{comment}{\#\ Num\ internals}}
\DoxyCodeLine{00025\ \ \ \ \ num\_internal\_hosts\ =\ hostnames\_mapped.count(\textcolor{keyword}{True})}
\DoxyCodeLine{00026\ }
\DoxyCodeLine{00027\ \ \ \ \ \textcolor{comment}{\#\ Num\ externals}}
\DoxyCodeLine{00028\ \ \ \ \ num\_external\_hosts\ =\ hostnames\_mapped.count(\textcolor{keyword}{False})}
\DoxyCodeLine{00029\ }
\DoxyCodeLine{00030\ \ \ \ \ \textcolor{keywordflow}{return}\ num\_external\_hosts\ >=\ 1\ \textcolor{keywordflow}{and}\ num\_internal\_hosts\ >=\ 1}
\DoxyCodeLine{00031\ }
\DoxyCodeLine{00032\ \textcolor{keyword}{class\ }NoMixMilter(Milter.Base):}
\DoxyCodeLine{00033\ }
\DoxyCodeLine{00034\ \ \ \textcolor{keyword}{def\ }\_\_init\_\_(self):\ \ \textcolor{comment}{\#\ A\ new\ instance\ with\ each\ new\ connection.}}
\DoxyCodeLine{00035\ \ \ \ \ self.id\ =\ Milter.uniqueID()\ \ \textcolor{comment}{\#\ Integer\ incremented\ with\ each\ call.}}
\DoxyCodeLine{00036\ }
\DoxyCodeLine{00037\ }
\DoxyCodeLine{00038\ \ \ }
\DoxyCodeLine{00039\ \ \ \textcolor{preprocessor}{@Milter.noreply}}
\DoxyCodeLine{00040\ \ \ \textcolor{keyword}{def\ }envfrom(self,\ mailfrom,\ *str):}
\DoxyCodeLine{00041\ \ \ \ \ self.mailfrom\ =\ mailfrom}
\DoxyCodeLine{00042\ \ \ \ \ self.domains\ =\ []}
\DoxyCodeLine{00043\ \ \ \ \ t\ =\ parse\_addr(mailfrom)}
\DoxyCodeLine{00044\ \ \ \ \ \textcolor{keywordflow}{if}\ len(t)\ >\ 1:}
\DoxyCodeLine{00045\ \ \ \ \ \ \ self.domains.append(t[1])}
\DoxyCodeLine{00046\ \ \ \ \ \textcolor{keywordflow}{else}:}
\DoxyCodeLine{00047\ \ \ \ \ \ \ self.domains.append(\textcolor{stringliteral}{'local'})}
\DoxyCodeLine{00048\ \ \ \ \ self.internal\ =\ \textcolor{keyword}{False}}
\DoxyCodeLine{00049\ \ \ \ \ \textcolor{keywordflow}{return}\ Milter.CONTINUE}
\DoxyCodeLine{00050\ }
\DoxyCodeLine{00051\ \ \ }
\DoxyCodeLine{00052\ \ \ \textcolor{keyword}{def\ }envrcpt(self,\ to,\ *str):}
\DoxyCodeLine{00053\ \ \ \ \ self.R.append(to)}
\DoxyCodeLine{00054\ \ \ \ \ t\ =\ parse\_addr(to)}
\DoxyCodeLine{00055\ \ \ \ \ \textcolor{keywordflow}{if}\ len(t)\ >\ 1:}
\DoxyCodeLine{00056\ \ \ \ \ \ \ self.domains.append(t[1])}
\DoxyCodeLine{00057\ \ \ \ \ \textcolor{keywordflow}{else}:}
\DoxyCodeLine{00058\ \ \ \ \ \ \ self.domains.append(\textcolor{stringliteral}{'local'})}
\DoxyCodeLine{00059\ }
\DoxyCodeLine{00060\ \ \ \ \ \textcolor{keywordflow}{if}\ are\_mixed(self.domains):}
\DoxyCodeLine{00061\ \ \ \ \ \ \ \textcolor{comment}{\#\ FIXME:\ log\ recipients\ collected\ in\ self.mailfrom\ and\ self.R}}
\DoxyCodeLine{00062\ \ \ \ \ \ \ self.setreply(\textcolor{stringliteral}{'550'},\textcolor{stringliteral}{'5.7.1'},\textcolor{stringliteral}{'Mixing\ internal\ and\ external\ TLDs'})}
\DoxyCodeLine{00063\ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ Milter.REJECT}
\DoxyCodeLine{00064\ \ \ \ \ \ \ \ \ }
\DoxyCodeLine{00065\ \ \ \ \ \textcolor{keywordflow}{return}\ Milter.CONTINUE}
\DoxyCodeLine{00066\ \ \ \ \ }
\DoxyCodeLine{00067\ \textcolor{keyword}{def\ }main():}
\DoxyCodeLine{00068\ \ \ socketname\ =\ \textcolor{stringliteral}{"{}/var/run/nomixsock"{}}}
\DoxyCodeLine{00069\ \ \ timeout\ =\ 600}
\DoxyCodeLine{00070\ \ \ \textcolor{comment}{\#\ Register\ to\ have\ the\ Milter\ factory\ create\ instances\ of\ your\ class:}}
\DoxyCodeLine{00071\ \ \ Milter.factory\ =\ NoMixMilter}
\DoxyCodeLine{00072\ \ \ print(\textcolor{stringliteral}{"{}\%s\ milter\ startup"{}}\ \%\ time.strftime(\textcolor{stringliteral}{'\%Y\%b\%d\ \%H:\%M:\%S'}))}
\DoxyCodeLine{00073\ \ \ sys.stdout.flush()}
\DoxyCodeLine{00074\ \ \ Milter.runmilter(\textcolor{stringliteral}{"{}nomixfilter"{}},socketname,timeout)}
\DoxyCodeLine{00075\ \ \ logq.put(\textcolor{keywordtype}{None})}
\DoxyCodeLine{00076\ \ \ bt.join()}
\DoxyCodeLine{00077\ \ \ print(\textcolor{stringliteral}{"{}\%s\ nomix\ milter\ shutdown"{}}\ \%\ time.strftime(\textcolor{stringliteral}{'\%Y\%b\%d\ \%H:\%M:\%S'}))}
\DoxyCodeLine{00078\ }
\DoxyCodeLine{00079\ \textcolor{keywordflow}{if}\ \_\_name\_\_\ ==\ \textcolor{stringliteral}{"{}\_\_main\_\_"{}}:}
\DoxyCodeLine{00080\ \ \ main()}

\end{DoxyCodeInclude}
 