From: Pierre Habouzit Date: Sun, 27 May 2007 20:17:32 +0000 (+0200) Subject: simplify newsrc parsing X-Git-Url: http://git.madism.org/?a=commitdiff_plain;h=7768c028980b5fb468decf4fbfbd640082c321b2;p=apps%2Fmadmutt.git simplify newsrc parsing Signed-off-by: Pierre Habouzit --- diff --git a/browser.c b/browser.c index 5db8c75..ddc6b47 100644 --- a/browser.c +++ b/browser.c @@ -450,7 +450,7 @@ static int examine_directory (MUTTMENU * menu, struct browser_state *state, #ifdef USE_NNTP if (option (OPTNEWS)) { nntp_data_t *data; - NNTP_SERVER *news = CurrentNewsSrv; + nntp_server_t *news = CurrentNewsSrv; /* buffy_check (0); */ init_state (state, menu); @@ -542,7 +542,7 @@ static int examine_mailboxes (MUTTMENU * menu, struct browser_state *state) #ifdef USE_NNTP if (option (OPTNEWS)) { nntp_data_t *data; - NNTP_SERVER *news = CurrentNewsSrv; + nntp_server_t *news = CurrentNewsSrv; /* buffy_check (0); */ init_state (state, menu); @@ -1366,7 +1366,7 @@ void mutt_select_file (char *f, ssize_t flen, int flags, char ***files, char *s = buf; int j = menu->current; nntp_data_t *nd; - NNTP_SERVER *news = CurrentNewsSrv; + nntp_server_t *news = CurrentNewsSrv; if (i == OP_SUBSCRIBE_PATTERN || i == OP_UNSUBSCRIBE_PATTERN) { char tmp[STRING]; diff --git a/nntp.c b/nntp.c index 364475e..9319c38 100644 --- a/nntp.c +++ b/nntp.c @@ -27,9 +27,9 @@ #define NNTP_PORT 119 #define NNTP_SSL_PORT 563 -static int nntp_get_cache_all (NNTP_SERVER *); -static int nntp_check_newgroups (NNTP_SERVER *, int); -static void nntp_delete_cache (nntp_data_t *); +static unsigned int _checked = 0; + +static int nntp_check_newgroups (nntp_server_t *, int); /* newsrc {{{ */ @@ -47,73 +47,63 @@ static void mutt_newsgroup_stat(nntp_data_t *data) } } -static int nntp_parse_newsrc_line (NNTP_SERVER * news, char *line) +static int nntp_parse_newsrc_line(nntp_server_t * news, const char *line) { - nntp_data_t *data; - char group[LONG_STRING]; - int x = 1; - char *p, *b, *h; - - for (p = line; p; p = strchr(p, ',')) { - x++; - } + nntp_data_t *data; + char group[LONG_STRING]; + const char *p; + int x = 1; - p = strpbrk(line, ":!"); - if (!p) - return -1; + for (p = line; *p; p++) { + x += *p == ','; + } - m_strncpy(group, ssizeof(group), line, p - line); - if ((data = hash_find(news->newsgroups, group)) == NULL) { - data = nntp_data_new(); - data->group = m_strdup(group); - data->nserv = news; - data->deleted = 1; - if (news->newsgroups->nelem < news->newsgroups->curnelem * 2) - hash_resize (news->newsgroups, news->newsgroups->nelem * 2); - hash_insert(news->newsgroups, data->group, data); - nntp_data_list_append(&news->list, data); - } else { - p_delete(&data->entries); - } + p = strpbrk(line, ":!"); + if (!p) + return -1; - data->rc = 1; - data->entries = p_new(NEWSRC_ENTRY, x * 2); - data->max = x * 2; + m_strncpy(group, ssizeof(group), line, p - line); + data = hash_find(news->newsgroups, group); + if (!data) { + data = nntp_data_new(); + data->group = p_dupstr(line, p - line); + data->nserv = news; + data->deleted = 1; + if (news->newsgroups->nelem < news->newsgroups->curnelem * 2) + hash_resize (news->newsgroups, news->newsgroups->nelem * 2); + hash_insert(news->newsgroups, data->group, data); + nntp_data_list_append(&news->list, data); + } else { + p_delete(&data->entries); + } - data->subscribed = (*p++ == ':'); + data->rc = 1; + data->entries = p_new(NEWSRC_ENTRY, x * 2); + data->max = x * 2; + data->subscribed = (*p++ == ':'); + + for (x = 0; *p; p++) { + p = skipspaces(p); + data->entries[x].first = strtol(p, (char **)&p, 10); + p = skipspaces(p); + if (*p == '-') { + data->entries[x].last = strtol(p + 1, (char **)&p, 10); + } else { + data->entries[x].last = data->entries[x].first; + } - b = p; - x = 0; - while (*b) { - while (*p && *p != ',' && *p != '\n') - p++; - if (*p) { - *p = '\0'; - p++; + p = strchrnul(p, ','); + x += data->entries[x].last != 0; } - if ((h = strchr (b, '-'))) { - *h = '\0'; - h++; - data->entries[x].first = atoi (b); - data->entries[x].last = atoi (h); - } - else { - data->entries[x].first = atoi (b); - data->entries[x].last = data->entries[x].first; - } - b = p; - if (data->entries[x].last != 0) - x++; - } - if (x && !data->lastMessage) - data->lastMessage = data->entries[x - 1].last; - data->num = x; - mutt_newsgroup_stat (data); - return 0; + if (x && !data->lastMessage) + data->lastMessage = data->entries[x - 1].last; + data->num = x; + mutt_newsgroup_stat(data); + return 0; } -static int slurp_newsrc (NNTP_SERVER * news) +static int slurp_newsrc (nntp_server_t * news) { FILE *fp; char *buf; @@ -132,8 +122,8 @@ static int slurp_newsrc (NNTP_SERVER * news) } buf = p_new(char, sb.st_size + 1); - while (fgets (buf, sb.st_size + 1, fp)) - nntp_parse_newsrc_line (news, buf); + while (fgets(buf, sb.st_size + 1, fp)) + nntp_parse_newsrc_line(news, buf); p_delete(&buf); mx_unlock_file (news->newsrc, fileno (fp), 0); @@ -149,7 +139,7 @@ static void nntp_cache_expand (char *dst, const char *src) /* Loads $news_cache_dir/.index into memory, loads newsserver data * and newsgroup cache names */ -static int nntp_parse_cacheindex (NNTP_SERVER * news) +static int nntp_parse_cacheindex (nntp_server_t * news) { struct stat st; char buf[HUGE_STRING], *cp; @@ -299,6 +289,84 @@ void nntp_expand_path (char *line, ssize_t len, ACCOUNT * act) p_delete(&url.path); } +static int add_group (char *buf, void *serv) +{ + nntp_server_t *s = serv; + char group[LONG_STRING], mod, desc[HUGE_STRING]; + int first, last; + nntp_data_t *nntp_data; + static int n = 0; + + _checked = n; /* _checked have N, where N = number of groups */ + if (!buf) /* at EOF must be zerouth */ + n = 0; + + if (!s || !buf) + return 0; + + *desc = 0; + sscanf (buf, "%s %d %d %c %[^\n]", group, &last, &first, &mod, desc); + if (!group) + return 0; + if ((nntp_data = hash_find(s->newsgroups, group)) == NULL) { + n++; + nntp_data = nntp_data_new(); + nntp_data->group = m_strdup(group); + nntp_data->nserv = s; + if (s->newsgroups->nelem < s->newsgroups->curnelem * 2) + hash_resize (s->newsgroups, s->newsgroups->nelem * 2); + hash_insert(s->newsgroups, nntp_data->group, nntp_data); + nntp_data_list_append(&s->list, nntp_data); + } + nntp_data->deleted = 0; + nntp_data->firstMessage = first; + nntp_data->lastMessage = last; + if (mod == 'y') + nntp_data->allowed = 1; + else + nntp_data->allowed = 0; + if (nntp_data->desc) + p_delete(&nntp_data->desc); + if (*desc) + nntp_data->desc = m_strdup(desc); + if (nntp_data->rc || nntp_data->lastCached) + mutt_newsgroup_stat (nntp_data); + else if (nntp_data->lastMessage && + nntp_data->firstMessage <= nntp_data->lastMessage) + nntp_data->unread = nntp_data->lastMessage - nntp_data->firstMessage + 1; + else + nntp_data->unread = 0; + + return 0; +} + +/* Load list of all newsgroups from cache ALL */ +static int nntp_get_cache_all (nntp_server_t * serv) +{ + char buf[HUGE_STRING]; + FILE *f; + + nntp_cache_expand (buf, serv->cache); + if ((f = safe_fopen (buf, "r"))) { + int i = 0; + + while (fgets (buf, sizeof (buf), f) != NULL) { + if (ReadInc && (i % ReadInc == 0)) + mutt_message (_("Loading list from cache... %d"), i); + add_group (buf, serv); + i++; + } + add_group (NULL, NULL); + m_fclose(&f); + mutt_clear_error (); + return 0; + } + else { + p_delete(&serv->cache); + return -1; + } +} + /* * Automatically loads a newsrc into memory, if necessary. * Checks the size/mtime of a newsrc file, if it doesn't match, load @@ -309,13 +377,13 @@ void nntp_expand_path (char *line, ssize_t len, ACCOUNT * act) * ':' denoting subscribed or '!' denoting unsubscribed, then a * comma separated list of article numbers and ranges. */ -NNTP_SERVER *mutt_select_newsserver (char *server) +nntp_server_t *mutt_select_newsserver (char *server) { char file[_POSIX_PATH_MAX]; char *buf, *p; nntp_data_t *list; ACCOUNT act; - NNTP_SERVER *serv; + nntp_server_t *serv; CONNECTION *conn; p_clear(&act, 1); @@ -347,7 +415,7 @@ NNTP_SERVER *mutt_select_newsserver (char *server) m_strformat(file, sizeof(file), 0, NewsRc, nntp_format_str, NULL, 0); mutt_expand_path(file, sizeof(file)); - serv = (NNTP_SERVER *) conn->data; + serv = (nntp_server_t *) conn->data; if (serv) { struct stat sb; @@ -357,9 +425,7 @@ NNTP_SERVER *mutt_select_newsserver (char *server) || serv->mtime != sb.st_mtime))) { for (list = serv->list; list; list = list->next) { - list->subscribed = 0; - list->rc = 0; - list->num = 0; + list->subscribed = list->rc = list->num = 0; } slurp_newsrc (serv); nntp_clear_cacheindex (serv); @@ -372,7 +438,7 @@ NNTP_SERVER *mutt_select_newsserver (char *server) } /* New newsserver */ - serv = p_new(NNTP_SERVER, 1); + serv = p_new(nntp_server_t, 1); serv->conn = conn; serv->newsrc = m_strdup(file); serv->newsgroups = hash_new(1009, false); @@ -632,7 +698,7 @@ static int mutt_update_list_file (char *filename, char *section, return 0; } -int mutt_newsrc_update (NNTP_SERVER * news) +int mutt_newsrc_update (nntp_server_t * news) { buffer_t buf; nntp_data_t *data; @@ -683,7 +749,7 @@ static FILE *mutt_mkname (char *s) } /* Updates info into .index file: ALL or about selected newsgroup */ -static int nntp_update_cacheindex (NNTP_SERVER * serv, nntp_data_t * data) +static int nntp_update_cacheindex (nntp_server_t * serv, nntp_data_t * data) { char buf[LONG_STRING]; char file[_POSIX_PATH_MAX]; @@ -706,8 +772,24 @@ static int nntp_update_cacheindex (NNTP_SERVER * serv, nntp_data_t * data) return mutt_update_list_file (file, serv->conn->account.host, key, buf); } +static void nntp_delete_cache (nntp_data_t * data) +{ + char buf[_POSIX_PATH_MAX]; + + if (!option (OPTNEWSCACHE) || !data || !data->cache || !data->nserv) + return; + + nntp_cache_expand (buf, data->cache); + unlink (buf); + p_delete(&data->cache); + data->lastCached = 0; + nntp_cache_expand (buf, ".index"); + mutt_update_list_file (buf, data->nserv->conn->account.host, data->group, + NULL); +} + /* Remove cache files of unsubscribed newsgroups */ -void nntp_clear_cacheindex (NNTP_SERVER * news) +void nntp_clear_cacheindex (nntp_server_t * news) { nntp_data_t *data; @@ -722,7 +804,7 @@ void nntp_clear_cacheindex (NNTP_SERVER * news) return; } -static int nntp_save_cache_index (NNTP_SERVER * news) +static int nntp_save_cache_index (nntp_server_t * news) { char buf[HUGE_STRING]; char file[_POSIX_PATH_MAX]; @@ -855,23 +937,7 @@ static int nntp_save_cache_group (CONTEXT * ctx) return 0; } -static void nntp_delete_cache (nntp_data_t * data) -{ - char buf[_POSIX_PATH_MAX]; - - if (!option (OPTNEWSCACHE) || !data || !data->cache || !data->nserv) - return; - - nntp_cache_expand (buf, data->cache); - unlink (buf); - p_delete(&data->cache); - data->lastCached = 0; - nntp_cache_expand (buf, ".index"); - mutt_update_list_file (buf, data->nserv->conn->account.host, data->group, - NULL); -} - -nntp_data_t *mutt_newsgroup_subscribe (NNTP_SERVER * news, char *group) +nntp_data_t *mutt_newsgroup_subscribe (nntp_server_t * news, char *group) { nntp_data_t *data; @@ -894,7 +960,7 @@ nntp_data_t *mutt_newsgroup_subscribe (NNTP_SERVER * news, char *group) return data; } -nntp_data_t *mutt_newsgroup_unsubscribe (NNTP_SERVER * news, char *group) +nntp_data_t *mutt_newsgroup_unsubscribe (nntp_server_t * news, char *group) { nntp_data_t *data; @@ -909,7 +975,7 @@ nntp_data_t *mutt_newsgroup_unsubscribe (NNTP_SERVER * news, char *group) return data; } -nntp_data_t *mutt_newsgroup_catchup (NNTP_SERVER * news, char *group) +nntp_data_t *mutt_newsgroup_catchup (nntp_server_t * news, char *group) { nntp_data_t *data; @@ -933,7 +999,7 @@ nntp_data_t *mutt_newsgroup_catchup (NNTP_SERVER * news, char *group) return data; } -nntp_data_t *mutt_newsgroup_uncatchup (NNTP_SERVER * news, char *group) +nntp_data_t *mutt_newsgroup_uncatchup (nntp_server_t * news, char *group) { nntp_data_t *data; @@ -1009,8 +1075,6 @@ void nntp_buffy (char* dst, ssize_t dstlen) { /* }}} */ /* nntp protocol {{{ */ -static unsigned int _checked = 0; - void nntp_sync_sidebar (nntp_data_t* data) { int i = 0; BUFFY* tmp = NULL; @@ -1044,7 +1108,7 @@ void nntp_sync_sidebar (nntp_data_t* data) { tmp->msgcount = data->lastMessage - data->firstMessage; } -static int nntp_auth (NNTP_SERVER * serv) +static int nntp_auth (nntp_server_t * serv) { CONNECTION *conn = serv->conn; char buf[STRING]; @@ -1083,7 +1147,7 @@ static int nntp_auth (NNTP_SERVER * serv) return 0; } -static int nntp_connect_error (NNTP_SERVER * serv) +static int nntp_connect_error (nntp_server_t * serv) { serv->status = NNTP_NONE; mutt_socket_close (serv->conn); @@ -1093,7 +1157,7 @@ static int nntp_connect_error (NNTP_SERVER * serv) return -1; } -static int nntp_connect_and_auth (NNTP_SERVER * serv) +static int nntp_connect_and_auth (nntp_server_t * serv) { CONNECTION *conn = serv->conn; char buf[STRING]; @@ -1153,7 +1217,7 @@ static int nntp_connect_and_auth (NNTP_SERVER * serv) return 0; } -static int nntp_attempt_features (NNTP_SERVER * serv) +static int nntp_attempt_features (nntp_server_t * serv) { char buf[LONG_STRING]; CONNECTION *conn = serv->conn; @@ -1196,7 +1260,7 @@ static int nntp_attempt_features (NNTP_SERVER * serv) return 0; } -static int nntp_open_connection (NNTP_SERVER * serv) +static int nntp_open_connection (nntp_server_t * serv) { if (serv->status == NNTP_OK) return 0; @@ -1209,7 +1273,7 @@ static int nntp_open_connection (NNTP_SERVER * serv) return 0; } -static int nntp_reconnect (NNTP_SERVER * serv) +static int nntp_reconnect (nntp_server_t * serv) { char buf[STRING]; @@ -1452,7 +1516,7 @@ static int parse_description (char *line, void *n) { register char *d = line; nntp_data_t *data; - NNTP_SERVER *news = n; + nntp_server_t *news = n; if (!line) return 0; @@ -1768,7 +1832,7 @@ static int nntp_fetch_headers(CONTEXT * ctx, int first, int last) static int nntp_open_mailbox (CONTEXT * ctx) { nntp_data_t *nntp_data; - NNTP_SERVER *serv; + nntp_server_t *serv; char buf[HUGE_STRING]; char server[LONG_STRING]; int count = 0, first; @@ -2099,7 +2163,7 @@ int nntp_close_mailbox (CONTEXT * ctx) } nntp_sync_mailbox (ctx, 0, NULL); if (ctx->data && ((nntp_data_t *) ctx->data)->nserv) { - NNTP_SERVER *news; + nntp_server_t *news; news = ((nntp_data_t *) ctx->data)->nserv; newsrc_gen_entries (ctx); @@ -2168,58 +2232,7 @@ static int nntp_check_mailbox (CONTEXT * ctx, int* unused1, int unused2) return _nntp_check_mailbox (ctx, (nntp_data_t *) ctx->data); } -static int add_group (char *buf, void *serv) -{ - NNTP_SERVER *s = serv; - char group[LONG_STRING], mod, desc[HUGE_STRING]; - int first, last; - nntp_data_t *nntp_data; - static int n = 0; - - _checked = n; /* _checked have N, where N = number of groups */ - if (!buf) /* at EOF must be zerouth */ - n = 0; - - if (!s || !buf) - return 0; - - *desc = 0; - sscanf (buf, "%s %d %d %c %[^\n]", group, &last, &first, &mod, desc); - if (!group) - return 0; - if ((nntp_data = hash_find(s->newsgroups, group)) == NULL) { - n++; - nntp_data = nntp_data_new(); - nntp_data->group = m_strdup(group); - nntp_data->nserv = s; - if (s->newsgroups->nelem < s->newsgroups->curnelem * 2) - hash_resize (s->newsgroups, s->newsgroups->nelem * 2); - hash_insert(s->newsgroups, nntp_data->group, nntp_data); - nntp_data_list_append(&s->list, nntp_data); - } - nntp_data->deleted = 0; - nntp_data->firstMessage = first; - nntp_data->lastMessage = last; - if (mod == 'y') - nntp_data->allowed = 1; - else - nntp_data->allowed = 0; - if (nntp_data->desc) - p_delete(&nntp_data->desc); - if (*desc) - nntp_data->desc = m_strdup(desc); - if (nntp_data->rc || nntp_data->lastCached) - mutt_newsgroup_stat (nntp_data); - else if (nntp_data->lastMessage && - nntp_data->firstMessage <= nntp_data->lastMessage) - nntp_data->unread = nntp_data->lastMessage - nntp_data->firstMessage + 1; - else - nntp_data->unread = 0; - - return 0; -} - -static int nntp_check_newgroups (NNTP_SERVER * serv, int force) +static int nntp_check_newgroups (nntp_server_t * serv, int force) { char buf[LONG_STRING]; nntp_data_t nntp_data; @@ -2279,35 +2292,8 @@ static int nntp_check_newgroups (NNTP_SERVER * serv, int force) return _checked; } -/* Load list of all newsgroups from cache ALL */ -static int nntp_get_cache_all (NNTP_SERVER * serv) -{ - char buf[HUGE_STRING]; - FILE *f; - - nntp_cache_expand (buf, serv->cache); - if ((f = safe_fopen (buf, "r"))) { - int i = 0; - - while (fgets (buf, sizeof (buf), f) != NULL) { - if (ReadInc && (i % ReadInc == 0)) - mutt_message (_("Loading list from cache... %d"), i); - add_group (buf, serv); - i++; - } - add_group (NULL, NULL); - m_fclose(&f); - mutt_clear_error (); - return 0; - } - else { - p_delete(&serv->cache); - return -1; - } -} - /* Load list of all newsgroups from active */ -int nntp_get_active (NNTP_SERVER * serv) +int nntp_get_active (nntp_server_t * serv) { char msg[STRING]; nntp_data_t nntp_data, **tmp = &serv->list; diff --git a/nntp.h b/nntp.h index 3bc9103..037d8a1 100644 --- a/nntp.h +++ b/nntp.h @@ -28,7 +28,7 @@ enum { typedef struct nntp_data_t nntp_data_t; -typedef struct { +typedef struct nntp_server_t { unsigned feat_known : 1; unsigned hasXPAT : 1; unsigned hasXGTITLE : 1; @@ -45,7 +45,7 @@ typedef struct { hash_t *newsgroups; nntp_data_t *list; /* list of newsgroups */ CONNECTION *conn; -} NNTP_SERVER; +} nntp_server_t; typedef struct { int index; @@ -76,7 +76,7 @@ struct nntp_data_t { char *group; char *desc; char *cache; - NNTP_SERVER *nserv; + nntp_server_t *nserv; NNTP_CACHE acache[NNTP_CACHE_LEN]; }; @@ -86,19 +86,19 @@ DO_NEW(nntp_data_t, nntp_data); DO_DELETE(nntp_data_t, nntp_data); DO_SLIST(nntp_data_t, nntp_data, nntp_data_delete); -NNTP_SERVER *mutt_select_newsserver (char *); -nntp_data_t *mutt_newsgroup_subscribe (NNTP_SERVER *, char *); -nntp_data_t *mutt_newsgroup_unsubscribe (NNTP_SERVER *, char *); -nntp_data_t *mutt_newsgroup_catchup (NNTP_SERVER *, char *); -nntp_data_t *mutt_newsgroup_uncatchup (NNTP_SERVER *, char *); -void nntp_clear_cacheindex (NNTP_SERVER *); -int mutt_newsrc_update (NNTP_SERVER *); +nntp_server_t *mutt_select_newsserver (char *); +nntp_data_t *mutt_newsgroup_subscribe (nntp_server_t *, char *); +nntp_data_t *mutt_newsgroup_unsubscribe (nntp_server_t *, char *); +nntp_data_t *mutt_newsgroup_catchup (nntp_server_t *, char *); +nntp_data_t *mutt_newsgroup_uncatchup (nntp_server_t *, char *); +void nntp_clear_cacheindex (nntp_server_t *); +int mutt_newsrc_update (nntp_server_t *); int nntp_close_mailbox (CONTEXT *); int nntp_fetch_message (MESSAGE *, CONTEXT *, int); int nntp_post (const char *); int nntp_check_msgid (CONTEXT *, const char *); int nntp_check_children (CONTEXT *, const char *); -int nntp_get_active (NNTP_SERVER *); +int nntp_get_active (nntp_server_t *); void nntp_buffy (char* dst, ssize_t dstlen); void nntp_expand_path (char *, ssize_t, ACCOUNT *); void nntp_logout_all(void); @@ -106,6 +106,6 @@ const char *nntp_format_str(char *, ssize_t, char, const char *, const char *, const char *, const char *, anytype, format_flag); void nntp_sync_sidebar (nntp_data_t*); -WHERE NNTP_SERVER *CurrentNewsSrv INITVAL (NULL); +WHERE nntp_server_t *CurrentNewsSrv INITVAL (NULL); #endif /* _NNTP_H_ */