File: menusend.c

package info (click to toggle)
mixmaster 3.0b2-4
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 1,356 kB
  • ctags: 1,173
  • sloc: ansic: 18,314; sh: 1,285; yacc: 698; perl: 314; makefile: 161
file content (556 lines) | stat: -rw-r--r-- 12,958 bytes parent folder | download | duplicates (3)
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/* Mixmaster version 3.0  --  (C) 1999 - 2004 Anonymizer Inc. and others.

   Mixmaster may be redistributed and modified under certain conditions.
   This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
   ANY KIND, either express or implied. See the file COPYRIGHT for
   details.

   Menu-based user interface -- send message
   $Id: menusend.c 794 2004-05-02 23:52:34Z weasel $ */


#include "menu.h"
#include "mix3.h"
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#ifdef POSIX
#include <unistd.h>
#else /* end of POSIX */
#include <io.h>
#endif /* else if not POSIX */

void send_message(int type, char *nym, BUFFER *in)
{
  char dest[LINELEN] = "", subject[LINELEN] = "";
  char chain[CHAINMAX], thisnym[LINELEN], path[PATHMAX];
  BUFFER *chainlist, *msg, *txt, *tmp, *field, *content, *cc, *cite;
  int numcopies;
  int hdr = 0;			/* txt buffer contains header lines */
  FILE *f;
  int n, err;

#ifdef USE_PGP
  int sign = 0, encrypt = 0, key = 0;

#endif /* USE_PGP */
#ifdef USE_NCURSES
  char reliability[9];
  int c;
  char line[LINELEN];

#endif /* USE_NCURSES */
  msg = buf_new();
  tmp = buf_new();
  txt = buf_new();
  field = buf_new();
  content = buf_new();
  chainlist = buf_new();
  cc = buf_new();
  cite = buf_new();
  strncpy(chain, CHAIN, CHAINMAX);
  numcopies = NUMCOPIES;

  mix_status("");
  strncpy(thisnym, nym, sizeof(thisnym));

  if (in != NULL)
    buf_set(txt, in);

  if (bufileft(txt, "From "))
    buf_getline(txt, field);	/* ignore envelope From */

  if (type == 'p' || type == 'm') {
#ifndef USE_NCURSES
    mix_status("Invalid option to -f");
    mix_exit();
    exit(1);
#else /* end of not USE_NCURSES */
    clear();
    echo();
    if (in != NULL)
      mvprintw(1, 0, "%s forwarding message...", thisnym);
    if (type == 'p')
      mvprintw(3, 0, "Newsgroups: ");
    else
      mvprintw(3, 0, "Send message to: ");
    refresh();
    wgetnstr(stdscr, dest, LINELEN);
    if (dest[0] == '\0') {
      noecho();
      cl(3, 0);
      goto quit;
    }
    if (txt->length == 0) {
      mvprintw(4, 0, "Subject: ");
      refresh();
      wgetnstr(stdscr, subject, LINELEN);
    } else {
      strcpy(subject, "Forwarded message");
      while (buf_getheader(txt, field, content) == 0) {
	if (bufieq(field, "subject")) {
	  strncpy(subject, content->data, sizeof(subject));
	  strcatn(subject, " (fwd)", sizeof(subject));
	}
	if (bufieq(field, "from") || bufieq(field, "subject") ||
	    bufieq(field, "date"))
	  buf_appendheader(tmp, field, content);
      }
      buf_nl(tmp);
      buf_rest(tmp, txt);
      buf_move(txt, tmp);
    }
    noecho();
#endif /* else if USE_NCURSES */
  } else {
    strcpy(subject, "Re: your mail");
    while (buf_getheader(txt, field, content) == 0) {
      if (bufieq(field, "subject")) {
	if (bufileft(content, "Re:"))
	  subject[0] = '\0';
	else
	  strcpy(subject, "Re: ");
	strcatn(subject, content->data, sizeof(subject));
      }
      if (bufieq(field, "from"))
	buf_set(cite, content);
      if (type == 'p' || type == 'f') {
	if (dest[0] == '\0' && bufieq(field, "newsgroups"))
	  strncpy(dest, content->data, sizeof(dest));
	if (bufieq(field, "followup-to") && !bufieq(content, "poster"))
	  strncpy(dest, content->data, sizeof(dest));
	if (bufieq(field, "message-id"))
	  buf_appendf(tmp, "References: %b\n", content);
      } else {
	if (dest[0] == '\0' && bufieq(field, "from"))
	  strncpy(dest, content->data, sizeof(dest));
	if (bufieq(field, "reply-to"))
	  strncpy(dest, content->data, sizeof(dest));
	if (type == 'g' && (bufieq(field, "to") || bufieq(field, "cc"))) {
	  if (cc->length)
	    buf_appends(cc, ", ");
	  buf_cat(cc, content);
	}
	if (bufieq(field, "message-id"))
	  buf_appendf(tmp, "In-Reply-To: %b\n", content);
      }
    }
    if (cc->length)
      buf_appendf(tmp, "Cc: %b\n", cc);
    if (tmp->length > 0)
      hdr = 1;
    if (hdr)
      buf_nl(tmp);

    if ((type == 'f' || type == 'g') && cite->length) {
      buf_appendf(tmp, "%b wrote:\n\n", cite);
    }
    if (type == 'r')
      buf_appends(tmp, "You wrote:\n\n");

    while (buf_getline(txt, content) != -1)
      buf_appendf(tmp, "> %b\n", content);
    buf_set(txt, tmp);
    if (dest[0] == '\0') {
#ifdef USE_NCURSES
      beep();
      mix_status("No recipient address found.");
#endif /* USE_NCURSES */
      goto quit;
    }
    goto edit;
  }

#ifdef USE_NCURSES
redraw:
  clear();

  for (;;) {
    standout();
    mvprintw(0, 0, "Mixmaster %s - ", VERSION);
    printw(type == 'p' || type == 'f' ? "posting to Usenet" : "sending mail");
    standend();
    mix_status(NULL);
    cl(2, 0);
#ifdef NYMSUPPORT
    printw("n)ym: %s", thisnym);
#endif /* NYMSUPPORT */
    if (!strleft(thisnym, NONANON)) {
      chain_reliability(chain, 0, reliability);   /* chaintype 0=mix */
      cl(4, 0);
      printw("c)hain: %-35s (reliability: %s)", chain, reliability);
      cl(5, 0);
      printw("r)edundancy: %3d copies ", numcopies);
    }
    cl(7, 0);
    printw("d)estination: %s", dest);
    cl(8, 0);
    printw("s)ubject: %s", subject);
#ifdef USE_PGP
    if (type != 'p' && type != 'f') {
      cl(10, 0);
      printw("pgp encry)ption: ");
      if (encrypt)
	printw("yes");
      else
	printw("no");
    }
    if (!streq(thisnym, ANON)) {
      cl(11, 0);
      printw("p)gp signature: ");
      if (sign)
	printw("yes");
      else
	printw("no");
      cl(12, 0);
      if (key == 0)
	printw("attach pgp k)ey: no");
    }
#endif /* USE_PGP */

    if (txt->length == 0)
      mvprintw(LINES - 3, 18,
	       "e)dit message           f)ile      q)uit w/o sending");
    else
      mvprintw(LINES - 3, 0,
	       "m)ail message      e)dit message         f)ile          q)uit w/o sending");
    move(LINES - 1, COLS - 1);
    refresh();
    c = getch();
    if (c != ERR) {
      mix_status("");
      if (c == '\r' || c == '\n') {	/* default action is edit or mail */
	if (txt->length == 0)
	  c = 'e';
	else
	  c = 'm';
      }
      switch (c) {
#ifdef NYMSUPPORT
      case 'n':
	menu_nym(thisnym);
	goto redraw;
#endif /* NYMSUPPORT */
      case '\014':
	goto redraw;
      case 'd':
	echo();
	cl(LINES - 3, 20);
	cl(7, 14);
	wgetnstr(stdscr, dest, LINELEN);
	noecho();
	break;
      case 's':
	echo();
	cl(LINES - 3, 20);
	cl(8, 10);
	wgetnstr(stdscr, subject, LINELEN);
	noecho();
	break;
      case 'c':
	menu_chain(chain, 0, (type == 'p' || type == 'f')
		   && streq(thisnym, ANON));
	goto redraw;
      case 'r':
	echo();
	cl(LINES - 5, 20);
	cl(5, 13);
	wgetnstr(stdscr, line, LINELEN);
	numcopies = strtol(line, NULL, 10);
	if (numcopies < 1 || numcopies > 10)
	  numcopies = 1;
	noecho();
	break;
      case 'f':
	cl(LINES - 3, 0);
	askfilename(path);
	cl(LINES - 3, 0);
	if (txt->length) {
	  buf_sets(tmp, path);
	  buf_clear(msg);
	  if (!hdr)
	    buf_nl(msg);
	  buf_cat(msg, txt);
	  if (attachfile(msg, tmp) == -1)
	    beep();
	  else {
	    buf_move(txt, msg);
	    hdr = 1;
	  }
	} else {
	  if ((f = fopen(path, "r")) != NULL) {
	    buf_clear(txt);
	    buf_read(txt, f);
	    fclose(f);
	  } else
	    beep();
	}
	break;
      case 'e':
#endif /* USE_NCURSES */
	{
	  int linecount;

	edit:
	  linecount = 1;
	  sprintf(path, "%s%cx%02x%02x%02x%02x.txt", POOLDIR, DIRSEP,
		  rnd_byte(), rnd_byte(), rnd_byte(), rnd_byte());
	  f = fopen(path, "w");
	  if (f == NULL) {
#ifdef USE_NCURSES
	    beep();
#endif /* USE_NCURSES */
	  } else {
	    if (type == 'f' || type == 'p')
	      fprintf(f, "Newsgroups: %s\n", dest);
	    if (type == 'r' || type == 'g' || type == 'm')
	      fprintf(f, "To: %s\n", dest);
	    fprintf(f, "Subject: %s\n", subject);
	    linecount += 2;
	    if (hdr)
	      while (buf_getline(txt, NULL) == 0) linecount++;
	    else
	      fprintf(f, "\n");
	    linecount++;
	    if (txt->length == 0)
	      fprintf(f, "\n");

	    buf_write(txt, f);
	    fclose(f);
	  }

	  menu_spawn_editor(path, linecount);

	  f = fopen(path, "r");
	  if (f == NULL) {
#ifdef USE_NCURSES
	    clear();
	    beep();
	    continue;
#else /* end of USE_NCURSES */
	    goto quit;
#endif /* else if not USE_NCURSES */
	  }
	  buf_reset(txt);
	  hdr = 0;

	  buf_reset(tmp);
	  buf_read(tmp, f);
	  while (buf_getheader(tmp, field, content) == 0) {
	    if (bufieq(field, "subject"))
	      strncpy(subject, content->data,
		      sizeof(subject));
	    else if ((type == 'p' || type == 'f') &&
		     bufieq(field, "newsgroups"))
	      strncpy(dest, content->data, sizeof(dest));
	    else if (bufieq(field, "to"))
	      strncpy(dest, content->data, sizeof(dest));
	    else {
	      buf_appendheader(txt, field, content);
	      hdr = 1;
	    }
	  }
	  if (hdr)
	    buf_nl(txt);
	  buf_rest(txt, tmp);

	  fclose(f);
	  unlink(path);
	  strcatn(path, "~", PATHMAX);
	  unlink(path);
#ifndef USE_NCURSES
	  {
	    char line[4];

	    fprintf(stderr, "Send message [y/n]? ");
	    scanf("%3s", line);
	    if (!strleft(line, "y"))
	      goto quit;
	  }
#else /* end of not USE_NCURSES */
	  goto redraw;
	}
	break;
      case 'm':
	if (txt->length == 0)
	  beep();
	else if (dest[0] == '\0') {
	  mix_status("No destination given.");
	  goto redraw;
	} else {
	  mix_status("Creating message...");
#endif /* else if USE_NCURSES */
	  buf_reset(msg);

	  if (type == 'p' || type == 'f')
	    buf_appends(msg, "Newsgroups: ");
	  else
	    buf_appends(msg, "To: ");
	  buf_appends(msg, dest);
	  buf_nl(msg);
	  buf_appends(msg, "Subject: ");
	  if (subject[0] == '\0')
	    buf_appends(msg, "(no subject)");
	  else
	    buf_appends(msg, subject);
	  buf_nl(msg);
	  if (!hdr)
	    buf_nl(msg);
	  buf_cat(msg, txt);
#ifdef USE_PGP
	  {
	    BUFFER *p;

	    p = buf_new();
	    if (streq(thisnym, ANON))
	      sign = 0;
	    if (sign || (key && !strileft(thisnym, NONANON)))
	      user_pass(p);

	    if (encrypt || sign) {
	      if (pgp_mailenc((encrypt ? PGP_ENCRYPT : 0)
			      | (sign ? PGP_SIGN : 0) | PGP_TEXT
			      | (strleft(thisnym, NONANON) ? 0 : PGP_REMAIL),
			      msg, strleft(thisnym, NONANON) ?
			      ADDRESS : thisnym, p, PGPPUBRING,
			      strleft(thisnym, NONANON) ?
			      PGPSECRING : NYMSECRING) == -1) {
		mix_genericerror();
#ifdef USE_NCURSES
		beep();
		goto redraw;
#endif /* USE_NCURSES */
	      }
	    }
	    buf_free(p);
	  }
#endif /* USE_PGP */

	  if (strleft(thisnym, NONANON)) {
	    FILE *f = NULL;

	    if (type == 'p' || type == 'f') {
	      if (strchr(NEWS, '@')) {
		/*  NOT_IMPLEMENTED; */
	      } else
		f = openpipe(NEWS);
	    } else {
	      if (NAME[0]) {
		buf_sets(tmp, NAME);
		buf_appends(tmp, " <");
		buf_appends(tmp, ADDRESS);
		buf_appends(tmp, ">");
	      } else
		buf_sets(tmp, ADDRESS);
	      mail_encode(msg, 0);
	      if (sendmail(msg, tmp->data, NULL) != 0) {
#ifdef USE_NCURSES
		clear();
#endif /* USE_NCURSES */
		mix_status("Error sending message.");
#ifdef USE_NCURSES
		goto redraw;
#else /* end of USE_NCURSES */
		goto quit;
#endif /* else if not USE_NCURSES */
	      }
	    }
#ifdef USE_NCURSES
	    clear();
#endif /* USE_NCURSES */
	    mix_status("Message sent non-anonymously.");
	    goto quit;
	  } else {
#ifdef USE_PGP
#ifdef NYMSUPPORT
	    if (!streq(thisnym, ANON)) {
	      if (nym_encrypt(msg, thisnym, (type == 'p' || type == 'f') ?
			      MSG_POST : MSG_MAIL) == 0)
		type = 'm';
	    }
#endif /* NYMSUPPORT */
#endif /* USE_PGP */
	    err = mix_encrypt((type == 'p' || type == 'f') ?
			      MSG_POST : MSG_MAIL,
			      msg, chain, numcopies, chainlist);
	    if (err == 0) {
#ifdef USE_NCURSES
	      clear();
#endif /* USE_NCURSES */
	      for (n = 0; buf_getline(chainlist, tmp) == 0; n++) ;
	      if (n > 1)
		mix_status("Done. (%d packets)", n);
	      else
		mix_status("Chain: %s", chainlist->data);
	      goto quit;
	    } else {
#ifdef USE_NCURSES
	      beep();
#endif /* USE_NCURSES */
	      if (chainlist->length)
		mix_status("%s", chainlist->data);
	      else
		mix_genericerror();
	    }
	  }
	}
#ifdef USE_NCURSES
	break;
      case 'q':
      case 'Q':
	clear();
	goto quit;
#ifdef USE_PGP
      case 'p':
	if (!streq(thisnym, ANON))
	  sign = !sign;
	break;
      case 'y':
	encrypt = !encrypt;
	break;
      case 'k':
	if (!streq(thisnym, ANON)) {
	  BUFFER *p, *keytxt, *uid;

	  key = 1;
	  p = buf_new();
	  keytxt = buf_new();
	  uid = buf_new();

	  buf_appendf(uid, "<%s>", strleft(thisnym, NONANON) ? ADDRESS :
		      thisnym);
	  user_pass(p);
	  pgp_pubkeycert(uid, strleft(thisnym, NONANON) ?
			 PGPSECRING : NYMSECRING, p, keytxt, PGP_ARMOR_NYMKEY);

	  buf_clear(msg);
	  if (!hdr)
	    buf_nl(msg);
	  buf_cat(msg, txt);
	  buf_sets(p, "application/pgp-keys");
	  mime_attach(msg, keytxt, p);
	  hdr = 1;
	  buf_move(txt, msg);

	  buf_free(p);
	  buf_free(keytxt);
	  buf_free(uid);
	}
	break;
#endif /* USE_PGP */
      default:
	beep();
      }
    }
  }
#endif /* USE_NCURSES */
quit:
  buf_free(cc);
  buf_free(cite);
  buf_free(msg);
  buf_free(txt);
  buf_free(field);
  buf_free(content);
  buf_free(chainlist);
  buf_free(tmp);
}