Click here to Skip to main content
15,868,002 members
Articles / Containers / Virtual Machine
Article

Implementation of spamd client (spamc) in VB.NET

Rate me:
Please Sign up or sign in to vote.
2.50/5 (2 votes)
27 Jun 2004CPOL1 min read 51.6K   159   17   4
.NET implementation of SpamAssassin's network client.

What is spamd and spamc?

Let's start with SpamAssassin. SA is probably the world's best software for filtering of spam. But it is written in Perl and that means slow, especially on Win32 platform. So, there is a little help, called 'spamd'. It's a daemon (UN*X equivalent for 'service') allowing to communicate with SA over network and limiting cost of calling SA. And spamc is a client component for this daemon.

This means that you can run some Unix-like OS on a separate (or maybe even only virtual) machine, and run SpamAssassin and spamd there. Then, from your Windows-based mail server, everything that must be done is to call the spamd using spamc, and process results.

Communication protocol

Well, the communication protocol is very simple and is described in SpamAssassin documentation. In short, you need to open TCP connection to port where spamd lives (usually 783), and then send command header (i.e., PROCESS SPAMC/1.0) and then RFC822-formatted source of message. Then you must drop the sending connection.

spamd would process the message with SpamAssassin and send the modified result in the same fashion.

Implementation under .NET

.NET implementation is very simple, with one exception: due to need for one-sided connection shutdown, we cannot use System.Net.Sockets.TcpClient. We must go one level down and use directly the socket communication.

The simple and dirty proof-of-concept code may look like:

VB
Module Module1

    Sub Main()
        Console.WriteLine("Preparing data...")
        Dim SB As New System.Text.StringBuilder
        SB.Append("PROCESS SPAMC/1.0")
        SB.Append("\nSubject: Test spam mail (GTUBE)")
        SB.Append("\nMessage-ID: <gtube1.1010101@example.net>")
        SB.Append("\nDate: Wed, 23 Jul 2003 23:30:00 +0200")
        SB.Append("\nFrom: Sender <sender@example.net>")
        SB.Append("\nTo: Recipient <recipient@example.net>")
        SB.Append("\nPrecedence: junk")
        SB.Append("\nMIME-Version: 1.0")
        SB.Append("\nContent-Type: text/plain; charset=us-ascii")
        SB.Append("\nContent-Transfer-Encoding: 7bit")
        SB.Append("\n")
        SB.Append("\nIf your spam filter supports it," & _
                       " the GTUBE provides a test by which you")
        SB.Append("\ncan verify that the filter is installed " & _
                           "correctly and is detecting incoming")
        SB.Append("\nspam. You can send yourself a test mail " & _
                            "containing the following string of")
        SB.Append("\ncharacters (in upper case and with " & _
                             "no white spaces and line breaks):")
        SB.Append("\n")
        SB.Append("\nXJS*C4JDBQADN1.NSBN3*2IDNEN*GTUBE-" & _
                            "STANDARD-ANTI-UBE-TEST-EMAIL*C.34X")
        SB.Append("\n")
        SB.Append("\nYou should send this test mail from an account " & _
                                      "outside of your network.")
        SB.Append("\n")
        SB.Replace("\n", vbCrLf)
        Dim Buffer() As Byte = _
            System.Text.Encoding.ASCII.GetBytes(SB.ToString())

        Console.WriteLine("Sending data...")
        Dim Socket As New System.Net.Sockets.Socket( _
            System.Net.Sockets.AddressFamily.InterNetwork, _
            System.Net.Sockets.SocketType.Stream, _
            System.Net.Sockets.ProtocolType.Tcp)
        Dim IPE As New System.Net.IPEndPoint( _
            System.Net.IPAddress.Parse("192.168.168.105"), 783)
        Socket.Connect(IPE)
        Socket.Send(Buffer)
        Socket.Shutdown(Net.Sockets.SocketShutdown.Send)

        Console.WriteLine("Receiving data")
        Dim R As Int32
        Do
           Dim RecBuf(1024) As Byte
           R = Socket.Receive(RecBuf)
           Console.Write(System.Text.Encoding.ASCII.GetString(RecBuf, 0, R))
        Loop Until R = 0

        Console.WriteLine("OK")
        Console.ReadLine()
    End Sub

End Module

The result

Result is the SpamdClient class, which allows you to incorporate the spamc functionality into your software. Also is provided a console application (spamc.exe), allowing to communicate with the spamd server from command line.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Altairis
Czech Republic Czech Republic

Software architect and developer in Altairis, dev shop in Czech republic. Microsoft Most Valuable Professional (MVP) since 2004.


See my open source project at Codeplex.


Comments and Discussions

 
GeneralProblem while running ZetaSpamAssassin project Pin
saleemmohd15-Mar-09 19:59
saleemmohd15-Mar-09 19:59 
QuestionProblems Pin
RafaMiranda21-Jul-06 10:42
RafaMiranda21-Jul-06 10:42 
QuestionThe best antispam? Pin
navi200030-Jun-04 21:35
navi200030-Jun-04 21:35 
Well, It's an opinion. I think that there are some better antispam programs, at least with a greater percentage of detection.

----------------------------
Ivan Gadea Saez
Senior Software developer
AnswerRe: The best antispam? Pin
JackRazz@gmail.com5-Jul-04 5:14
JackRazz@gmail.com5-Jul-04 5:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.