From a384af7cde6a66b02dc98ddd3f38d7e4c3921af9 Mon Sep 17 00:00:00 2001 From: ak1 Date: Sun, 20 Mar 2005 12:44:16 +0000 Subject: [PATCH] Andreas Krennmair: removed more magic numbers in the POP code. git-svn-id: svn://svn.berlios.de/mutt-ng/trunk@208 e385b8ad-14ed-0310-8656-cc95a2468c6d --- pop/pop.c | 19 ++++++++++--------- pop/pop.h | 20 ++++++++++---------- pop/pop_auth.c | 26 ++++++++++++------------- pop/pop_lib.c | 51 +++++++++++++++++++++++++++----------------------- 4 files changed, 61 insertions(+), 55 deletions(-) diff --git a/pop/pop.c b/pop/pop.c index 9eb6172..358bed6 100644 --- a/pop/pop.c +++ b/pop/pop.c @@ -39,11 +39,12 @@ static int fetch_message (char *line, void *file) * -2 - invalid command or execution error, * -3 - error writing to tempfile */ -static int pop_read_header (POP_DATA * pop_data, HEADER * h) +static pop_query_status pop_read_header (POP_DATA * pop_data, HEADER * h) { FILE *f; int index; pop_query_status ret; + cmd_status status; long length; char buf[LONG_STRING]; char tempfile[_POSIX_PATH_MAX]; @@ -51,7 +52,7 @@ static int pop_read_header (POP_DATA * pop_data, HEADER * h) mutt_mktemp (tempfile); if (!(f = safe_fopen (tempfile, "w+"))) { mutt_perror (tempfile); - return -3; + return PFD_FUNCT_ERROR; } snprintf (buf, sizeof (buf), "LIST %d\r\n", h->refno); @@ -62,15 +63,15 @@ static int pop_read_header (POP_DATA * pop_data, HEADER * h) snprintf (buf, sizeof (buf), "TOP %d 0\r\n", h->refno); ret = pop_fetch_data (pop_data, buf, NULL, fetch_message, f); - if (pop_data->cmd_top == 2) { + if (pop_data->cmd_top == CMD_UNKNOWN) { if (ret == PQ_OK) { - pop_data->cmd_top = 1; + pop_data->cmd_top = CMD_AVAILABLE; dprint (1, (debugfile, "pop_read_header: set TOP capability\n")); } if (ret == PQ_ERR) { - pop_data->cmd_top = 0; + pop_data->cmd_top = CMD_NOT_AVAILABLE; dprint (1, (debugfile, "pop_read_header: unset TOP capability\n")); snprintf (pop_data->err_msg, sizeof (pop_data->err_msg), @@ -168,15 +169,15 @@ static int pop_fetch_headers (CONTEXT * ctx) new_count = ctx->msgcount; ctx->msgcount = old_count; - if (pop_data->cmd_uidl == 2) { + if (pop_data->cmd_uidl == CMD_UNKNOWN) { if (ret == PQ_OK) { - pop_data->cmd_uidl = 1; + pop_data->cmd_uidl = CMD_AVAILABLE; dprint (1, (debugfile, "pop_fetch_headers: set UIDL capability\n")); } - if (ret == PQ_ERR && pop_data->cmd_uidl == 2) { - pop_data->cmd_uidl = 0; + if (ret == PQ_ERR && pop_data->cmd_uidl == CMD_UNKNOWN) { + pop_data->cmd_uidl = CMD_NOT_AVAILABLE; dprint (1, (debugfile, "pop_fetch_headers: unset UIDL capability\n")); snprintf (pop_data->err_msg, sizeof (pop_data->err_msg), diff --git a/pop/pop.h b/pop/pop.h index 453d2ce..5821491 100644 --- a/pop/pop.h +++ b/pop/pop.h @@ -49,22 +49,22 @@ typedef enum pop_query_status_e { PQ_OK = 0 } pop_query_status; -typedef enum cmd_user_status_e { - USER_NOT_AVAILABLE = 0, - USER_AVAILABLE, - USER_UNKNOWN -} cmd_user_status; +typedef enum cmd_status_e { + CMD_NOT_AVAILABLE = 0, + CMD_AVAILABLE, + CMD_UNKNOWN /* unknown whether it is available or not */ +} cmd_status; typedef struct { CONNECTION *conn; unsigned int status:2; unsigned int capabilities:1; unsigned int use_stls:2; - unsigned int cmd_capa:1; /* optional command CAPA */ - unsigned int cmd_stls:1; /* optional command STLS */ - cmd_user_status cmd_user; /* optional command USER */ - unsigned int cmd_uidl:2; /* optional command UIDL */ - unsigned int cmd_top:2; /* optional command TOP */ + cmd_status cmd_capa; /* optional command CAPA */ + cmd_status cmd_stls; /* optional command STLS */ + cmd_status cmd_user; /* optional command USER */ + cmd_status cmd_uidl; /* optional command UIDL */ + cmd_status cmd_top; /* optional command TOP */ unsigned int resp_codes:1; /* server supports extended response codes */ unsigned int expire:1; /* expire is greater than 0 */ unsigned int clear_cache:1; diff --git a/pop/pop_auth.c b/pop/pop_auth.c index 51cdeb0..d715d7e 100644 --- a/pop/pop_auth.c +++ b/pop/pop_auth.c @@ -235,7 +235,7 @@ static pop_auth_res_t pop_auth_user (POP_DATA * pop_data, const char *method) char buf[LONG_STRING]; pop_query_status ret; - if (pop_data->cmd_user == USER_NOT_AVAILABLE) + if (pop_data->cmd_user == CMD_NOT_AVAILABLE) return POP_A_UNAVAIL; mutt_message _("Logging in..."); @@ -243,15 +243,15 @@ static pop_auth_res_t pop_auth_user (POP_DATA * pop_data, const char *method) snprintf (buf, sizeof (buf), "USER %s\r\n", pop_data->conn->account.user); ret = pop_query (pop_data, buf, sizeof (buf)); - if (pop_data->cmd_user == USER_UNKNOWN) { + if (pop_data->cmd_user == CMD_UNKNOWN) { if (ret == PQ_OK) { - pop_data->cmd_user = USER_AVAILABLE; + pop_data->cmd_user = CMD_AVAILABLE; dprint (1, (debugfile, "pop_auth_user: set USER capability\n")); } if (ret == PQ_ERR) { - pop_data->cmd_user = USER_NOT_AVAILABLE; + pop_data->cmd_user = CMD_NOT_AVAILABLE; dprint (1, (debugfile, "pop_auth_user: unset USER capability\n")); snprintf (pop_data->err_msg, sizeof (pop_data->err_msg), @@ -298,7 +298,7 @@ static pop_auth_t pop_authenticators[] = { * -2 - login failed, * -3 - authentication canceled. */ -int pop_authenticate (POP_DATA * pop_data) +pop_query_status pop_authenticate (POP_DATA * pop_data) { ACCOUNT *acct = &pop_data->conn->account; pop_auth_t *authenticator; @@ -310,7 +310,7 @@ int pop_authenticate (POP_DATA * pop_data) if (mutt_account_getuser (acct) || !acct->user[0] || mutt_account_getpass (acct) || !acct->pass[0]) - return -3; + return PFD_FUNCT_ERROR; if (PopAuthenticators && *PopAuthenticators) { /* Try user-specified list of authentication methods */ @@ -330,12 +330,12 @@ int pop_authenticate (POP_DATA * pop_data) ret = authenticator->authenticate (pop_data, method); if (ret == POP_A_SOCKET) switch (pop_connect (pop_data)) { - case 0: + case PQ_OK: { ret = authenticator->authenticate (pop_data, method); break; } - case -2: + case PQ_ERR: ret = POP_A_FAILURE; } @@ -365,13 +365,13 @@ int pop_authenticate (POP_DATA * pop_data) ret = authenticator->authenticate (pop_data, authenticator->method); if (ret == POP_A_SOCKET) switch (pop_connect (pop_data)) { - case 0: + case PQ_OK: { ret = authenticator->authenticate (pop_data, authenticator->method); break; } - case -2: + case PQ_ERR: ret = POP_A_FAILURE; } @@ -387,13 +387,13 @@ int pop_authenticate (POP_DATA * pop_data) switch (ret) { case POP_A_SUCCESS: - return 0; + return PQ_OK; case POP_A_SOCKET: - return -1; + return PQ_NOT_CONNECTED; case POP_A_UNAVAIL: if (!attempts) mutt_error (_("No authenticators available")); } - return -2; + return PQ_ERR; } diff --git a/pop/pop_lib.c b/pop/pop_lib.c index 3a86c6a..093a70e 100644 --- a/pop/pop_lib.c +++ b/pop/pop_lib.c @@ -86,16 +86,16 @@ static int fetch_capa (char *line, void *data) } else if (!ascii_strncasecmp (line, "STLS", 4)) - pop_data->cmd_stls = 1; + pop_data->cmd_stls = CMD_AVAILABLE; else if (!ascii_strncasecmp (line, "USER", 4)) - pop_data->cmd_user = 1; + pop_data->cmd_user = CMD_AVAILABLE; else if (!ascii_strncasecmp (line, "UIDL", 4)) - pop_data->cmd_uidl = 1; + pop_data->cmd_uidl = CMD_AVAILABLE; else if (!ascii_strncasecmp (line, "TOP", 3)) - pop_data->cmd_top = 1; + pop_data->cmd_top = CMD_AVAILABLE; return 0; } @@ -135,11 +135,11 @@ static pop_query_status pop_capabilities (POP_DATA * pop_data, int mode) /* init capabilities */ if (mode == 0) { - pop_data->cmd_capa = 0; - pop_data->cmd_stls = 0; - pop_data->cmd_user = 0; - pop_data->cmd_uidl = 0; - pop_data->cmd_top = 0; + pop_data->cmd_capa = CMD_NOT_AVAILABLE; + pop_data->cmd_stls = CMD_NOT_AVAILABLE; + pop_data->cmd_user = CMD_NOT_AVAILABLE; + pop_data->cmd_uidl = CMD_NOT_AVAILABLE; + pop_data->cmd_top = CMD_NOT_AVAILABLE; pop_data->resp_codes = 0; pop_data->expire = 1; pop_data->login_delay = 0; @@ -147,12 +147,17 @@ static pop_query_status pop_capabilities (POP_DATA * pop_data, int mode) } /* Execute CAPA command */ - if (mode == 0 || pop_data->cmd_capa) { + if (mode == 0 || pop_data->cmd_capa != CMD_NOT_AVAILABLE) { strfcpy (buf, "CAPA\r\n", sizeof (buf)); switch (pop_fetch_data (pop_data, buf, NULL, fetch_capa, pop_data)) { case PQ_OK: { - pop_data->cmd_capa = 1; + pop_data->cmd_capa = CMD_AVAILABLE; + break; + } + case PQ_ERR: + { + pop_data->cmd_capa = CMD_NOT_AVAILABLE; break; } case PQ_NOT_CONNECTED: @@ -161,10 +166,10 @@ static pop_query_status pop_capabilities (POP_DATA * pop_data, int mode) } /* CAPA not supported, use defaults */ - if (mode == 0 && !pop_data->cmd_capa) { - pop_data->cmd_user = 2; - pop_data->cmd_uidl = 2; - pop_data->cmd_top = 2; + if (mode == 0 && pop_data->cmd_capa == CMD_NOT_AVAILABLE) { + pop_data->cmd_user = CMD_UNKNOWN; + pop_data->cmd_uidl = CMD_UNKNOWN; + pop_data->cmd_top = CMD_UNKNOWN; strfcpy (buf, "AUTH\r\n", sizeof (buf)); if (pop_fetch_data (pop_data, buf, NULL, fetch_auth, pop_data) == PQ_NOT_CONNECTED) @@ -177,11 +182,11 @@ static pop_query_status pop_capabilities (POP_DATA * pop_data, int mode) if (!pop_data->expire) msg = _("Unable to leave messages on server."); - if (!pop_data->cmd_top) + if (pop_data->cmd_top == CMD_NOT_AVAILABLE) msg = _("Command TOP is not supported by server."); - if (!pop_data->cmd_uidl) + if (pop_data->cmd_uidl == CMD_NOT_AVAILABLE) msg = _("Command UIDL is not supported by server."); - if (msg && pop_data->cmd_capa) { + if (msg && pop_data->cmd_capa != CMD_AVAILABLE) { mutt_error (msg); return PQ_ERR; } @@ -197,7 +202,7 @@ static pop_query_status pop_capabilities (POP_DATA * pop_data, int mode) * -1 - conection lost, * -2 - invalid response. */ -int pop_connect (POP_DATA * pop_data) +pop_query_status pop_connect (POP_DATA * pop_data) { char buf[LONG_STRING]; @@ -206,7 +211,7 @@ int pop_connect (POP_DATA * pop_data) mutt_socket_readln (buf, sizeof (buf), pop_data->conn) < 0) { mutt_error (_("Error connecting to server: %s"), pop_data->conn->account.host); - return -1; + return PQ_NOT_CONNECTED; } pop_data->status = POP_CONNECTED; @@ -215,12 +220,12 @@ int pop_connect (POP_DATA * pop_data) *pop_data->err_msg = '\0'; pop_error (pop_data, buf); mutt_error ("%s", pop_data->err_msg); - return -2; + return PQ_ERR; } pop_apop_timestamp (pop_data, buf); - return 0; + return PQ_OK; } /* @@ -483,7 +488,7 @@ static int check_uidl (char *line, void *data) return 0; } -/* reconnect and verify idnexes if connection was lost */ +/* reconnect and verify indexes if connection was lost */ pop_query_status pop_reconnect (CONTEXT * ctx) { pop_query_status ret; -- 2.20.1