*/
/** @file disobedience/login.c
* @brief Login box for Disobedience
+ *
+ * As of 2.1 we have only two buttons: Login and Cancel.
+ *
+ * If you hit Login then a login is attempted. If it works the window
+ * disappears and the settings are saved, otherwise they are NOT saved and the
+ * window remains.
+ *
+ * It you hit Cancel then the window disappears without saving anything.
*/
#include "disobedience.h"
#include "split.h"
#include "filepart.h"
+#include "client.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#define LWI_HIDDEN 0x0001
/** @brief Current login window */
-static GtkWidget *login_window;
+GtkWidget *login_window;
/** @brief Set connection defaults */
static void default_connect(void) {
static GtkWidget *lwi_entry[NLWIS];
-static void update_config(void) {
+static void login_update_config(void) {
size_t n;
for(n = 0; n < NLWIS; ++n)
lwis[n].set(xstrdup(gtk_entry_get_text(GTK_ENTRY(lwi_entry[n]))));
}
-#if 0
-static int modified_config(void) {
- size_t n;
-
- for(n = 0; n < NLWIS; ++n) {
- const char *entered = gtk_entry_get_text(GTK_ENTRY(lwi_entry[n]));
- const char *current = lwis[n].get();
- if(strcmp(entered, current))
- return 1;
- }
- return 0;
-}
-#endif
-
-static void login_ok(GtkButton attribute((unused)) *button,
- gpointer attribute((unused)) userdata) {
- update_config();
- reset();
-}
-
-static void login_save(GtkButton attribute((unused)) *button,
- gpointer attribute((unused)) userdata) {
+/** @brief Save current login details */
+static void login_save_config(void) {
char *path = config_userconf(0, 0), *tmp;
FILE *fp;
- GtkWidget *yorn = 0;
- update_config();
- /* See if the file already exists */
- if(access(path, F_OK) == 0) {
- yorn = gtk_message_dialog_new
- (GTK_WINDOW(login_window),
- GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT,
- GTK_MESSAGE_QUESTION,
- GTK_BUTTONS_NONE,
- "File %s already exists.", path);
- gtk_window_set_title(GTK_WINDOW(yorn),
- "Configuration file already exists");
- gtk_dialog_add_buttons(GTK_DIALOG(yorn),
- "Overwrite it", GTK_RESPONSE_ACCEPT,
- "Don't save after all", GTK_RESPONSE_REJECT,
- (char *)0);
- if(gtk_dialog_run(GTK_DIALOG(yorn)) != GTK_RESPONSE_ACCEPT)
- goto done;
- gtk_widget_destroy(yorn);
- yorn = 0;
- }
byte_xasprintf(&tmp, "%s.tmp", path);
/* Make sure the directory exists; don't care if it already exists. */
mkdir(d_dirname(tmp), 02700);
tmp, strerror(errno));
goto done;
}
- fpopup_msg(GTK_MESSAGE_INFO, "Saved login configuration to %s", path);
- gtk_widget_destroy(login_window);
done:
- if(yorn)
- gtk_widget_destroy(yorn);
+ ;
}
+/** @brief User pressed OK in login window */
+static void login_ok(GtkButton attribute((unused)) *button,
+ gpointer attribute((unused)) userdata) {
+ disorder_client *c;
+
+ /* Copy the new config into @ref config */
+ login_update_config();
+ /* Attempt a login with the new details */
+ c = disorder_new(0);
+ if(!disorder_connect(c)) {
+ /* Success; save the config and start using it */
+ login_save_config();
+ reset();
+ /* Pop down login window */
+ gtk_widget_destroy(login_window);
+ } else {
+ /* Failed to connect - report the error */
+ popup_msg(GTK_MESSAGE_ERROR, disorder_last(c));
+ }
+ disorder_close(c); /* no use for this any more */
+}
+
+/** @brief User pressed cancel in the login window */
static void login_cancel(GtkButton attribute((unused)) *button,
gpointer attribute((unused)) userdata) {
gtk_widget_destroy(login_window);
login_ok,
"(Re-)connect using these settings",
},
- {
- GTK_STOCK_SAVE,
- login_save,
- "Save these settings and close window",
- },
{
GTK_STOCK_CLOSE,
login_cancel,
/** @brief Report errors to @c stderr */
int verbose;
/** @brief Last error string */
- char *last;
+ const char *last;
};
/** @brief Create a new client
static int response(disorder_client *c, char **rp) {
char *r;
- if(inputline(c->ident, c->fpin, &r, '\n'))
+ if(inputline(c->ident, c->fpin, &r, '\n')) {
+ byte_xasprintf((char **)&c->last, "input error: %s", strerror(errno));
return -1;
+ }
D(("response: %s", r));
if(rp)
*rp = r;
c->last = r + 4;
return (r[0] * 10 + r[1]) * 10 + r[2] - 111 * '0';
} else {
+ c->last = "invalid reply format";
error(0, "invalid reply format from %s", c->ident);
return -1;
}
struct dynstr d;
if(!c->fpout) {
+ c->last = "not connected";
error(0, "not connected to server");
return -1;
}
dynstr_terminate(&d);
D(("command: %s", d.vec));
if(fputs(d.vec, c->fpout) < 0 || fflush(c->fpout)) {
+ byte_xasprintf((char **)&c->last, "write error: %s", strerror(errno));
error(errno, "error writing to %s", c->ident);
return -1;
}
return -1;
c->fpin = c->fpout = 0;
if((fd = socket(sa->sa_family, SOCK_STREAM, 0)) < 0) {
+ byte_xasprintf((char **)&c->last, "socket: %s", strerror(errno));
error(errno, "error calling socket");
return -1;
}
if(connect(fd, sa, salen) < 0) {
+ byte_xasprintf((char **)&c->last, "connect: %s", strerror(errno));
error(errno, "error calling connect");
goto error;
}
if((fd2 = dup(fd)) < 0) {
+ byte_xasprintf((char **)&c->last, "dup: %s", strerror(errno));
error(errno, "error calling dup");
goto error;
}
if(!(c->fpin = fdopen(fd, "rb"))) {
+ byte_xasprintf((char **)&c->last, "fdopen: %s", strerror(errno));
error(errno, "error calling fdopen");
goto error;
}
fd = -1;
if(!(c->fpout = fdopen(fd2, "wb"))) {
+ byte_xasprintf((char **)&c->last, "fdopen: %s", strerror(errno));
error(errno, "error calling fdopen");
goto error;
}
if(!(rvec = split(r, &nrvec, SPLIT_QUOTES, 0, 0)))
goto error;
if(nrvec != 3) {
+ c->last = "cannot parse server greeting";
error(0, "cannot parse server greeting %s", r);
goto error;
}
protocol = *rvec++;
if(strcmp(protocol, "2")) {
+ c->last = "unknown protocol version";
error(0, "unknown protocol version: %s", protocol);
goto error;
}
&c->user))
return 0; /* success */
if(!username) {
+ c->last = "cookie failed and no username";
error(0, "cookie did not work and no username available");
goto error;
}
}
- if(!(res = authhash(nonce, nl, password, algorithm))) goto error;
+ if(!(res = authhash(nonce, nl, password, algorithm))) {
+ c->last = "error computing authorization hash";
+ goto error;
+ }
if((rc = disorder_simple(c, 0, "user", username, res, (char *)0)))
goto error_rc;
c->user = xstrdup(username);
const char *username, *password;
if(!(username = config->username)) {
+ c->last = "no username";
error(0, "no username configured");
return -1;
}
}
if(!password) {
/* Oh well */
+ c->last = "no password";
error(0, "no password configured");
return -1;
}
if(c->fpin) {
if(fclose(c->fpin) < 0) {
+ byte_xasprintf((char **)&c->last, "fclose: %s", strerror(errno));
error(errno, "error calling fclose");
ret = -1;
}
}
if(c->fpout) {
if(fclose(c->fpout) < 0) {
+ byte_xasprintf((char **)&c->last, "fclose: %s", strerror(errno));
error(errno, "error calling fclose");
ret = -1;
}
qt = &q->next;
}
}
- if(ferror(c->fpin))
+ if(ferror(c->fpin)) {
+ byte_xasprintf((char **)&c->last, "input error: %s", strerror(errno));
error(errno, "error reading %s", c->ident);
- else
+ } else {
+ c->last = "input error: unexpxected EOF";
error(0, "error reading %s: unexpected EOF", c->ident);
+ }
return -1;
}
}
vector_append(&v, l + (*l == '.'));
}
- if(ferror(c->fpin))
+ if(ferror(c->fpin)) {
+ byte_xasprintf((char **)&c->last, "input error: %s", strerror(errno));
error(errno, "error reading %s", c->ident);
- else
+ } else {
+ c->last = "input error: unexpxected EOF";
error(0, "error reading %s: unexpected EOF", c->ident);
+ }
return -1;
}
if((rc = disorder_simple(c, &r, "volume", (char *)0)))
return rc;
if(sscanf(r, "%d %d", left, right) != 2) {
+ c->last = "malformed volume response";
error(0, "error parsing response to 'volume': '%s'", r);
return -1;
}
return rc;
while(inputline(c->ident, c->fpin, &l, '\n') >= 0 && strcmp(l, "."))
if(sink_printf(s, "%s\n", l) < 0) return -1;
- if(ferror(c->fpin) || feof(c->fpin)) return -1;
+ if(ferror(c->fpin) || feof(c->fpin)) {
+ byte_xasprintf((char **)&c->last, "input error: %s",
+ ferror(c->fpin) ? strerror(errno) : "unexpxected EOF");
+ return -1;
+ }
return 0;
}
return rc;
vec = split(r, &n, SPLIT_QUOTES, 0, 0);
if(n != 2) {
+ c->last = "malformed RTP address";
error(0, "malformed rtp-address reply");
return -1;
}