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>
31 #include <moira_site.h>
32 #include <mit-copyright.h>
41 #define CTL(ch) ((ch) & 037)
42 #define MAX(A,B) ((A) > (B) ? (A) : (B))
44 char *whoami; /* should not be static, for logging package */
47 extern char *strsave();
49 void menu_err_hook(const char *who, long code, const char *fmt, va_list args);
55 typedef struct list_info {
69 static char *ascbuff = {"0123456789"};
70 static int print_2(), print_1();
71 static List_info *current_li = (List_info *) NULL;
72 static int get_list_info();
73 static int fetch_list_info();
77 extern char *strsave();
83 typedef struct _menu {
89 MENU *main_menu, *help_menu;
91 int position[2], oldpos[2];
92 int level, found_some, currow, page, num_members;
93 int moreflg, toggle, first_time;
96 /* This crock is because the original code was very broken and this makes
97 * it work. Someday, we should abandon the code or fix it right.
99 #define mvcur(oy,ox,ny,nx) move(ny,nx)
101 /****************************************************/
109 void (*old_hook)(const char *, long, const char *, va_list);
114 char buf[BUFSIZ], *motd;
116 if ((whoami = rindex(argv[0], '/')) == NULL)
120 uname = calloc(20, 1);
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 ((uname = getlogin()) == NULL) {
135 struct passwd *getpwuid();
137 uname = getpwuid((int) getuid())->pw_name;
139 uname = (uname && strlen(uname)) ? strsave(uname) : "";
141 printf("Connecting to database for %s...please hold on.\n", uname);
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 /****************************************************/
341 printbuf = calloc((unsigned)maxcol, 1);
342 for (i = 0; i <= strlen(buf); i++) {
343 printbuf[cnt] = buf[i];
346 (void) start_display_buff(printbuf);
349 printbuf = calloc((unsigned)maxcol, 1);
352 if (strlen(buf) % maxcol != 0) {
353 (void) start_display_buff(printbuf);
359 /****************************************************/
360 start_display_buff(buff)
368 if (currow >= LINES - 2) {
371 mvcur(0, 0, currow, STARTCOL);
373 if (Prompt("--RETURN for more, ctl-c to exit--", buffer, 1, 0) == 0) {
374 display_buff("Flushing query...");
379 currow = DISPROW + 2;
380 show_text(currow, STARTCOL, "continued");
383 show_text(currow, STARTCOL, buff);
388 /****************************************************/
394 show_text(DISPROW, STARTCOL, "Add yourself to a list\n");
395 buf = calloc(LISTMAX, 1);
396 if (Prompt("Enter List Name: ", buf, LISTSIZE, 1) == 1) {
398 argv[0] = strsave(buf);
399 argv[1] = strsave("user");
400 argv[2] = strsave(uname);
401 if (status = mr_query("add_member_to_list", 3, argv,
402 scream, (char *) NULL)) {
404 com_err(whoami, status, " found.\n");
407 (void) sprintf(buf, "User %s added to list\n", uname);
408 show_text(DISPROW + 3, STARTCOL, buf);
410 currow = DISPROW + 4;
411 show_text(DISPROW + 4, STARTCOL, "Press any Key to continue...");
417 /****************************************************/
423 show_text(DISPROW, STARTCOL, "Remove yourself from a list\n");
424 buf = calloc(LISTMAX, 1);
425 if (Prompt("Enter List Name: ", buf, LISTSIZE, 1) == 1) {
427 argv[0] = strsave(buf);
428 argv[1] = strsave("user");
429 argv[2] = strsave(uname);
430 if (status = mr_query("delete_member_from_list", 3, argv,
431 scream, (char *) NULL)) {
433 com_err(whoami, status, " found.\n");
436 (void) sprintf(buf, "User %s deleted from list\n", uname);
437 show_text(DISPROW + 3, STARTCOL, buf);
439 currow = DISPROW + 4;
440 show_text(DISPROW + 4, STARTCOL, "Press any Key to continue...");
446 /****************************************************/
452 nargv[1] = strsave("ruser");
453 nargv[2] = strsave(uname);
454 buf = calloc(BUFSIZ, 1);
455 (void) sprintf(buf, "%s is on the following lists:\n", uname);
456 show_text(DISPROW, STARTCOL, buf);
457 mvcur(0, 0, currow, STARTCOL);
459 if (status = mr_query("get_lists_of_member", 2, nargv + 1,
460 print_1, (char *) NULL)) {
462 com_err(whoami, status, " in get_lists_of_member");
465 show_text(currow, STARTCOL, "Press any Key to continue...");
471 /****************************************************/
476 show_text(DISPROW, STARTCOL, "This function may take a \
477 while... proceed? [y] ");
478 c = getchar() & 0x7f;
479 if (c == 'y' || c == 'Y' || c == '\n') {
480 move(DISPROW + 1, STARTCOL);
481 addstr("Processing query...please hold");
483 (void) list_all_groups();
486 erase_line(DISPROW, STARTCOL);
491 /****************************************************/
494 print_1(argc, argv, callback)
496 char *argv[], *callback;
500 /* no newline 'cause display_buff adds one */
501 (void) sprintf(buf, "%s\n", argv[0]);
502 (void) start_display(buf);
507 /****************************************************/
510 print_all(argc, argv, callback)
512 char *argv[], *callback;
519 erase_line(DISPROW + 1, STARTCOL);
520 show_text(DISPROW + 1, STARTCOL, "All mailing lists:");
523 (void) sprintf(buf, "%s\n", argv[0]);
524 (void) start_display(buf);
529 /****************************************************/
533 argv[0] = argv[1] = argv[3] = "true";
534 argv[4] = "dontcare";
537 if (status = mr_query("qualified_get_lists", 5, argv,
538 print_all, (char *) NULL)) {
540 com_err(whoami, status, " in list_all_groups\n");
547 /****************************************************/
555 move(DISPROW, STARTCOL);
556 mvcur(0, 0, DISPROW, STARTCOL);
558 buf = calloc(LISTMAX, 1);
559 if (Prompt("Enter List Name: ", buf, LISTSIZE, 1) == 1) {
560 (void) sprintf(buffer, "The members of list '%s' are:", buf);
561 show_text(DISPROW + 1, STARTCOL, buffer);
563 if (status = mr_query("get_members_of_list", 1, argv,
564 print_2, (char *) NULL)) {
566 com_err(whoami, status, " found.\n");
570 show_text(currow, STARTCOL, "List is empty (no members).");
572 show_text(currow, STARTCOL, "Press any key to continue...");
584 /****************************************************/
587 print_2(argc, argv, callback)
589 char *argv[], *callback;
594 (void) sprintf(buf, "%s %s", argv[0], argv[1]);
595 (void) start_display(buf);
600 /****************************************************/
609 buffer = calloc(50, 1);
610 if (currow >= LINES - 2) {
612 mvcur(0, 0, currow, STARTCOL);
614 if (Prompt("--RETURN for more, ctl-c to exit--", buffer, 1, 0) == 0) {
615 display_buff("Flushing query...");
620 currow = DISPROW + 2;
621 (void) sprintf(buffer, "Continued (Page %d)", page);
622 show_text(currow, STARTCOL, buffer);
627 show_text(currow, STARTCOL, buff);
629 show_text(currow, SECONDCOL, buff);
636 /****************************************************/
646 buffer = calloc(50, 1);
648 (void) sprintf(buffer, "End of List. %d Total Members\n", num_members - 1);
649 show_text(currow, STARTCOL, buffer);
651 show_text(currow, STARTCOL, "Press any key to continue...");
657 /****************************************************/
665 mvcur(0, 0, STARTROW, STARTCOL);
667 for (i = 0; i <= menu->num_items - 1; i++) {
668 move(STARTROW + i, STARTCOL);
670 addstr(menu->items[i]);
673 center_text(STARTROW + menu->num_items + 2,
674 "Enter a number, <up arrow>, or <down arrow>.");
676 center_text(STARTROW + menu->num_items + 3,
677 "Press 'q' to exit, <return> to confirm choice.");
679 center_text(STARTROW + menu->num_items + 3,
680 "Press 'q' to exit, 'r' for main menu, <return> to confirm choice.");
683 highlight(main_menu);
686 /****************************************************/
691 main_menu = (MENU *) malloc((unsigned) sizeof(MENU));
692 main_menu->num_items = 7;
693 main_menu->items = (char **) malloc((unsigned) sizeof(char *) * main_menu->num_items);
696 (void) sprintf(buf, "Mail List Program for %s", uname);
697 main_menu->title = strsave(buf);
698 main_menu->items[0] = strsave("1. Show all public mailing lists.");
699 main_menu->items[1] = strsave("2. Get all members of a mailing list.");
700 main_menu->items[2] = strsave("3. Display lists of which you are a member.");
701 main_menu->items[3] = strsave("4. Show description of list.");
702 main_menu->items[4] = strsave("5. Add yourself to a mailing list.");
703 main_menu->items[5] = strsave("6. Delete yourself from a mailing list.");
704 main_menu->items[6] = strsave("q. Quit.");
707 /****************************************************/
710 help_menu = (MENU *) malloc((unsigned) sizeof(MENU));
711 help_menu->num_items = 5;
712 help_menu->items = (char **) malloc((unsigned) sizeof(char *) * help_menu->num_items);
714 help_menu->title = strsave("mailmaint is designed as a basic mail list administration program.");
715 help_menu->items[0] = strsave("if you need to perform more advanced list manipulation like");
716 help_menu->items[1] = strsave("adding lists, or changing list characteristics, refer to the");
717 help_menu->items[2] = strsave("program listmaint.");
718 help_menu->items[3] = strsave(" ");
719 help_menu->items[4] = strsave("Press any key to continue.");
722 /****************************************************/
728 if (oldpos[level] != position[level]) {
729 move(STARTROW + oldpos[level] - 1, STARTCOL);
731 addstr(menu->items[oldpos[level] - 1]);
735 move(STARTROW + position[level] - 1, STARTCOL);
737 addstr(menu->items[position[level] - 1]);
743 /****************************************************/
747 move(0, MAX(0, (COLS - strlen(buff)) >> 1));
754 /****************************************************/
755 center_text(row, buff)
759 move(row, MAX(0, (COLS - strlen(buff)) >> 1));
764 /****************************************************/
765 show_text(row, col, buff)
769 mvcur(0, 0, row, col);
774 /****************************************************/
781 buff = calloc((unsigned)COLS, 1);
782 for (i = 0; i <= COLS - 2; i++)
784 buff[i] = 0; /* just to be sure ! */
786 mvcur(0, 0, row, col);
791 /****************************************************/
798 /****************************************************/
808 maxcol = 80; /* limit width */
810 buff = calloc((unsigned)maxcol + 1, 1);
811 for (i = 0; i <= maxcol - 1; i++)
813 buff[i] = 0; /* just to be sure ! */
814 mvcur(0, 0, erase_row, STARTCOL);
816 for (i = erase_row; i <= currow - 1; i++) {
820 mvcur(erase_row, STARTCOL, STARTROW + oldpos[level] - 1, STARTCOL);
824 /****************************************************/
829 com_err(whoami, status, "\nA Moira update returned a value -- programmer \
833 return(0); /* to keep compiler happy */
836 /****************************************************/
839 fetch_list_info(list, li)
846 return mr_query("get_list_info", 1, argv, get_list_info, (char *) NULL);
851 get_list_info(argc, argv)
856 if (current_li->acl_type)
857 free(current_li->acl_type);
858 current_li->acl_type = strsave(argv[7]);
859 if (current_li->acl_name)
860 free(current_li->acl_name);
861 current_li->acl_name = strsave(argv[8]);
862 if (current_li->desc)
863 free(current_li->desc);
864 current_li->desc = strsave(argv[9]);
865 if (current_li->modtime)
866 free(current_li->modtime);
867 current_li->modtime = strsave(argv[10]);
868 if (current_li->modby)
869 free(current_li->modby);
870 current_li->modby = strsave(argv[11]);
871 if (current_li->modwith)
872 free(current_li->modwith);
873 current_li->modwith = strsave(argv[12]);
880 /****************************************************/
881 /* Prompt the user for input */
883 Prompt(prompt, buf, buflen, crok)
894 for (p = buf; abs(strlen(p) - strlen(buf)) <= buflen;) {
896 c = getchar() & 0x7f;
904 display_menu(main_menu);
911 if (strlen(buf) < 1)/* only \n or \r in buff */
930 if (abs(strlen(p) - strlen(buf)) >= buflen) {
939 (void) putchar(CTL('G'));
948 * Hook function to cause error messages to be printed through
949 * curses instead of around it.
953 menu_err_hook(who, code, fmt, args)
959 char buf[BUFSIZ], *cp;
961 (void) strcpy(buf, who);
962 for (cp = buf; *cp; cp++);
966 (void) strcpy(cp, error_message(code));
970 #if defined(AIX386) || defined(sun)
971 vsprintf(cp, fmt, args);
973 /* can do this because we never pass more than 1 arg here anyway... */
974 sprintf(cp, fmt, args);