chiark / gitweb /
cgi/cgimain.c: Make the CGI program be (a little) locale-aware.
[disorder] / disobedience / login.c
CommitLineData
73f1b9f3
RK
1/*
2 * This file is part of DisOrder
6398c3b7 3 * Copyright (C) 2007, 2008 Richard Kettlewell
73f1b9f3 4 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
73f1b9f3 6 * it under the terms of the GNU General Public License as published by
e7eb3a27 7 * the Free Software Foundation, either version 3 of the License, or
73f1b9f3
RK
8 * (at your option) any later version.
9 *
e7eb3a27
RK
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
73f1b9f3 15 * You should have received a copy of the GNU General Public License
e7eb3a27 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
73f1b9f3
RK
17 */
18/** @file disobedience/login.c
19 * @brief Login box for Disobedience
e9e8a16d
RK
20 *
21 * As of 2.1 we have only two buttons: Login and Cancel.
22 *
23 * If you hit Login then a login is attempted. If it works the window
24 * disappears and the settings are saved, otherwise they are NOT saved and the
25 * window remains.
26 *
27 * It you hit Cancel then the window disappears without saving anything.
d62c03cd
RK
28 *
29 * TODO
d62c03cd 30 * - cancel/close should be consistent with properties
73f1b9f3
RK
31 */
32
33#include "disobedience.h"
34#include "split.h"
35#include "filepart.h"
e9e8a16d 36#include "client.h"
73f1b9f3
RK
37#include <sys/types.h>
38#include <sys/stat.h>
043d60b1 39#include <unistd.h>
73f1b9f3
RK
40
41/** @brief One field in the login window */
42struct login_window_item {
43 /** @brief Description label */
44 const char *description;
45
46 /** @brief Return the current value */
47 const char *(*get)(void);
48
49 /** @brief Set a new value */
319d7107 50 void (*set)(struct config *c, const char *value);
73f1b9f3
RK
51
52 /** @brief Flags
53 *
54 * - @ref LWI_HIDDEN - this is a password
ec7109f3 55 * - @ref LWI_REMOTE - this is for remote connections
73f1b9f3
RK
56 */
57 unsigned flags;
58
59};
60
61/** @brief This is a password */
62#define LWI_HIDDEN 0x0001
63
ec7109f3
RK
64/** @brief This is for remote connections */
65#define LWI_REMOTE 0x0002
66
73f1b9f3 67/** @brief Current login window */
e9e8a16d 68GtkWidget *login_window;
73f1b9f3
RK
69
70/** @brief Set connection defaults */
71static void default_connect(void) {
ba9d856b
RK
72 /* If a password is set assume we're good */
73 if(config->password)
74 return;
75 /* If we already have a host and/or port that's good too */
e41a9999 76 if(config->connect.af != -1)
ba9d856b
RK
77 return;
78 /* If there's a suitable socket that's probably what we wanted */
79 const char *s = config_get_file("socket");
80 struct stat st;
81 if(s && *s && stat(s, &st) == 0 && S_ISSOCK(st.st_mode))
82 return;
83 /* TODO can we use some mdns thing to find a DisOrder server? */
73f1b9f3
RK
84}
85
ec7109f3 86static const char *get_hostname(void) {
e41a9999
RK
87 if(config->connect.af == -1 || !config->connect.address)
88 return "";
89 else
90 return config->connect.address;
ec7109f3
RK
91}
92
93static const char *get_service(void) {
e41a9999
RK
94 if(config->connect.af == -1)
95 return "";
96 else {
97 char *s;
98
99 byte_xasprintf(&s, "%d", config->connect.port);
100 return s;
101 }
ec7109f3
RK
102}
103
104static const char *get_username(void) {
105 return config->username;
106}
107
108static const char *get_password(void) {
109 return config->password ? config->password : "";
110}
73f1b9f3 111
319d7107 112static void set_hostname(struct config *c, const char *s) {
e41a9999
RK
113 if(c->connect.af == -1)
114 c->connect.af = AF_UNSPEC;
115 c->connect.address = xstrdup(s);
319d7107
RK
116}
117
118static void set_service(struct config *c, const char *s) {
e41a9999 119 c->connect.port = atoi(s);
319d7107
RK
120}
121
122static void set_username(struct config *c, const char *s) {
47854c5f
RK
123 xfree(c->username);
124 c->username = xstrdup(s);
319d7107
RK
125}
126
127static void set_password(struct config *c, const char *s) {
47854c5f
RK
128 xfree(c->password);
129 c->password = xstrdup(s);
319d7107 130}
73f1b9f3
RK
131
132/** @brief Table used to generate the form */
133static const struct login_window_item lwis[] = {
ec7109f3
RK
134 { "Hostname", get_hostname, set_hostname, LWI_REMOTE },
135 { "Service", get_service, set_service, LWI_REMOTE },
73f1b9f3
RK
136 { "User name", get_username, set_username, 0 },
137 { "Password", get_password, set_password, LWI_HIDDEN },
138};
139#define NLWIS (sizeof lwis / sizeof *lwis)
140
ec7109f3 141static GtkWidget *lwi_remote;
73f1b9f3
RK
142static GtkWidget *lwi_entry[NLWIS];
143
319d7107 144static void login_update_config(struct config *c) {
73f1b9f3 145 size_t n;
ec7109f3 146 const gboolean remote = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(lwi_remote));
73f1b9f3 147
e41a9999
RK
148 if(remote)
149 c->connect.af = AF_UNSPEC;
150 else
151 c->connect.af = -1;
73f1b9f3 152 for(n = 0; n < NLWIS; ++n)
ec7109f3
RK
153 if(remote || !(lwis[n].flags & LWI_REMOTE))
154 lwis[n].set(c, xstrdup(gtk_entry_get_text(GTK_ENTRY(lwi_entry[n]))));
73f1b9f3
RK
155}
156
e9e8a16d
RK
157/** @brief Save current login details */
158static void login_save_config(void) {
73f1b9f3
RK
159 char *path = config_userconf(0, 0), *tmp;
160 FILE *fp;
e9e8a16d 161
73f1b9f3
RK
162 byte_xasprintf(&tmp, "%s.tmp", path);
163 /* Make sure the directory exists; don't care if it already exists. */
164 mkdir(d_dirname(tmp), 02700);
165 /* Write out the file */
166 if(!(fp = fopen(tmp, "w"))) {
043d60b1
RK
167 fpopup_msg(GTK_MESSAGE_ERROR, "error opening %s: %s",
168 tmp, strerror(errno));
169 goto done;
73f1b9f3 170 }
ec7109f3
RK
171 int rc = fprintf(fp, "username %s\n"
172 "password %s\n",
173 quoteutf8(config->username),
174 quoteutf8(config->password));
e41a9999
RK
175 if(rc >= 0 && config->connect.af != -1) {
176 char **vec;
177
178 netaddress_format(&config->connect, NULL, &vec);
179 rc = fprintf(fp, "connect %s %s %s\n", vec[0], vec[1], vec[2]);
180 }
ec7109f3 181 if(rc < 0) {
043d60b1
RK
182 fpopup_msg(GTK_MESSAGE_ERROR, "error writing to %s: %s",
183 tmp, strerror(errno));
73f1b9f3 184 fclose(fp);
043d60b1 185 goto done;
73f1b9f3
RK
186 }
187 if(fclose(fp) < 0) {
043d60b1
RK
188 fpopup_msg(GTK_MESSAGE_ERROR, "error closing %s: %s",
189 tmp, strerror(errno));
190 goto done;
73f1b9f3
RK
191 }
192 /* Rename into place */
193 if(rename(tmp, path) < 0) {
043d60b1
RK
194 fpopup_msg(GTK_MESSAGE_ERROR, "error renaming %s: %s",
195 tmp, strerror(errno));
196 goto done;
73f1b9f3 197 }
043d60b1 198done:
e9e8a16d 199 ;
73f1b9f3
RK
200}
201
e9e8a16d
RK
202/** @brief User pressed OK in login window */
203static void login_ok(GtkButton attribute((unused)) *button,
204 gpointer attribute((unused)) userdata) {
205 disorder_client *c;
319d7107 206 struct config *tmpconfig = xmalloc(sizeof *tmpconfig);
e9e8a16d 207
ec7109f3 208 tmpconfig->home = xstrdup(pkgstatedir);
e9e8a16d 209 /* Copy the new config into @ref config */
319d7107 210 login_update_config(tmpconfig);
e9e8a16d
RK
211 /* Attempt a login with the new details */
212 c = disorder_new(0);
319d7107
RK
213 if(!disorder_connect_generic(tmpconfig, c,
214 tmpconfig->username, tmpconfig->password,
215 NULL/*cookie*/)) {
e9e8a16d 216 /* Success; save the config and start using it */
319d7107 217 login_update_config(config);
e9e8a16d 218 login_save_config();
7c30fc75 219 logged_in();
e9e8a16d
RK
220 /* Pop down login window */
221 gtk_widget_destroy(login_window);
222 } else {
223 /* Failed to connect - report the error */
224 popup_msg(GTK_MESSAGE_ERROR, disorder_last(c));
225 }
226 disorder_close(c); /* no use for this any more */
227}
228
229/** @brief User pressed cancel in the login window */
73f1b9f3
RK
230static void login_cancel(GtkButton attribute((unused)) *button,
231 gpointer attribute((unused)) userdata) {
232 gtk_widget_destroy(login_window);
233}
234
cc345694
RK
235/** @brief User pressed cancel in the login window */
236static void login_help(GtkButton attribute((unused)) *button,
237 gpointer attribute((unused)) userdata) {
238 popup_help("intro.html#login");
239}
240
453bdfe0
RK
241/** @brief Keypress handler */
242static gboolean login_keypress(GtkWidget attribute((unused)) *widget,
243 GdkEventKey *event,
244 gpointer attribute((unused)) user_data) {
245 if(event->state)
246 return FALSE;
247 switch(event->keyval) {
248 case GDK_Return:
249 login_ok(0, 0);
250 return TRUE;
251 case GDK_Escape:
252 login_cancel(0, 0);
253 return TRUE;
254 default:
255 return FALSE;
256 }
257}
258
73f1b9f3 259/* Buttons that appear at the bottom of the window */
f44417cf 260static struct button buttons[] = {
73f1b9f3 261 {
cc345694
RK
262 GTK_STOCK_HELP,
263 login_help,
264 "Go to manual",
ad424400 265 0,
cc345694 266 gtk_box_pack_start,
73f1b9f3
RK
267 },
268 {
0f24b368 269 GTK_STOCK_CLOSE,
73f1b9f3 270 login_cancel,
f44417cf 271 "Discard changes and close window",
ad424400 272 0,
cc345694
RK
273 gtk_box_pack_end,
274 },
275 {
276 "Login",
277 login_ok,
278 "(Re-)connect using these settings",
279 0,
280 gtk_box_pack_end,
73f1b9f3
RK
281 },
282};
283
284#define NBUTTONS (int)(sizeof buttons / sizeof *buttons)
285
ec7109f3
RK
286/** @brief Called when the remote/local button is toggled (and initially)
287 *
288 * Sets the sensitivity of the host/port entries.
289 */
290static void lwi_remote_toggled(GtkToggleButton *togglebutton,
291 gpointer attribute((unused)) user_data) {
292 const gboolean remote = gtk_toggle_button_get_active(togglebutton);
293
294 for(unsigned n = 0; n < NLWIS; ++n)
295 if(lwis[n].flags & LWI_REMOTE)
296 gtk_widget_set_sensitive(lwi_entry[n], remote);
297}
298
73f1b9f3
RK
299/** @brief Pop up a login box */
300void login_box(void) {
301 GtkWidget *table, *label, *entry, *buttonbox, *vbox;
302 size_t n;
303
304 /* If there's one already then bring it to the front */
305 if(login_window) {
306 gtk_window_present(GTK_WINDOW(login_window));
307 return;
308 }
309 default_connect();
310 /* Create a new login window */
311 login_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
33288048 312 gtk_widget_set_style(login_window, tool_style);
73f1b9f3
RK
313 g_signal_connect(login_window, "destroy",
314 G_CALLBACK(gtk_widget_destroyed), &login_window);
315 gtk_window_set_title(GTK_WINDOW(login_window), "Login Details");
316 /* Construct the form */
ec7109f3 317 table = gtk_table_new(1 + NLWIS/*rows*/, 2/*columns*/, FALSE/*homogenous*/);
33288048 318 gtk_widget_set_style(table, tool_style);
ec7109f3
RK
319 label = gtk_label_new("Remote");
320 gtk_widget_set_style(label, tool_style);
321 gtk_misc_set_alignment(GTK_MISC(label), 1/*right*/, 0/*bottom*/);
322 gtk_table_attach(GTK_TABLE(table), label,
323 0, 1, /* left/right_attach */
324 0, 1, /* top/bottom_attach */
325 GTK_FILL, 0, /* x/yoptions */
326 1, 1); /* x/ypadding */
327 lwi_remote = gtk_check_button_new();
328 gtk_widget_set_style(lwi_remote, tool_style);
329 gtk_table_attach(GTK_TABLE(table), lwi_remote,
330 1, 2, /* left/right_attach */
331 0, 1, /* top/bottom_attach */
332 GTK_EXPAND|GTK_FILL, 0, /* x/yoptions */
333 1, 1); /* x/ypadding */
334 g_signal_connect(lwi_remote, "toggled", G_CALLBACK(lwi_remote_toggled), 0);
73f1b9f3
RK
335 for(n = 0; n < NLWIS; ++n) {
336 label = gtk_label_new(lwis[n].description);
33288048 337 gtk_widget_set_style(label, tool_style);
73f1b9f3
RK
338 gtk_misc_set_alignment(GTK_MISC(label), 1/*right*/, 0/*bottom*/);
339 gtk_table_attach(GTK_TABLE(table), label,
340 0, 1, /* left/right_attach */
ec7109f3 341 n+1, n+2, /* top/bottom_attach */
73f1b9f3
RK
342 GTK_FILL, 0, /* x/yoptions */
343 1, 1); /* x/ypadding */
344 entry = gtk_entry_new();
33288048 345 gtk_widget_set_style(entry, tool_style);
73f1b9f3
RK
346 gtk_entry_set_visibility(GTK_ENTRY(entry),
347 lwis[n].flags & LWI_HIDDEN ? FALSE : TRUE);
348 gtk_entry_set_text(GTK_ENTRY(entry), lwis[n].get());
349 gtk_table_attach(GTK_TABLE(table), entry,
350 1, 2, /* left/right_attach */
ec7109f3 351 n+1, n+2, /* top/bottom_attach */
73f1b9f3
RK
352 GTK_EXPAND|GTK_FILL, 0, /* x/yoptions */
353 1, 1); /* x/ypadding */
354 lwi_entry[n] = entry;
355 }
ec7109f3 356 /* Initial settings */
ec7109f3 357 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(lwi_remote),
e41a9999 358 config->connect.af != -1);
ba9d856b 359 lwi_remote_toggled(GTK_TOGGLE_BUTTON(lwi_remote), 0);
73f1b9f3
RK
360 buttonbox = create_buttons(buttons, NBUTTONS);
361 vbox = gtk_vbox_new(FALSE, 1);
b2ea2aa6
RK
362 gtk_box_pack_start(GTK_BOX(vbox),
363 gtk_image_new_from_pixbuf(find_image("logo256.png")),
364 TRUE/*expand*/,
365 TRUE/*fill*/,
366 4/*padding*/);
73f1b9f3
RK
367 gtk_box_pack_start(GTK_BOX(vbox), table,
368 TRUE/*expand*/, TRUE/*fill*/, 1/*padding*/);
369 gtk_box_pack_start(GTK_BOX(vbox), buttonbox,
370 FALSE/*expand*/, FALSE/*fill*/, 1/*padding*/);
e18c4734 371 gtk_container_add(GTK_CONTAINER(login_window), frame_widget(vbox, NULL));
458620c5
RK
372 gtk_window_set_transient_for(GTK_WINDOW(login_window),
373 GTK_WINDOW(toplevel));
453bdfe0
RK
374 /* Keyboard shortcuts */
375 g_signal_connect(login_window, "key-press-event",
376 G_CALLBACK(login_keypress), 0);
73f1b9f3
RK
377 gtk_widget_show_all(login_window);
378}
379
380/*
381Local Variables:
382c-basic-offset:2
383comment-column:40
384fill-column:79
385indent-tabs-mode:nil
386End:
387*/