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 {
48 static char *ascbuff = {"0123456789"};
51 static List_info *current_li = (List_info *) NULL;
52 static int get_list_info();
53 static int fetch_list_info();
58 extern char *strsave();
64 typedef struct _menu {
70 MENU *main_menu, *help_menu;
72 int position[2], oldpos[2];
73 int level, found_some, currow, page, num_members;
74 int moreflg, toggle, first_time;
78 /****************************************************/
92 if ((whoami = rindex(argv[0], '/')) == NULL)
96 uname = calloc(20, 1);
97 if ((current_li = (List_info *) malloc(sizeof(List_info)))
98 == (List_info *) NULL) {
99 (void) sprintf(buf, ": allocating list info");
103 current_li->acl = (char *) NULL;
104 current_li->desc = (char *) NULL;
105 current_li->expdate = (char *) NULL;
107 if ((uname = getlogin()) == NULL) {
108 struct passwd *getpwuid();
110 uname = getpwuid((int) getuid())->pw_name;
112 uname = (uname && strlen(uname)) ? strsave(uname) : "";
114 printf("Connecting to database for %s...please hold on.\n", uname);
116 status = sms_connect();
118 (void) sprintf(buf, "\nConnection to SMS server failed");
124 (void) sprintf(buf, "\nAuthorization failed.\n");
130 if ((LINES < 24) || (COLS < 60)) {
131 Put_message("Display window too small.\n\n");
132 (void) sprintf(buf, "Current window parameters are (%d \
133 lines, %d columns)\n", LINES, COLS);
135 Put_message("Please resize your window\n");
136 Put_message("to at least 24 lines and 60 columns.\n");
141 old_hook = set_com_err_hook(menu_err_hook);
142 position[0] = oldpos[0] = 1;
146 display_menu(main_menu);
150 set_com_err_hook(old_hook);
155 com_err(whoami, status, buf);
159 /****************************************************/
166 oldpos[level] = position[level];
168 currow = DISPROW + 2;
170 toggle = num_members = moreflg = 0;
173 if (position[level] == 7)
176 c = ascbuff[position[level]];
180 case 'L' & 037: /* clear screen */
181 display_menu(main_menu);
186 highlight(main_menu);
192 case '1': /* show all lists */
198 case '2': /* get all members of a list */
201 (void) list_members();
204 case '3': /* display list which user is a recipient */
210 case '4': /* show description */
216 case '5': /* add to list */
222 case '6': /* delete */
228 case 27: /* up arrow */
234 if (!position[level])
240 if (position[level] > 7)
250 highlight(main_menu);
254 /****************************************************/
260 show_text(DISPROW, STARTCOL, "Show information about a list.\n\r");
261 buf = calloc((unsigned)1024, 1);
262 if (Prompt("Enter List Name: ", buf, LISTSIZE) == 1) {
264 if (fetch_list_info(buf, current_li) == 0) {
265 (void) sprintf(buf, "Description: %s\n\r", current_li->desc);
266 if (strlen(buf) > 60)
267 (void) display_buff(buf);
269 show_text(currow, STARTCOL, buf);
271 (void) sprintf(buf, "List Administrator: %s", current_li->acl);
272 show_text(currow, STARTCOL, buf);
274 (void) sprintf(buf, "Expiration date: %s", current_li->expdate);
275 show_text(currow, STARTCOL, buf);
279 show_text(currow, STARTCOL, "mailmaint: No such list found.");
282 show_text(currow, STARTCOL, "Press any Key to continue...");
288 /****************************************************/
301 printbuf = calloc((unsigned)maxcol, 1);
302 for (i = 0; i <= strlen(buf); i++) {
303 printbuf[cnt] = buf[i];
306 (void) start_display_buff(printbuf);
309 printbuf = calloc((unsigned)maxcol, 1);
312 if (strlen(buf) % maxcol != 0) {
313 (void) start_display_buff(printbuf);
319 /****************************************************/
320 start_display_buff(buff)
328 if (currow >= LINES - 2) {
331 mvcur(0, 0, currow, STARTCOL);
333 if (Prompt("--RETURN for more, ctl-c to exit--", buffer, 1) == 0) {
334 Put_message("Flushing query...");
339 currow = DISPROW + 2;
340 show_text(currow, STARTCOL, "continued");
343 show_text(currow, STARTCOL, buff);
348 /****************************************************/
351 static char *argv[] = {
352 "add_member_to_list",
360 show_text(DISPROW, STARTCOL, "Add yourself to a list\n\r");
361 buf = calloc(LISTMAX, 1);
362 if (Prompt("Enter List Name: ", buf, LISTSIZE) == 1) {
364 argv[1] = strsave(buf);
365 argv[2] = strsave("user");
366 argv[3] = strsave(uname);
367 if (status = sms_query_internal(3, argv, scream, (char *) NULL)) {
369 com_err(whoami, status, " found.\n");
372 (void) sprintf(buf, "User %s added to list\r", uname);
373 show_text(DISPROW + 3, STARTCOL, buf);
375 currow = DISPROW + 4;
376 show_text(DISPROW + 4, STARTCOL, "Press any Key to continue...");
382 /****************************************************/
385 static char *argv[] = {
386 "delete_member_from_list",
394 show_text(DISPROW, STARTCOL, "Remove yourself from a list\n\r");
395 buf = calloc(LISTMAX, 1);
396 if (Prompt("Enter List Name: ", buf, LISTSIZE) == 1) {
398 argv[1] = strsave(buf);
399 argv[2] = strsave("user");
400 argv[3] = strsave(uname);
401 if (status = sms_query_internal(3, argv, scream, (char *) NULL)) {
403 com_err(whoami, status, " found.\n");
406 (void) sprintf(buf, "User %s deleted from list\r", uname);
407 show_text(DISPROW + 3, STARTCOL, buf);
409 currow = DISPROW + 4;
410 show_text(DISPROW + 4, STARTCOL, "Press any Key to continue...");
416 /****************************************************/
423 nargv[1] = strsave("user");
424 nargv[2] = strsave(uname);
425 buf = calloc(BUFSIZ, 1);
426 (void) sprintf(buf, "%s is on the following lists:\r", uname);
427 show_text(DISPROW, STARTCOL, buf);
428 mvcur(0, 0, currow, STARTCOL);
430 if (status = sms_query("get_lists_of_member", 2, nargv + 1,
431 print_1, (char *) NULL)) {
433 com_err(whoami, status, " in get_lists_of_member");
435 show_text(currow, STARTCOL, "Press any Key to continue...");
441 /****************************************************/
446 show_text(DISPROW, STARTCOL, "This function may take a \
447 while... proceed? [y] ");
449 if (c == 'y' || c == 'Y') {
450 move(DISPROW + 1, STARTCOL);
451 addstr("Processing query...please hold");
453 (void) list_all_groups();
456 erase_line(DISPROW, STARTCOL);
461 /****************************************************/
464 print_1(argc, argv, callback)
466 char *argv[], *callback;
470 /* no newline 'cause Put_message adds one */
471 (void) sprintf(buf, "%s\r", argv[0]);
472 (void) start_display(buf);
477 /****************************************************/
480 print_all(argc, argv, callback)
482 char *argv[], *callback;
489 erase_line(DISPROW + 1, STARTCOL);
490 show_text(DISPROW + 1, STARTCOL, "All mailing lists:");
493 (void) sprintf(buf, "%s\r", argv[0]);
494 (void) start_display(buf);
499 /****************************************************/
504 if (status = sms_query("get_all_visible_maillists", 0, (char **) NULL,
505 print_all, (char *) NULL)) {
507 com_err(whoami, status, " in list_all_groups\n");
511 return (DM_NORMAL); /* HA! */
514 /****************************************************/
522 move(DISPROW, STARTCOL);
523 mvcur(0, 0, DISPROW, STARTCOL);
525 buf = calloc(LISTMAX, 1);
526 if (Prompt("Enter List Name: ", buf, LISTSIZE) == 1) {
527 (void) sprintf(buffer, "The members of list '%s' are:", buf);
528 show_text(DISPROW + 1, STARTCOL, buffer);
530 if (status = sms_query("get_members_of_list", 1, argv,
531 print_2, (char *) NULL)) {
533 com_err(whoami, status, " found.\n");
537 show_text(currow, STARTCOL, "List is empty (no members).");
539 show_text(currow, STARTCOL, "Press any key to continue...");
548 return (DM_NORMAL); /* HA! */
551 /****************************************************/
554 print_2(argc, argv, callback)
556 char *argv[], *callback;
561 (void) sprintf(buf, "%s %s", argv[0], argv[1]);
562 (void) start_display(buf);
567 /****************************************************/
576 buffer = calloc(50, 1);
577 if (currow >= LINES - 2) {
580 mvcur(0, 0, currow, STARTCOL);
582 if (Prompt("--RETURN for more, ctl-c to exit--", buffer, 1) == 0) {
583 Put_message("Flushing query...");
588 currow = DISPROW + 2;
589 (void) sprintf(buffer, "Continued (Page %d)", page);
590 show_text(currow, STARTCOL, buffer);
595 show_text(currow, STARTCOL, buff);
597 show_text(currow, SECONDCOL, buff);
604 /****************************************************/
614 buffer = calloc(50, 1);
616 (void) sprintf(buffer, "End of List. %d Total Members\r", num_members);
617 show_text(currow, STARTCOL, buffer);
619 show_text(currow, STARTCOL, "Press any key to continue...");
625 /****************************************************/
633 mvcur(0, 0, STARTROW, STARTCOL);
635 for (i = 0; i <= menu->num_items - 1; i++) {
636 move(STARTROW + i, STARTCOL);
638 addstr(menu->items[i]);
641 center_text(STARTROW + menu->num_items + 2,
642 "Enter a number, <up arrow>, or <down arrow>.");
644 center_text(STARTROW + menu->num_items + 3,
645 "Press 'q' to exit, <return> to confirm choice.");
647 center_text(STARTROW + menu->num_items + 3,
648 "Press 'q' to exit, 'r' for main menu, <return> to confirm choice.");
651 highlight(main_menu);
654 /****************************************************/
659 main_menu = (MENU *) malloc((unsigned) sizeof(MENU));
660 main_menu->num_items = 7;
661 main_menu->items = (char **) malloc((unsigned) sizeof(char *) * main_menu->num_items);
664 (void) sprintf(buf, "Mail List Program for %s", uname);
665 main_menu->title = strsave(buf);
666 main_menu->items[0] = strsave("1. Show all mailing lists.");
667 main_menu->items[1] = strsave("2. Get all members of a mailing list.");
668 main_menu->items[2] = strsave("3. Display lists of which you are a member.");
669 main_menu->items[3] = strsave("4. Show description of list.");
670 main_menu->items[4] = strsave("5. Add yourself to a mailing list.");
671 main_menu->items[5] = strsave("6. Delete yourself from a mailing list.");
672 main_menu->items[6] = strsave("q. Quit.");
675 /****************************************************/
678 help_menu = (MENU *) malloc((unsigned) sizeof(MENU));
679 help_menu->num_items = 5;
680 help_menu->items = (char **) malloc((unsigned) sizeof(char *) * help_menu->num_items);
682 help_menu->title = strsave("mailmaint is designed as a basic mail list administration program.");
683 help_menu->items[0] = strsave("if you need to perform more advanced list manipulation like");
684 help_menu->items[1] = strsave("adding lists, or changing list characteristics, refer to the");
685 help_menu->items[2] = strsave("program listmaint.");
686 help_menu->items[3] = strsave(" ");
687 help_menu->items[4] = strsave("Press any key to continue.");
690 /****************************************************/
696 if (oldpos[level] != position[level]) {
697 move(STARTROW + oldpos[level] - 1, STARTCOL);
699 addstr(menu->items[oldpos[level] - 1]);
703 move(STARTROW + position[level] - 1, STARTCOL);
705 addstr(menu->items[position[level] - 1]);
711 /****************************************************/
715 move(0, MAX(0, (COLS - strlen(buff)) >> 1));
722 /****************************************************/
723 center_text(row, buff)
727 move(row, MAX(0, (COLS - strlen(buff)) >> 1));
732 /****************************************************/
733 show_text(row, col, buff)
737 mvcur(0, 0, row, col);
742 /****************************************************/
749 buff = calloc((unsigned)COLS, 1);
750 for (i = 0; i <= COLS - 2; i++)
752 buff[i] = 0; /* just to be sure ! */
754 mvcur(0, 0, row, col);
759 /****************************************************/
766 /****************************************************/
776 maxcol = 80; /* limit width */
778 buff = calloc((unsigned)maxcol + 1, 1);
779 for (i = 0; i <= maxcol - 1; i++)
781 buff[i] = 0; /* just to be sure ! */
782 mvcur(0, 0, erase_row, STARTCOL);
784 for (i = erase_row; i <= currow - 1; i++) {
785 printf("%s\n\r", buff);
788 mvcur(erase_row, STARTCOL, STARTROW + oldpos[level] - 1, STARTCOL);
792 /****************************************************/
797 com_err(whoami, status, "\nAn SMS update returned a value -- programmer \
803 /****************************************************/
806 fetch_list_info(list, li)
813 return sms_query("get_list_info", 1, argv, get_list_info, (char *) NULL);
818 get_list_info(argc, argv)
824 free(current_li->acl);
825 current_li->acl = strsave(argv[2]);
826 if (current_li->desc)
827 free(current_li->desc);
828 current_li->desc = strsave(argv[3]);
829 if (current_li->expdate)
830 free(current_li->expdate);
831 current_li->expdate = strsave(argv[4]);
838 /****************************************************/
839 /* Prompt the user for input */
841 Prompt(prompt, buf, buflen)
850 printf("%s", prompt);
852 for (p = buf; abs(strlen(p) - strlen(buf)) <= buflen;) {
862 display_menu(main_menu);
868 if (strlen(buf) < 1)/* only \n or \r in buff */
889 if (abs(strlen(p) - strlen(buf)) >= buflen) {
899 (void) putchar(CTL('G'));
908 * Hook function to cause error messages to be printed through
909 * curses instead of around it.
913 menu_err_hook(who, code, fmt, args)
919 char buf[BUFSIZ], *cp;
923 (void) strcpy(buf, who);
924 for (cp = buf; *cp; cp++);
928 (void) strcpy(cp, error_message(code));
932 _strbuf._flag = _IOWRT + _IOSTRG;
934 _strbuf._cnt = BUFSIZ - (cp - buf);
935 _doprnt(fmt, args, &_strbuf);
936 (void) putc('\0', &_strbuf);
945 * c-continued-statement-offset: 4
947 * c-argdecl-indent: 4