2 * ========================================================================
3 * Copyright 2006-2007 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 /*======================================================================
18 Session initiation support routines
25 #include "../pith/init.h"
26 #include "../pith/conf.h"
27 #include "../pith/status.h"
28 #include "../pith/folder.h"
29 #include "../pith/context.h"
34 /* these are used to report folder directory creation problems */
35 CONF_TXT_T init_md_exists
[] = "The \"%s\" subdirectory already exists, but it is not writable by Alpine so Alpine cannot run. Please correct the permissions and restart Alpine.";
37 CONF_TXT_T init_md_file
[] = "Alpine requires a directory called \"%s\" and usually creates it. You already have a regular file by that name which means Alpine cannot create the directory. Please move or remove it and start Alpine again.";
39 CONF_TXT_T init_md_create
[] = "Creating subdirectory \"%s\" where Alpine will store its mail folders.";
45 char *context_string(char *);
46 int prune_folders(CONTEXT_S
*, char *, int, char *, unsigned);
47 int prune_move_folder(char *, char *, CONTEXT_S
*);
48 void delete_old_mail(struct sm_folder
*, CONTEXT_S
*, char *);
52 /*----------------------------------------------------------------------
53 Make sure the alpine folders directory exists, with proper folders
55 Args: ps -- alpine structure to get mail directory and contexts from
57 Result: returns 0 if it exists or it is created and all is well
58 1 if it is missing and can't be created.
61 init_mail_dir(struct pine
*ps
)
64 /* MAIL_LIST: can we use imap4 CREATE? */
66 * We don't really care if mail_dir exists if it isn't
67 * part of the first folder collection specified. If this
68 * is the case, it must have been created external to us, so
71 if(ps
->VAR_FOLDER_SPEC
&& ps
->VAR_FOLDER_SPEC
[0]){
72 char *p
= context_string(ps
->VAR_FOLDER_SPEC
[0]);
73 int rv
= strncmp(p
, ps
->VAR_MAIL_DIRECTORY
,
74 strlen(ps
->VAR_MAIL_DIRECTORY
));
80 switch(is_writable_dir(ps
->folders_dir
)){
82 /* --- all is well --- */
86 snprintf(tmp_20k_buf
, SIZEOF_20KBUF
, init_md_exists
, ps
->folders_dir
);
87 display_init_err(tmp_20k_buf
, 1);
91 snprintf(tmp_20k_buf
, SIZEOF_20KBUF
, init_md_file
, ps
->folders_dir
);
92 display_init_err(tmp_20k_buf
, 1);
96 snprintf(tmp_20k_buf
, SIZEOF_20KBUF
, init_md_create
, ps
->folders_dir
);
97 display_init_err(tmp_20k_buf
, 0);
101 if(create_mail_dir(ps
->folders_dir
) < 0){
102 snprintf(tmp_20k_buf
, SIZEOF_20KBUF
, "Error creating subdirectory \"%s\" : %s",
103 ps
->folders_dir
, error_description(errno
));
104 display_init_err(tmp_20k_buf
, 1);
113 /*----------------------------------------------------------------------
114 Make sure the default save folders exist in the default
118 display_init_err(char *s
, int err
)
121 mswin_messagebox(s
, err
);
129 if(++n
> 60 && isspace((unsigned char)*s
)){
132 while(*(s
+1) && isspace((unsigned char)*(s
+1)))
144 * Return malloc'd string containing only the context specifier given
145 * a string containing the raw pinerc entry
148 context_string(char *s
)
150 CONTEXT_S
*t
= new_context(s
, NULL
);
155 * Take what we want from context, then free the rest...
158 t
->context
= NULL
; /* don't free it! */
162 return(rv
? rv
: cpystr(""));
167 /*----------------------------------------------------------------------
168 Rename the current sent-mail folder to sent-mail for last month
170 open up sent-mail and get date of very first message
171 if date is last month rename and...
172 if files from 3 months ago exist ask if they should be deleted and...
173 if files from previous months and yes ask about them, too.
174 ----------------------------------------------------------------------*/
176 expire_sent_mail(void)
178 int cur_month
, ok
= 1;
182 CONTEXT_S
*prune_cntxt
;
184 dprint((5, "==== expire_mail called ====\n"));
186 if(!check_prune_time(&now
, &tm_now
))
189 cur_month
= (1900 + tm_now
->tm_year
) * 12 + tm_now
->tm_mon
;
190 dprint((5, "Current month %d\n", cur_month
));
193 * locate the default save context...
195 if(!(prune_cntxt
= default_save_context(ps_global
->context_list
)))
196 prune_cntxt
= ps_global
->context_list
;
199 * Since fcc's and read-mail can be an IMAP mailbox, be sure to only
200 * try expiring a list if it's an ambiguous name associated with some
203 * If sentmail set outside a context, then pruning is up to the
207 if(ps_global
->VAR_DEFAULT_FCC
&& *ps_global
->VAR_DEFAULT_FCC
208 && context_isambig(ps_global
->VAR_DEFAULT_FCC
))
209 ok
= prune_folders(prune_cntxt
, ps_global
->VAR_DEFAULT_FCC
,
211 ps_global
->pruning_rule
);
213 if(ok
&& ps_global
->VAR_READ_MESSAGE_FOLDER
214 && *ps_global
->VAR_READ_MESSAGE_FOLDER
215 && context_isambig(ps_global
->VAR_READ_MESSAGE_FOLDER
))
216 ok
= prune_folders(prune_cntxt
, ps_global
->VAR_READ_MESSAGE_FOLDER
,
218 ps_global
->pruning_rule
);
222 * Within the default prune context,
223 * prune back the folders with the given name
225 if(ok
&& prune_cntxt
&& (p
= ps_global
->VAR_PRUNED_FOLDERS
))
227 if(**p
&& context_isambig(*p
))
228 ok
= prune_folders(prune_cntxt
, *p
, cur_month
, "",
229 ps_global
->pruning_rule
);
232 * Mark that we're done for this month...
235 ps_global
->last_expire_year
= tm_now
->tm_year
;
236 ps_global
->last_expire_month
= tm_now
->tm_mon
;
237 snprintf(tmp
, sizeof(tmp
), "%d.%d", ps_global
->last_expire_year
,
238 ps_global
->last_expire_month
+ 1);
239 set_variable(V_LAST_TIME_PRUNE_QUESTION
, tmp
, 1, 1, Main
);
246 /*----------------------------------------------------------------------
247 Offer to delete old sent-mail folders
249 Args: sml -- The list of sent-mail folders
253 prune_folders(CONTEXT_S
*prune_cntxt
, char *folder_base
, int cur_month
,
254 char *type
, unsigned int pr
)
256 char path2
[MAXPATH
+1], prompt
[128], tmp
[21];
257 int month_to_use
, exists
;
258 struct sm_folder
*mail_list
, *sm
;
260 mail_list
= get_mail_list(prune_cntxt
, folder_base
);
261 free_folder_list(prune_cntxt
);
264 for(sm
= mail_list
; sm
!= NULL
&& sm
->name
!= NULL
; sm
++)
265 dprint((5,"Old sent-mail: %5d %s\n", sm
->month_num
,
266 sm
->name
[0] ? sm
->name
: "?"));
269 for(sm
= mail_list
; sm
!= NULL
&& sm
->name
!= NULL
; sm
++)
270 if(sm
->month_num
== cur_month
- 1)
271 break; /* matched a month */
273 month_to_use
= (sm
== NULL
|| sm
->name
== NULL
) ? cur_month
- 1 : 0;
275 dprint((5, "Month_to_use : %d\n", month_to_use
));
277 if(month_to_use
== 0 || pr
== PRUNE_NO_AND_ASK
|| pr
== PRUNE_NO_AND_NO
)
280 strncpy(path2
, folder_base
, sizeof(path2
)-1);
281 path2
[sizeof(path2
)-1] = '\0';
283 if(F_ON(F_PRUNE_USES_ISO
,ps_global
)){ /* sent-mail-yyyy-mm */
284 snprintf(path2
+ strlen(path2
), sizeof(path2
)-strlen(path2
), "-%4.4d-%2.2d", month_to_use
/12,
285 month_to_use
% 12 + 1);
288 strncpy(tmp
, month_abbrev((month_to_use
% 12)+1), 20);
289 tmp
[sizeof(tmp
)-1] = '\0';
290 lcase((unsigned char *) tmp
);
292 if(*prune_cntxt
->context
!= '{'){
296 snprintf(path2
+ (size_t)((i
> 4) ? 4 : i
),
297 sizeof(path2
)- ((i
> 4) ? 4 : i
),
298 "%2.2d%2.2d", (month_to_use
% 12) + 1,
299 ((month_to_use
/ 12) - 1900) % 100);
303 snprintf(path2
+ strlen(path2
), sizeof(path2
)-strlen(path2
), "-%.20s-%d", tmp
, month_to_use
/12);
307 snprintf(prompt
, sizeof(prompt
), "Move current \"%.50s\" to \"%.50s\"", folder_base
, path2
);
308 if((exists
= folder_exists(prune_cntxt
, folder_base
)) == FEX_ERROR
){
309 dprint((5, "prune_folders: Error testing existence\n"));
312 else if(exists
== FEX_NOENT
){ /* doesn't exist */
313 dprint((5, "prune_folders: nothing to prune <%s %s>\n",
314 prune_cntxt
->context
? prune_cntxt
->context
: "?",
315 folder_base
? folder_base
: "?"));
318 else if(!(exists
& FEX_ISFILE
)
319 || pr
== PRUNE_NO_AND_ASK
|| pr
== PRUNE_NO_AND_NO
320 || ((pr
== PRUNE_ASK_AND_ASK
|| pr
== PRUNE_ASK_AND_NO
) &&
321 want_to(prompt
, 'n', 0, h_wt_expire
, WT_FLUSH_IN
) == 'n')){
322 dprint((5, "User declines renaming %s\n",
323 ps_global
->VAR_DEFAULT_FCC
? ps_global
->VAR_DEFAULT_FCC
: "?"));
327 prune_move_folder(folder_base
, path2
, prune_cntxt
);
330 if(pr
== PRUNE_ASK_AND_ASK
|| pr
== PRUNE_YES_AND_ASK
331 || pr
== PRUNE_NO_AND_ASK
)
332 delete_old_mail(mail_list
, prune_cntxt
, type
);
334 if((sm
= mail_list
) != NULL
){
336 fs_give((void **)&(sm
->name
));
340 fs_give((void **)&mail_list
);
347 /*----------------------------------------------------------------------
348 Offer to delete old sent-mail folders
350 Args: sml -- The list of sent-mail folders
351 fcc_cntxt -- context to delete list of folders in
352 type -- label indicating type of folders being deleted
356 delete_old_mail(struct sm_folder
*sml
, CONTEXT_S
*fcc_cntxt
, char *type
)
359 struct sm_folder
*sm
;
361 for(sm
= sml
; sm
!= NULL
&& sm
->name
!= NULL
; sm
++){
362 if(sm
->name
[0] == '\0') /* can't happen */
365 snprintf(prompt
, sizeof(prompt
),
366 "To save disk space, delete old%.10s mail folder \"%.30s\" ",
368 if(want_to(prompt
, 'n', 0, h_wt_delete_old
, WT_FLUSH_IN
) == 'y'){
370 if(!context_delete(fcc_cntxt
, NULL
, sm
->name
)){
371 q_status_message1(SM_ORDER
,
372 3, 3, "Error deleting \"%s\".", sm
->name
);
373 dprint((1, "Error context_deleting %s in %s\n", sm
->name
,
374 (fcc_cntxt
&& fcc_cntxt
->context
)
375 ? fcc_cntxt
->context
: "<null>"));
380 if((index
= folder_index(sm
->name
, fcc_cntxt
, FI_FOLDER
)) >= 0)
381 folder_delete(index
, FOLDERS(fcc_cntxt
));
384 /* break;*/ /* skip out of the whole thing when he says no */
385 /* Decided to keep asking anyway */