The function to retrieve the password has inline-code for spawning an
external process and reading its stdout in order to handle the PassCmd
option. Extract that function into its own to make it re-usable for a
UserCmd option.
---
 src/drv_imap.c | 71 ++++++++++++++++++++++++++++++++--------------------------
 1 file changed, 39 insertions(+), 32 deletions(-)

diff --git a/src/drv_imap.c b/src/drv_imap.c
index 56d71cb..30a3856 100644
--- a/src/drv_imap.c
+++ b/src/drv_imap.c
@@ -1849,6 +1849,41 @@ imap_open_store_authenticate_p3( imap_store_t *ctx, 
imap_cmd_t *cmd ATTR_UNUSED,
 }
 #endif
 
+static char *
+spawn_cmd(const char *cmd, const char *type, const char *name)
+{
+       FILE *fp;
+       int ret;
+       char buffer[80];
+
+       if (*cmd == '+') {
+               flushn();
+               cmd++;
+       }
+       if (!(fp = popen( cmd, "r" ))) {
+         pipeerr:
+               sys_error( "Skipping account %s, %s command failed", name, type 
);
+               return NULL;
+       }
+       if (!fgets( buffer, sizeof(buffer), fp ))
+               buffer[0] = 0;
+       if ((ret = pclose( fp )) < 0)
+               goto pipeerr;
+       if (ret) {
+               if (WIFSIGNALED( ret ))
+                       error( "Skipping account %s, %s command crashed\n", 
name, type );
+               else
+                       error( "Skipping account %s, %s command exited with 
status %d\n", name, type, WEXITSTATUS( ret ) );
+               return NULL;
+       }
+       if (!buffer[0]) {
+               error( "Skipping account %s, %s command produced no output\n", 
name, type );
+               return NULL;
+       }
+       buffer[strcspn( buffer, "\n" )] = 0; /* Strip trailing newline */
+       return nfstrdup( buffer );
+}
+
 static const char *
 ensure_user( imap_server_conf_t *srvc )
 {
@@ -1862,40 +1897,12 @@ ensure_user( imap_server_conf_t *srvc )
 static const char *
 ensure_password( imap_server_conf_t *srvc )
 {
-       char *cmd = srvc->pass_cmd;
-
-       if (cmd) {
-               FILE *fp;
-               int ret;
-               char buffer[80];
-
-               if (*cmd == '+') {
-                       flushn();
-                       cmd++;
-               }
-               if (!(fp = popen( cmd, "r" ))) {
-                 pipeerr:
-                       sys_error( "Skipping account %s, password command 
failed", srvc->name );
+       if (srvc->pass_cmd) {
+               char *pass = spawn_cmd( srvc->pass_cmd, "password", srvc->name 
);
+               if (!pass)
                        return 0;
-               }
-               if (!fgets( buffer, sizeof(buffer), fp ))
-                       buffer[0] = 0;
-               if ((ret = pclose( fp )) < 0)
-                       goto pipeerr;
-               if (ret) {
-                       if (WIFSIGNALED( ret ))
-                               error( "Skipping account %s, password command 
crashed\n", srvc->name );
-                       else
-                               error( "Skipping account %s, password command 
exited with status %d\n", srvc->name, WEXITSTATUS( ret ) );
-                       return 0;
-               }
-               if (!buffer[0]) {
-                       error( "Skipping account %s, password command produced 
no output\n", srvc->name );
-                       return 0;
-               }
-               buffer[strcspn( buffer, "\n" )] = 0; /* Strip trailing newline 
*/
                free( srvc->pass ); /* From previous runs */
-               srvc->pass = nfstrdup( buffer );
+               srvc->pass = pass;
        } else if (!srvc->pass) {
                char *pass, prompt[80];
 
-- 
2.16.1


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
isync-devel mailing list
isync-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/isync-devel

Reply via email to