anntp

a nntp implementation in pure C99
Log | Files | Refs | README | LICENSE

commit d6db399d7593abedb6569d335a7f337c32b25e42
parent c37b61f3d3e5fd204f8b76389a5b9be51ecbcfa4
Author: Mario Rosell R. Martinez <mario@mariorosell.es>
Date:   Thu, 19 Mar 2026 19:51:02 +0100

Add anntp_readdot_cb

Basically so you don't have to allocate a eighty-mb buffer on the heap and parse
it all bulk ;)

Diffstat:
Manntp.h | 29+++++++++++++++++++++++++++++
1 file changed, 29 insertions(+), 0 deletions(-)

diff --git a/anntp.h b/anntp.h @@ -76,6 +76,8 @@ struct anntp_connection { typedef struct anntp_connection AnntpConnection; +typedef int (*AnntpLineCb)(const char* line, void* extra); + /*** function declarations ***/ ANNTP_API void anntp_init(void); @@ -87,6 +89,7 @@ ANNTP_API ssize_t anntp_write_all(AnntpConnection* conn, const uchar_t* ANNTP_API ssize_t anntp_writeline(AnntpConnection* conn, const char* buf); ANNTP_API ssize_t anntp_readline(AnntpConnection* conn, char* buf, size_t maxlen); ANNTP_API ssize_t anntp_readdot(AnntpConnection* conn, char* buf, size_t maxlen); +ANNTP_API int anntp_readdot_cb(AnntpConnection* conn, AnntpLineCb cb, void* extra); ANNTP_API int anntp_auth(AnntpConnection* conn, const char* login, const char* password); #endif /* ANNTP_H */ @@ -309,6 +312,32 @@ anntp_readdot(AnntpConnection* cv, char* buf, size_t maxlen) return (ssize_t)pos; } +int +anntp_readdot_cb(AnntpConnection* cv, AnntpLineCb cb, void* extra) +{ + if (!cv || !cb) return -1; + + char line[ANNTP_BUFSIZE]; + ssize_t n; + + for (;;) { + n = anntp_readline(cv, line, sizeof(line)); + if (n <= 0) + return -1; + + /* logic here is a bit like on anntp_readdot */ + if (strcmp(line, ".\r\n") == 0) + break; + + char* out = line; + if (line[0] == '.' && line[1] == '.') + out++; + + if (cb(out, extra) != 0) + return 1; /* user aborted */ + } +} + /* NOTE: I have a gut feeling this auth mechanism is probably a bit insecure... */ int anntp_auth(AnntpConnection* cv, const char* user, const char* pass)