summaryrefslogtreecommitdiffstats
path: root/exim_monitor/em_menu.c
blob: 2a70a1831b0ae8f159223344e7d845f0b9c4cf88 (plain)
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
/*************************************************
*                  Exim Monitor                  *
*************************************************/

/* Copyright (c) University of Cambridge 1995 - 2018 */
/* See the file NOTICE for conditions of use and distribution. */


#include "em_hdr.h"

/* This module contains code for handling the popup menus. */

static Widget menushell;
static Widget queue_text_sink;
static Widget dialog_shell, dialog_widget;

static Widget text_create(uschar *, int);

static int highlighted_start, highlighted_end, highlighted_x, highlighted_y;



static Arg queue_get_arg[] = {
  { "textSink",   (XtArgVal)NULL },
  { "textSource", (XtArgVal)NULL },
  { "string",     (XtArgVal)NULL } };

static Arg dialog_arg[] = {
  { "label",      (XtArgVal)"dialog" },
  { "value",      (XtArgVal)"value" } };

static Arg get_pos_args[] = {
  {"x",           (XtArgVal)NULL },
  {"y",           (XtArgVal)NULL } };

static Arg menushell_arg[] = {
  { "label",      (XtArgVal)NULL } };

static Arg button_arg[] = {
  { XtNfromVert, (XtArgVal) NULL },         /* must be first */
  { XtNlabel,    (XtArgVal) " Dismiss " },
  { "left",      XawChainLeft },
  { "right",     XawChainLeft },
  { "top",       XawChainBottom },
  { "bottom",    XawChainBottom } };

static Arg text_arg[] = {
  { XtNfromVert, (XtArgVal) NULL },         /* must be first */
  { "editType",  XawtextEdit },
  { "string",    (XtArgVal)"" },            /* dummy to get it going */
  { "scrollVertical", XawtextScrollAlways },
  { "wrap",      XawtextWrapWord },
  { "top",       XawChainTop },
  { "bottom",    XawChainBottom } };

static Arg item_1_arg[] = {
  { XtNfromVert,  (XtArgVal)NULL },         /* must be first */
  { "label",      (XtArgVal)" Message log" } };

static Arg item_2_arg[] = {
  { XtNfromVert,  (XtArgVal) NULL },        /* must be first */
  { "label",      (XtArgVal)" Headers" } };

static Arg item_3_arg[] = {
  { XtNfromVert,  (XtArgVal) NULL },        /* must be first */
  { "label",      (XtArgVal)" Body" } };

static Arg item_4_arg[] = {
  { XtNfromVert,  (XtArgVal) NULL },        /* must be first */
  { "label",      (XtArgVal)" Deliver message" } };

static Arg item_5_arg[] = {
  { XtNfromVert,  (XtArgVal) NULL },        /* must be first */
  { "label",      (XtArgVal)" Freeze message" } };

static Arg item_6_arg[] = {
  { XtNfromVert,  (XtArgVal) NULL },        /* must be first */
  { "label",      (XtArgVal)" Thaw message" } };

static Arg item_7_arg[] = {
  { XtNfromVert,  (XtArgVal) NULL },        /* must be first */
  { "label",      (XtArgVal)" Give up on msg" } };

static Arg item_8_arg[] = {
  { XtNfromVert,  (XtArgVal) NULL },        /* must be first */
  { "label",      (XtArgVal)" Remove message" } };

static Arg item_9_arg[] = {
  { XtNfromVert,  (XtArgVal) NULL },        /* must be first */
  { "label",      (XtArgVal)"----------------" } };

static Arg item_10_arg[] = {
  { XtNfromVert,  (XtArgVal) NULL },        /* must be first */
  { "label",      (XtArgVal)" Add recipient" } };

static Arg item_11_arg[] = {
  { XtNfromVert,  (XtArgVal) NULL },        /* must be first */
  { "label",      (XtArgVal)" Mark delivered" } };

static Arg item_12_arg[] = {
  { XtNfromVert,  (XtArgVal) NULL },        /* must be first */
  { "label",      (XtArgVal)" Mark all delivered" } };

static Arg item_13_arg[] = {
  { XtNfromVert,  (XtArgVal) NULL },        /* must be first */
  { "label",      (XtArgVal)" Edit sender" } };

static Arg item_99_arg[] = {
  { XtNfromVert,  (XtArgVal) NULL },        /* must be first */
  { "label",      (XtArgVal)" " } };



/*************************************************
*        Destroy the menu when popped down       *
*************************************************/

static void popdownAction(Widget w, XtPointer client_data, XtPointer call_data)
{
client_data = client_data;    /* Keep picky compilers happy */
call_data = call_data;
if (highlighted_x >= 0)
  XawTextSinkDisplayText(queue_text_sink,
    highlighted_x, highlighted_y,
    highlighted_start, highlighted_end, 0);
XtDestroyWidget(w);
menu_is_up = FALSE;
}



/*************************************************
*          Display the message log               *
*************************************************/

static void
msglogAction(Widget w, XtPointer client_data, XtPointer call_data)
{
int i;
Widget text = text_create(US client_data, text_depth);
uschar * fname = NULL;
FILE * f = NULL;

w = w;      /* Keep picky compilers happy */
call_data = call_data;

/* End up with the split version, so message looks right when non-exist */

for (i = 0; i < (spool_is_split ? 2:1); i++)
  {
  message_subdir[0] = i != 0 ? (US client_data)[5] : 0;
  fname = spool_fname(US"msglog", message_subdir, US client_data, US"");
  if ((f = fopen(CS fname, "r")))
    break;
  }

if (!f)
  text_showf(text, "%s: %s\n", fname, strerror(errno));
else
  {
  uschar buffer[256];
  while (Ufgets(buffer, sizeof(buffer), f) != NULL) text_show(text, buffer);
  fclose(f);
  }
}



/*************************************************
*          Display the message body               *
*************************************************/

static void
bodyAction(Widget w, XtPointer client_data, XtPointer call_data)
{
int i;
Widget text = text_create(US client_data, text_depth);
FILE *f = NULL;

w = w;      /* Keep picky compilers happy */
call_data = call_data;

for (i = 0; i < (spool_is_split? 2:1); i++)
  {
  uschar * fname;
  message_subdir[0] = i != 0 ? (US client_data)[5] : 0;
  fname = spool_fname(US"input", message_subdir, US client_data, US"-D");
  if ((f = fopen(CS fname, "r")))
    break;
  }

if (f == NULL)
  text_showf(text, "Failed to open file: %s\n", strerror(errno));
else
  {
  uschar buffer[256];
  int count = 0;

  while (Ufgets(buffer, sizeof(buffer), f) != NULL)
    {
    text_show(text, buffer);
    count += Ustrlen(buffer);
    if (count > body_max)
      {
      text_show(text, US"\n*** Message length exceeds BODY_MAX ***\n");
      break;
      }
    }
  fclose(f);
  }
}



/*************************************************
*        Do something to a message               *
*************************************************/

/* The output is not shown in a window for non-delivery actions that succeed,
unless action_output is set. We can't, however, tell until we have run
the command whether we want the output or not, so the pipe has to be set up in
all cases. */

static void ActOnMessage(uschar *id, uschar *action, uschar *address_arg)
{
int pid;
int pipe_fd[2];
int delivery = Ustrcmp(action + Ustrlen(action) - 2, "-M") == 0;
uschar *quote = US"";
uschar *at = US"";
uschar *qualify = US"";
uschar buffer[256];
queue_item *qq;
Widget text = NULL;

/* If the address arg is not empty and does not contain @ and there is a
qualify domain, qualify it. (But don't qualify '<>'.)*/

if (address_arg[0] != 0)
  {
  quote = US"\'";
  if (Ustrchr(address_arg, '@') == NULL &&
      Ustrcmp(address_arg, "<>") != 0 &&
      qualify_domain != NULL &&
      qualify_domain[0] != 0)
    {
    at = US"@";
    qualify = qualify_domain;
    }
  }
sprintf(CS buffer, "%s %s %s %s %s %s%s%s%s%s", exim_path,
  (alternate_config == NULL)? US"" : US"-C",
  (alternate_config == NULL)? US"" : alternate_config,
  action, id, quote, address_arg, at, qualify, quote);

/* If we know we are going to need the window, create it now. */

if (action_output || delivery)
  {
  text = text_create(id, text_depth);
  text_showf(text, "%s\n", buffer);
  }

/* Create the pipe for output. Remember, on most systems pipe[0] is
for reading and pipe[1] is for writing! Solaris, with its two-way
pipes is a trap! */

if (pipe(pipe_fd) != 0)
  {
  if (text == NULL)
    {
    text = text_create(id, text_depth);
    text_showf(text, "%s\n", buffer);
    }
  text_show(text, US"*** Failed to create pipe ***\n");
  return;
  }

if (  fcntl(pipe_fd[0], F_SETFL, O_NONBLOCK)
   || fcntl(pipe_fd[1], F_SETFL, O_NONBLOCK))
  {
  perror("set nonblocking on pipe");
  exit(1);
  }

/* Delivering a message can take some time, and we want to show the
output as it goes along. This requires subprocesses and is coded below. For
other commands, we can assume an immediate response, and so need not waste
resources with subprocesses. If action_output is FALSE, don't show the
output at all. */

if (!delivery)
  {
  int count, rc;
  int save_stdout = dup(1);
  int save_stderr = dup(2);

  close(1);
  close(2);

  dup2(pipe_fd[1], 1);
  dup2(pipe_fd[1], 2);
  close(pipe_fd[1]);

  rc = system(CS buffer);

  close(1);
  close(2);

  if (action_output || rc != 0)
    {
    if (text == NULL)
      {
      text = text_create(id, text_depth);
      text_showf(text, "%s\n", buffer);
      }
    while ((count = read(pipe_fd[0], buffer, 254)) > 0)
      {
      buffer[count] = 0;
      text_show(text, buffer);
      }
    }

  close(pipe_fd[0]);

  dup2(save_stdout, 1);
  dup2(save_stderr, 2);
  close(save_stdout);
  close(save_stderr);

  /* If action was to change the sender, and it succeeded, we have to
  update the in-store data. */

  if (rc == 0 && Ustrcmp(action + Ustrlen(action) - 4, "-Mes") == 0)
    {
    queue_item *q = find_queue(id, queue_noop, 0);
    if (q)
      {
      if (q->sender) store_free(q->sender);
      q->sender = store_malloc(Ustrlen(address_arg) + 1);
      Ustrcpy(q->sender, address_arg);
      }
    }

  /* If configured, cause a display update and return */

  if (action_queue_update) tick_queue_accumulator = 999999;
  return;
  }

/* Message is to be delivered. Ensure that it is marked unfrozen,
because nothing will get written to the log to show that this has
happened. (Other freezing/unfreezings get logged and picked up from
there.) */

qq = find_queue(id, queue_noop, 0);
if (qq != NULL) qq->frozen = FALSE;

/* New, asynchronous code runs in a subprocess for commands that
will take some time. The main process does not wait. There is a
SIGCHLD handler in the main program that cleans up any terminating
sub processes. */

if ((pid = fork()) == 0)
  {
  close(1);
  close(2);

  dup2(pipe_fd[1], 1);
  dup2(pipe_fd[1], 2);
  close(pipe_fd[1]);

  system(CS buffer);

  close(1);
  close(2);
  close(pipe_fd[0]);
  _exit(0);
  }

/* Main process - set up an item for the main ticker to watch. */

if (pid < 0) text_showf(text, "Failed to fork: %s\n", strerror(errno)); else
  {
  pipe_item *p = (pipe_item *)store_malloc(sizeof(pipe_item));

  if (p == NULL)
    {
    text_show(text, US"Run out of store\n");
    return;
    }

  p->widget = text;
  p->fd = pipe_fd[0];

  p->next = pipe_chain;
  pipe_chain = p;

  close(pipe_fd[1]);
  }
}




/*************************************************
*        Cause a message to be delivered         *
*************************************************/

static void deliverAction(Widget w, XtPointer client_data, XtPointer call_data)
{
w = w;      /* Keep picky compilers happy */
call_data = call_data;
ActOnMessage(US client_data, US"-v -M", US"");
}



/*************************************************
*        Cause a message to be Frozen            *
*************************************************/

static void freezeAction(Widget w, XtPointer client_data, XtPointer call_data)
{
w = w;      /* Keep picky compilers happy */
call_data = call_data;
ActOnMessage(US client_data, US"-Mf", US"");
}



/*************************************************
*        Cause a message to be thawed            *
*************************************************/

static void thawAction(Widget w, XtPointer client_data, XtPointer call_data)
{
w = w;      /* Keep picky compilers happy */
call_data = call_data;
ActOnMessage(US client_data, US"-Mt", US"");
}



/*************************************************
*          Take action using dialog data         *
*************************************************/

/* This function is called after a dialog box has been filled
in. It is global because it is set up in the action table at
start-up time. If the string is empty, do nothing. */

XtActionProc dialogAction(Widget w, XEvent *event, String *ss, Cardinal *c)
{
uschar *s = US XawDialogGetValueString(dialog_widget);

w = w;      /* Keep picky compilers happy */
event = event;
ss = ss;
c = c;

XtPopdown((Widget)dialog_shell);
XtDestroyWidget((Widget)dialog_shell);
while (isspace(*s)) s++;
if (s[0] != 0)
  {
  if (actioned_message[0] != 0)
    ActOnMessage(actioned_message, action_required, s);
  else
    NonMessageDialogue(s);    /* When called from somewhere else */
  }
return NULL;
}



/*************************************************
*              Create a dialog box               *
*************************************************/

/* The focus is grabbed exclusively, so nothing else can
be done to the application until the box is filled in. This
function is also used by the Hide button handler. */

void create_dialog(uschar *label, uschar *value)
{
Arg warg[4];
Dimension x, y, xx, yy;
XtTranslations pop_trans;
Widget text;

/* Get the position of a reference widget so the dialog box can be put
near to it. */

get_pos_args[0].value = (XtArgVal)(&x);
get_pos_args[1].value = (XtArgVal)(&y);
XtGetValues(dialog_ref_widget, get_pos_args, 2);

/* When this is not a message_specific thing, the position of the reference
widget is relative to the window. Get the position of the top level widget and
add to the position. */

if (dialog_ref_widget != menushell)
  {
  get_pos_args[0].value = (XtArgVal)(&xx);
  get_pos_args[1].value = (XtArgVal)(&yy);
  XtGetValues(toplevel_widget, get_pos_args, 2);
  x += xx;
  y += yy;
  }

/* Create a transient shell for the dialog box. */

XtSetArg(warg[0], XtNtransientFor, queue_widget);
XtSetArg(warg[1], XtNx, x + 50);
XtSetArg(warg[2], XtNy, y + 50);
XtSetArg(warg[3], XtNallowShellResize, True);
dialog_shell = XtCreatePopupShell("forDialog", transientShellWidgetClass,
   toplevel_widget, warg, 4);

/* Create the dialog box. */

dialog_arg[0].value = (XtArgVal)label;
dialog_arg[1].value = (XtArgVal)value;
dialog_widget = XtCreateManagedWidget("dialog", dialogWidgetClass, dialog_shell,
  dialog_arg, XtNumber(dialog_arg));

/* Get the text widget from within the dialog box, give it the keyboard focus,
make it wider than the default, and override its translations to make Return
call the dialog action function. */

text = XtNameToWidget(dialog_widget, "value");
XawTextSetInsertionPoint(text, Ustrlen(value));
XtSetKeyboardFocus(dialog_widget, text);
xs_SetValues(text, 1, "width", 200);
pop_trans = XtParseTranslationTable(
  "<Key>Return:         dialogAction()\n");
XtOverrideTranslations(text, pop_trans);

/* Pop the thing up. */

XtPopup(dialog_shell, XtGrabExclusive);
XFlush(X_display);
}





/*************************************************
*        Cause a recipient to be added           *
*************************************************/

/* This just sets up the dialog box; the action happens when it has been filled
in. */

static void addrecipAction(Widget w, XtPointer client_data, XtPointer call_data)
{
w = w;      /* Keep picky compilers happy */
call_data = call_data;
Ustrncpy(actioned_message, client_data, 24);
actioned_message[23] = '\0';
action_required = US"-Mar";
dialog_ref_widget = menushell;
create_dialog(US"Recipient address to add?", US"");
}



/*************************************************
*    Cause an address to be marked delivered     *
*************************************************/

static void markdelAction(Widget w, XtPointer client_data, XtPointer call_data)
{
w = w;      /* Keep picky compilers happy */
call_data = call_data;
Ustrncpy(actioned_message, client_data, 24);
actioned_message[23] = '\0';
action_required = US"-Mmd";
dialog_ref_widget = menushell;
create_dialog(US"Recipient address to mark delivered?", US"");
}


/*************************************************
*   Cause all addresses to be marked delivered   *
*************************************************/

static void markalldelAction(Widget w, XtPointer client_data, XtPointer call_data)
{
w = w;      /* Keep picky compilers happy */
call_data = call_data;
ActOnMessage(US client_data, US"-Mmad", US"");
}


/*************************************************
*        Edit the message's sender               *
*************************************************/

static void editsenderAction(Widget w, XtPointer client_data,
  XtPointer call_data)
{
queue_item *q;
uschar *sender;
w = w;      /* Keep picky compilers happy */
call_data = call_data;
Ustrncpy(actioned_message, client_data, 24);
actioned_message[23] = '\0';
q = find_queue(actioned_message, queue_noop, 0);
sender = !q ? US"" : q->sender[0] == 0 ? US"<>" : q->sender;
action_required = US"-Mes";
dialog_ref_widget = menushell;
create_dialog(US"New sender address?", sender);
}


/*************************************************
*    Cause a message to be returned to sender    *
*************************************************/

static void giveupAction(Widget w, XtPointer client_data, XtPointer call_data)
{
w = w;      /* Keep picky compilers happy */
call_data = call_data;
ActOnMessage(US client_data, US"-v -Mg", US"");
}



/*************************************************
*      Cause a message to be cancelled           *
*************************************************/

static void removeAction(Widget w, XtPointer client_data, XtPointer call_data)
{
w = w;      /* Keep picky compilers happy */
call_data = call_data;
ActOnMessage(US client_data, US"-Mrm", US"");
}



/*************************************************
*             Display a message's headers        *
*************************************************/

static void headersAction(Widget w, XtPointer client_data, XtPointer call_data)
{
uschar buffer[256];
header_line *h, *next;
Widget text = text_create(US client_data, text_depth);
rmark reset_point;

w = w;      /* Keep picky compilers happy */
call_data = call_data;

/* Remember the point in the dynamic store so we can recover to it afterwards.
Then use Exim's function to read the header. */

reset_point = store_mark();

sprintf(CS buffer, "%s-H", US client_data);
if (spool_read_header(buffer, TRUE, FALSE) != spool_read_OK)
  {
  if (errno == ERRNO_SPOOLFORMAT)
    {
    struct stat statbuf;
    sprintf(CS big_buffer, "%s/input/%s", spool_directory, buffer);
    if (Ustat(big_buffer, &statbuf) == 0)
      text_showf(text, "Format error in spool file %s: size=%lu\n", buffer,
        (unsigned long)statbuf.st_size);
    else text_showf(text, "Format error in spool file %s\n", buffer);
    }
  else text_showf(text, "Read error for spool file %s\n", buffer);
  store_reset(reset_point);
  return;
  }

if (sender_address != NULL)
  {
  text_showf(text, "%s sender: <%s>\n", f.sender_local ? "Local" : "Remote",
    sender_address);
  }

if (recipients_list != NULL)
  {
  int i;
  text_show(text, US"Recipients:\n");
  for (i = 0; i < recipients_count; i++)
    {
    text_showf(text, "  %s %s\n",
      (tree_search(tree_nonrecipients, recipients_list[i].address) == NULL)?
        " ":"*", recipients_list[i].address);
    }
  text_show(text, US"\n");
  }

for (h = header_list; h != NULL; h = next)
  {
  next = h->next;
  text_showf(text, "%c ", h->type);   /* Don't push h->text through a %s */
  text_show(text, h->text);           /* expansion as it may be v large */
  }

store_reset(reset_point);
}




/*************************************************
*              Dismiss a text window             *
*************************************************/

static void dismissAction(Widget w, XtPointer client_data, XtPointer call_data)
{
pipe_item *p = pipe_chain;

w = w;      /* Keep picky compilers happy */
call_data = call_data;

XtPopdown((Widget)client_data);
XtDestroyWidget((Widget)client_data);

/* If this is a text widget for a sub-process, clear it out of
the chain so that subsequent data doesn't try to use it. We have
to search the parents of the saved widget to see if one of them
is what we have just destroyed. */

while (p != NULL)
  {
  Widget pp = p->widget;
  while (pp != NULL)
    {
    if (pp == (Widget)client_data) { p->widget = NULL; return; }
    pp = XtParent(pp);
    }
  p = p->next;
  }
}



/*************************************************
*             Set up popup text window           *
*************************************************/

static Widget text_create(uschar *name, int height)
{
Widget textshell, form, text, button;

/* Create a popup shell widget to display as an additional
toplevel window. */

textshell = XtCreatePopupShell("textshell", topLevelShellWidgetClass,
  toplevel_widget, NULL, 0);
xs_SetValues(textshell, 4,
  "title",     name,
  "iconName",  name,
  "minWidth",  100,
  "minHeight", 100);

/* Create a form widget, containing the text widget and the
dismiss button widget. */

form = XtCreateManagedWidget("textform", formWidgetClass,
  textshell, NULL, 0);
xs_SetValues(form, 1, "defaultDistance", 8);

text = XtCreateManagedWidget("texttext", asciiTextWidgetClass,
  form, text_arg, XtNumber(text_arg));
xs_SetValues(text, 4,
  "editType",        XawtextAppend,
  "width",           700,
  "height",          height,
  "translations",    text_trans);
XawTextDisplayCaret(text, TRUE);

/* Use the same font as for the queue display */

if (queue_font != NULL)
  {
  XFontStruct *f = XLoadQueryFont(X_display, CS queue_font);
  if (f != NULL) xs_SetValues(text, 1, "font", f);
  }

button_arg[0].value = (XtArgVal)text;
button = XtCreateManagedWidget("dismiss", commandWidgetClass,
  form, button_arg, XtNumber(button_arg));
XtAddCallback(button, "callback",  dismissAction, (XtPointer)textshell);

/* Get the toplevel popup displayed, and yield the text widget so
that text can be put into it. */

XtPopup(textshell, XtGrabNone);
return text;
}




/*************************************************
*            Set up menu in queue window         *
*************************************************/

/* We have added an action table that causes this function to
be called, and set up button 2 in the text widgets to call it. */

void menu_create(Widget w, XEvent *event, String *actargs, Cardinal *count)
{
int line;
int i;
uschar *s;
XawTextPosition p;
Widget src, menu_line, item_1, item_2, item_3, item_4,
  item_5, item_6, item_7, item_8, item_9, item_10, item_11,
  item_12, item_13;
XtTranslations menu_trans = XtParseTranslationTable(
  "<EnterWindow>:   highlight()\n\
   <LeaveWindow>:   unhighlight()\n\
   <BtnMotion>:     highlight()\n\
   <BtnUp>:         MenuPopdown()notify()unhighlight()\n\
  ");

actargs = actargs;   /* Keep picky compilers happy */
count = count;

/* Get the sink and source and the current text pointer */

queue_get_arg[0].value = (XtArgVal)(&queue_text_sink);
queue_get_arg[1].value = (XtArgVal)(&src);
queue_get_arg[2].value = (XtArgVal)(&s);
XtGetValues(w, queue_get_arg, 3);

/* Find the line number of the pointer in the window, and the
character offset of the top lefthand of the window. */

line = (event->xbutton).y / XawTextSinkMaxHeight(queue_text_sink, 1);
p = XawTextTopPosition(w);

/* Find the start of the line on which the button was clicked. */

i = line;
while (i-- > 0)
  {
  while (s[p] != 0 && s[p++] != '\n');
  }

/* Now pointing either at 0 or 1st uschar after \n, or very 1st uschar.
If 0, the click was beyond the end of the data; just set up a dummy
menu. (Not easy to ignore as several actions are specified for the
mouse click and it expects this one to set up a menu.) If on a
continuation line, move back to the main line. */

if (s[p] == 0)
  {
  menushell_arg[0].value = (XtArgVal)"No message selected";
  menushell = XtCreatePopupShell("menu", simpleMenuWidgetClass,
    queue_widget, menushell_arg, XtNumber(menushell_arg));
  XtAddCallback(menushell, "popdownCallback", popdownAction, NULL);
  xs_SetValues(menushell, 2,
    "cursor",       XCreateFontCursor(X_display, XC_arrow),
    "translations", menu_trans);

  /* To keep the widgets in XFree86 happy, we have to create at least one menu
  item, it seems. (Openwindows doesn't mind a menu with no items.) Otherwise
  there's a complaint about a zero width menu, and a crash. */

  menu_line = XtCreateManagedWidget("line", smeLineObjectClass, menushell,
    NULL, 0);

  item_99_arg[0].value = (XtArgVal)menu_line;
  (void)XtCreateManagedWidget("item99", smeBSBObjectClass, menushell,
    item_99_arg, XtNumber(item_99_arg));

  highlighted_x = -1;
  return;
  }

while (p > 0 && s[p+11] == ' ')
  {
  line--;
  p--;
  while (p > 0 && s[p-1] != '\n') p--;
  }

/* Now pointing at first character of a main line. */

Ustrncpy(message_id, s+p+11, MESSAGE_ID_LENGTH);
message_id[MESSAGE_ID_LENGTH] = 0;

/* Highlight the line being menued, and save its parameters so that it
can be de-highlighted at popdown. */

highlighted_start = highlighted_end = p;
while (s[highlighted_end] != '\n') highlighted_end++;
highlighted_x = 17;
highlighted_y = line * XawTextSinkMaxHeight(queue_text_sink, 1) + 2;

XawTextSinkDisplayText(queue_text_sink,
  highlighted_x, highlighted_y,
  highlighted_start, highlighted_end, 1);

/* Create the popup shell and the other widgets that comprise the menu.
Set the translations and pointer shape, and add the callback pointers. */

menushell_arg[0].value = (XtArgVal)message_id;
menushell = XtCreatePopupShell("menu", simpleMenuWidgetClass,
  queue_widget, menushell_arg, XtNumber(menushell_arg));
XtAddCallback(menushell, "popdownCallback", popdownAction, NULL);

xs_SetValues(menushell, 2,
  "cursor",       XCreateFontCursor(X_display, XC_arrow),
  "translations", menu_trans);

menu_line = XtCreateManagedWidget("line", smeLineObjectClass, menushell,
  NULL, 0);

item_1_arg[0].value = (XtArgVal)menu_line;
item_1 = XtCreateManagedWidget("item1", smeBSBObjectClass, menushell,
  item_1_arg, XtNumber(item_1_arg));
XtAddCallback(item_1, "callback",  msglogAction, (XtPointer)message_id);

item_2_arg[0].value = (XtArgVal)item_1;
item_2 = XtCreateManagedWidget("item2", smeBSBObjectClass, menushell,
  item_2_arg, XtNumber(item_2_arg));
XtAddCallback(item_2, "callback",  headersAction, (XtPointer)message_id);

item_3_arg[0].value = (XtArgVal)item_2;
item_3 = XtCreateManagedWidget("item3", smeBSBObjectClass, menushell,
  item_3_arg, XtNumber(item_3_arg));
XtAddCallback(item_3, "callback",  bodyAction, (XtPointer)message_id);

item_4_arg[0].value = (XtArgVal)item_3;
item_4 = XtCreateManagedWidget("item4", smeBSBObjectClass, menushell,
  item_4_arg, XtNumber(item_4_arg));
XtAddCallback(item_4, "callback",  deliverAction, (XtPointer)message_id);

item_5_arg[0].value = (XtArgVal)item_4;
item_5 = XtCreateManagedWidget("item5", smeBSBObjectClass, menushell,
  item_5_arg, XtNumber(item_5_arg));
XtAddCallback(item_5, "callback",  freezeAction, (XtPointer)message_id);

item_6_arg[0].value = (XtArgVal)item_5;
item_6 = XtCreateManagedWidget("item6", smeBSBObjectClass, menushell,
  item_6_arg, XtNumber(item_6_arg));
XtAddCallback(item_6, "callback",  thawAction, (XtPointer)message_id);

item_7_arg[0].value = (XtArgVal)item_6;
item_7 = XtCreateManagedWidget("item7", smeBSBObjectClass, menushell,
  item_7_arg, XtNumber(item_7_arg));
XtAddCallback(item_7, "callback",  giveupAction, (XtPointer)message_id);

item_8_arg[0].value = (XtArgVal)item_7;
item_8 = XtCreateManagedWidget("item8", smeBSBObjectClass, menushell,
  item_8_arg, XtNumber(item_8_arg));
XtAddCallback(item_8, "callback",  removeAction, (XtPointer)message_id);

item_9_arg[0].value = (XtArgVal)item_8;
item_9 = XtCreateManagedWidget("item9", smeBSBObjectClass, menushell,
  item_9_arg, XtNumber(item_9_arg));

item_10_arg[0].value = (XtArgVal)item_9;
item_10 = XtCreateManagedWidget("item10", smeBSBObjectClass, menushell,
  item_10_arg, XtNumber(item_10_arg));
XtAddCallback(item_10, "callback",  addrecipAction, (XtPointer)message_id);

item_11_arg[0].value = (XtArgVal)item_10;
item_11 = XtCreateManagedWidget("item11", smeBSBObjectClass, menushell,
  item_11_arg, XtNumber(item_11_arg));
XtAddCallback(item_11, "callback",  markdelAction, (XtPointer)message_id);

item_12_arg[0].value = (XtArgVal)item_11;
item_12 = XtCreateManagedWidget("item12", smeBSBObjectClass, menushell,
  item_12_arg, XtNumber(item_12_arg));
XtAddCallback(item_12, "callback",  markalldelAction, (XtPointer)message_id);

item_13_arg[0].value = (XtArgVal)item_12;
item_13 = XtCreateManagedWidget("item13", smeBSBObjectClass, menushell,
  item_13_arg, XtNumber(item_13_arg));
XtAddCallback(item_13, "callback",  editsenderAction, (XtPointer)message_id);

/* Arrange that the menu pops up with the first item selected. */

xs_SetValues(menushell, 1, "popupOnEntry", item_1);

/* Flag that the menu is up to suppress queue updates. */

menu_is_up = TRUE;
}

/* End of em_menu.c */