]> diplodocus.org Git - nmh/blob - docs/historical/SRI-NOSC/pickup.c
Remove the #ifdef UK for the default scan format
[nmh] / docs / historical / SRI-NOSC / pickup.c
1 #include "mh.h"
2 #include "iobuf.h"
3 #include "stat.h"
4 #include "errors.h"
5
6 char *anoyes[], /* Std no/yes gans array */
7 *mypath,
8 mailbox[],
9 defalt[];
10
11 char scanl[];
12 struct msgs *mp;
13 struct iobuf in, fout, *aud;
14 struct inode stbuf;
15
16 struct swit switches[] {
17 "audit audit-file", 0, /* 0 */
18 "ms ms-folder", 0, /* 1 */
19 "help", 4, /* 2 */
20 0, 0
21 };
22
23 main(argc, argv)
24 char *argv[];
25 {
26
27 char newmail[128], maildir[128], *folder, *from, *audfile;
28 register char *cp;
29 register int i, msgnum;
30 long now;
31 char *ap, *inp;
32 char *arguments[50], **argp;
33
34 fout.b_fildes = dup(1);
35 #ifdef NEWS
36 m_news();
37 #endif
38 from = folder = audfile = 0;
39 ap = cp = argv[0];
40 while(*cp)
41 if(*cp++ == '/')
42 ap = cp;
43 inp = ap;
44 if((cp = m_find(ap)) != -1) {
45 ap = brkstring(cp = getcpy(cp), " ", "\n");
46 ap = copyip(ap, arguments);
47 } else
48 ap = arguments;
49 copyip(argv+1, ap);
50 argp = arguments;
51 while(cp = *argp++) {
52 if(*cp == '-')
53 switch(smatch(++cp, switches)) {
54 case -2:ambigsw(cp, switches); /* ambiguous */
55 goto leave;
56 /* unknown */
57 case -1:printf("-%s unknown\n", cp);
58 goto leave;
59 case 0: if(!(audfile = *argp++)) { /* -audit */
60 missing: printf("Missing argument for %s switch\n", argp[-2]);
61 goto leave;
62 }
63 continue;
64 case 1: if(!(from = *argp++)) /* -ms */
65 goto missing;
66 continue;
67 case 2: /* -help */
68 help(concat( inp, " [+folder] [switches]", 0), switches);
69 goto leave;
70 }
71 if(*cp == '+') {
72 if(folder) {
73 printf("Only one folder at a time.\n");
74 goto leave;
75 } else
76 folder = cp + 1;
77 } else {
78 printf("Bad arg: %s\n", argp[-1]);
79 printf(concat("Usage: ", inp, " [+folder] [-ms ms-folder] [-audit audit-file]\n", 0));
80 goto leave;
81 }
82 }
83 if(from)
84 copy(from, newmail);
85 else {
86 mypath = getpath(getruid());
87 copy(mailbox, copy(mypath, newmail));
88 if(stat(newmail, &stbuf) < 0 ||
89 !(stbuf.i_size0 | stbuf.i_size1)) {
90 printf("No new mail.\n");
91 goto leave;
92 }
93 }
94 if(fopen(newmail, &in) < 0) {
95 printf("Can't read \"%s\"??\n", newmail);
96 goto leave;
97 }
98 if(!folder) {
99 folder = defalt;
100 if(from && !equal(from, "inbox")) {
101 cp = concat("Do you really want to convert from ",
102 from, " into ", folder, "?? ", 0);
103 if(!gans(cp, anoyes))
104 goto leave;
105 }
106 }
107 copy(m_maildir(folder), maildir);
108 if(stat(maildir, &stbuf) < 0) {
109 if(errno != ENOENT) {
110 printf("Error on folder "); flush();
111 perror(maildir);
112 goto leave;
113 }
114 /* cp = concat("Create folder \"", maildir, "\"? ", 0);
115 if(!gans(cp, anoyes))
116 goto leave; */
117 if(!makedir(maildir)) {
118 printf("Can't create folder \"%s\"\n", maildir);
119 goto leave;
120 }
121 }
122 if(chdir(maildir) < 0) {
123 printf("Can't chdir to: ");
124 perror(maildir);
125 goto leave;
126 }
127 if(!(mp = m_gmsg(folder))) {
128 printf("Can't read folder!?\n");
129 goto leave;
130 }
131 if(audfile) {
132 aud = alloc(sizeof *aud);
133 cp = m_maildir(audfile);
134 if(fopen2(cp, aud) == -1) {
135 printf("Creating Receive-Audit: %s\n", cp);
136 if(fcreat(cp, aud) == -1) {
137 printf("Can't create "); flush();
138 perror(cp);
139 goto leave;
140 }
141 chmod(cp, 0600);
142 putchar('\n');
143 } else
144 seek(aud->b_fildes, 0, 2);
145 time(&now);
146 puts("<<inc>> ", aud);
147 cp = cdate(&now);
148 cp[9] = ' ';
149 puts(cp, aud);
150 if(from) {
151 puts(" -ms ", aud);
152 puts(from, aud);
153 }
154 putc('\n', aud);
155 }
156 printf("Incorporating new mail into %s...\n\n", folder);
157 flush();
158 msgnum = mp->hghmsg;
159
160 while((i = scan(&in, msgnum+1, msgnum+1, msgnum == mp->hghmsg))) {
161 if(i == -1) {
162 printf("command aborted!\n");
163 if(aud)
164 puts("inc aborted!\n", aud);
165 goto leave;
166 }
167 if(aud)
168 puts(scanl, aud);
169 flush();
170 msgnum++;
171 }
172
173 close(in.b_fildes);
174 if(aud)
175 fflush(aud);
176
177 if(!from) {
178 if((i = creat(newmail, 0600)) >= 0) /* Zap .mail file */
179 close(i);
180 else
181 printf("Error zeroing %s\n", newmail);
182 } else
183 printf("%s not zero'd\n", newmail);
184
185 i = msgnum - mp->hghmsg;
186 /* printf("%d new message%s\n", i, i==1? "":"s"); */
187 if(!i)
188 printf("[No messages incorporated.]\n");
189 else {
190 if(!equal(m_getfolder(), folder))
191 m_replace("folder", folder);
192 m_setcur(mp->hghmsg + 1);
193 }
194 leave:
195 m_update();
196 flush();
197 }
198
199
200
201