6 /* (c) Copyright 1988 by the Massachusetts Institute of Technology. */
7 /* For copying and distribution information, please see the file */
8 /* <mit-copyright.h>. */
11 static char rcsid_mailmaint_c[] = "$Header$";
14 /***********************************************************************/
15 /* mailmaint.c - pjlevine - 20 August 1987
18 /***********************************************************************/
24 #include <sys/types.h>
33 #include <moira_site.h>
34 #include <mit-copyright.h>
42 #define CTL(ch) ((ch) & 037)
43 #define MAX(A,B) ((A) > (B) ? (A) : (B))
45 char *whoami; /* should not be static, for logging package */
48 extern char *strsave();
50 void menu_err_hook(const char *who, long code, const char *fmt, va_list args);
56 typedef struct list_info {
70 static char *ascbuff = {"0123456789"};
71 static int print_2(), print_1();
72 static List_info *current_li = (List_info *) NULL;
73 static int get_list_info();
74 static int fetch_list_info();
78 extern char *strsave();
84 typedef struct _menu {
90 MENU *main_menu, *help_menu;
92 int position[2], oldpos[2];
93 int level, found_some, currow, page, num_members;
94 int moreflg, toggle, first_time;
97 /* This crock is because the original code was very broken and this makes
98 * it work. Someday, we should abandon the code or fix it right.
100 #define mvcur(oy,ox,ny,nx) move(ny,nx)
102 /****************************************************/
110 void (*old_hook)(const char *, long, const char *, va_list);
115 char buf[BUFSIZ], *motd;
117 if ((whoami = strrchr(argv[0], '/')) == NULL)
121 if ((current_li = (List_info *) malloc(sizeof(List_info)))
122 == (List_info *) NULL) {
123 (void) sprintf(buf, ": allocating list info");
127 current_li->acl_type = (char *) NULL;
128 current_li->acl_name = (char *) NULL;
129 current_li->desc = (char *) NULL;
130 current_li->modtime = (char *) NULL;
131 current_li->modby = (char *) NULL;
132 current_li->modwith = (char *) NULL;
134 if ((username = getlogin()) == NULL) {
135 struct passwd *getpwuid();
137 username = getpwuid((int) getuid())->pw_name;
139 username = (username && strlen(username)) ? strsave(username) : "";
141 printf("Connecting to database for %s...please hold on.\n", username);
143 status = mr_connect(NULL);
145 (void) sprintf(buf, "\nConnection to Moira server failed");
149 status = mr_motd(&motd);
151 com_err(whoami, status, " unable to check server status");
156 fprintf(stderr, "The Moira server is currently unavailable:\n%s\n", motd);
160 status = mr_auth("mailmaint");
162 (void) sprintf(buf, "\nAuthorization failed.\n");
168 if ((LINES < 24) || (COLS < 60)) {
169 display_buff("Display window too small.\n\n");
170 (void) sprintf(buf, "Current window parameters are (%d \
171 lines, %d columns)\n", LINES, COLS);
173 display_buff("Please resize your window\n");
174 display_buff("to at least 24 lines and 60 columns.\n");
179 old_hook = set_com_err_hook(menu_err_hook);
180 position[0] = oldpos[0] = 1;
184 display_menu(main_menu);
188 set_com_err_hook(old_hook);
193 com_err(whoami, status, buf);
197 /****************************************************/
204 oldpos[level] = position[level];
206 currow = DISPROW + 2;
208 toggle = num_members = moreflg = 0;
209 c = getchar() & 0x7f; /* mask parity bit */
210 if (c == '\r' || c == '\n') {
211 if (position[level] == 7)
214 c = ascbuff[position[level]];
218 case 'L' & 037: /* clear screen */
219 display_menu(main_menu);
224 highlight(main_menu);
230 case '1': /* show all lists */
236 case '2': /* get all members of a list */
239 (void) list_members();
242 case '3': /* display list which user is a recipient */
248 case '4': /* show description */
254 case '5': /* add to list */
260 case '6': /* delete */
266 case 27: /* up arrow */
267 c = getchar() & 0x7f;
269 c = getchar() & 0x7f;
272 if (!position[level])
278 if (position[level] > 7)
288 highlight(main_menu);
292 /****************************************************/
297 show_text(DISPROW, STARTCOL, "Show information about a list.\n");
298 buf = calloc((unsigned)1024, 1);
299 if (Prompt("Enter List Name: ", buf, LISTSIZE, 1) == 1) {
301 if (fetch_list_info(buf, current_li) == 0) {
302 (void) sprintf(buf, "Description: %s\n", current_li->desc);
303 if (strlen(buf) > 60)
304 (void) display_buff(buf);
306 show_text(currow, STARTCOL, buf);
308 (void) sprintf(buf, "List Administrator: %s %s",
309 current_li->acl_type, current_li->acl_name);
310 show_text(currow, STARTCOL, buf);
312 (void) sprintf(buf, "Modified on %s by user %s with %s",
313 current_li->modtime, current_li->modby,
314 current_li->modwith);
315 show_text(currow, STARTCOL, buf);
319 show_text(currow, STARTCOL, "mailmaint: No such list found.");
322 show_text(currow, STARTCOL, "Press any Key to continue...");
328 /****************************************************/
338 /* I don't understand the point of this limitation. -jweiss 3/25/94
343 printbuf = calloc((unsigned)maxcol, 1);
344 for (i = 0; i <= strlen(buf); i++) {
345 printbuf[cnt] = buf[i];
348 (void) start_display_buff(printbuf);
351 printbuf = calloc((unsigned)maxcol, 1);
354 if (strlen(buf) % maxcol != 0) {
355 (void) start_display_buff(printbuf);
361 /****************************************************/
362 start_display_buff(buff)
370 if (currow >= LINES - 2) {
373 mvcur(0, 0, currow, STARTCOL);
375 if (Prompt("--RETURN for more, ctl-c to exit--", buffer, 1, 0) == 0) {
376 erase_line(currow, STARTCOL);
377 show_text(currow, STARTCOL, "Flushing query...");
382 currow = DISPROW + 2;
383 show_text(currow, STARTCOL, "continued");
386 show_text(currow, STARTCOL, buff);
391 /****************************************************/
397 show_text(DISPROW, STARTCOL, "Add yourself to a list\n");
398 buf = calloc(LISTMAX, 1);
399 if (Prompt("Enter List Name: ", buf, LISTSIZE, 1) == 1) {
401 argv[0] = strsave(buf);
402 argv[1] = strsave("user");
403 argv[2] = strsave(username);
404 if (status = mr_query("add_member_to_list", 3, argv,
405 scream, (char *) NULL)) {
407 com_err(whoami, status, " found.\n");
410 (void) sprintf(buf, "User %s added to list\n", username);
411 show_text(DISPROW + 3, STARTCOL, buf);
413 currow = DISPROW + 4;
414 show_text(DISPROW + 4, STARTCOL, "Press any Key to continue...");
420 /****************************************************/
426 show_text(DISPROW, STARTCOL, "Remove yourself from a list\n");
427 buf = calloc(LISTMAX, 1);
428 if (Prompt("Enter List Name: ", buf, LISTSIZE, 1) == 1) {
430 argv[0] = strsave(buf);
431 argv[1] = strsave("user");
432 argv[2] = strsave(username);
433 if (status = mr_query("delete_member_from_list", 3, argv,
434 scream, (char *) NULL)) {
436 com_err(whoami, status, " found.\n");
439 (void) sprintf(buf, "User %s deleted from list\n", username);
440 show_text(DISPROW + 3, STARTCOL, buf);
442 currow = DISPROW + 4;
443 show_text(DISPROW + 4, STARTCOL, "Press any Key to continue...");
449 /****************************************************/
455 nargv[1] = strsave("ruser");
456 nargv[2] = strsave(username);
457 buf = calloc(BUFSIZ, 1);
458 (void) sprintf(buf, "%s is on the following lists:\n", username);
459 show_text(DISPROW, STARTCOL, buf);
460 mvcur(0, 0, currow, STARTCOL);
462 if (status = mr_query("get_lists_of_member", 2, nargv + 1,
463 print_1, (char *) NULL)) {
465 com_err(whoami, status, " in get_lists_of_member");
468 show_text(currow, STARTCOL, "Press any Key to continue...");
474 /****************************************************/
479 show_text(DISPROW, STARTCOL, "This function may take a \
480 while... proceed? [n] ");
481 c = getchar() & 0x7f;
482 if (c == 'y' || c == 'Y') {
483 move(DISPROW + 1, STARTCOL);
484 addstr("Processing query...please hold");
486 (void) list_all_groups();
489 erase_line(DISPROW, STARTCOL);
494 /****************************************************/
497 print_1(argc, argv, callback)
499 char *argv[], *callback;
503 /* no newline 'cause display_buff adds one */
504 (void) sprintf(buf, "%s\n", argv[0]);
505 (void) start_display(buf);
510 /****************************************************/
513 print_all(argc, argv, callback)
515 char *argv[], *callback;
522 erase_line(DISPROW + 1, STARTCOL);
523 show_text(DISPROW + 1, STARTCOL, "All mailing lists:");
526 (void) sprintf(buf, "%s\n", argv[0]);
527 (void) start_display(buf);
532 /****************************************************/
536 argv[0] = argv[1] = argv[3] = "true";
537 argv[4] = "dontcare";
540 if (status = mr_query("qualified_get_lists", 5, argv,
541 print_all, (char *) NULL)) {
543 com_err(whoami, status, " in list_all_groups\n");
550 /****************************************************/
558 move(DISPROW, STARTCOL);
559 mvcur(0, 0, DISPROW, STARTCOL);
561 buf = calloc(LISTMAX, 1);
562 if (Prompt("Enter List Name: ", buf, LISTSIZE, 1) == 1) {
563 (void) sprintf(buffer, "The members of list '%s' are:", buf);
564 show_text(DISPROW + 1, STARTCOL, buffer);
566 if (status = mr_query("get_members_of_list", 1, argv,
567 print_2, (char *) NULL)) {
569 com_err(whoami, status, " found.\n");
573 show_text(currow, STARTCOL, "List is empty (no members).");
575 show_text(currow, STARTCOL, "Press any key to continue...");
587 /****************************************************/
590 print_2(argc, argv, callback)
592 char *argv[], *callback;
597 (void) sprintf(buf, "%s %s", argv[0], argv[1]);
598 (void) start_display(buf);
603 /****************************************************/
608 int secondcol; /* where to start the second column of text */
610 secondcol = (COLS / 2); /* 1/2 was accross the screen */
614 buffer = calloc(50, 1);
615 if (currow >= LINES - 2) {
617 mvcur(0, 0, currow, STARTCOL);
619 if (Prompt("--RETURN for more, ctl-c to exit--", buffer, 1, 0) == 0) {
620 erase_line(currow, STARTCOL);
621 show_text(currow, STARTCOL, "Flushing query...");
626 currow = DISPROW + 2;
627 (void) sprintf(buffer, "Continued (Page %d)", page);
628 show_text(currow, STARTCOL, buffer);
633 show_text(currow, STARTCOL, buff);
635 erase_line(currow, secondcol-1); /* in case the 1st col is too long */
636 show_text(currow, secondcol, buff);
643 /****************************************************/
653 buffer = calloc(50, 1);
655 (void) sprintf(buffer, "End of List. %d Total Members\n", num_members - 1);
656 show_text(currow, STARTCOL, buffer);
658 show_text(currow, STARTCOL, "Press any key to continue...");
664 /****************************************************/
672 mvcur(0, 0, STARTROW, STARTCOL);
674 for (i = 0; i <= menu->num_items - 1; i++) {
675 move(STARTROW + i, STARTCOL);
677 addstr(menu->items[i]);
680 center_text(STARTROW + menu->num_items + 2,
681 "Enter a number, <up arrow>, or <down arrow>.");
683 center_text(STARTROW + menu->num_items + 3,
684 "Press 'q' to exit, <return> to confirm choice.");
686 center_text(STARTROW + menu->num_items + 3,
687 "Press 'q' to exit, 'r' for main menu, <return> to confirm choice.");
690 highlight(main_menu);
693 /****************************************************/
698 main_menu = (MENU *) malloc((unsigned) sizeof(MENU));
699 main_menu->num_items = 7;
700 main_menu->items = (char **) malloc((unsigned) sizeof(char *) * main_menu->num_items);
703 (void) sprintf(buf, "Mail List Program for %s", username);
704 main_menu->title = strsave(buf);
705 main_menu->items[0] = strsave("1. Show all public mailing lists.");
706 main_menu->items[1] = strsave("2. Get all members of a mailing list.");
707 main_menu->items[2] = strsave("3. Display lists of which you are a member.");
708 main_menu->items[3] = strsave("4. Show description of list.");
709 main_menu->items[4] = strsave("5. Add yourself to a mailing list.");
710 main_menu->items[5] = strsave("6. Delete yourself from a mailing list.");
711 main_menu->items[6] = strsave("q. Quit.");
714 /****************************************************/
717 help_menu = (MENU *) malloc((unsigned) sizeof(MENU));
718 help_menu->num_items = 5;
719 help_menu->items = (char **) malloc((unsigned) sizeof(char *) * help_menu->num_items);
721 help_menu->title = strsave("mailmaint is designed as a basic mail list administration program.");
722 help_menu->items[0] = strsave("if you need to perform more advanced list manipulation like");
723 help_menu->items[1] = strsave("adding lists, or changing list characteristics, refer to the");
724 help_menu->items[2] = strsave("program listmaint.");
725 help_menu->items[3] = strsave(" ");
726 help_menu->items[4] = strsave("Press any key to continue.");
729 /****************************************************/
735 if (oldpos[level] != position[level]) {
736 move(STARTROW + oldpos[level] - 1, STARTCOL);
738 addstr(menu->items[oldpos[level] - 1]);
742 move(STARTROW + position[level] - 1, STARTCOL);
744 addstr(menu->items[position[level] - 1]);
750 /****************************************************/
754 move(0, MAX(0, (COLS - strlen(buff)) >> 1));
761 /****************************************************/
762 center_text(row, buff)
766 move(row, MAX(0, (COLS - strlen(buff)) >> 1));
771 /****************************************************/
772 show_text(row, col, buff)
776 mvcur(0, 0, row, col);
781 /****************************************************/
788 buff = calloc((unsigned)COLS, 1);
789 for (i = 0; i <= COLS - 2; i++)
791 buff[i] = 0; /* just to be sure ! */
793 mvcur(0, 0, row, col);
796 free(buff); /* close mem. leak */
799 /****************************************************/
806 /****************************************************/
815 /* I don't understand the point of this limitation. -jweiss 3/25/94
816 and I believe it was preventing the window from being cleared properly.
818 maxcol = 80; */ /* limit width */
820 buff = calloc((unsigned)maxcol + 1, 1);
821 for (i = 0; i <= maxcol - 1; i++)
823 buff[i] = 0; /* just to be sure ! */
824 mvcur(0, 0, erase_row, STARTCOL);
826 for (i = erase_row; i <= currow - 1; i++) {
830 mvcur(erase_row, STARTCOL, STARTROW + oldpos[level] - 1, STARTCOL);
832 free (buff); /* fix mem. leak */
835 /****************************************************/
840 com_err(whoami, status, "\nA Moira update returned a value -- programmer \
844 return(0); /* to keep compiler happy */
847 /****************************************************/
850 fetch_list_info(list, li)
857 return mr_query("get_list_info", 1, argv, get_list_info, (char *) NULL);
862 get_list_info(argc, argv)
867 if (current_li->acl_type)
868 free(current_li->acl_type);
869 current_li->acl_type = strsave(argv[7]);
870 if (current_li->acl_name)
871 free(current_li->acl_name);
872 current_li->acl_name = strsave(argv[8]);
873 if (current_li->desc)
874 free(current_li->desc);
875 current_li->desc = strsave(argv[9]);
876 if (current_li->modtime)
877 free(current_li->modtime);
878 current_li->modtime = strsave(argv[10]);
879 if (current_li->modby)
880 free(current_li->modby);
881 current_li->modby = strsave(argv[11]);
882 if (current_li->modwith)
883 free(current_li->modwith);
884 current_li->modwith = strsave(argv[12]);
891 /****************************************************/
892 /* Prompt the user for input */
894 Prompt(prompt, buf, buflen, crok)
905 for (p = buf; abs(strlen(p) - strlen(buf)) <= buflen;) {
907 c = getchar() & 0x7f;
915 display_menu(main_menu);
922 if (strlen(buf) < 1)/* only \n or \r in buff */
941 if (abs(strlen(p) - strlen(buf)) >= buflen) {
950 (void) putchar(CTL('G'));
959 * Hook function to cause error messages to be printed through
960 * curses instead of around it.
964 menu_err_hook(who, code, fmt, args)
970 char buf[BUFSIZ], *cp;
972 (void) strcpy(buf, who);
973 for (cp = buf; *cp; cp++);
977 (void) strcpy(cp, error_message(code));
981 vsprintf(cp, fmt, args);