]> granicus.if.org Git - neomutt/commitdiff
tidy pop
authorRichard Russon <rich@flatcap.org>
Tue, 11 Sep 2018 16:00:42 +0000 (17:00 +0100)
committerRichard Russon <rich@flatcap.org>
Thu, 13 Sep 2018 01:02:48 +0000 (02:02 +0100)
pop/pop.c
pop/pop_auth.c
pop/pop_lib.c

index 58ae44d37f6916ab6eded1a86f24a1f2e61d7af2..fcd84181ce57f148b3abf6e4cd3fe377a17e1477 100644 (file)
--- a/pop/pop.c
+++ b/pop/pop.c
@@ -101,7 +101,7 @@ static const char *cache_id(const char *id)
  */
 static int fetch_message(char *line, void *file)
 {
-  FILE *f = (FILE *) file;
+  FILE *f = file;
 
   fputs(line, f);
   if (fputc('\n', f) == EOF)
@@ -121,10 +121,6 @@ static int fetch_message(char *line, void *file)
  */
 static int pop_read_header(struct PopData *pop_data, struct Header *h)
 {
-  int rc, index;
-  size_t length;
-  char buf[LONG_STRING];
-
   FILE *f = mutt_file_mkstemp();
   if (!f)
   {
@@ -132,8 +128,12 @@ static int pop_read_header(struct PopData *pop_data, struct Header *h)
     return -3;
   }
 
+  int index = 0;
+  size_t length = 0;
+  char buf[LONG_STRING];
+
   snprintf(buf, sizeof(buf), "LIST %d\r\n", h->refno);
-  rc = pop_query(pop_data, buf, sizeof(buf));
+  int rc = pop_query(pop_data, buf, sizeof(buf));
   if (rc == 0)
   {
     sscanf(buf, "+OK %d %zu", &index, &length);
@@ -201,13 +201,12 @@ static int pop_read_header(struct PopData *pop_data, struct Header *h)
  */
 static int fetch_uidl(char *line, void *data)
 {
-  int i, index;
   struct Mailbox *mailbox = data;
   struct PopData *pop_data = mailbox->data;
   char *endp = NULL;
 
   errno = 0;
-  index = strtol(line, &endp, 10);
+  int index = strtol(line, &endp, 10);
   if (errno)
     return -1;
   while (*endp == ' ')
@@ -218,6 +217,7 @@ static int fetch_uidl(char *line, void *data)
   if (strlen(line) == 0)
     return -1;
 
+  int i;
   for (i = 0; i < mailbox->msg_count; i++)
     if (mutt_str_strcmp(line, mailbox->hdrs[i]->data) == 0)
       break;
@@ -250,6 +250,7 @@ static int msg_cache_check(const char *id, struct BodyCache *bcache, void *data)
   struct Mailbox *mailbox = data;
   if (!mailbox)
     return -1;
+
   struct PopData *pop_data = mailbox->data;
   if (!pop_data)
     return -1;
@@ -290,12 +291,12 @@ static int pop_hcache_namer(const char *path, char *dest, size_t destlen)
  */
 static header_cache_t *pop_hcache_open(struct PopData *pop_data, const char *path)
 {
-  struct Url url;
-  char p[LONG_STRING];
-
   if (!pop_data || !pop_data->conn)
     return mutt_hcache_open(HeaderCache, path, NULL);
 
+  struct Url url;
+  char p[LONG_STRING];
+
   mutt_account_tourl(&pop_data->conn->account, &url);
   url.path = HC_FNAME;
   url_tostring(&url, p, sizeof(p), U_PATH);
@@ -501,23 +502,19 @@ static void pop_clear_cache(struct PopData *pop_data)
  */
 void pop_fetch_mail(void)
 {
-  char buffer[LONG_STRING];
-  char msgbuf[SHORT_STRING];
-  char *url = NULL, *p = NULL;
-  int delanswer, last = 0, msgs, bytes, rset = 0, ret;
-  struct Connection *conn = NULL;
-  struct Message *msg = NULL;
-  struct ConnAccount acct;
-  struct PopData *pop_data = NULL;
-
   if (!PopHost)
   {
     mutt_error(_("POP host is not defined"));
     return;
   }
 
-  p = mutt_mem_calloc(strlen(PopHost) + 7, sizeof(char));
-  url = p;
+  char buffer[LONG_STRING];
+  char msgbuf[SHORT_STRING];
+  int delanswer, last = 0, msgs, bytes, rset = 0, ret;
+  struct ConnAccount acct;
+
+  char *p = mutt_mem_calloc(strlen(PopHost) + 7, sizeof(char));
+  char *url = p;
   if (url_check_scheme(PopHost) == U_UNKNOWN)
   {
     strcpy(url, "pop://");
@@ -533,11 +530,11 @@ void pop_fetch_mail(void)
     return;
   }
 
-  conn = mutt_conn_find(NULL, &acct);
+  struct Connection *conn = mutt_conn_find(NULL, &acct);
   if (!conn)
     return;
 
-  pop_data = mutt_mem_calloc(1, sizeof(struct PopData));
+  struct PopData *pop_data = mutt_mem_calloc(1, sizeof(struct PopData));
   pop_data->conn = conn;
 
   if (pop_open_connection(pop_data) < 0)
@@ -595,7 +592,7 @@ void pop_fetch_mail(void)
 
   for (int i = last + 1; i <= msgs; i++)
   {
-    msg = mx_msg_open_new(ctx, NULL, MUTT_ADD_FROM);
+    struct Message *msg = mx_msg_open_new(ctx, NULL, MUTT_ADD_FROM);
     if (!msg)
       ret = -3;
     else
@@ -670,17 +667,15 @@ fail:
 }
 
 /**
- * pop_mbox_open - open POP mailbox, fetch only headers
- * @param ctx Mailbox
- * @retval  0 Success
- * @retval -1 Failure
+ * pop_mbox_open - Implements MxOps::mbox_open()
+ *
+ * Fetch only headers
  */
 static int pop_mbox_open(struct Context *ctx)
 {
   char buf[PATH_MAX];
   struct Connection *conn = NULL;
   struct ConnAccount acct;
-  struct PopData *pop_data = NULL;
   struct Url url;
 
   if (pop_parse_path(ctx->mailbox->path, &acct))
@@ -700,7 +695,7 @@ static int pop_mbox_open(struct Context *ctx)
   mutt_str_strfcpy(ctx->mailbox->realpath, ctx->mailbox->path,
                    sizeof(ctx->mailbox->realpath));
 
-  pop_data = mutt_mem_calloc(1, sizeof(struct PopData));
+  struct PopData *pop_data = mutt_mem_calloc(1, sizeof(struct PopData));
   pop_data->conn = conn;
   ctx->mailbox->data = pop_data;
 
@@ -743,15 +738,10 @@ static int pop_mbox_open(struct Context *ctx)
 }
 
 /**
- * pop_mbox_check - Check for new messages and fetch headers
- * @param ctx        Mailbox
- * @param index_hint Current Message
- * @retval  0 Success
- * @retval -1 Failure
+ * pop_mbox_check - Implements MxOps::mbox_check()
  */
 static int pop_mbox_check(struct Context *ctx, int *index_hint)
 {
-  int ret;
   struct PopData *pop_data = ctx->mailbox->data;
 
   if ((pop_data->check_time + PopCheckinterval) > time(NULL))
@@ -768,7 +758,7 @@ static int pop_mbox_check(struct Context *ctx, int *index_hint)
 
   mutt_message(_("Checking for new messages..."));
 
-  ret = pop_fetch_headers(ctx);
+  int ret = pop_fetch_headers(ctx);
   pop_clear_cache(pop_data);
 
   if (ret < 0)
@@ -781,11 +771,9 @@ static int pop_mbox_check(struct Context *ctx, int *index_hint)
 }
 
 /**
- * pop_mbox_sync - update POP mailbox, delete messages from server
- * @param ctx        Mailbox
- * @param index_hint Current Message
- * @retval  0 Success
- * @retval -1 Failure
+ * pop_mbox_sync - Implements MxOps::mbox_sync()
+ *
+ * Update POP mailbox, delete messages from server
  */
 static int pop_mbox_sync(struct Context *ctx, int *index_hint)
 {
@@ -873,14 +861,11 @@ static int pop_mbox_sync(struct Context *ctx, int *index_hint)
 }
 
 /**
- * pop_mbox_close - close POP mailbox
- * @param ctx Mailbox
- * @retval 0 Always
+ * pop_mbox_close - Implements MxOps::mbox_close()
  */
 static int pop_mbox_close(struct Context *ctx)
 {
   struct PopData *pop_data = ctx->mailbox->data;
-
   if (!pop_data)
     return 0;
 
@@ -903,23 +888,16 @@ static int pop_mbox_close(struct Context *ctx)
 }
 
 /**
- * pop_msg_open - fetch message from POP server
- * @param ctx   Mailbox
- * @param msg   Message
- * @param msgno Message number
- * @retval  0 Success
- * @retval -1 Failure
+ * pop_msg_open - Implements MxOps::msg_open()
  */
 static int pop_msg_open(struct Context *ctx, struct Message *msg, int msgno)
 {
-  void *uidl = NULL;
   char buf[LONG_STRING];
   char path[PATH_MAX];
   struct Progress progressbar;
   struct PopData *pop_data = ctx->mailbox->data;
-  struct PopCache *cache = NULL;
   struct Header *h = ctx->mailbox->hdrs[msgno];
-  unsigned short bcache = 1;
+  bool bcache = true;
 
   /* see if we already have the message in body cache */
   msg->fp = mutt_bcache_get(pop_data->bcache, cache_id(h->data));
@@ -929,7 +907,7 @@ static int pop_msg_open(struct Context *ctx, struct Message *msg, int msgno)
   /* see if we already have the message in our cache in
    * case $message_cachedir is unset
    */
-  cache = &pop_data->cache[h->index % POP_CACHE_LEN];
+  struct PopCache *cache = &pop_data->cache[h->index % POP_CACHE_LEN];
 
   if (cache->path)
   {
@@ -972,7 +950,7 @@ static int pop_msg_open(struct Context *ctx, struct Message *msg, int msgno)
     if (!msg->fp)
     {
       /* no */
-      bcache = 0;
+      bcache = false;
       mutt_mktemp(path, sizeof(path));
       msg->fp = mutt_file_fopen(path, "w+");
       if (!msg->fp)
@@ -1020,9 +998,9 @@ static int pop_msg_open(struct Context *ctx, struct Message *msg, int msgno)
     cache->path = mutt_str_strdup(path);
   }
   rewind(msg->fp);
-  uidl = h->data;
+  void *uidl = h->data;
 
-  /* we replace envelop, key in subj_hash has to be updated as well */
+  /* we replace envelope, key in subj_hash has to be updated as well */
   if (ctx->mailbox->subj_hash && h->env->real_subj)
     mutt_hash_delete(ctx->mailbox->subj_hash, h->env->real_subj, h);
   mutt_label_hash_remove(ctx->mailbox, h);
@@ -1054,9 +1032,7 @@ static int pop_msg_open(struct Context *ctx, struct Message *msg, int msgno)
 }
 
 /**
- * pop_msg_close - Close POP Message
- * @param ctx Mailbox
- * @param msg Message
+ * pop_msg_close - Implements MxOps::msg_close()
  * @retval 0   Success
  * @retval EOF Error, see errno
  */
index e51700fc052fd45a80fe71099cd6bab3d09638ae..f745cea78759c4ea476e4e8ed0ac862158e2b26c 100644 (file)
@@ -58,8 +58,6 @@ static enum PopAuthRes pop_auth_sasl(struct PopData *pop_data, const char *metho
   sasl_conn_t *saslconn = NULL;
   sasl_interact_t *interaction = NULL;
   int rc;
-  char *buf = NULL;
-  size_t bufsize = 0;
   char inbuf[LONG_STRING];
   const char *mech = NULL;
   const char *pc = NULL;
@@ -102,8 +100,8 @@ static enum PopAuthRes pop_auth_sasl(struct PopData *pop_data, const char *metho
 
   mutt_message(_("Authenticating (SASL)..."));
 
-  bufsize = ((olen * 2) > LONG_STRING) ? (olen * 2) : LONG_STRING;
-  buf = mutt_mem_malloc(bufsize);
+  size_t bufsize = ((olen * 2) > LONG_STRING) ? (olen * 2) : LONG_STRING;
+  char *buf = mutt_mem_malloc(bufsize);
 
   snprintf(buf, bufsize, "AUTH %s", mech);
   olen = strlen(buf);
@@ -281,9 +279,6 @@ static enum PopAuthRes pop_auth_apop(struct PopData *pop_data, const char *metho
  */
 static enum PopAuthRes pop_auth_user(struct PopData *pop_data, const char *method)
 {
-  char buf[LONG_STRING];
-  int ret;
-
   if (!pop_data->cmd_user)
     return POP_A_UNAVAIL;
 
@@ -292,8 +287,9 @@ static enum PopAuthRes pop_auth_user(struct PopData *pop_data, const char *metho
 
   mutt_message(_("Logging in..."));
 
+  char buf[LONG_STRING];
   snprintf(buf, sizeof(buf), "USER %s\r\n", pop_data->conn->account.user);
-  ret = pop_query(pop_data, buf, sizeof(buf));
+  int ret = pop_query(pop_data, buf, sizeof(buf));
 
   if (pop_data->cmd_user == 2)
   {
@@ -343,21 +339,14 @@ static enum PopAuthRes pop_auth_user(struct PopData *pop_data, const char *metho
  */
 static enum PopAuthRes pop_auth_oauth(struct PopData *pop_data, const char *method)
 {
-  char *oauthbearer = NULL;
-  char decoded_err[LONG_STRING];
-  char *err = NULL;
-  char *auth_cmd = NULL;
-  size_t auth_cmd_len;
-  int len;
-
   mutt_message(_("Authenticating (OAUTHBEARER)..."));
 
-  oauthbearer = mutt_account_getoauthbearer(&pop_data->conn->account);
+  char *oauthbearer = mutt_account_getoauthbearer(&pop_data->conn->account);
   if (!oauthbearer)
     return POP_A_FAILURE;
 
-  auth_cmd_len = strlen(oauthbearer) + 30;
-  auth_cmd = mutt_mem_malloc(auth_cmd_len);
+  size_t auth_cmd_len = strlen(oauthbearer) + 30;
+  char *auth_cmd = mutt_mem_malloc(auth_cmd_len);
   snprintf(auth_cmd, auth_cmd_len, "AUTH OAUTHBEARER %s\r\n", oauthbearer);
   FREE(&oauthbearer);
 
@@ -382,8 +371,9 @@ static enum PopAuthRes pop_auth_oauth(struct PopData *pop_data, const char *meth
    * See RFC7628 3.2.3 */
   mutt_socket_send(pop_data->conn, "\001");
 
-  err = pop_data->err_msg;
-  len = mutt_b64_decode(pop_data->err_msg, decoded_err, sizeof(decoded_err) - 1);
+  char *err = pop_data->err_msg;
+  char decoded_err[LONG_STRING];
+  int len = mutt_b64_decode(pop_data->err_msg, decoded_err, sizeof(decoded_err) - 1);
   if (len >= 0)
   {
     decoded_err[len] = '\0';
index 659005247e6de8e60658594bfd53f8b1ca54030e..87f99a9142f22ff7266599b72bddfcf370b967c4 100644 (file)
@@ -304,11 +304,9 @@ int pop_connect(struct PopData *pop_data)
 */
 int pop_open_connection(struct PopData *pop_data)
 {
-  int rc;
-  unsigned int n, size;
   char buf[LONG_STRING];
 
-  rc = pop_connect(pop_data);
+  int rc = pop_connect(pop_data);
   if (rc < 0)
   {
     mutt_sleep(2);
@@ -405,6 +403,7 @@ int pop_open_connection(struct PopData *pop_data)
     return rc;
   }
 
+  unsigned int n = 0, size = 0;
   sscanf(buf, "+OK %u %u", &n, &size);
   pop_data->size = size;
   return 0;
@@ -463,7 +462,6 @@ void pop_logout(struct Mailbox *mailbox)
 int pop_query_d(struct PopData *pop_data, char *buf, size_t buflen, char *msg)
 {
   int dbg = MUTT_SOCK_LOG_CMD;
-  char *c = NULL;
 
   if (pop_data->status != POP_CONNECTED)
     return -1;
@@ -477,7 +475,7 @@ int pop_query_d(struct PopData *pop_data, char *buf, size_t buflen, char *msg)
 
   mutt_socket_send_d(pop_data->conn, buf, dbg);
 
-  c = strpbrk(buf, " \r\n");
+  char *c = strpbrk(buf, " \r\n");
   if (c)
     *c = '\0';
   snprintf(pop_data->err_msg, sizeof(pop_data->err_msg), "%s: ", buf);