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");
447 show_text(currow, STARTCOL, "Press any Key to continue...");
453 /****************************************************/
458 show_text(DISPROW, STARTCOL, "This function may take a \
459 while... proceed? [y] ");
461 if (c == 'y' || c == 'Y' || c == '\n') {
462 move(DISPROW + 1, STARTCOL);
463 addstr("Processing query...please hold");
465 (void) list_all_groups();
468 erase_line(DISPROW, STARTCOL);
473 /****************************************************/
476 print_1(argc, argv, callback)
478 char *argv[], *callback;
482 /* no newline 'cause Put_message adds one */
483 (void) sprintf(buf, "%s\r", argv[0]);
484 (void) start_display(buf);
489 /****************************************************/
492 print_all(argc, argv, callback)
494 char *argv[], *callback;
501 erase_line(DISPROW + 1, STARTCOL);
502 show_text(DISPROW + 1, STARTCOL, "All mailing lists:");
505 (void) sprintf(buf, "%s\r", argv[0]);
506 (void) start_display(buf);
511 /****************************************************/
515 argv[0] = argv[3] = "true";
516 argv[1] = argv[4] = "dontcare";
519 if (status = sms_query("qualified_get_lists", 5, argv,
520 print_all, (char *) NULL)) {
522 com_err(whoami, status, " in list_all_groups\n");
526 return (DM_NORMAL); /* HA! */
529 /****************************************************/
537 move(DISPROW, STARTCOL);
538 mvcur(0, 0, DISPROW, STARTCOL);
540 buf = calloc(LISTMAX, 1);
541 if (Prompt("Enter List Name: ", buf, LISTSIZE, 1) == 1) {
542 (void) sprintf(buffer, "The members of list '%s' are:", buf);
543 show_text(DISPROW + 1, STARTCOL, buffer);
545 if (status = sms_query("get_members_of_list", 1, argv,
546 print_2, (char *) NULL)) {
548 com_err(whoami, status, " found.\n");
552 show_text(currow, STARTCOL, "List is empty (no members).");
554 show_text(currow, STARTCOL, "Press any key to continue...");
563 return (DM_NORMAL); /* HA! */
566 /****************************************************/
569 print_2(argc, argv, callback)
571 char *argv[], *callback;
576 (void) sprintf(buf, "%s %s", argv[0], argv[1]);
577 (void) start_display(buf);
582 /****************************************************/
591 buffer = calloc(50, 1);
592 if (currow >= LINES - 2) {
595 mvcur(0, 0, currow, STARTCOL);
597 if (Prompt("--RETURN for more, ctl-c to exit--", buffer, 1, 0) == 0) {
598 Put_message("Flushing query...");
603 currow = DISPROW + 2;
604 (void) sprintf(buffer, "Continued (Page %d)", page);
605 show_text(currow, STARTCOL, buffer);
610 show_text(currow, STARTCOL, buff);
612 show_text(currow, SECONDCOL, buff);
619 /****************************************************/
629 buffer = calloc(50, 1);
631 (void) sprintf(buffer, "End of List. %d Total Members\r", num_members);
632 show_text(currow, STARTCOL, buffer);
634 show_text(currow, STARTCOL, "Press any key to continue...");
640 /****************************************************/
648 mvcur(0, 0, STARTROW, STARTCOL);
650 for (i = 0; i <= menu->num_items - 1; i++) {
651 move(STARTROW + i, STARTCOL);
653 addstr(menu->items[i]);
656 center_text(STARTROW + menu->num_items + 2,
657 "Enter a number, <up arrow>, or <down arrow>.");
659 center_text(STARTROW + menu->num_items + 3,
660 "Press 'q' to exit, <return> to confirm choice.");
662 center_text(STARTROW + menu->num_items + 3,
663 "Press 'q' to exit, 'r' for main menu, <return> to confirm choice.");
666 highlight(main_menu);
669 /****************************************************/
674 main_menu = (MENU *) malloc((unsigned) sizeof(MENU));
675 main_menu->num_items = 7;
676 main_menu->items = (char **) malloc((unsigned) sizeof(char *) * main_menu->num_items);
679 (void) sprintf(buf, "Mail List Program for %s", uname);
680 main_menu->title = strsave(buf);
681 main_menu->items[0] = strsave("1. Show all mailing lists.");
682 main_menu->items[1] = strsave("2. Get all members of a mailing list.");
683 main_menu->items[2] = strsave("3. Display lists of which you are a member.");
684 main_menu->items[3] = strsave("4. Show description of list.");
685 main_menu->items[4] = strsave("5. Add yourself to a mailing list.");
686 main_menu->items[5] = strsave("6. Delete yourself from a mailing list.");
687 main_menu->items[6] = strsave("q. Quit.");
690 /****************************************************/
693 help_menu = (MENU *) malloc((unsigned) sizeof(MENU));
694 help_menu->num_items = 5;
695 help_menu->items = (char **) malloc((unsigned) sizeof(char *) * help_menu->num_items);
697 help_menu->title = strsave("mailmaint is designed as a basic mail list administration program.");
698 help_menu->items[0] = strsave("if you need to perform more advanced list manipulation like");
699 help_menu->items[1] = strsave("adding lists, or changing list characteristics, refer to the");
700 help_menu->items[2] = strsave("program listmaint.");
701 help_menu->items[3] = strsave(" ");
702 help_menu->items[4] = strsave("Press any key to continue.");
705 /****************************************************/
711 if (oldpos[level] != position[level]) {
712 move(STARTROW + oldpos[level] - 1, STARTCOL);
714 addstr(menu->items[oldpos[level] - 1]);
718 move(STARTROW + position[level] - 1, STARTCOL);
720 addstr(menu->items[position[level] - 1]);
726 /****************************************************/
730 move(0, MAX(0, (COLS - strlen(buff)) >> 1));
737 /****************************************************/
738 center_text(row, buff)
742 move(row, MAX(0, (COLS - strlen(buff)) >> 1));
747 /****************************************************/
748 show_text(row, col, buff)
752 mvcur(0, 0, row, col);
757 /****************************************************/
764 buff = calloc((unsigned)COLS, 1);
765 for (i = 0; i <= COLS - 2; i++)
767 buff[i] = 0; /* just to be sure ! */
769 mvcur(0, 0, row, col);
774 /****************************************************/
781 /****************************************************/
791 maxcol = 80; /* limit width */
793 buff = calloc((unsigned)maxcol + 1, 1);
794 for (i = 0; i <= maxcol - 1; i++)
796 buff[i] = 0; /* just to be sure ! */
797 mvcur(0, 0, erase_row, STARTCOL);
799 for (i = erase_row; i <= currow - 1; i++) {
800 printf("%s\n\r", buff);
803 mvcur(erase_row, STARTCOL, STARTROW + oldpos[level] - 1, STARTCOL);
807 /****************************************************/
812 com_err(whoami, status, "\nAn SMS update returned a value -- programmer \
818 /****************************************************/
821 fetch_list_info(list, li)
828 return sms_query("get_list_info", 1, argv, get_list_info, (char *) NULL);
833 get_list_info(argc, argv)
838 if (current_li->acl_type)
839 free(current_li->acl_type);
840 current_li->acl_type = strsave(argv[7]);
841 if (current_li->acl_name)
842 free(current_li->acl_name);
843 current_li->acl_name = strsave(argv[8]);
844 if (current_li->desc)
845 free(current_li->desc);
846 current_li->desc = strsave(argv[9]);
847 if (current_li->modtime)
848 free(current_li->modtime);
849 current_li->modtime = strsave(argv[10]);
850 if (current_li->modby)
851 free(current_li->modby);
852 current_li->modby = strsave(argv[11]);
853 if (current_li->modwith)
854 free(current_li->modwith);
855 current_li->modwith = strsave(argv[12]);
862 /****************************************************/
863 /* Prompt the user for input */
865 Prompt(prompt, buf, buflen, crok)
875 printf("%s", prompt);
877 for (p = buf; abs(strlen(p) - strlen(buf)) <= buflen;) {
887 display_menu(main_menu);
894 if (strlen(buf) < 1)/* only \n or \r in buff */
915 if (abs(strlen(p) - strlen(buf)) >= buflen) {
925 (void) putchar(CTL('G'));
934 * Hook function to cause error messages to be printed through
935 * curses instead of around it.
939 menu_err_hook(who, code, fmt, args)
945 char buf[BUFSIZ], *cp;
949 (void) strcpy(buf, who);
950 for (cp = buf; *cp; cp++);
954 (void) strcpy(cp, error_message(code));
958 _strbuf._flag = _IOWRT + _IOSTRG;
960 _strbuf._cnt = BUFSIZ - (cp - buf);
961 _doprnt(fmt, args, &_strbuf);
962 (void) putc('\0', &_strbuf);