Click here to Skip to main content
12,077,753 members (48,879 online)
Click here to Skip to main content
Add your own
alternative version

Stats

43.8K views
144 downloads
17 bookmarked
Posted

Implementation of spamd client (spamc) in VB.NET

, 27 Jun 2004
Rate this:
Please Sign up or sign in to vote.
.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

Share

About the Author

Michal Altair Valášek
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.


You may also be interested in...

Comments and Discussions

 
GeneralProblem while running ZetaSpamAssassin project Pin
saleemmohd15-Mar-09 20:59
membersaleemmohd15-Mar-09 20:59 
QuestionProblems Pin
RafaMiranda21-Jul-06 11:42
memberRafaMiranda21-Jul-06 11:42 
QuestionThe best antispam? Pin
navi200030-Jun-04 22:35
membernavi200030-Jun-04 22:35 
AnswerRe: The best antispam? Pin
JackRazz@sbcglobal.net5-Jul-04 6:14
memberJackRazz@sbcglobal.net5-Jul-04 6: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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160212.1 | Last Updated 28 Jun 2004
Article Copyright 2004 by Michal Altair Valášek
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid