summaryrefslogtreecommitdiff
path: root/alpine/xoauth2conf.c
blob: 3509ef6e56a9017857da91fcade1d40f38ceac71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
 * ========================================================================
 * Copyright 2006-2008 University of Washington
 * Copyright 2013-2020 Eduardo Chappa
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * ========================================================================
 */

#include "headers.h"
#include "xoauth2conf.h"
#include "xoauth2.h"
#include "keymenu.h"
#include "status.h"
#include "confscroll.h"
#include "../pith/state.h"
#include "../pith/conf.h"
#include "../pith/list.h"

extern OAUTH2_S alpine_oauth2_list[];

XOAUTH2_INFO_S xoauth_default[] = {
  { GMAIL_NAME, GMAIL_ID, GMAIL_SECRET},
#if 0
  { OUTLOOK_NAME, OUTLOOK_ID, OUTLOOK_SECRET},
#endif
  { NULL, NULL, NULL}
};


#define NXSERVERS (sizeof(xoauth_default)/sizeof(xoauth_default[0])-1)
#define XOAUTH2_CLIENT_ID       _("Client-Id")
#define XOAUTH2_CLIENT_SECRET   _("Client-Secret")
#define XNAME			"/NAME="
#define XID			"/ID="
#define XSECRET			"/SECRET="

void write_xoauth_configuration(struct variable  *, struct variable **, EditWhich);

char *
xoauth_config_line(char *server, char *id, char *secret)
{
  size_t n;
  char *rv;

  n = strlen(XNAME) + strlen(XID) + strlen(XSECRET) 
	    + strlen(server) + strlen(id) + strlen(secret) + 9;
  rv = fs_get(n*sizeof(char));
  sprintf(rv, "%s\"%s\" %s\"%s\" %s\"%s\"", XNAME, server, XID, id, 
						XSECRET, secret);
  return rv;
}

/* call this function when id and secret are unknown.
 * precedence is as follows:
 * If the user has configured something, return that;
 * else if we are already using a value, return that;
 * else return default values.
 */
void
oauth2_get_client_info(char *name, char **id, char **secret)
{
  int i;
  char **lval, *name_lval, *idp, *secretp;

  *id = *secret = NULL;

  /* first check the value configured by the user */
  lval = ps_global->vars[V_XOAUTH2_INFO].current_val.l;
  for(i = 0; lval && lval[i]; i++){
     xoauth_parse_client_info(lval[i], &name_lval, &idp, &secretp);
     if(name_lval && !strcmp(name_lval, name)){
	*id = idp ? cpystr(idp) : NULL;
	*secret = secretp ? cpystr(secretp) : NULL;
     }
     if(name_lval) fs_give((void **) &name_lval);
     if(idp) fs_give((void **) &idp);
     if(secretp) fs_give((void **) &secretp);
     break;
  }

  if(*id && **id && *secret && **secret) return;

  /* if not, now see if we already have a value set, and use that */
  for(i = 0; alpine_oauth2_list[i].name != NULL; i++){
     if(!strcmp(alpine_oauth2_list[i].name, name)){
        *id = alpine_oauth2_list[i].param[OA2_Id].value 
		? cpystr(alpine_oauth2_list[i].param[OA2_Id].value) : NULL;
        *secret = alpine_oauth2_list[i].param[OA2_Secret].value
		? cpystr(alpine_oauth2_list[i].param[OA2_Secret].value) : NULL;
	break;
     }
  }

  if(*id && **id && *secret && **secret) return;

  /* if nothing, use the default value */
  for(i = 0; xoauth_default[i].name != NULL; i++)
     if(!strcmp(xoauth_default[i].name, name)){
        *id = cpystr(xoauth_default[i].client_id);
        *secret = cpystr(xoauth_default[i].client_secret);
	break;
     }
}

/* write vlist to v 
 * Each vlist member is of type "p", while "v" is of type "l", so we
 * each entry in "l" by using each of the "p" entries.
 */
void
write_xoauth_configuration(struct variable  *v, struct variable **vlist, EditWhich ew)
{
  int i, j, k;
  size_t n;
  char ***alval, **lval, *p, *q, *l;

  alval  = ALVAL(v, ew);
  for (i = 0, k = 0; vlist[i] != NULL;){
      if(PVAL(vlist[i], ew)){
	j = i/2;	/* this is the location in the alpine_oauth2_list array */
	i = 2*j;	/* reset i */
	p = PVAL(vlist[i], ew);
	if(p == NULL) p = vlist[i]->current_val.p;
	q = PVAL(vlist[i+1], ew);
	if(q == NULL) q = vlist[i+1]->current_val.p;
	if(k == 0) lval = fs_get((NXSERVERS +1)*sizeof(char *));
	lval[k++] = xoauth_config_line(alpine_oauth2_list[j].name, p, q);
	if(alpine_oauth2_list[j].param[OA2_Id].value)
	   fs_give((void **) &alpine_oauth2_list[j].param[OA2_Id].value);
	if(alpine_oauth2_list[j].param[OA2_Secret].value)
	   fs_give((void **) &alpine_oauth2_list[j].param[OA2_Secret].value);
	alpine_oauth2_list[j].param[OA2_Id].value = cpystr(p);
	alpine_oauth2_list[j].param[OA2_Secret].value = cpystr(q);
	i += 2;
      }
      else i++;
  }
  if(k > 0){
     lval[k] = NULL;
     if(*alval) free_list_array(alval);
     *alval = lval;
  }
  else
     *alval = NULL;
  set_current_val(&ps_global->vars[V_XOAUTH2_INFO], FALSE, FALSE);
}


/* parse line of the form
  /NAME="text" /ID="text" /SECRET="text"
 */
void
xoauth_parse_client_info(char *lvalp, char **namep, char **idp, char **secretp)
{
  char *s, *t, c;
  *namep = *idp = *secretp = NULL;

  if (lvalp == NULL) return;

  if(s = strstr(lvalp, XNAME)){
	s += strlen(XNAME);
	if(*s == '"') s++;
	for(t = s; *t && *t != '"' && *t != ' '; t++);
	c = *t;
	*t = '\0';
	*namep = cpystr(s);
	*t = c;
  }

  if(s = strstr(lvalp, XID)){
	s += strlen(XID);
	if(*s == '"') s++;
	for(t = s; *t && *t != '"' && *t != ' '; t++);
	c = *t;
	*t = '\0';
	*idp = cpystr(s);
	*t = c;
  }

  if(s = strstr(lvalp, XSECRET)){
	s += strlen(XSECRET);
	if(*s == '"') s++;
	for(t = s; *t && *t != '"' && *t != ' '; t++);
	c = *t;
	*t = '\0';
	*secretp = cpystr(s);
	*t = c;
  }
}


/*----------------------------------------------------------------------
  Screen to add client_id and client_secret for a service

  ---*/
void
alpine_xoauth2_configuration(struct pine *ps, int edit_exceptions)
{
    struct variable gmail_client_id_var, gmail_client_secret_var;
#if 0
    struct variable outlook_client_id_var, outlook_client_secret_var;
#endif
    struct variable *varlist[2*NXSERVERS + 1];
    char	    tmp[MAXPATH+1], *pval, **lval;
    char	    *id, *secret;
    char	    *name_lval, *id_lval, *id_conf, *id_def, *secret_lval, *secret_conf, *secret_def;
    int		    i, j, k, l, ln = 0, readonly_warning = 0, pos;
    CONF_S	   *ctmpa = NULL, *ctmpb, *first_line = NULL;
    FEATURE_S	   *feature;
    PINERC_S       *prc = NULL;
    OPT_SCREEN_S    screen;
    int             expose_hidden_config, add_hidden_vars_title = 0;
    SAVED_CONFIG_S *vsave;

    dprint((3, "--  alpine_xoauth2_configuration --\n"));

    ew = edit_exceptions ? ps_global->ew_for_except_vars : Main;

    if(ps->restricted)
      readonly_warning = 1;
    else{
	switch(ew){
	  case Main:
	    prc = ps->prc;
	    break;
	  case Post:
	    prc = ps->post_prc;
	    break;
	  default:
	    break;
	}

	readonly_warning = prc ? prc->readonly : 1;
    }

    ps->next_screen = SCREEN_FUN_NULL;

    mailcap_free(); /* free resources we won't be using for a while */

    varlist[0] = &gmail_client_id_var;
    varlist[1] = &gmail_client_secret_var;
#if 0
    varlist[2] = &outlook_client_id_var;
    varlist[3] = &outlook_client_secret_var;
#endif
    varlist[2*NXSERVERS] = NULL;

    for(i = 0; i < 2*NXSERVERS; i++)
      memset((void *) varlist[i], 0, sizeof(struct variable));

    pos = -1;
    do {
        ctmpa = first_line = NULL;

        ln = strlen(XOAUTH2_CLIENT_ID);
        i = strlen(XOAUTH2_CLIENT_SECRET);
        if(ln < i) ln = i;

	lval  = LVAL(&ps->vars[V_XOAUTH2_INFO], ew);

        for(i = 0, l = 0; alpine_oauth2_list[i].name != NULL; i++){
	   id_conf = id_def = secret_conf = secret_def = NULL;
	   name_lval = id_lval = secret_lval = NULL;

	   id_conf = alpine_oauth2_list[i].param[OA2_Id].value;
	   secret_conf = alpine_oauth2_list[i].param[OA2_Secret].value;

	   for(j = 0; xoauth_default[j].name != NULL; j++)
	      if(!strcmp(alpine_oauth2_list[i].name, xoauth_default[j].name))
		break;

	   if(xoauth_default[j].name != NULL){
	     id_def = xoauth_default[j].client_id;
	     secret_def = xoauth_default[j].client_secret;
	   }

	   /* fix this: the purpose is to search for the name in lval */
	   for(k = 0; lval && lval[k]; k++){
	      xoauth_parse_client_info(lval[k], &name_lval, &id_lval, &secret_lval);
	      if(name_lval && !strcmp(name_lval, alpine_oauth2_list[i].name))
		break;
	      if (name_lval) fs_give((void **) &name_lval);
	      if (id_lval) fs_give((void **) &id_lval);
	      if (secret_lval) fs_give((void **) &secret_lval);
	   }

	   /* Here we have three values. The one being used by c-client in
	    * id_conf, secret_conf. The default value in Alpine, obtained
	    * by the programmer by registering Alpine, and the value
	    * configured in Alpine by the user in id_lval, and secret_lval.
	    *
	    * The rules are:
	    *  1. If id_lval && secret_lval are not null, we use those.
	    *  2. else we use the default values.
	    */
	    id = id_lval ? id_lval : id_def;
	    secret = secret_lval ? secret_lval : secret_def;

	   new_confline(&ctmpa)->var = NULL;
	   if(!first_line) first_line = ctmpa;

	   /*  Write Name of provider first */
	   ctmpa->flags	    |= CF_NOSELECT;
	   ctmpa->help	     = NO_HELP;
	   ctmpa->valoffset  = 1;
	   ctmpa->value	     = cpystr(alpine_oauth2_list[i].name);

	   /* Setup client-id variable */
	   varlist[l]->name = cpystr(XOAUTH2_CLIENT_ID);
	   varlist[l]->is_used = 1;
	   varlist[l]->is_user = 1;
	   varlist[l]->main_user_val.p = strcmp(id, id_def)? cpystr(id) : NULL;
	   varlist[l]->global_val.p = cpystr(id_def);
	   set_current_val(varlist[l], FALSE, FALSE);

	   /* Write client-id variable */
	   new_confline(&ctmpa)->var = varlist[l];
	   utf8_snprintf(tmp, sizeof(tmp), "   %-*.100w =", ln, XOAUTH2_CLIENT_ID);
	   tmp[sizeof(tmp)-1] = '\0';
	   ctmpa->varname   = cpystr(tmp);
	   ctmpa->varmem    = l++;
	   ctmpa->valoffset = ln + 3 + 3;
	   ctmpa->value     = pretty_value(ps, ctmpa);
	   ctmpa->keymenu   = &config_text_keymenu;
	   ctmpa->help      = h_config_xoauth2_client_id;
	   ctmpa->tool      = text_tool;
	   ctmpa->varnamep  = ctmpb;

	   /* Set up client-secret variable */
	   varlist[l]->name = cpystr(XOAUTH2_CLIENT_SECRET);
	   varlist[l]->is_used = 1;
	   varlist[l]->is_user = 1;
	   varlist[l]->main_user_val.p = strcmp(secret, secret_def) ? cpystr(secret) : NULL;
	   varlist[l]->global_val.p = cpystr(secret_def);
	   set_current_val(varlist[l], FALSE, FALSE);

	   /* Write client-secret variable */
	   new_confline(&ctmpa)->var = varlist[l];
	   utf8_snprintf(tmp, sizeof(tmp), "   %-*.100w =", ln, XOAUTH2_CLIENT_SECRET);
	   tmp[sizeof(tmp)-1] = '\0';
	   ctmpa->varname   = cpystr(tmp);
	   ctmpa->varmem    = l++;
	   ctmpa->valoffset = ln + 3 + 3;
	   ctmpa->value     = pretty_value(ps, ctmpa);
	   ctmpa->keymenu   = &config_text_keymenu;
	   ctmpa->help      = h_config_xoauth2_client_secret;
	   ctmpa->tool      = text_tool;
	   ctmpa->varnamep  = ctmpb;

	   /* Separate servers with a blank line */
	   new_confline(&ctmpa);
	   ctmpa->flags    |= CF_NOSELECT | CF_B_LINE;

	   /* clean up the house */
	   if(id_lval) fs_give((void **) &id_lval);
	   if(secret_lval) fs_give((void **) &secret_lval);
	   if(name_lval) fs_give((void **) &name_lval);
	}

	vsave = save_config_vars(ps, expose_hidden_config);
	first_line = pos < 0 ? first_sel_confline(first_line) : set_confline_number(first_line, pos);
	pos = -1;
	memset(&screen, 0, sizeof(screen));
	screen.ro_warning = readonly_warning;
	/* TRANSLATORS: Print something1 using something2.
	"configuration" is something1 */
	switch(conf_scroll_screen(ps, &screen, first_line, "XOAUTH2 Alpine Info",
				      _("configuration"), 0, &pos)){
	      case 0:
		break;

	      case 1:
		write_xoauth_configuration(&ps->vars[V_XOAUTH2_INFO], varlist, ew);
		write_pinerc(ps, ew, WRP_NONE);
		break;

	      case 10:
		revert_to_saved_config(ps, vsave, expose_hidden_config);
		if(prc)
		  prc->outstanding_pinerc_changes = 0;
		break;
      
	      default:
		q_status_message(SM_ORDER,7,10,
		    "conf_scroll_screen bad ret, not supposed to happen");
		break;
	}
    } while (pos >= 0);

#ifdef _WINDOWS
    mswin_set_quit_confirm (F_OFF(F_QUIT_WO_CONFIRM, ps_global));
#endif
}