Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Implementation of spamd client (spamc) in VB.NET

0.00/5 (No votes)
27 Jun 2004 1  
.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:

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here