7 static char rcsid_mailmaint_c[] = "$Header$";
11 /***********************************************************************/
12 /* mailmaint.c - pjlevine - 20 August 1987
15 /***********************************************************************/
22 #include </usr/include/sys/types.h>
24 #include "mit-copyright.h"
32 #define CTL(ch) ((ch) & 037)
33 #define MAX(A,B) ((A) > (B) ? (A) : (B))
39 static int bool_answer();
41 typedef struct list_info {
47 static char *ascbuff={"0123456789"};
48 static int print_2(), print_1();
49 static List_info *current_li = (List_info *) NULL;
50 static int get_list_info();
51 static int fetch_list_info();
60 typedef struct _menu {
66 MENU *main_menu, *help_menu;
68 int position[2], oldpos[2];
69 int level, found_some, currow, page, num_members;
70 int moreflg, toggle, first_time;
74 /****************************************************/
87 if (getenv("TERM") == (char *) NULL) {
88 printf("Please set your TERM environment variable\n");
91 if (getenv("TERMCAP") == (char *) NULL) {
92 printf("Please set your TERMCAP environment variable\n");
95 if ((whoami = rindex(argv[0], '/')) == NULL)
99 uname = (char *) calloc(20, 1);
100 if ((current_li = (List_info *) malloc(sizeof(List_info)))
101 == (List_info *) NULL) {
102 (void) sprintf(buf, ": allocating list info");
105 current_li->acl = (char *) NULL;
106 current_li->desc = (char *) NULL;
107 current_li->expdate = (char *) NULL;
109 if ((uname = getlogin()) == NULL) {
110 struct passwd *getpwuid();
111 uname = getpwuid(getuid())->pw_name;
113 uname = (uname && strlen(uname))? ds(uname): "";
115 printf("Connecting to database for %s...please hold on.\n", uname);
117 status = sms_connect();
119 (void) sprintf(buf, "\nConnection to SMS server failed");
125 (void) sprintf(buf, "\nAuthorization failed.\n");
131 if ((LINES < 24) || (COLS < 60)) {
132 printf("Display window too small.\n\n");
134 printf("Current window parameters are (%d lines, %d columns)\n", LINES, COLS);
135 printf("Please resize your window\n");
136 printf("to at least 24 lines and 60 columns.\n");
141 position[0] = oldpos[0] = 1;
145 display_menu(main_menu);
154 com_err(whoami, status, buf);
155 /* sms_disconnect(); */
159 /****************************************************/
166 oldpos[level] = position[level];
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);
183 case 'q': case 'Q': /*quit*/
185 highlight(main_menu);
191 case '1': /* show all lists */
197 case '2': /* get all members of a list */
203 case '3': /* display list which user is a recipient */
209 case '4': /* show description */
215 case '5': /* add to list */
221 case '6': /* delete */
227 case 27: /* up arrow */
233 if (!position[level])
239 if (position[level] > 7)
249 highlight(main_menu);
252 /****************************************************/
259 show_text(DISPROW, STARTCOL, "Show information about a list.\n\r");
260 buf = (char *) calloc(1024, 1);
261 if (Prompt("Enter List Name: ", buf, LISTSIZE)==1) {
263 if (fetch_list_info(buf, current_li) == 0) {
264 (void) sprintf(buf, "Description: %s\n\r", current_li->desc);
265 if (strlen(buf) > 60)
268 show_text(currow, STARTCOL, buf);
270 (void) sprintf(buf, "List Administrator: %s", current_li->acl);
271 show_text(currow, STARTCOL, buf);
273 (void) sprintf(buf, "Expiration date: %s", current_li->expdate);
274 show_text(currow, STARTCOL, buf);
278 show_text(currow, STARTCOL, "madm: No such list found.");
281 show_text(currow, STARTCOL, "Press any Key to continue...");
286 /****************************************************/
299 printbuf = (char *) calloc(maxcol, 1);
300 for(i=0; i<=strlen(buf); i++) {
301 printbuf[cnt] = buf[i];
304 start_display_buff(printbuf);
307 printbuf = (char *) calloc(maxcol, 1);
310 if (strlen(buf)%maxcol != 0) {
311 start_display_buff(printbuf);
316 /****************************************************/
317 start_display_buff(buff)
326 if (currow >= LINES-2) {
329 mvcur(0,0,currow,STARTCOL);
331 if (Prompt("--RETURN for more, ctl-c to exit--", buffer, 1) == 0) {
332 printf("Flushing query...");
338 show_text(currow, STARTCOL, "continued");
341 show_text(currow, STARTCOL, buff);
344 /****************************************************/
351 show_text(DISPROW, STARTCOL, "Add yourself to a list\n\r");
352 buf = (char *) calloc(LISTMAX, 1);
353 if (Prompt("Enter List Name: ", buf, LISTSIZE)==1) {
355 argv[0] = strsave(buf);
356 argv[1] = strsave("user");
357 argv[2] = strsave(uname);
358 if (status = sms_query("add_member_to_list", 3, argv,
359 scream, (char *) NULL)) {
361 com_err(whoami, status, " found.\n");
364 sprintf(buf, "User %s added to list\r", uname);
365 show_text(DISPROW+3, STARTCOL, buf);
368 show_text(DISPROW+4, STARTCOL, "Press any Key to continue...");
374 /****************************************************/
381 show_text(DISPROW, STARTCOL, "Remove yourself from a list\n\r");
382 buf = (char *) calloc(LISTMAX, 1);
383 if (Prompt("Enter List Name: ", buf, LISTSIZE)==1) {
385 argv[0] = strsave(buf);
386 argv[1] = strsave("user");
387 argv[2] = strsave(uname);
388 if (status = sms_query("delete_member_from_list", 3, argv,
389 scream, (char *) NULL)) {
391 com_err(whoami, status, " found.\n");
394 sprintf(buf, "User %s deleted from list\r", uname);
395 show_text(DISPROW+3, STARTCOL, buf);
398 show_text(DISPROW+4, STARTCOL, "Press any Key to continue...");
403 /****************************************************/
404 list_by_member(argc, argv)
411 argv[1] = strsave("user");
412 argv[2] = strsave(uname);
413 buf = (char *) calloc(BUFSIZ, 1);
414 sprintf(buf, "%s is on the following lists:\r", uname);
415 show_text(DISPROW, STARTCOL, buf);
416 mvcur(0,0,currow, STARTCOL);
418 if (status = sms_query("get_lists_of_member", 2, argv+1,
419 print_1, (char *) NULL)) {
421 com_err(whoami, status, " in get_lists_of_member");
423 show_text(currow, STARTCOL, "Press any Key to continue...");
428 /****************************************************/
433 show_text(DISPROW, STARTCOL, "This function may take a while...proceed[y]?");
435 if (c == 'n' || c == 'N') {
436 erase_line(DISPROW, STARTCOL);
439 move(DISPROW+1, STARTCOL);
440 addstr("Processing query...please hold");
445 /****************************************************/
447 print_1(argc, argv, callback)
449 char *argv[], *callback;
453 /* no newline 'cause Put_message adds one */
454 (void) sprintf(buf, "%s\r", argv[0]);
459 /****************************************************/
461 print_all(argc, argv, callback)
463 char *argv[], *callback;
469 erase_line(DISPROW+1, STARTCOL);
470 show_text(DISPROW+1, STARTCOL, "All mailing lists:");
473 (void) sprintf(buf, "%s\r", argv[0]);
479 /****************************************************/
484 if (status = sms_query("get_all_visible_maillists", 0, (char **) NULL,
485 print_all, (char *) NULL)) {
487 com_err(whoami, status, " in list_all_groups\n");
491 return (DM_NORMAL); /* HA! */
493 /****************************************************/
501 move(DISPROW, STARTCOL);
502 mvcur(0,0,DISPROW,STARTCOL);
504 buf = (char *) calloc(LISTMAX, 1);
505 if (Prompt("Enter List Name: ", buf, LISTSIZE)==1) {
506 sprintf(buffer, "The members of list '%s' are:", buf);
507 show_text(DISPROW+1, STARTCOL, buffer);
509 if (status = sms_query("get_members_of_list", 1, argv,
510 print_2, (char *) NULL)) {
512 com_err(whoami, status, " found.\n");
516 show_text(currow, STARTCOL,"List is empty (no members).");
523 return (DM_NORMAL); /* HA! */
526 /****************************************************/
528 print_2(argc, argv, callback)
530 char *argv[], *callback;
535 (void) sprintf(buf, "%s %s", argv[0], argv[1]);
540 /****************************************************/
550 buffer = (char *) calloc(50, 1);
551 if (currow >= LINES-2) {
554 mvcur(0,0,currow,STARTCOL);
556 if (Prompt("--RETURN for more, ctl-c to exit--", buffer, 1) == 0) {
557 printf("Flushing query...");
563 sprintf(buffer, "Continued (Page %d)", page);
564 show_text(currow, STARTCOL, buffer);
569 show_text(currow, STARTCOL, buff);
571 show_text(currow, SECONDCOL, buff);
576 /****************************************************/
586 buffer = (char *) calloc(50, 1);
588 sprintf(buffer, "End of List. %d Total Members\r", num_members);
589 show_text(currow, STARTCOL, buffer);
591 show_text(currow, STARTCOL, "Press any key to continue...");
596 /****************************************************/
604 mvcur(0,0,STARTROW, STARTCOL);
606 for(i=0; i<=menu->num_items-1; i++) {
607 move(STARTROW+i, STARTCOL);
609 addstr(menu->items[i]);
612 center_text(STARTROW+menu->num_items+2,
613 "Enter a number, <up arrow>, or <down arrow>.");
615 center_text(STARTROW+menu->num_items+3,
616 "Press 'q' to exit, <return> to confirm choice.");
618 center_text(STARTROW+menu->num_items+3,
619 "Press 'q' to exit, 'r' for main menu, <return> to confirm choice.");
622 highlight(main_menu);
625 /****************************************************/
630 main_menu = (MENU *) malloc((unsigned) sizeof(MENU));
631 main_menu->num_items = 7;
632 main_menu->items = (char **) malloc((unsigned) sizeof(char *) * main_menu->num_items);
634 buf = (char *) calloc(50, 1);
635 sprintf(buf, "Mail List Program for %s", uname);
636 main_menu->title = strsave(buf);
637 main_menu->items[0] = strsave("1. Show all mailing lists.");
638 main_menu->items[1] = strsave("2. Get all members of a mailing list.");
639 main_menu->items[2] = strsave("3. Display lists of which you are a member.");
640 main_menu->items[3] = strsave("4. Show description of list.");
641 main_menu->items[4] = strsave("5. Add yourself to a mailing list.");
642 main_menu->items[5] = strsave("6. Delete yourself from a mailing list.");
643 main_menu->items[6] = strsave("q. Quit.");
646 /****************************************************/
649 help_menu = (MENU *) malloc((unsigned) sizeof(MENU));
650 help_menu->num_items = 5;
651 help_menu->items = (char **) malloc((unsigned) sizeof(char *) * help_menu->num_items);
653 help_menu->title = strsave("madm is designed as a basic mail list administration program.");
654 help_menu->items[0] = strsave("if you need to perform more advanced list manipulation like");
655 help_menu->items[1] = strsave("adding lists, or changing list characteristics, refer to the");
656 help_menu->items[2] = strsave("program listmaint.");
657 help_menu->items[3] = strsave(" ");
658 help_menu->items[4] = strsave("Press any key to continue.");
660 /****************************************************/
666 show_text(currow, STARTCOL, help_menu->title);
667 for(i=0;i<=help_menu->num_items-1;i++) {
669 show_text(currow, STARTCOL, help_menu->items[i]);
674 /****************************************************/
680 if (oldpos[level] != position[level]) {
681 move(STARTROW+oldpos[level]-1, STARTCOL);
683 addstr(menu->items[oldpos[level]-1]);
687 move(STARTROW+position[level]-1, STARTCOL);
689 addstr(menu->items[position[level]-1]);
695 /****************************************************/
699 move(0, MAX(0, (COLS - strlen(buff))>>1));
705 /****************************************************/
711 mvcur(0,0,row,STARTCOL);
716 mvcur(0,0,row,STARTCOL);
724 /****************************************************/
725 center_text(row, buff)
729 move(row, MAX(0, (COLS - strlen(buff))>>1));
734 /****************************************************/
735 show_text(row, col, buff)
744 /****************************************************/
751 buff = (char *) calloc(COLS, 1);
752 for(i=0; i<=COLS-2; i++)
754 buff[i] = 0; /* just to be sure ! */
761 /****************************************************/
768 /****************************************************/
778 maxcol = 80; /* limit width */
780 buff = (char *) calloc(maxcol+1, 1);
781 for(i=0; i<=maxcol-1; i++)
783 buff[i] = 0; /* just to be sure ! */
784 mvcur(0,0,erase_row, STARTCOL);
786 for(i=erase_row; i<=currow-1; i++) {
787 printf("%s\n\r", buff);
790 mvcur(erase_row,STARTCOL,STARTROW+oldpos[level]-1, STARTCOL);
794 /****************************************************/
800 if ((p = (char *) calloc(1, strlen(s)+1)) != NULL) {
804 printf("error in alloc\n\r");
807 /****************************************************/
808 /* jean's routine ??? */
811 com_err(whoami, status, "\nAn SMS update returned a value -- programmer \
816 /****************************************************/
817 static int fetch_list_info(list, li)
824 if ((status = sms_query("get_list_info", 1, argv,
825 get_list_info, (char *) NULL)) == 0) {
829 com_err(whoami, status, " found.\n");
835 static int get_list_info(argc, argv)
841 free(current_li->acl);
842 current_li->acl = strsave(argv[2]);
843 if (current_li->desc)
844 free(current_li->desc);
845 current_li->desc = strsave(argv[3]);
846 if (current_li->expdate)
847 free(current_li->expdate);
848 current_li->expdate = strsave(argv[4]);
855 /****************************************************/
856 /* Prompt the user for input */
857 int Prompt(prompt, buf, buflen)
866 printf("%s", prompt);
868 for (p = buf; abs(strlen(p)-strlen(buf)) <= buflen;) {
878 display_menu(main_menu);
884 if (strlen(buf) < 1) /* only /n or /r in buff */
905 if (abs(strlen(p)-strlen(buf)) >= buflen) {
926 register char *newstr = malloc((unsigned) strlen(str) + 1);
928 if (newstr == (char *) NULL)
929 return ((char *) NULL);
931 return (strcpy(newstr, str));
934 int bool_query(name, argc, argv)
940 sms_query(name, argc, argv, bool_answer, (caddr_t) &result);
944 static int bool_answer(argc, argv, arg)
949 int *result = (int *) arg;
950 if (strcmp(argv[0], "true") == 0)
952 else if (strcmp(argv[0], "false") == 0)
961 * c-continued-statement-offset: 4
963 * c-argdecl-indent: 4