1 /*:ts=8*/
2 /*****************************************************************************
3  * FIDOGATE --- Gateway UNIX Mail/News <-> FIDO NetMail/EchoMail
4  *
5  *
6  * Create RFC messages in mail/news dir
7  *
8  *****************************************************************************
9  * Copyright (C) 1990-2002
10  *  _____ _____
11  * |     |___  |   Martin Junius             <mj@fidogate.org>
12  * | | | |   | |   Radiumstr. 18
13  * |_|_|_|@home|   D-51069 Koeln, Germany
14  *
15  * This file is part of FIDOGATE.
16  *
17  * FIDOGATE is free software; you can redistribute it and/or modify it
18  * under the terms of the GNU General Public License as published by the
19  * Free Software Foundation; either version 2, or (at your option) any
20  * later version.
21  *
22  * FIDOGATE is distributed in the hope that it will be useful, but
23  * WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
25  * General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with FIDOGATE; see the file COPYING.  If not, write to the Free
29  * Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
30  *****************************************************************************/
31 
32 #include "fidogate.h"
33 
34 char mail_dir[MAXPATH];
35 char news_dir[MAXPATH];
36 
37 static char m_name[MAXPATH];
38 static char m_tmp[MAXPATH];
39 static FILE *m_file = NULL;
40 
41 static char n_name[MAXPATH];
42 static char n_tmp[MAXPATH];
43 static FILE *n_file = NULL;
44 
45 /*
46  * Open RFC mail file
47  */
mail_open(int sel)48 int mail_open(int sel)
49 {
50     long n;
51 
52     switch (sel) {
53     case 'm':
54     case 'M':
55         n = sequencer(cf_p_seq_mail());
56         str_printf(m_tmp, sizeof(m_tmp), "%s/%08ld.tmp", mail_dir, n);
57         str_printf(m_name, sizeof(m_name), "%s/%08ld.rfc", mail_dir, n);
58         m_file = fopen(m_tmp, W_MODE);
59         if (!m_file) {
60             fglog("$Can't create mail file %s", m_tmp);
61             return ERROR;
62         }
63         break;
64 
65     case 'n':
66     case 'N':
67         n = sequencer(cf_p_seq_news());
68         str_printf(n_tmp, sizeof(n_tmp), "%s/%08ld.tmp", news_dir, n);
69         str_printf(n_name, sizeof(n_name), "%s/%08ld.rfc", news_dir, n);
70         n_file = fopen(n_tmp, W_MODE);
71         if (!n_file) {
72             fglog("$Can't create mail file %s", n_tmp);
73             return ERROR;
74         }
75         break;
76 
77     default:
78         fglog("mail_open(%d): illegal value", sel);
79         return ERROR;
80     }
81 
82     return OK;
83 }
84 
85 /*
86  * Return mail file pointer
87  */
mail_file(int sel)88 FILE *mail_file(int sel)
89 {
90     switch (sel) {
91     case 'm':
92     case 'M':
93         return m_file;
94         break;
95     case 'n':
96     case 'N':
97         return n_file;
98         break;
99     }
100 
101     return NULL;
102 }
103 
104 /*
105  * Close mail file
106  */
mail_close(int sel)107 void mail_close(int sel)
108 {
109 
110     switch (sel) {
111     case 'm':
112     case 'M':
113         fclose(m_file);
114         if (rename(m_tmp, m_name) == ERROR)
115             fglog("$Can't rename mail file %s to %s", m_tmp, m_name);
116 
117         m_tmp[0] = 0;
118         m_name[0] = 0;
119         m_file = NULL;
120         break;
121 
122     case 'n':
123     case 'N':
124         fclose(n_file);
125         if (rename(n_tmp, n_name) == ERROR)
126             fglog("$Can't rename mail file %s to %s", n_tmp, n_name);
127 
128         n_tmp[0] = 0;
129         n_name[0] = 0;
130         n_file = NULL;
131         break;
132     }
133 
134     return;
135 }
136