mjcaley / aiospamc

Python asyncio-based library that implements the SPAMC/SPAMD client protocol used by SpamAssassin.
MIT License
13 stars 9 forks source link

return json / to_dict of parsed body #460

Open danieleperera opened 3 months ago

danieleperera commented 3 months ago

The code returns the body as a single field, however, it would be nice if you can already parse the triggered rule names, related description and other items in the body. I'm interested in getting the triggered rules, pts, description and spam score with the related threshold. However i need to apply a parser on top of the output provided by aiospamc.

Is this something in roadmap?

mjcaley commented 1 month ago

Hi @danieleperera, sorry I didn't notice your issue until now.

There's definitely some issues with returning some of those values already parsed, but there should be a way to get most of that information already.

So if you're looking for the spam score and threshold then that's available in the headers attribute from the Response object. The triggered rules would be returned from the aiospamc.process function.

So putting it altogether:

response = await aiospamc.process(my_message)

print(response.headers.spam)
# Prints:
# SpamValue(value=True, score=1000.0, threshold=5.0)

print(response.body)
# Prints:
# b'GTUBE,NO_RECEIVED,NO_RELAYS'

The rules might be a little parsing, but isn't too bad. I could maybe add something in for that specific response type. I'm not sure what you mean by the description though.

The main reason I'm shying away from any extra parsing though is that I had a handful of bugs with parsing the message that's returned. It's basically an email, and ended up running into character set and Unicode issues. I thought it'd be better to let the consumer of the library handle it since they'd probably know better (or even not both with it). And the to_json method encodes the body as base 64 since it may encounter characters that can't be represented as Unicode.

Hopefully this helps, please let me know if you have anymore questions or if you'd like me to clarify anything.

Reference to symbols function: https://aiospamc.readthedocs.io/en/v1.0.0/aiospamc.html#aiospamc.frontend.symbols Reference to the headers object: https://aiospamc.readthedocs.io/en/v1.0.0/aiospamc.html#aiospamc.header_values.Headers.spam