Click here to Skip to main content
15,888,026 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.7K   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 
AnswerRe: The best antispam? Pin
JackRazz@gmail.com5-Jul-04 5:14
JackRazz@gmail.com5-Jul-04 5:14 
In case you missed it, this study shows that it may, in fact, be the best of the free open source solutions out there.

Spamassassin Beats CRM-114 In Anti-Spam Shootout
Spam

Posted by timothy on Tuesday June 22, @11:24PM 2004

Simon Lyall writes "A new study of antispam software shows that Spamassassin performed well in various configurations along with Spamprobe, Bogofilter and Spambayes also came out good while CRM-114 failed to live up to its previous claims. The study shows: 'The best-performing filters reduced the volume of incoming spam from about 150 messages per day to about 2 messages per day.'"

This is the original link to the article'
http://developers.slashdot.org/developers/04/06/23/014223.shtml?tid=111&tid=126&tid=95

JackRazz

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.