7 static char rcsid_mailmaint_c[] = "$Header$";
10 /***********************************************************************/
11 /* mailmaint.c - pjlevine - 20 August 1987
14 /***********************************************************************/
21 #include <sys/types.h>
23 #include "mit-copyright.h"
31 #define CTL(ch) ((ch) & 037)
32 #define MAX(A,B) ((A) > (B) ? (A) : (B))
38 static int bool_answer();
40 typedef struct list_info {
46 static char *ascbuff={"0123456789"};
47 static int print_2(), print_1();
48 static List_info *current_li = (List_info *) NULL;
49 static int get_list_info();
50 static int fetch_list_info();
59 typedef struct _menu {
65 MENU *main_menu, *help_menu;
67 int position[2], oldpos[2];
68 int level, found_some, currow, page, num_members;
69 int moreflg, toggle, first_time;
73 /****************************************************/
86 if (getenv("TERM") == (char *) NULL) {
87 printf("Please set your TERM environment variable\n");
90 if (getenv("TERMCAP") == (char *) NULL) {
91 printf("Please set your TERMCAP environment variable\n");
94 if ((whoami = rindex(argv[0], '/')) == NULL)
98 uname = (char *) calloc(20, 1);
99 if ((current_li = (List_info *) malloc(sizeof(List_info)))
100 == (List_info *) NULL) {
101 (void) sprintf(buf, ": allocating list info");
104 current_li->acl = (char *) NULL;
105 current_li->desc = (char *) NULL;
106 current_li->expdate = (char *) NULL;
108 if ((uname = getlogin()) == NULL) {
109 struct passwd *getpwuid();
110 uname = getpwuid(getuid())->pw_name;
112 uname = (uname && strlen(uname))? ds(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 printf("Display window too small.\n\n");
133 printf("Current window parameters are (%d lines, %d columns)\n", LINES, COLS);
134 printf("Please resize your window\n");
135 printf("to at least 24 lines and 60 columns.\n");
140 position[0] = oldpos[0] = 1;
144 display_menu(main_menu);
153 com_err(whoami, status, buf);
154 /* sms_disconnect(); */
158 /****************************************************/
165 oldpos[level] = position[level];
169 toggle = num_members = moreflg = 0;
172 if (position[level] == 7)
175 c = ascbuff[position[level]];
179 case 'L' & 037: /* clear screen */
180 display_menu(main_menu);
182 case 'q': case 'Q': /*quit*/
184 highlight(main_menu);
190 case '1': /* show all lists */
196 case '2': /* get all members of a list */
202 case '3': /* display list which user is a recipient */
208 case '4': /* show description */
214 case '5': /* add to list */
220 case '6': /* delete */
226 case 27: /* up arrow */
232 if (!position[level])
238 if (position[level] > 7)
248 highlight(main_menu);
251 /****************************************************/
258 show_text(DISPROW, STARTCOL, "Show information about a list.\n\r");
259 buf = (char *) calloc(1024, 1);
260 if (Prompt("Enter List Name: ", buf, LISTSIZE)==1) {
262 if (fetch_list_info(buf, current_li) == 0) {
263 (void) sprintf(buf, "Description: %s\n\r", current_li->desc);
264 if (strlen(buf) > 60)
267 show_text(currow, STARTCOL, buf);
269 (void) sprintf(buf, "List Administrator: %s", current_li->acl);
270 show_text(currow, STARTCOL, buf);
272 (void) sprintf(buf, "Expiration date: %s", current_li->expdate);
273 show_text(currow, STARTCOL, buf);
277 show_text(currow, STARTCOL, "madm: No such list found.");
280 show_text(currow, STARTCOL, "Press any Key to continue...");
285 /****************************************************/
298 printbuf = (char *) calloc(maxcol, 1);
299 for(i=0; i<=strlen(buf); i++) {
300 printbuf[cnt] = buf[i];
303 start_display_buff(printbuf);
306 printbuf = (char *) calloc(maxcol, 1);
309 if (strlen(buf)%maxcol != 0) {
310 start_display_buff(printbuf);
315 /****************************************************/
316 start_display_buff(buff)
325 if (currow >= LINES-2) {
328 mvcur(0,0,currow,STARTCOL);
330 if (Prompt("--RETURN for more, ctl-c to exit--", buffer, 1) == 0) {
331 printf("Flushing query...");
337 show_text(currow, STARTCOL, "continued");
340 show_text(currow, STARTCOL, buff);
343 /****************************************************/
350 show_text(DISPROW, STARTCOL, "Add yourself to a list\n\r");
351 buf = (char *) calloc(LISTMAX, 1);
352 if (Prompt("Enter List Name: ", buf, LISTSIZE)==1) {
354 argv[0] = strsave(buf);
355 argv[1] = strsave("user");
356 argv[2] = strsave(uname);
357 if (status = sms_query("add_member_to_list", 3, argv,
358 scream, (char *) NULL)) {
360 com_err(whoami, status, " found.\n");
363 sprintf(buf, "User %s added to list\r", uname);
364 show_text(DISPROW+3, STARTCOL, buf);
367 show_text(DISPROW+4, STARTCOL, "Press any Key to continue...");
373 /****************************************************/
380 show_text(DISPROW, STARTCOL, "Remove yourself from a list\n\r");
381 buf = (char *) calloc(LISTMAX, 1);
382 if (Prompt("Enter List Name: ", buf, LISTSIZE)==1) {
384 argv[0] = strsave(buf);
385 argv[1] = strsave("user");
386 argv[2] = strsave(uname);
387 if (status = sms_query("delete_member_from_list", 3, argv,
388 scream, (char *) NULL)) {
390 com_err(whoami, status, " found.\n");
393 sprintf(buf, "User %s deleted from list\r", uname);
394 show_text(DISPROW+3, STARTCOL, buf);
397 show_text(DISPROW+4, STARTCOL, "Press any Key to continue...");
402 /****************************************************/
403 list_by_member(argc, argv)
410 argv[1] = strsave("user");
411 argv[2] = strsave(uname);
412 buf = (char *) calloc(BUFSIZ, 1);
413 sprintf(buf, "%s is on the following lists:\r", uname);
414 show_text(DISPROW, STARTCOL, buf);
415 mvcur(0,0,currow, STARTCOL);
417 if (status = sms_query("get_lists_of_member", 2, argv+1,
418 print_1, (char *) NULL)) {
420 com_err(whoami, status, " in get_lists_of_member");
422 show_text(currow, STARTCOL, "Press any Key to continue...");
427 /****************************************************/
432 show_text(DISPROW, STARTCOL, "This function may take a while...proceed[y]?");
434 if (c == 'n' || c == 'N') {
435 erase_line(DISPROW, STARTCOL);
438 move(DISPROW+1, STARTCOL);
439 addstr("Processing query...please hold");
444 /****************************************************/
446 print_1(argc, argv, callback)
448 char *argv[], *callback;
452 /* no newline 'cause Put_message adds one */
453 (void) sprintf(buf, "%s\r", argv[0]);
458 /****************************************************/
460 print_all(argc, argv, callback)
462 char *argv[], *callback;
468 erase_line(DISPROW+1, STARTCOL);
469 show_text(DISPROW+1, STARTCOL, "All mailing lists:");
472 (void) sprintf(buf, "%s\r", argv[0]);
478 /****************************************************/
483 if (status = sms_query("get_all_visible_maillists", 0, (char **) NULL,
484 print_all, (char *) NULL)) {
486 com_err(whoami, status, " in list_all_groups\n");
490 return (DM_NORMAL); /* HA! */
492 /****************************************************/
500 move(DISPROW, STARTCOL);
501 mvcur(0,0,DISPROW,STARTCOL);
503 buf = (char *) calloc(LISTMAX, 1);
504 if (Prompt("Enter List Name: ", buf, LISTSIZE)==1) {
505 sprintf(buffer, "The members of list '%s' are:", buf);
506 show_text(DISPROW+1, STARTCOL, buffer);
508 if (status = sms_query("get_members_of_list", 1, argv,
509 print_2, (char *) NULL)) {
511 com_err(whoami, status, " found.\n");
515 show_text(currow, STARTCOL,"List is empty (no members).");
522 return (DM_NORMAL); /* HA! */
525 /****************************************************/
527 print_2(argc, argv, callback)
529 char *argv[], *callback;
534 (void) sprintf(buf, "%s %s", argv[0], argv[1]);
539 /****************************************************/
549 buffer = (char *) calloc(50, 1);
550 if (currow >= LINES-2) {
553 mvcur(0,0,currow,STARTCOL);
555 if (Prompt("--RETURN for more, ctl-c to exit--", buffer, 1) == 0) {
556 printf("Flushing query...");
562 sprintf(buffer, "Continued (Page %d)", page);
563 show_text(currow, STARTCOL, buffer);
568 show_text(currow, STARTCOL, buff);
570 show_text(currow, SECONDCOL, buff);
575 /****************************************************/
585 buffer = (char *) calloc(50, 1);
587 sprintf(buffer, "End of List. %d Total Members\r", num_members);
588 show_text(currow, STARTCOL, buffer);
590 show_text(currow, STARTCOL, "Press any key to continue...");
595 /****************************************************/
603 mvcur(0,0,STARTROW, STARTCOL);
605 for(i=0; i<=menu->num_items-1; i++) {
606 move(STARTROW+i, STARTCOL);
608 addstr(menu->items[i]);
611 center_text(STARTROW+menu->num_items+2,
612 "Enter a number, <up arrow>, or <down arrow>.");
614 center_text(STARTROW+menu->num_items+3,
615 "Press 'q' to exit, <return> to confirm choice.");
617 center_text(STARTROW+menu->num_items+3,
618 "Press 'q' to exit, 'r' for main menu, <return> to confirm choice.");
621 highlight(main_menu);
624 /****************************************************/
629 main_menu = (MENU *) malloc((unsigned) sizeof(MENU));
630 main_menu->num_items = 7;
631 main_menu->items = (char **) malloc((unsigned) sizeof(char *) * main_menu->num_items);
633 buf = (char *) calloc(50, 1);
634 sprintf(buf, "Mail List Program for %s", uname);
635 main_menu->title = strsave(buf);
636 main_menu->items[0] = strsave("1. Show all mailing lists.");
637 main_menu->items[1] = strsave("2. Get all members of a mailing list.");
638 main_menu->items[2] = strsave("3. Display lists of which you are a member.");
639 main_menu->items[3] = strsave("4. Show description of list.");
640 main_menu->items[4] = strsave("5. Add yourself to a mailing list.");
641 main_menu->items[5] = strsave("6. Delete yourself from a mailing list.");
642 main_menu->items[6] = strsave("q. Quit.");
645 /****************************************************/
648 help_menu = (MENU *) malloc((unsigned) sizeof(MENU));
649 help_menu->num_items = 5;
650 help_menu->items = (char **) malloc((unsigned) sizeof(char *) * help_menu->num_items);
652 help_menu->title = strsave("madm is designed as a basic mail list administration program.");
653 help_menu->items[0] = strsave("if you need to perform more advanced list manipulation like");
654 help_menu->items[1] = strsave("adding lists, or changing list characteristics, refer to the");
655 help_menu->items[2] = strsave("program listmaint.");
656 help_menu->items[3] = strsave(" ");
657 help_menu->items[4] = strsave("Press any key to continue.");
659 /****************************************************/
665 show_text(currow, STARTCOL, help_menu->title);
666 for(i=0;i<=help_menu->num_items-1;i++) {
668 show_text(currow, STARTCOL, help_menu->items[i]);
673 /****************************************************/
679 if (oldpos[level] != position[level]) {
680 move(STARTROW+oldpos[level]-1, STARTCOL);
682 addstr(menu->items[oldpos[level]-1]);
686 move(STARTROW+position[level]-1, STARTCOL);
688 addstr(menu->items[position[level]-1]);
694 /****************************************************/
698 move(0, MAX(0, (COLS - strlen(buff))>>1));
704 /****************************************************/
710 mvcur(0,0,row,STARTCOL);
715 mvcur(0,0,row,STARTCOL);
723 /****************************************************/
724 center_text(row, buff)
728 move(row, MAX(0, (COLS - strlen(buff))>>1));
733 /****************************************************/
734 show_text(row, col, buff)
743 /****************************************************/
750 buff = (char *) calloc(COLS, 1);
751 for(i=0; i<=COLS-2; i++)
753 buff[i] = 0; /* just to be sure ! */
760 /****************************************************/
767 /****************************************************/
777 maxcol = 80; /* limit width */
779 buff = (char *) calloc(maxcol+1, 1);
780 for(i=0; i<=maxcol-1; i++)
782 buff[i] = 0; /* just to be sure ! */
783 mvcur(0,0,erase_row, STARTCOL);
785 for(i=erase_row; i<=currow-1; i++) {
786 printf("%s\n\r", buff);
789 mvcur(erase_row,STARTCOL,STARTROW+oldpos[level]-1, STARTCOL);
793 /****************************************************/
799 if ((p = (char *) calloc(1, strlen(s)+1)) != NULL) {
803 printf("error in alloc\n\r");
806 /****************************************************/
807 /* jean's routine ??? */
810 com_err(whoami, status, "\nAn SMS update returned a value -- programmer \
815 /****************************************************/
816 static int fetch_list_info(list, li)
823 if ((status = sms_query("get_list_info", 1, argv,
824 get_list_info, (char *) NULL)) == 0) {
828 com_err(whoami, status, " found.\n");
834 static int get_list_info(argc, argv)
840 free(current_li->acl);
841 current_li->acl = strsave(argv[2]);
842 if (current_li->desc)
843 free(current_li->desc);
844 current_li->desc = strsave(argv[3]);
845 if (current_li->expdate)
846 free(current_li->expdate);
847 current_li->expdate = strsave(argv[4]);
854 /****************************************************/
855 /* Prompt the user for input */
856 int Prompt(prompt, buf, buflen)
865 printf("%s", prompt);
867 for (p = buf; abs(strlen(p)-strlen(buf)) <= buflen;) {
877 display_menu(main_menu);
883 if (strlen(buf) < 1) /* only /n or /r in buff */
904 if (abs(strlen(p)-strlen(buf)) >= buflen) {
925 register char *newstr = malloc((unsigned) strlen(str) + 1);
927 if (newstr == (char *) NULL)
928 return ((char *) NULL);
930 return (strcpy(newstr, str));
933 int bool_query(name, argc, argv)
939 sms_query(name, argc, argv, bool_answer, (caddr_t) &result);
943 static int bool_answer(argc, argv, arg)
948 int *result = (int *) arg;
949 if (strcmp(argv[0], "true") == 0)
951 else if (strcmp(argv[0], "false") == 0)
960 * c-continued-statement-offset: 4
962 * c-argdecl-indent: 4