From 4e817148363811d5c76ccb3a4cdcc3a1730a368b Mon Sep 17 00:00:00 2001 From: Richard Russon Date: Sat, 7 Jul 2018 11:50:34 +0100 Subject: [PATCH] create libnntp --- Makefile.autosetup | 19 ++++++- browser.c | 2 +- complete.c | 2 +- compose.c | 2 +- curs_main.c | 2 +- main.c | 2 +- mx.c | 2 +- newsrc.c => nntp/newsrc.c | 1 + nntp.c => nntp/nntp.c | 22 ++++++++ nntp.h => nntp/nntp.h | 104 ++++++++++++++------------------------ nntp/nntp_private.h | 66 ++++++++++++++++++++++++ pager.c | 2 +- po/POTFILES.in | 4 +- send.c | 2 +- sendlib.c | 2 +- sort.c | 26 ++++------ sort.h | 3 ++ 17 files changed, 166 insertions(+), 97 deletions(-) rename newsrc.c => nntp/newsrc.c (99%) rename nntp.c => nntp/nntp.c (99%) rename nntp.h => nntp/nntp.h (74%) create mode 100644 nntp/nntp_private.h diff --git a/Makefile.autosetup b/Makefile.autosetup index fd7901caa..db5fc7b94 100644 --- a/Makefile.autosetup +++ b/Makefile.autosetup @@ -67,8 +67,8 @@ NEOMUTTOBJS= addrbook.o alias.o bcache.o browser.o buffy.o \ enriched.o enter.o filter.o flags.o from.o group.o handler.o \ hdrline.o help.o history.o hook.o init.o keymap.o \ main.o mbox.o menu.o mh.o muttlib.o mutt_account.o mutt_attach.o mutt_body.o mutt_header.o \ - mutt_logging.o mutt_signal.o mutt_socket.o mutt_thread.o mutt_window.o mx.o newsrc.o \ - nntp.o pager.o parse.o pattern.o \ + mutt_logging.o mutt_signal.o mutt_socket.o mutt_thread.o mutt_window.o mx.o \ + pager.o parse.o pattern.o \ postpone.o progress.o query.o recvattach.o recvcmd.o resize.o rfc1524.o \ rfc2047.o rfc2231.o rfc3676.o safe_asprintf.o score.o send.o \ sendlib.o sidebar.o smtp.o sort.o state.o status.o system.o \ @@ -106,6 +106,14 @@ CLEANFILES+= $(LIBPOP) $(LIBPOPOBJS) MUTTLIBS+= $(LIBPOP) ALLOBJS+= $(LIBPOPOBJS) +############################################################################### +# libnntp +LIBNNTP= libnntp.a +LIBNNTPOBJS= nntp/newsrc.o nntp/nntp.o +CLEANFILES+= $(LIBNNTP) $(LIBNNTPOBJS) +MUTTLIBS+= $(LIBNNTP) +ALLOBJS+= $(LIBNNTPOBJS) + ############################################################################### # libmutt LIBMUTT= libmutt.a @@ -250,6 +258,13 @@ $(LIBPOP): $(PWD)/pop $(LIBPOPOBJS) $(PWD)/pop: $(MKDIR_P) $(PWD)/pop +# libnntp +$(LIBNNTP): $(PWD)/nntp $(LIBNNTPOBJS) + $(AR) cr $@ $(LIBNNTPOBJS) + $(RANLIB) $@ +$(PWD)/nntp: + $(MKDIR_P) $(PWD)/nntp + # libncrypt $(LIBNCRYPT): $(PWD)/ncrypt $(LIBNCRYPTOBJS) $(AR) cr $@ $(LIBNCRYPTOBJS) diff --git a/browser.c b/browser.c index c31a5c21b..49bf8cc4e 100644 --- a/browser.c +++ b/browser.c @@ -59,7 +59,7 @@ #include "imap/imap.h" #endif #ifdef USE_NNTP -#include "nntp.h" +#include "nntp/nntp.h" #endif #ifdef USE_NOTMUCH #include "mutt_notmuch.h" diff --git a/complete.c b/complete.c index 6e2b44fc2..d3f71db28 100644 --- a/complete.c +++ b/complete.c @@ -41,7 +41,7 @@ #include "mailbox.h" #endif #ifdef USE_NNTP -#include "nntp.h" +#include "nntp/nntp.h" #endif /** diff --git a/compose.c b/compose.c index 14f53391e..09836d157 100644 --- a/compose.c +++ b/compose.c @@ -58,7 +58,7 @@ #include "remailer.h" #endif #ifdef USE_NNTP -#include "nntp.h" +#include "nntp/nntp.h" #endif static const char *There_are_no_attachments = N_("There are no attachments."); diff --git a/curs_main.c b/curs_main.c index c8bb39343..4f7522140 100644 --- a/curs_main.c +++ b/curs_main.c @@ -67,7 +67,7 @@ #include "mutt_notmuch.h" #endif #ifdef USE_NNTP -#include "nntp.h" +#include "nntp/nntp.h" #endif #ifdef ENABLE_NLS #include diff --git a/main.c b/main.c index 66f14b531..7a616b78d 100644 --- a/main.c +++ b/main.c @@ -70,7 +70,7 @@ #include "imap/imap.h" #endif #ifdef USE_NNTP -#include "nntp.h" +#include "nntp/nntp.h" #endif #define MUTT_IGNORE (1 << 0) /* -z */ diff --git a/mx.c b/mx.c index fd928fc4c..d64129be3 100644 --- a/mx.c +++ b/mx.c @@ -69,7 +69,7 @@ #include "pop/pop.h" #endif #ifdef USE_NNTP -#include "nntp.h" +#include "nntp/nntp.h" #endif #ifdef USE_NOTMUCH #include "mutt_notmuch.h" diff --git a/newsrc.c b/nntp/newsrc.c similarity index 99% rename from newsrc.c rename to nntp/newsrc.c index 8c4393fea..3f38d3d80 100644 --- a/newsrc.c +++ b/nntp/newsrc.c @@ -38,6 +38,7 @@ #include #include #include +#include "nntp_private.h" #include "mutt/mutt.h" #include "conn/conn.h" #include "mutt.h" diff --git a/nntp.c b/nntp/nntp.c similarity index 99% rename from nntp.c rename to nntp/nntp.c index a377c9471..b71a21190 100644 --- a/nntp.c +++ b/nntp/nntp.c @@ -35,6 +35,7 @@ #include #include #include +#include "nntp_private.h" #include "mutt/mutt.h" #include "email/email.h" #include "conn/conn.h" @@ -53,6 +54,7 @@ #include "options.h" #include "progress.h" #include "protos.h" +#include "sort.h" #include "url.h" #ifdef USE_HCACHE #include "hcache/hcache.h" @@ -2594,6 +2596,26 @@ int nntp_check_children(struct Context *ctx, const char *msgid) return (rc < 0) ? -1 : 0; } +/** + * nntp_compare_order - Sort to mailbox order + * @param a First Header to compare + * @param b First Header to compare + * @retval -1 a precedes b + * @retval 0 a and b are identical + * @retval 1 b precedes a + */ +int nntp_compare_order(const void *a, const void *b) +{ + struct Header **ha = (struct Header **) a; + struct Header **hb = (struct Header **) b; + + anum_t na = NHDR(*ha)->article_num; + anum_t nb = NHDR(*hb)->article_num; + int result = (na == nb) ? 0 : (na > nb) ? 1 : -1; + result = perform_auxsort(result, a, b); + return (SORTCODE(result)); +} + // clang-format off /** * struct mx_nntp_ops - Mailbox callback functions for NNTP mailboxes diff --git a/nntp.h b/nntp/nntp.h similarity index 74% rename from nntp.h rename to nntp/nntp.h index f314bfdac..5262ac955 100644 --- a/nntp.h +++ b/nntp/nntp.h @@ -22,8 +22,8 @@ * this program. If not, see . */ -#ifndef _MUTT_NNTP_H -#define _MUTT_NNTP_H +#ifndef _NNTP_NNTP_H +#define _NNTP_NNTP_H #include #include @@ -41,44 +41,28 @@ struct Account; struct Header; struct Context; -#define NNTP_PORT 119 -#define NNTP_SSL_PORT 563 - -/* number of entries in article cache */ -#define NNTP_ACACHE_LEN 10 - /* article number type and format */ #define anum_t uint32_t #define ANUM "%u" -/** - * enum NntpStatus - NNTP server return values - */ -enum NntpStatus -{ - NNTP_NONE = 0, - NNTP_OK, - NNTP_BYE -}; - /** * struct NntpServer - NNTP-specific server data */ struct NntpServer { - bool hasCAPABILITIES : 1; - bool hasSTARTTLS : 1; - bool hasDATE : 1; + bool hasCAPABILITIES : 1; + bool hasSTARTTLS : 1; + bool hasDATE : 1; bool hasLIST_NEWSGROUPS : 1; - bool hasXGTITLE : 1; - bool hasLISTGROUP : 1; - bool hasLISTGROUPrange : 1; - bool hasOVER : 1; - bool hasXOVER : 1; - unsigned int use_tls : 3; - unsigned int status : 3; - bool cacheable : 1; - bool newsrc_modified : 1; + bool hasXGTITLE : 1; + bool hasLISTGROUP : 1; + bool hasLISTGROUPrange : 1; + bool hasOVER : 1; + bool hasXOVER : 1; + unsigned int use_tls : 3; + unsigned int status : 3; + bool cacheable : 1; + bool newsrc_modified : 1; FILE *newsrc_fp; char *newsrc_file; char *authenticators; @@ -95,12 +79,12 @@ struct NntpServer }; /** - * struct NewsrcEntry - An entry in a .newsrc (subscribed newsgroups) + * struct NntpHeaderData - NNTP-specific header data */ -struct NewsrcEntry +struct NntpHeaderData { - anum_t first; - anum_t last; + anum_t article_num; + bool parsed : 1; }; /** @@ -112,6 +96,18 @@ struct NntpAcache char *path; }; +/** + * struct NewsrcEntry - An entry in a .newsrc (subscribed newsgroups) + */ +struct NewsrcEntry +{ + anum_t first; + anum_t last; +}; + +/* number of entries in article cache */ +#define NNTP_ACACHE_LEN 10 + /** * struct NntpData - NNTP-specific server data */ @@ -125,9 +121,9 @@ struct NntpData anum_t last_cached; anum_t unread; bool subscribed : 1; - bool new : 1; - bool allowed : 1; - bool deleted : 1; + bool new : 1; + bool allowed : 1; + bool deleted : 1; unsigned int newsrc_len; struct NewsrcEntry *newsrc_ent; struct NntpServer *nserv; @@ -135,31 +131,6 @@ struct NntpData struct BodyCache *bcache; }; -/** - * struct NntpHeaderData - NNTP-specific header data - */ -struct NntpHeaderData -{ - anum_t article_num; - bool parsed : 1; -}; - -#define NHDR(hdr) ((struct NntpHeaderData *) ((hdr)->data)) - -/* internal functions */ -int nntp_add_group(char *line, void *data); -int nntp_active_save_cache(struct NntpServer *nserv); -int nntp_check_new_groups(struct NntpServer *nserv); -int nntp_open_connection(struct NntpServer *nserv); -void nntp_newsrc_gen_entries(struct Context *ctx); -void nntp_bcache_update(struct NntpData *nntp_data); -void nntp_article_status(struct Context *ctx, struct Header *hdr, char *group, anum_t anum); -void nntp_group_unread_stat(struct NntpData *nntp_data); -void nntp_data_free(void *data); -void nntp_acache_free(struct NntpData *nntp_data); -void nntp_delete_group_cache(struct NntpData *nntp_data); - -/* exposed interface */ struct NntpServer *nntp_select_server(char *server, bool leave_lock); struct NntpData *mutt_newsgroup_subscribe(struct NntpServer *nserv, char *group); struct NntpData *mutt_newsgroup_unsubscribe(struct NntpServer *nserv, char *group); @@ -179,13 +150,12 @@ const char *nntp_format_str(char *buf, size_t buflen, size_t col, int cols, char const char *src, const char *prec, const char *if_str, const char *else_str, unsigned long data, enum FormatFlag flags); +void nntp_article_status(struct Context *ctx, struct Header *hdr, char *group, anum_t anum); + extern struct NntpServer *CurrentNewsSrv; -#ifdef USE_HCACHE -header_cache_t *nntp_hcache_open(struct NntpData *nntp_data); -void nntp_hcache_update(struct NntpData *nntp_data, header_cache_t *hc); -#endif +int nntp_compare_order(const void *a, const void *b); extern struct MxOps mx_nntp_ops; -#endif /* _MUTT_NNTP_H */ +#endif /* _NNTP_NNTP_H */ diff --git a/nntp/nntp_private.h b/nntp/nntp_private.h new file mode 100644 index 000000000..feb946e24 --- /dev/null +++ b/nntp/nntp_private.h @@ -0,0 +1,66 @@ +/** + * @file + * Usenet network mailbox type; talk to an NNTP server + * + * @authors + * Copyright (C) 2018 Richard Russon + * + * @copyright + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation, either version 2 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#ifndef _NNTP_PRIVATE_H +#define _NNTP_PRIVATE_H + +#ifdef USE_HCACHE +#include "hcache/hcache.h" +#endif + +struct Context; +struct Header; +struct NntpData; +struct NntpServer; + +#define NNTP_PORT 119 +#define NNTP_SSL_PORT 563 + +/** + * enum NntpStatus - NNTP server return values + */ +enum NntpStatus +{ + NNTP_NONE = 0, + NNTP_OK, + NNTP_BYE +}; + +#define NHDR(hdr) ((struct NntpHeaderData *) ((hdr)->data)) + +void nntp_acache_free(struct NntpData *nntp_data); +int nntp_active_save_cache(struct NntpServer *nserv); +int nntp_add_group(char *line, void *data); +void nntp_bcache_update(struct NntpData *nntp_data); +int nntp_check_new_groups(struct NntpServer *nserv); +void nntp_data_free(void *data); +void nntp_delete_group_cache(struct NntpData *nntp_data); +void nntp_group_unread_stat(struct NntpData *nntp_data); +void nntp_newsrc_gen_entries(struct Context *ctx); +int nntp_open_connection(struct NntpServer *nserv); + +#ifdef USE_HCACHE +header_cache_t *nntp_hcache_open(struct NntpData *nntp_data); +void nntp_hcache_update(struct NntpData *nntp_data, header_cache_t *hc); +#endif + +#endif /* _NNTP_PRIVATE_H */ diff --git a/pager.c b/pager.c index 5847cd3ea..470ed2a7a 100644 --- a/pager.c +++ b/pager.c @@ -58,7 +58,7 @@ #include "sidebar.h" #endif #ifdef USE_NNTP -#include "nntp.h" +#include "nntp/nntp.h" #endif #ifdef ENABLE_NLS #include diff --git a/po/POTFILES.in b/po/POTFILES.in index e35b136c7..27c972c4f 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -117,8 +117,8 @@ ncrypt/pgplib.c ncrypt/pgpmicalg.c ncrypt/pgppacket.c ncrypt/smime.c -newsrc.c -nntp.c +nntp/newsrc.c +nntp/nntp.c opcodes.h pager.c parse.c diff --git a/send.c b/send.c index 5c3e61a33..4d434f279 100644 --- a/send.c +++ b/send.c @@ -50,7 +50,7 @@ #include "url.h" #ifdef USE_NNTP #include "mx.h" -#include "nntp.h" +#include "nntp/nntp.h" #endif #ifdef MIXMASTER #include "remailer.h" diff --git a/sendlib.c b/sendlib.c index 23a3e5d57..78cb889bd 100644 --- a/sendlib.c +++ b/sendlib.c @@ -57,7 +57,7 @@ #include "rfc2231.h" #include "state.h" #ifdef USE_NNTP -#include "nntp.h" +#include "nntp/nntp.h" #endif #ifdef HAVE_SYSEXITS_H #include diff --git a/sort.c b/sort.c index 825d1823d..33467eaf2 100644 --- a/sort.c +++ b/sort.c @@ -34,15 +34,13 @@ #include "protos.h" #ifdef USE_NNTP #include "mx.h" -#include "nntp.h" +#include "nntp/nntp.h" #endif -#define SORTCODE(x) (Sort & SORT_REVERSE) ? -(x) : x - /* function to use as discriminator when normal sort method is equal */ static sort_t *AuxSort = NULL; -static int perform_auxsort(int retval, const void *a, const void *b) +int perform_auxsort(int retval, const void *a, const void *b) { /* If the items compared equal by the main sort * and we're not already doing an 'aux' sort... */ @@ -164,19 +162,8 @@ static int compare_order(const void *a, const void *b) struct Header **ha = (struct Header **) a; struct Header **hb = (struct Header **) b; -#ifdef USE_NNTP - if (Context && Context->magic == MUTT_NNTP) - { - anum_t na = NHDR(*ha)->article_num; - anum_t nb = NHDR(*hb)->article_num; - int result = na == nb ? 0 : na > nb ? 1 : -1; - result = perform_auxsort(result, a, b); - return (SORTCODE(result)); - } - else -#endif - /* no need to auxsort because you will never have equality here */ - return (SORTCODE((*ha)->index - (*hb)->index)); + /* no need to auxsort because you will never have equality here */ + return (SORTCODE((*ha)->index - (*hb)->index)); } static int compare_spam(const void *a, const void *b) @@ -276,6 +263,11 @@ sort_t *mutt_get_sort_func(int method) case SORT_LABEL: return compare_label; case SORT_ORDER: +#ifdef USE_NNTP + if (Context && (Context->magic == MUTT_NNTP)) + return nntp_compare_order; + else +#endif return compare_order; case SORT_RECEIVED: return compare_date_received; diff --git a/sort.h b/sort.h index 393a9eba0..47059498c 100644 --- a/sort.h +++ b/sort.h @@ -67,11 +67,14 @@ struct Context; #define SORT_REVERSE (1 << 8) #define SORT_LAST (1 << 9) +#define SORTCODE(x) (Sort & SORT_REVERSE) ? -(x) : x + typedef int sort_t(const void *a, const void *b); sort_t *mutt_get_sort_func(int method); void mutt_sort_headers(struct Context *ctx, int init); int mutt_select_sort(int reverse); +int perform_auxsort(int retval, const void *a, const void *b); extern const struct Mapping SortMethods[]; -- 2.40.0