* Change x10host.com to alpineapp.email.
[alpine.git] / alpine / remote.c
blob685d3c34607e65d19411b1ffc38f26cb81e8c54d
1 /*
2 * ========================================================================
3 * Copyright 2006-2008 University of Washington
4 * Copyright 2013-2022 Eduardo Chappa
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * ========================================================================
15 #include "headers.h"
16 #include "remote.h"
17 #include "keymenu.h"
18 #include "mailview.h"
19 #include "status.h"
20 #include "radio.h"
21 #include "../pith/msgno.h"
22 #include "../pith/filter.h"
23 #include "../pith/util.h"
24 #include "../pith/conf.h"
25 #include "../pith/tempfile.h"
26 #include "../pith/margin.h"
30 * Internal prototypes
32 int rd_answer_forge_warning(int, MSGNO_S *, SCROLL_S *);
35 int
36 rd_prompt_about_forged_remote_data(int reason, REMDATA_S *rd, char *extra)
38 char *p, tmp[2000];
39 char *unknown = "<unknown>";
40 int rv = -1;
41 char *foldertype, *foldername, *special;
43 foldertype = (rd && rd->t.i.special_hdr && !strucmp(rd->t.i.special_hdr, REMOTE_ABOOK_SUBTYPE)) ? "address book" : (rd && rd->t.i.special_hdr && !strucmp(rd->t.i.special_hdr, REMOTE_PINERC_SUBTYPE)) ? "configuration" : "data";
44 foldername = (rd && rd->rn) ? rd->rn : unknown;
45 special = (rd && rd->t.i.special_hdr) ? rd->t.i.special_hdr : unknown;
46 tmp[0] = '\0';
47 p = tmp;
49 dprint((1, "rd_check_out_forged_remote_data:\n"));
50 dprint((1, " reason=%d\n", reason));
51 dprint((1, " folder_type=%s\n", foldertype ? foldertype : "?"));
52 dprint((1, " remotename=%s\n\n", foldername ? foldername : "?"));
54 if(rd && rd->flags & USER_SAID_NO)
55 return rv;
57 if(reason == -2){
58 dprint((1, "The special header \"%s\" is missing from the last message in the folder.\nThis indicates that something is wrong.\nYou should probably answer \"No\"\nso that you don't use the corrupt data.\nThen you should investigate further.\n", special ? special : "?"));
60 else if(reason == -1){
61 dprint((1, "The last message in the folder contains \"Received\" headers.\nThis usually indicates that the message was put there by the mail\ndelivery system. Alpine does not add those Received headers.\nYou should probably answer \"No\" so that you don't use the corrupt data.\nThen you should investigate further.\n"));
63 else if(reason == 0){
64 dprint((1, "The special header \"%s\" in the last message\nin the folder has an unexpected value (%s)\nafter it. This could indicate that something is wrong.\nThis value should not normally be put there by Alpine.\nHowever, since there are no Received lines in the message we choose to\nbelieve that everything is ok and we will proceed.\n",
65 special ? special : "?", (extra && *extra) ? extra : "?"));
67 else if(reason == 1){
68 dprint((1, "The special header \"%s\" in the last message\nin the folder has an unexpected value (1)\nafter it. It appears that it may have been put there by an Pine\nwith a version number less than 4.50.\nSince there are no Received lines in the message we choose to believe that\nthe header was added by an old Pine and we will proceed.\n",
69 special ? special : "?"));
71 else if(reason > 1){
72 dprint((1, "The special header \"%s\" in the last message\nin the folder has an unexpected value (%s)\nafter it. This is the right sort of value that Alpine would normally put there,\nbut it doesn't match the value from the first message in the folder.\nThis may indicate that something is wrong.\nHowever, since there are no Received lines in the message we choose to\nbelieve that everything is ok and we will proceed.\n",
73 special ? special : "?", (extra && *extra) ? extra : "?"));
76 if(reason >= 0){
78 * This check should not really be here. We have a cookie that
79 * has the wrong value so something is possibly wrong.
80 * But we are worried that old pines will put the bad value in
81 * there (which they will) and then the questions will bother
82 * users and mystify them. So we're just going to pretend the user
83 * said Yes in this case, and we'll try to fix the cookie.
84 * We still catch Received lines and use that or the complete absence
85 * of a special header as indicators of trouble.
87 rd->flags |= USER_SAID_YES;
88 return(1);
91 if(ps_global->ttyo){
92 SCROLL_S sargs;
93 STORE_S *in_store, *out_store;
94 gf_io_t pc, gc;
95 HANDLE_S *handles = NULL;
96 int the_answer = 'n';
98 if(!(in_store = so_get(CharStar, NULL, EDIT_ACCESS)) ||
99 !(out_store = so_get(CharStar, NULL, EDIT_ACCESS)))
100 goto try_wantto;
102 /* TRANSLATORS: the first %s is the folder type, it may be address book or
103 configuration. The second %s is the folder name. Of course, the HTML
104 tags should be left as is. */
105 snprintf(tmp, sizeof(tmp), _("<HTML><P>The data in the remote %s folder<P><CENTER>%s</CENTER><P>looks suspicious. The reason for the suspicion is<P><CENTER>"),
106 foldertype, foldername);
107 tmp[sizeof(tmp)-1] = '\0';
108 so_puts(in_store, tmp);
110 if(reason == -2){
111 snprintf(tmp, sizeof(tmp), _("header \"%s\" is missing</CENTER><P>The special header \"%s\" is missing from the last message in the folder. This indicates that something is wrong. You should probably answer \"No\" so that you don't use the corrupt data. Then you should investigate further."),
112 special, special);
113 tmp[sizeof(tmp)-1] = '\0';
114 so_puts(in_store, tmp);
116 else if(reason == -1){
117 so_puts(in_store, _("\"Received\" headers detected</CENTER><P>The last message in the folder contains \"Received\" headers. This usually indicates that the message was put there by the mail delivery system. Alpine does not add those Received headers. You should probably answer \"No\" so that you don't use the corrupt data. Then you should investigate further."));
119 else if(reason == 0){
120 snprintf(tmp, sizeof(tmp), _("Unexpected value for header \"%s\"</CENTER><P>The special header \"%s\" in the last message in the folder has an unexpected value (%s) after it. This probably indicates that something is wrong. This value would not normally be put there by Alpine. You should probably answer \"No\" so that you don't use the corrupt data. Then you should investigate further."),
121 special, special, (extra && *extra) ? extra : "?");
122 tmp[sizeof(tmp)-1] = '\0';
123 so_puts(in_store, tmp);
125 else if(reason == 1){
126 snprintf(tmp, sizeof(tmp), _("Unexpected value for header \"%s\"</CENTER><P>The special header \"%s\" in the last message in the folder has an unexpected value (1) after it. It appears that it may have been put there by a Pine with a version number less than 4.50. If you believe that you have changed this data with an older Pine more recently than you've changed it with this version of Alpine, then you can probably safely answer \"Yes\". If you do not understand why this has happened, you should probably answer \"No\" so that you don't use the corrupt data. Then you should investigate further."),
127 special, special);
128 tmp[sizeof(tmp)-1] = '\0';
129 so_puts(in_store, tmp);
131 else if(reason > 1){
132 snprintf(tmp, sizeof(tmp), _("Unexpected value for header \"%s\"</CENTER><P>The special header \"%s\" in the last message in the folder has an unexpected value (%s) after it. This is the right sort of value that Alpine would normally put there, but it doesn't match the value from the first message in the folder. This may indicate that something is wrong. Unless you understand why this has happened, you should probably answer \"No\" so that you don't use the corrupt data. Then you should investigate further."),
133 special, special, (extra && *extra) ? extra : "?");
134 tmp[sizeof(tmp)-1] = '\0';
135 so_puts(in_store, tmp);
138 so_seek(in_store, 0L, 0);
139 init_handles(&handles);
140 gf_filter_init();
141 gf_link_filter(gf_html2plain,
142 gf_html2plain_opt(NULL,
143 ps_global->ttyo->screen_cols, non_messageview_margin(),
144 &handles, NULL, GFHP_LOCAL_HANDLES));
145 gf_set_so_readc(&gc, in_store);
146 gf_set_so_writec(&pc, out_store);
147 gf_pipe(gc, pc);
148 gf_clear_so_writec(out_store);
149 gf_clear_so_readc(in_store);
151 memset(&sargs, 0, sizeof(SCROLL_S));
152 sargs.text.handles = handles;
153 sargs.text.text = so_text(out_store);
154 sargs.text.src = CharStar;
155 sargs.bar.title = _("REMOTE DATA FORGERY WARNING");
156 sargs.proc.tool = rd_answer_forge_warning;
157 sargs.proc.data.p = (void *)&the_answer;
158 sargs.keys.menu = &forge_keymenu;
159 setbitmap(sargs.keys.bitmap);
161 scrolltool(&sargs);
163 if(the_answer == 'y'){
164 rv = 1;
165 rd->flags |= USER_SAID_YES;
167 else if(rd)
168 rd->flags |= USER_SAID_NO;
170 ps_global->mangled_screen = 1;
171 ps_global->painted_body_on_startup = 0;
172 ps_global->painted_footer_on_startup = 0;
173 so_give(&in_store);
174 so_give(&out_store);
175 free_handles(&handles);
177 else{
178 snprintf(p, sizeof(tmp), _("\nThe data in the remote %s folder\n\n %s\n\nlooks suspicious. The reason for the suspicion is\n\n "),
179 foldertype, foldername);
180 tmp[sizeof(tmp)-1] = '\0';
181 p += strlen(p);
183 if(reason == -2){
184 snprintf(p, sizeof(tmp)-(p-tmp), _("header \"%s\" is missing\n\nThe special header \"%s\" is missing from the last message\nin the folder. This indicates that something is wrong.\nYou should probably answer \"No\" so that you don't use the corrupt data.\nThen you should investigate further.\n\n"),
185 special, special);
186 tmp[sizeof(tmp)-1] = '\0';
188 else if(reason == -1){
189 snprintf(p, sizeof(tmp)-(p-tmp), _("\"Received\" headers detected\n\nThe last message in the folder contains \"Received\" headers.\nThis usually indicates that the message was put there by the\nmail delivery system. Alpine does not add those Received headers.\nYou should probably answer \"No\" so that you don't use the corrupt data.\nThen you should investigate further.\n\n"));
190 tmp[sizeof(tmp)-1] = '\0';
192 else if(reason == 0){
193 snprintf(p, sizeof(tmp)-(p-tmp), _("Unexpected value for header \"%s\"\n\nThe special header \"%s\" in the last message in the folder\nhas an unexpected value (%s) after it. This probably\nindicates that something is wrong. This value would not normally be put\nthere by Alpine. You should probably answer \"No\" so that you don't use\nthe corrupt data. Then you should investigate further.\n\n"),
194 special, special, (extra && *extra) ? extra : "?");
195 tmp[sizeof(tmp)-1] = '\0';
197 else if(reason == 1){
198 snprintf(p, sizeof(tmp)-(p-tmp), _("Unexpected value for header \"%s\"\n\nThe special header \"%s\" in the last message in the folder\nhas an unexpected value (1) after it. It appears that it may have been\nput there by a Pine with a version number less than 4.50.\nIf you believe that you have changed this data with an older Pine more\nrecently than you've changed it with this version of Alpine, then you can\nprobably safely answer \"Yes\". If you do not understand why this has\nhappened, you should probably answer \"No\" so that you don't use the\ncorrupt data. Then you should investigate further.\n\n"),
199 special, special);
200 tmp[sizeof(tmp)-1] = '\0';
202 else if(reason > 1){
203 snprintf(p, sizeof(tmp)-(p-tmp), _("Unexpected value for header \"%s\"\n\nThe special header \"%s\" in the last message in the folder\nhas an unexpected\nvalue (%s) after it. This is\nthe right sort of value that Alpine would normally put there, but it\ndoesn't match the value from the first message in the folder. This may\nindicate that something is wrong. Unless you understand why this has happened,\nyou should probably answer \"No\" so that you don't use the\ncorrupt data. Then you should investigate further.\n\n"),
204 special, special, (extra && *extra) ? extra : "?");
205 tmp[sizeof(tmp)-1] = '\0';
208 try_wantto:
209 p += strlen(p);
210 snprintf(p, sizeof(tmp)-(p-tmp), _("Suspicious data in \"%s\": Continue anyway "),
211 (rd && rd->t.i.special_hdr) ? rd->t.i.special_hdr
212 : unknown);
213 tmp[sizeof(tmp)-1] = '\0';
214 if(want_to(tmp, 'n', 'x', NO_HELP, WT_NORM) == 'y'){
215 rv = 1;
216 rd->flags |= USER_SAID_YES;
218 else if(rd)
219 rd->flags |= USER_SAID_NO;
222 if(rv < 0)
223 q_status_message1(SM_ORDER, 1, 3, _("Can't open remote %s"),
224 (rd && rd->rn) ? rd->rn : "<noname>");
226 return(rv);
231 rd_answer_forge_warning(int cmd, MSGNO_S *msgmap, SCROLL_S *sparms)
233 int rv = 1;
235 ps_global->next_screen = SCREEN_FUN_NULL;
237 switch(cmd){
238 case MC_YES :
239 *(int *)(sparms->proc.data.p) = 'y';
240 break;
242 case MC_NO :
243 *(int *)(sparms->proc.data.p) = 'n';
244 break;
246 default:
247 alpine_panic("Unexpected command in rd_answer_forge_warning");
248 break;
251 return(rv);
256 char *
257 rd_metadata_name(void)
259 char *p, *q, *metafile;
260 char path[MAXPATH], pinerc_dir[MAXPATH];
261 struct variable *vars = ps_global->vars;
263 dprint((9, "rd_metadata_name\n"));
265 pinerc_dir[0] = '\0';
266 if(ps_global->pinerc){
267 char *prcn = ps_global->pinerc;
268 char *lc;
270 if((lc = last_cmpnt(prcn)) != NULL){
271 int to_copy;
273 to_copy = (lc - prcn > 1) ? (lc - prcn - 1) : 1;
274 strncpy(pinerc_dir, prcn, MIN(to_copy, sizeof(pinerc_dir)-1));
275 pinerc_dir[MIN(to_copy, sizeof(pinerc_dir)-1)] = '\0';
277 else{
278 pinerc_dir[0] = '.';
279 pinerc_dir[1] = '\0';
284 * If there is no metadata file specified in the pinerc, create a filename.
286 if(!(VAR_REMOTE_ABOOK_METADATA && VAR_REMOTE_ABOOK_METADATA[0])){
287 if(pinerc_dir[0] && (p = tempfile_in_same_dir(ps_global->pinerc,
288 meta_prefix, NULL))){
289 /* fill in the pinerc variable */
290 q = p + strlen(pinerc_dir) + 1;
291 set_variable(V_REMOTE_ABOOK_METADATA, q, 1, 0, Main);
292 dprint((2, "creating name for metadata file: %s\n",
293 q ? q : "?"));
295 /* something's broken, return NULL rab */
296 if(!VAR_REMOTE_ABOOK_METADATA || !VAR_REMOTE_ABOOK_METADATA[0]){
297 our_unlink(p);
298 fs_give((void **)&p);
299 return(NULL);
302 fs_give((void **)&p);
304 else{
305 q_status_message(SM_ORDER, 3, 5,
306 "can't create metadata file in pinerc directory, continuing");
307 return(NULL);
311 build_path(path, pinerc_dir ? pinerc_dir : NULL,
312 VAR_REMOTE_ABOOK_METADATA, sizeof(path));
313 metafile = path;
316 * If the metadata file doesn't exist, create it.
318 if(can_access(metafile, ACCESS_EXISTS) != 0){
319 int fd;
321 if((fd = our_open(metafile, O_CREAT|O_EXCL|O_WRONLY|O_BINARY, 0600)) < 0){
323 set_variable(V_REMOTE_ABOOK_METADATA, NULL, 1, 0, Main);
325 q_status_message2(SM_ORDER, 3, 5,
326 "can't create cache file %.200s, continuing (%.200s)",
327 metafile, error_description(errno));
329 dprint((2, "can't create metafile %s: %s\n",
330 metafile ? metafile : "?", error_description(errno)));
332 return(NULL);
335 dprint((2, "created metadata file: %s\n",
336 metafile ? metafile : "?"));
338 (void)close(fd);
341 return(cpystr(metafile));;