7 static char rcsid_mailmaint_c[] = "$Header$";
10 /***********************************************************************/
11 /* mailmaint.c - pjlevine - 20 August 1987
14 /***********************************************************************/
21 #include <sys/types.h>
25 #include "mit-copyright.h"
33 #define CTL(ch) ((ch) & 037)
34 #define MAX(A,B) ((A) > (B) ? (A) : (B))
36 char *whoami; /* should not be static, for logging package */
39 extern char *strsave();
42 typedef struct list_info {
56 static char *ascbuff = {"0123456789"};
59 static List_info *current_li = (List_info *) NULL;
60 static int get_list_info();
61 static int fetch_list_info();
66 extern char *strsave();
72 typedef struct _menu {
78 MENU *main_menu, *help_menu;
80 int position[2], oldpos[2];
81 int level, found_some, currow, page, num_members;
82 int moreflg, toggle, first_time;
86 /****************************************************/
100 if ((whoami = rindex(argv[0], '/')) == NULL)
104 uname = calloc(20, 1);
105 if ((current_li = (List_info *) malloc(sizeof(List_info)))
106 == (List_info *) NULL) {
107 (void) sprintf(buf, ": allocating list info");
111 current_li->acl_type = (char *) NULL;
112 current_li->acl_name = (char *) NULL;
113 current_li->desc = (char *) NULL;
114 current_li->modtime = (char *) NULL;
115 current_li->modby = (char *) NULL;
116 current_li->modwith = (char *) NULL;
118 if ((uname = getlogin()) == NULL) {
119 struct passwd *getpwuid();
121 uname = getpwuid((int) getuid())->pw_name;
123 uname = (uname && strlen(uname)) ? strsave(uname) : "";
125 printf("Connecting to database for %s...please hold on.\n", uname);
127 status = sms_connect();
129 (void) sprintf(buf, "\nConnection to SMS server failed");
133 status = sms_auth("mailmaint");
135 (void) sprintf(buf, "\nAuthorization failed.\n");
141 if ((LINES < 24) || (COLS < 60)) {
142 Put_message("Display window too small.\n\n");
143 (void) sprintf(buf, "Current window parameters are (%d \
144 lines, %d columns)\n", LINES, COLS);
146 Put_message("Please resize your window\n");
147 Put_message("to at least 24 lines and 60 columns.\n");
152 old_hook = set_com_err_hook(menu_err_hook);
153 position[0] = oldpos[0] = 1;
157 display_menu(main_menu);
161 set_com_err_hook(old_hook);
166 com_err(whoami, status, buf);
170 /****************************************************/
177 oldpos[level] = position[level];
179 currow = DISPROW + 2;
181 toggle = num_members = moreflg = 0;
184 if (position[level] == 7)
187 c = ascbuff[position[level]];
191 case 'L' & 037: /* clear screen */
192 display_menu(main_menu);
197 highlight(main_menu);
203 case '1': /* show all lists */
209 case '2': /* get all members of a list */
212 (void) list_members();
215 case '3': /* display list which user is a recipient */
221 case '4': /* show description */
227 case '5': /* add to list */
233 case '6': /* delete */
239 case 27: /* up arrow */
245 if (!position[level])
251 if (position[level] > 7)
261 highlight(main_menu);
265 /****************************************************/
271 show_text(DISPROW, STARTCOL, "Show information about a list.\n\r");
272 buf = calloc((unsigned)1024, 1);
273 if (Prompt("Enter List Name: ", buf, LISTSIZE, 1) == 1) {
275 if (fetch_list_info(buf, current_li) == 0) {
276 (void) sprintf(buf, "Description: %s\n\r", current_li->desc);
277 if (strlen(buf) > 60)
278 (void) display_buff(buf);
280 show_text(currow, STARTCOL, buf);
282 (void) sprintf(buf, "List Administrator: %s %s",
283 current_li->acl_type, current_li->acl_name);
284 show_text(currow, STARTCOL, buf);
286 (void) sprintf(buf, "Modified on %s by user %s with %s",
287 current_li->modtime, current_li->modby,
288 current_li->modwith);
289 show_text(currow, STARTCOL, buf);
293 show_text(currow, STARTCOL, "mailmaint: No such list found.");
296 show_text(currow, STARTCOL, "Press any Key to continue...");
302 /****************************************************/
315 printbuf = calloc((unsigned)maxcol, 1);
316 for (i = 0; i <= strlen(buf); i++) {
317 printbuf[cnt] = buf[i];
320 (void) start_display_buff(printbuf);
323 printbuf = calloc((unsigned)maxcol, 1);
326 if (strlen(buf) % maxcol != 0) {
327 (void) start_display_buff(printbuf);
333 /****************************************************/
334 start_display_buff(buff)
342 if (currow >= LINES - 2) {
345 mvcur(0, 0, currow, STARTCOL);
347 if (Prompt("--RETURN for more, ctl-c to exit--", buffer, 1, 0) == 0) {
348 Put_message("Flushing query...");
353 currow = DISPROW + 2;
354 show_text(currow, STARTCOL, "continued");
357 show_text(currow, STARTCOL, buff);
362 /****************************************************/
369 show_text(DISPROW, STARTCOL, "Add yourself to a list\n\r");
370 buf = calloc(LISTMAX, 1);
371 if (Prompt("Enter List Name: ", buf, LISTSIZE, 1) == 1) {
373 argv[0] = strsave(buf);
374 argv[1] = strsave("user");
375 argv[2] = strsave(uname);
376 if (status = sms_query("add_member_to_list", 3, argv,
377 scream, (char *) NULL)) {
379 com_err(whoami, status, " found.\n");
382 (void) sprintf(buf, "User %s added to list\r", uname);
383 show_text(DISPROW + 3, STARTCOL, buf);
385 currow = DISPROW + 4;
386 show_text(DISPROW + 4, STARTCOL, "Press any Key to continue...");
392 /****************************************************/
399 show_text(DISPROW, STARTCOL, "Remove yourself from a list\n\r");
400 buf = calloc(LISTMAX, 1);
401 if (Prompt("Enter List Name: ", buf, LISTSIZE, 1) == 1) {
403 argv[0] = strsave(buf);
404 argv[1] = strsave("user");
405 argv[2] = strsave(uname);
406 if (status = sms_query("delete_member_from_list", 3, argv,
407 scream, (char *) NULL)) {
409 com_err(whoami, status, " found.\n");
412 (void) sprintf(buf, "User %s deleted from list\r", uname);
413 show_text(DISPROW + 3, STARTCOL, buf);
415 currow = DISPROW + 4;
416 show_text(DISPROW + 4, STARTCOL, "Press any Key to continue...");
422 /****************************************************/
429 nargv[1] = strsave("ruser");
430 nargv[2] = strsave(uname);
431 buf = calloc(BUFSIZ, 1);
432 (void) sprintf(buf, "%s is on the following lists:\r", uname);
433 show_text(DISPROW, STARTCOL, buf);
434 mvcur(0, 0, currow, STARTCOL);
436 if (status = sms_query("get_lists_of_member", 2, nargv + 1,
437 print_1, (char *) NULL)) {
439 com_err(whoami, status, " in get_lists_of_member");
441 show_text(currow, STARTCOL, "Press any Key to continue...");
447 /****************************************************/
452 show_text(DISPROW, STARTCOL, "This function may take a \
453 while... proceed? [y] ");
455 if (c == 'y' || c == 'Y' || c == '\n') {
456 move(DISPROW + 1, STARTCOL);
457 addstr("Processing query...please hold");
459 (void) list_all_groups();
462 erase_line(DISPROW, STARTCOL);
467 /****************************************************/
470 print_1(argc, argv, callback)
472 char *argv[], *callback;
476 /* no newline 'cause Put_message adds one */
477 (void) sprintf(buf, "%s\r", argv[0]);
478 (void) start_display(buf);
483 /****************************************************/
486 print_all(argc, argv, callback)
488 char *argv[], *callback;
495 erase_line(DISPROW + 1, STARTCOL);
496 show_text(DISPROW + 1, STARTCOL, "All mailing lists:");
499 (void) sprintf(buf, "%s\r", argv[0]);
500 (void) start_display(buf);
505 /****************************************************/
509 argv[0] = argv[3] = "true";
510 argv[1] = argv[4] = "dontcare";
513 if (status = sms_query("qualified_get_lists", 5, argv,
514 print_all, (char *) NULL)) {
516 com_err(whoami, status, " in list_all_groups\n");
520 return (DM_NORMAL); /* HA! */
523 /****************************************************/
531 move(DISPROW, STARTCOL);
532 mvcur(0, 0, DISPROW, STARTCOL);
534 buf = calloc(LISTMAX, 1);
535 if (Prompt("Enter List Name: ", buf, LISTSIZE, 1) == 1) {
536 (void) sprintf(buffer, "The members of list '%s' are:", buf);
537 show_text(DISPROW + 1, STARTCOL, buffer);
539 if (status = sms_query("get_members_of_list", 1, argv,
540 print_2, (char *) NULL)) {
542 com_err(whoami, status, " found.\n");
546 show_text(currow, STARTCOL, "List is empty (no members).");
548 show_text(currow, STARTCOL, "Press any key to continue...");
557 return (DM_NORMAL); /* HA! */
560 /****************************************************/
563 print_2(argc, argv, callback)
565 char *argv[], *callback;
570 (void) sprintf(buf, "%s %s", argv[0], argv[1]);
571 (void) start_display(buf);
576 /****************************************************/
585 buffer = calloc(50, 1);
586 if (currow >= LINES - 2) {
589 mvcur(0, 0, currow, STARTCOL);
591 if (Prompt("--RETURN for more, ctl-c to exit--", buffer, 1, 0) == 0) {
592 Put_message("Flushing query...");
597 currow = DISPROW + 2;
598 (void) sprintf(buffer, "Continued (Page %d)", page);
599 show_text(currow, STARTCOL, buffer);
604 show_text(currow, STARTCOL, buff);
606 show_text(currow, SECONDCOL, buff);
613 /****************************************************/
623 buffer = calloc(50, 1);
625 (void) sprintf(buffer, "End of List. %d Total Members\r", num_members);
626 show_text(currow, STARTCOL, buffer);
628 show_text(currow, STARTCOL, "Press any key to continue...");
634 /****************************************************/
642 mvcur(0, 0, STARTROW, STARTCOL);
644 for (i = 0; i <= menu->num_items - 1; i++) {
645 move(STARTROW + i, STARTCOL);
647 addstr(menu->items[i]);
650 center_text(STARTROW + menu->num_items + 2,
651 "Enter a number, <up arrow>, or <down arrow>.");
653 center_text(STARTROW + menu->num_items + 3,
654 "Press 'q' to exit, <return> to confirm choice.");
656 center_text(STARTROW + menu->num_items + 3,
657 "Press 'q' to exit, 'r' for main menu, <return> to confirm choice.");
660 highlight(main_menu);
663 /****************************************************/
668 main_menu = (MENU *) malloc((unsigned) sizeof(MENU));
669 main_menu->num_items = 7;
670 main_menu->items = (char **) malloc((unsigned) sizeof(char *) * main_menu->num_items);
673 (void) sprintf(buf, "Mail List Program for %s", uname);
674 main_menu->title = strsave(buf);
675 main_menu->items[0] = strsave("1. Show all mailing lists.");
676 main_menu->items[1] = strsave("2. Get all members of a mailing list.");
677 main_menu->items[2] = strsave("3. Display lists of which you are a member.");
678 main_menu->items[3] = strsave("4. Show description of list.");
679 main_menu->items[4] = strsave("5. Add yourself to a mailing list.");
680 main_menu->items[5] = strsave("6. Delete yourself from a mailing list.");
681 main_menu->items[6] = strsave("q. Quit.");
684 /****************************************************/
687 help_menu = (MENU *) malloc((unsigned) sizeof(MENU));
688 help_menu->num_items = 5;
689 help_menu->items = (char **) malloc((unsigned) sizeof(char *) * help_menu->num_items);
691 help_menu->title = strsave("mailmaint is designed as a basic mail list administration program.");
692 help_menu->items[0] = strsave("if you need to perform more advanced list manipulation like");
693 help_menu->items[1] = strsave("adding lists, or changing list characteristics, refer to the");
694 help_menu->items[2] = strsave("program listmaint.");
695 help_menu->items[3] = strsave(" ");
696 help_menu->items[4] = strsave("Press any key to continue.");
699 /****************************************************/
705 if (oldpos[level] != position[level]) {
706 move(STARTROW + oldpos[level] - 1, STARTCOL);
708 addstr(menu->items[oldpos[level] - 1]);
712 move(STARTROW + position[level] - 1, STARTCOL);
714 addstr(menu->items[position[level] - 1]);
720 /****************************************************/
724 move(0, MAX(0, (COLS - strlen(buff)) >> 1));
731 /****************************************************/
732 center_text(row, buff)
736 move(row, MAX(0, (COLS - strlen(buff)) >> 1));
741 /****************************************************/
742 show_text(row, col, buff)
746 mvcur(0, 0, row, col);
751 /****************************************************/
758 buff = calloc((unsigned)COLS, 1);
759 for (i = 0; i <= COLS - 2; i++)
761 buff[i] = 0; /* just to be sure ! */
763 mvcur(0, 0, row, col);
768 /****************************************************/
775 /****************************************************/
785 maxcol = 80; /* limit width */
787 buff = calloc((unsigned)maxcol + 1, 1);
788 for (i = 0; i <= maxcol - 1; i++)
790 buff[i] = 0; /* just to be sure ! */
791 mvcur(0, 0, erase_row, STARTCOL);
793 for (i = erase_row; i <= currow - 1; i++) {
794 printf("%s\n\r", buff);
797 mvcur(erase_row, STARTCOL, STARTROW + oldpos[level] - 1, STARTCOL);
801 /****************************************************/
806 com_err(whoami, status, "\nAn SMS update returned a value -- programmer \
812 /****************************************************/
815 fetch_list_info(list, li)
822 return sms_query("get_list_info", 1, argv, get_list_info, (char *) NULL);
827 get_list_info(argc, argv)
832 if (current_li->acl_type)
833 free(current_li->acl_type);
834 current_li->acl_type = strsave(argv[7]);
835 if (current_li->acl_name)
836 free(current_li->acl_name);
837 current_li->acl_name = strsave(argv[8]);
838 if (current_li->desc)
839 free(current_li->desc);
840 current_li->desc = strsave(argv[9]);
841 if (current_li->modtime)
842 free(current_li->modtime);
843 current_li->modtime = strsave(argv[10]);
844 if (current_li->modby)
845 free(current_li->modby);
846 current_li->modby = strsave(argv[11]);
847 if (current_li->modwith)
848 free(current_li->modwith);
849 current_li->modwith = strsave(argv[12]);
856 /****************************************************/
857 /* Prompt the user for input */
859 Prompt(prompt, buf, buflen, crok)
869 printf("%s", prompt);
871 for (p = buf; abs(strlen(p) - strlen(buf)) <= buflen;) {
881 display_menu(main_menu);
888 if (strlen(buf) < 1)/* only \n or \r in buff */
909 if (abs(strlen(p) - strlen(buf)) >= buflen) {
919 (void) putchar(CTL('G'));
928 * Hook function to cause error messages to be printed through
929 * curses instead of around it.
933 menu_err_hook(who, code, fmt, args)
939 char buf[BUFSIZ], *cp;
943 (void) strcpy(buf, who);
944 for (cp = buf; *cp; cp++);
948 (void) strcpy(cp, error_message(code));
952 _strbuf._flag = _IOWRT + _IOSTRG;
954 _strbuf._cnt = BUFSIZ - (cp - buf);
955 _doprnt(fmt, args, &_strbuf);
956 (void) putc('\0', &_strbuf);
965 * c-continued-statement-offset: 4
967 * c-argdecl-indent: 4