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 /***********************************************************************/
25 #include <sys/types.h>
31 #include <mit-copyright.h>
39 #define CTL(ch) ((ch) & 037)
40 #define MAX(A,B) ((A) > (B) ? (A) : (B))
42 char *whoami; /* should not be static, for logging package */
45 extern char *strsave();
48 typedef struct list_info {
62 static char *ascbuff = {"0123456789"};
65 static List_info *current_li = (List_info *) NULL;
66 static int get_list_info();
67 static int fetch_list_info();
72 extern char *strsave();
78 typedef struct _menu {
84 MENU *main_menu, *help_menu;
86 int position[2], oldpos[2];
87 int level, found_some, currow, page, num_members;
88 int moreflg, toggle, first_time;
92 /****************************************************/
106 if ((whoami = rindex(argv[0], '/')) == NULL)
110 uname = calloc(20, 1);
111 if ((current_li = (List_info *) malloc(sizeof(List_info)))
112 == (List_info *) NULL) {
113 (void) sprintf(buf, ": allocating list info");
117 current_li->acl_type = (char *) NULL;
118 current_li->acl_name = (char *) NULL;
119 current_li->desc = (char *) NULL;
120 current_li->modtime = (char *) NULL;
121 current_li->modby = (char *) NULL;
122 current_li->modwith = (char *) NULL;
124 if ((uname = getlogin()) == NULL) {
125 struct passwd *getpwuid();
127 uname = getpwuid((int) getuid())->pw_name;
129 uname = (uname && strlen(uname)) ? strsave(uname) : "";
131 printf("Connecting to database for %s...please hold on.\n", uname);
133 status = sms_connect(SMS_SERVER);
135 (void) sprintf(buf, "\nConnection to SMS server failed");
139 status = sms_auth("mailmaint");
141 (void) sprintf(buf, "\nAuthorization failed.\n");
147 if ((LINES < 24) || (COLS < 60)) {
148 Put_message("Display window too small.\n\n");
149 (void) sprintf(buf, "Current window parameters are (%d \
150 lines, %d columns)\n", LINES, COLS);
152 Put_message("Please resize your window\n");
153 Put_message("to at least 24 lines and 60 columns.\n");
158 old_hook = set_com_err_hook(menu_err_hook);
159 position[0] = oldpos[0] = 1;
163 display_menu(main_menu);
167 set_com_err_hook(old_hook);
172 com_err(whoami, status, buf);
176 /****************************************************/
183 oldpos[level] = position[level];
185 currow = DISPROW + 2;
187 toggle = num_members = moreflg = 0;
190 if (position[level] == 7)
193 c = ascbuff[position[level]];
197 case 'L' & 037: /* clear screen */
198 display_menu(main_menu);
203 highlight(main_menu);
209 case '1': /* show all lists */
215 case '2': /* get all members of a list */
218 (void) list_members();
221 case '3': /* display list which user is a recipient */
227 case '4': /* show description */
233 case '5': /* add to list */
239 case '6': /* delete */
245 case 27: /* up arrow */
251 if (!position[level])
257 if (position[level] > 7)
267 highlight(main_menu);
271 /****************************************************/
277 show_text(DISPROW, STARTCOL, "Show information about a list.\n\r");
278 buf = calloc((unsigned)1024, 1);
279 if (Prompt("Enter List Name: ", buf, LISTSIZE, 1) == 1) {
281 if (fetch_list_info(buf, current_li) == 0) {
282 (void) sprintf(buf, "Description: %s\n\r", current_li->desc);
283 if (strlen(buf) > 60)
284 (void) display_buff(buf);
286 show_text(currow, STARTCOL, buf);
288 (void) sprintf(buf, "List Administrator: %s %s",
289 current_li->acl_type, current_li->acl_name);
290 show_text(currow, STARTCOL, buf);
292 (void) sprintf(buf, "Modified on %s by user %s with %s",
293 current_li->modtime, current_li->modby,
294 current_li->modwith);
295 show_text(currow, STARTCOL, buf);
299 show_text(currow, STARTCOL, "mailmaint: No such list found.");
302 show_text(currow, STARTCOL, "Press any Key to continue...");
308 /****************************************************/
321 printbuf = calloc((unsigned)maxcol, 1);
322 for (i = 0; i <= strlen(buf); i++) {
323 printbuf[cnt] = buf[i];
326 (void) start_display_buff(printbuf);
329 printbuf = calloc((unsigned)maxcol, 1);
332 if (strlen(buf) % maxcol != 0) {
333 (void) start_display_buff(printbuf);
339 /****************************************************/
340 start_display_buff(buff)
348 if (currow >= LINES - 2) {
351 mvcur(0, 0, currow, STARTCOL);
353 if (Prompt("--RETURN for more, ctl-c to exit--", buffer, 1, 0) == 0) {
354 Put_message("Flushing query...");
359 currow = DISPROW + 2;
360 show_text(currow, STARTCOL, "continued");
363 show_text(currow, STARTCOL, buff);
368 /****************************************************/
375 show_text(DISPROW, STARTCOL, "Add yourself to a list\n\r");
376 buf = calloc(LISTMAX, 1);
377 if (Prompt("Enter List Name: ", buf, LISTSIZE, 1) == 1) {
379 argv[0] = strsave(buf);
380 argv[1] = strsave("user");
381 argv[2] = strsave(uname);
382 if (status = sms_query("add_member_to_list", 3, argv,
383 scream, (char *) NULL)) {
385 com_err(whoami, status, " found.\n");
388 (void) sprintf(buf, "User %s added to list\r", uname);
389 show_text(DISPROW + 3, STARTCOL, buf);
391 currow = DISPROW + 4;
392 show_text(DISPROW + 4, STARTCOL, "Press any Key to continue...");
398 /****************************************************/
405 show_text(DISPROW, STARTCOL, "Remove yourself from a list\n\r");
406 buf = calloc(LISTMAX, 1);
407 if (Prompt("Enter List Name: ", buf, LISTSIZE, 1) == 1) {
409 argv[0] = strsave(buf);
410 argv[1] = strsave("user");
411 argv[2] = strsave(uname);
412 if (status = sms_query("delete_member_from_list", 3, argv,
413 scream, (char *) NULL)) {
415 com_err(whoami, status, " found.\n");
418 (void) sprintf(buf, "User %s deleted from list\r", uname);
419 show_text(DISPROW + 3, STARTCOL, buf);
421 currow = DISPROW + 4;
422 show_text(DISPROW + 4, STARTCOL, "Press any Key to continue...");
428 /****************************************************/
435 nargv[1] = strsave("ruser");
436 nargv[2] = strsave(uname);
437 buf = calloc(BUFSIZ, 1);
438 (void) sprintf(buf, "%s is on the following lists:\r", uname);
439 show_text(DISPROW, STARTCOL, buf);
440 mvcur(0, 0, currow, STARTCOL);
442 if (status = sms_query("get_lists_of_member", 2, nargv + 1,
443 print_1, (char *) NULL)) {
445 com_err(whoami, status, " in get_lists_of_member");
448 show_text(currow, STARTCOL, "Press any Key to continue...");
454 /****************************************************/
459 show_text(DISPROW, STARTCOL, "This function may take a \
460 while... proceed? [y] ");
462 if (c == 'y' || c == 'Y' || c == '\n') {
463 move(DISPROW + 1, STARTCOL);
464 addstr("Processing query...please hold");
466 (void) list_all_groups();
469 erase_line(DISPROW, STARTCOL);
474 /****************************************************/
477 print_1(argc, argv, callback)
479 char *argv[], *callback;
483 /* no newline 'cause Put_message adds one */
484 (void) sprintf(buf, "%s\r", argv[0]);
485 (void) start_display(buf);
490 /****************************************************/
493 print_all(argc, argv, callback)
495 char *argv[], *callback;
502 erase_line(DISPROW + 1, STARTCOL);
503 show_text(DISPROW + 1, STARTCOL, "All mailing lists:");
506 (void) sprintf(buf, "%s\r", argv[0]);
507 (void) start_display(buf);
512 /****************************************************/
516 argv[0] = argv[3] = "true";
517 argv[1] = argv[4] = "dontcare";
520 if (status = sms_query("qualified_get_lists", 5, argv,
521 print_all, (char *) NULL)) {
523 com_err(whoami, status, " in list_all_groups\n");
527 return (DM_NORMAL); /* HA! */
530 /****************************************************/
538 move(DISPROW, STARTCOL);
539 mvcur(0, 0, DISPROW, STARTCOL);
541 buf = calloc(LISTMAX, 1);
542 if (Prompt("Enter List Name: ", buf, LISTSIZE, 1) == 1) {
543 (void) sprintf(buffer, "The members of list '%s' are:", buf);
544 show_text(DISPROW + 1, STARTCOL, buffer);
546 if (status = sms_query("get_members_of_list", 1, argv,
547 print_2, (char *) NULL)) {
549 com_err(whoami, status, " found.\n");
553 show_text(currow, STARTCOL, "List is empty (no members).");
555 show_text(currow, STARTCOL, "Press any key to continue...");
564 return (DM_NORMAL); /* HA! */
567 /****************************************************/
570 print_2(argc, argv, callback)
572 char *argv[], *callback;
577 (void) sprintf(buf, "%s %s", argv[0], argv[1]);
578 (void) start_display(buf);
583 /****************************************************/
592 buffer = calloc(50, 1);
593 if (currow >= LINES - 2) {
596 mvcur(0, 0, currow, STARTCOL);
598 if (Prompt("--RETURN for more, ctl-c to exit--", buffer, 1, 0) == 0) {
599 Put_message("Flushing query...");
604 currow = DISPROW + 2;
605 (void) sprintf(buffer, "Continued (Page %d)", page);
606 show_text(currow, STARTCOL, buffer);
611 show_text(currow, STARTCOL, buff);
613 show_text(currow, SECONDCOL, buff);
620 /****************************************************/
630 buffer = calloc(50, 1);
632 (void) sprintf(buffer, "End of List. %d Total Members\r", num_members);
633 show_text(currow, STARTCOL, buffer);
635 show_text(currow, STARTCOL, "Press any key to continue...");
641 /****************************************************/
649 mvcur(0, 0, STARTROW, STARTCOL);
651 for (i = 0; i <= menu->num_items - 1; i++) {
652 move(STARTROW + i, STARTCOL);
654 addstr(menu->items[i]);
657 center_text(STARTROW + menu->num_items + 2,
658 "Enter a number, <up arrow>, or <down arrow>.");
660 center_text(STARTROW + menu->num_items + 3,
661 "Press 'q' to exit, <return> to confirm choice.");
663 center_text(STARTROW + menu->num_items + 3,
664 "Press 'q' to exit, 'r' for main menu, <return> to confirm choice.");
667 highlight(main_menu);
670 /****************************************************/
675 main_menu = (MENU *) malloc((unsigned) sizeof(MENU));
676 main_menu->num_items = 7;
677 main_menu->items = (char **) malloc((unsigned) sizeof(char *) * main_menu->num_items);
680 (void) sprintf(buf, "Mail List Program for %s", uname);
681 main_menu->title = strsave(buf);
682 main_menu->items[0] = strsave("1. Show all mailing lists.");
683 main_menu->items[1] = strsave("2. Get all members of a mailing list.");
684 main_menu->items[2] = strsave("3. Display lists of which you are a member.");
685 main_menu->items[3] = strsave("4. Show description of list.");
686 main_menu->items[4] = strsave("5. Add yourself to a mailing list.");
687 main_menu->items[5] = strsave("6. Delete yourself from a mailing list.");
688 main_menu->items[6] = strsave("q. Quit.");
691 /****************************************************/
694 help_menu = (MENU *) malloc((unsigned) sizeof(MENU));
695 help_menu->num_items = 5;
696 help_menu->items = (char **) malloc((unsigned) sizeof(char *) * help_menu->num_items);
698 help_menu->title = strsave("mailmaint is designed as a basic mail list administration program.");
699 help_menu->items[0] = strsave("if you need to perform more advanced list manipulation like");
700 help_menu->items[1] = strsave("adding lists, or changing list characteristics, refer to the");
701 help_menu->items[2] = strsave("program listmaint.");
702 help_menu->items[3] = strsave(" ");
703 help_menu->items[4] = strsave("Press any key to continue.");
706 /****************************************************/
712 if (oldpos[level] != position[level]) {
713 move(STARTROW + oldpos[level] - 1, STARTCOL);
715 addstr(menu->items[oldpos[level] - 1]);
719 move(STARTROW + position[level] - 1, STARTCOL);
721 addstr(menu->items[position[level] - 1]);
727 /****************************************************/
731 move(0, MAX(0, (COLS - strlen(buff)) >> 1));
738 /****************************************************/
739 center_text(row, buff)
743 move(row, MAX(0, (COLS - strlen(buff)) >> 1));
748 /****************************************************/
749 show_text(row, col, buff)
753 mvcur(0, 0, row, col);
758 /****************************************************/
765 buff = calloc((unsigned)COLS, 1);
766 for (i = 0; i <= COLS - 2; i++)
768 buff[i] = 0; /* just to be sure ! */
770 mvcur(0, 0, row, col);
775 /****************************************************/
782 /****************************************************/
792 maxcol = 80; /* limit width */
794 buff = calloc((unsigned)maxcol + 1, 1);
795 for (i = 0; i <= maxcol - 1; i++)
797 buff[i] = 0; /* just to be sure ! */
798 mvcur(0, 0, erase_row, STARTCOL);
800 for (i = erase_row; i <= currow - 1; i++) {
801 printf("%s\n\r", buff);
804 mvcur(erase_row, STARTCOL, STARTROW + oldpos[level] - 1, STARTCOL);
808 /****************************************************/
813 com_err(whoami, status, "\nAn SMS update returned a value -- programmer \
819 /****************************************************/
822 fetch_list_info(list, li)
829 return sms_query("get_list_info", 1, argv, get_list_info, (char *) NULL);
834 get_list_info(argc, argv)
839 if (current_li->acl_type)
840 free(current_li->acl_type);
841 current_li->acl_type = strsave(argv[7]);
842 if (current_li->acl_name)
843 free(current_li->acl_name);
844 current_li->acl_name = strsave(argv[8]);
845 if (current_li->desc)
846 free(current_li->desc);
847 current_li->desc = strsave(argv[9]);
848 if (current_li->modtime)
849 free(current_li->modtime);
850 current_li->modtime = strsave(argv[10]);
851 if (current_li->modby)
852 free(current_li->modby);
853 current_li->modby = strsave(argv[11]);
854 if (current_li->modwith)
855 free(current_li->modwith);
856 current_li->modwith = strsave(argv[12]);
863 /****************************************************/
864 /* Prompt the user for input */
866 Prompt(prompt, buf, buflen, crok)
876 printf("%s", prompt);
878 for (p = buf; abs(strlen(p) - strlen(buf)) <= buflen;) {
888 display_menu(main_menu);
895 if (strlen(buf) < 1)/* only \n or \r in buff */
916 if (abs(strlen(p) - strlen(buf)) >= buflen) {
926 (void) putchar(CTL('G'));
935 * Hook function to cause error messages to be printed through
936 * curses instead of around it.
940 menu_err_hook(who, code, fmt, args)
946 char buf[BUFSIZ], *cp;
950 (void) strcpy(buf, who);
951 for (cp = buf; *cp; cp++);
955 (void) strcpy(cp, error_message(code));
959 _strbuf._flag = _IOWRT + _IOSTRG;
961 _strbuf._cnt = BUFSIZ - (cp - buf);
962 _doprnt(fmt, args, &_strbuf);
963 (void) putc('\0', &_strbuf);