summaryrefslogtreecommitdiffstats
path: root/util/bbsmail.c
blob: 0afcdf7e803fc10b7fe8b7a932b10cd5f9a2a20d (plain) (blame)
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
/* $Id: bbsmail.c,v 1.2 2002/03/09 17:44:30 in2 Exp $ */

#define _BBS_UTIL_C_
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "config.h"
#include "pttstruct.h"
#include "util.h"
#include "perm.h"
#include "common.h"
#include "proto.h"

#define LOG_FILE    (BBSHOME "/etc/mailog")

#ifdef HMM_USE_ANTI_SPAM
extern char *notitle[], *nofrom[], *nocont[];
#endif

extern userec_t xuser;

int mailalertuid(int tuid)
{
    userinfo_t *uentp=NULL;
    if(tuid>0 && (uentp = (userinfo_t *)search_ulist(tuid)) )
         uentp->mailalert=1;
    return 0;
}      

void
mailog(msg)
    char *msg;
{
    FILE *fp;

    if ((fp = fopen(LOG_FILE, "a")))
    {
    time_t now;
    struct tm *p;

    time(&now);
    p = localtime(&now);
    fprintf(fp, "%02d/%02d/%02d %02d:%02d:%02d <bbsmail> %s\n",
        p->tm_year % 100, p->tm_mon + 1, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec,
        msg);
    fclose(fp);
    }
}


int
mail2bbs(userid)
    char *userid;
{
    int uid;
    fileheader_t mymail;
    char genbuf[256], title[80], sender[80], filename[80], *ip, *ptr;
    time_t tmp_time;
    struct stat st;
    FILE *fout;
/* check if the userid is in our bbs now */
    if (!(uid=getuser(userid)) )
    {
    sprintf(genbuf, "BBS user <%s> not existed", userid);
    puts(genbuf);
    mailog(genbuf);
    return -1;//EX_NOUSER;
    }

    if(xuser.userlevel&PERM_NOOUTMAIL)
    return -1;

    sprintf(filename, BBSHOME "/home/%c/%s", userid[0], userid);

    if (stat(filename, &st) == -1)
    {
    if (mkdir(filename, 0755) == -1)
    {
        printf("mail box create error %s \n", filename);
        return -1;
    }
    }
    else if (!(st.st_mode & S_IFDIR))
    {
    printf("mail box error\n");
    return -1;
    }

 //   printf("dir: %s\n", filename);

/* allocate a file for the new mail */

    stampfile(filename, &mymail);
   // printf("file: %s\n", filename);

/* copy the stdin to the specified file */

/* parse header */

    while (fgets(genbuf, 255, stdin))
    {
    if (!strncmp(genbuf, "From", 4))
    {
        if ((ip = strchr(genbuf, '<')) && (ptr = strrchr(ip, '>')))
        {
        *ptr = '\0';
        if (ip[-1] == ' ')
            ip[-1] = '\0';
        ptr = (char *) strchr(genbuf, ' ');
        while (*ptr == ' ') ptr++;
        sprintf(sender, "%s (%s)", ip + 1, ptr);
        }
        else
        {
        strtok(genbuf, " \t\n\r");
        ptr= strtok(NULL, " \t\n\r");
        if(ptr)
         strcpy(sender, ptr);
        }
        continue;
    }
    if (!strncmp(genbuf, "Subject: ", 9))
    {
        strcpy(title, genbuf + 9);
        continue;
    }
    if (genbuf[0] == '\n')
        break;
    }

    if ((ptr = strchr(sender, '\n')))
    *ptr = '\0';

    if ((ptr = strchr(title, '\n')))
    *ptr = '\0';

    if (strchr(sender, '@') == NULL)    /* 由 local host 寄信 */
    {
    strcat(sender, "@" MYHOSTNAME);
    }

    time(&tmp_time);

#ifdef HMM_USE_ANTI_SPAM
    for (n = 0; notitle[n]; n++)
    if (strstr(title, notitle[n]))
    {
        sprintf(genbuf, "Title <%s> not accepted", title);
        puts(genbuf);
        mailog(genbuf);
        return -1;
    }   
    for (n = 0; nofrom[n]; n++)
    if (strstr(sender, nofrom[n]))
    {
        sprintf(genbuf, "From <%s> not accepted", sender);
        puts(genbuf);
        mailog(genbuf);
        return -1;
    }   
#endif

    if ((fout = fopen(filename, "w")) == NULL)
    {
    printf("Cannot open %s\n", filename);
    return -1;
    }

    if (!title[0])
    sprintf(title, "來自 %.64s", sender);
    title[TTLEN] = 0;
    fprintf(fout, "作者: %s\n標題: %s\n時間: %s\n",
        sender, title, ctime(&tmp_time));

    while (fgets(genbuf, 255, stdin))
    {
#ifdef HMM_USE_ANTI_SPAM
    for (n = 0; nocont[n]; n++)
        if (strstr(genbuf, nocont[n]))
        {
        fclose(fout);
        unlink(filename);
        sprintf(genbuf, "Content <%s> not accepted", nocont[n]);
        puts(genbuf);
        mailog(genbuf);
        return -1;
        }
#endif
    fputs(genbuf, fout);
    }
    fclose(fout);

    sprintf(genbuf, "%s => %s", sender, userid);
    mailog(genbuf);

/* append the record to the MAIL control file */

    strcpy(mymail.title, title);

    if (strtok(sender, " .@\t\n\r"))
    strcat(sender, ".");
    sender[IDLEN + 1] = '\0';
    strcpy(mymail.owner, sender);

    sprintf(genbuf, BBSHOME "/home/%c/%s/.DIR", userid[0], userid);
    mailalertuid(uid);
    return append_record(genbuf, &mymail, sizeof(mymail));
}


int
main(int argc, char* argv[])
{
    char receiver[256];

/* argv[1] is userid in bbs   */

    if (argc < 2)
    {
    printf("Usage:\t%s <bbs_uid>\n", argv[0]);
    exit(-1);
    }
    (void) setgid(BBSGID);
    (void) setuid(BBSUID);

    if(passwd_mmap()) exit(-1);
    strcpy(receiver, argv[1]);

    strtok(receiver,".");
    if (mail2bbs(receiver))
    {
    /* eat mail queue */
    while (fgets(receiver, sizeof(receiver), stdin)) ;
    }
    return 0;
}