7 static char rcsid_mailmaint_c[] = "$Header$";
11 /***********************************************************************/
12 /* mailmaint.c - pjlevine - 20 August 1987
15 /***********************************************************************/
22 #include <sys/types.h>
24 #include "mit-copyright.h"
32 #define CTL(ch) ((ch) & 037)
33 #define MAX(A,B) ((A) > (B) ? (A) : (B))
40 typedef struct list_info {
46 static char *ascbuff = {"0123456789"};
49 static List_info *current_li = (List_info *) NULL;
50 static int get_list_info();
51 static int fetch_list_info();
56 extern char *strsave();
62 typedef struct _menu {
68 MENU *main_menu, *help_menu;
70 int position[2], oldpos[2];
71 int level, found_some, currow, page, num_members;
72 int moreflg, toggle, first_time;
76 /****************************************************/
90 if ((whoami = rindex(argv[0], '/')) == NULL)
94 uname = calloc(20, 1);
95 if ((current_li = (List_info *) malloc(sizeof(List_info)))
96 == (List_info *) NULL) {
97 (void) sprintf(buf, ": allocating list info");
101 current_li->acl = (char *) NULL;
102 current_li->desc = (char *) NULL;
103 current_li->expdate = (char *) NULL;
105 if ((uname = getlogin()) == NULL) {
106 struct passwd *getpwuid();
108 uname = getpwuid((int) getuid())->pw_name;
110 uname = (uname && strlen(uname)) ? ds(uname) : "";
112 printf("Connecting to database for %s...please hold on.\n", uname);
114 status = sms_connect();
116 (void) sprintf(buf, "\nConnection to SMS server failed");
122 (void) sprintf(buf, "\nAuthorization failed.\n");
128 if ((LINES < 24) || (COLS < 60)) {
129 Put_message("Display window too small.\n\n");
130 (void) sprintf(buf, "Current window parameters are (%d \
131 lines, %d columns)\n", LINES, COLS);
133 Put_message("Please resize your window\n");
134 Put_message("to at least 24 lines and 60 columns.\n");
139 position[0] = oldpos[0] = 1;
143 display_menu(main_menu);
151 com_err(whoami, status, buf);
155 /****************************************************/
162 oldpos[level] = position[level];
164 currow = DISPROW + 2;
166 toggle = num_members = moreflg = 0;
169 if (position[level] == 7)
172 c = ascbuff[position[level]];
176 case 'L' & 037: /* clear screen */
177 display_menu(main_menu);
182 highlight(main_menu);
188 case '1': /* show all lists */
194 case '2': /* get all members of a list */
197 (void) list_members();
200 case '3': /* display list which user is a recipient */
206 case '4': /* show description */
212 case '5': /* add to list */
218 case '6': /* delete */
224 case 27: /* up arrow */
230 if (!position[level])
236 if (position[level] > 7)
246 highlight(main_menu);
250 /****************************************************/
256 show_text(DISPROW, STARTCOL, "Show information about a list.\n\r");
257 buf = calloc((unsigned)1024, 1);
258 if (Prompt("Enter List Name: ", buf, LISTSIZE) == 1) {
260 if (fetch_list_info(buf, current_li) == 0) {
261 (void) sprintf(buf, "Description: %s\n\r", current_li->desc);
262 if (strlen(buf) > 60)
263 (void) display_buff(buf);
265 show_text(currow, STARTCOL, buf);
267 (void) sprintf(buf, "List Administrator: %s", current_li->acl);
268 show_text(currow, STARTCOL, buf);
270 (void) sprintf(buf, "Expiration date: %s", current_li->expdate);
271 show_text(currow, STARTCOL, buf);
275 show_text(currow, STARTCOL, "madm: No such list found.");
278 show_text(currow, STARTCOL, "Press any Key to continue...");
284 /****************************************************/
297 printbuf = calloc((unsigned)maxcol, 1);
298 for (i = 0; i <= strlen(buf); i++) {
299 printbuf[cnt] = buf[i];
302 (void) start_display_buff(printbuf);
305 printbuf = calloc((unsigned)maxcol, 1);
308 if (strlen(buf) % maxcol != 0) {
309 (void) start_display_buff(printbuf);
315 /****************************************************/
316 start_display_buff(buff)
324 if (currow >= LINES - 2) {
327 mvcur(0, 0, currow, STARTCOL);
329 if (Prompt("--RETURN for more, ctl-c to exit--", buffer, 1) == 0) {
330 Put_message("Flushing query...");
335 currow = DISPROW + 2;
336 show_text(currow, STARTCOL, "continued");
339 show_text(currow, STARTCOL, buff);
344 /****************************************************/
347 static char *argv[] = {
348 "add_member_to_list",
356 show_text(DISPROW, STARTCOL, "Add yourself to a list\n\r");
357 buf = calloc(LISTMAX, 1);
358 if (Prompt("Enter List Name: ", buf, LISTSIZE) == 1) {
360 argv[1] = strsave(buf);
361 argv[2] = strsave("user");
362 argv[3] = strsave(uname);
363 if (status = sms_query_internal(3, argv, scream, (char *) NULL)) {
365 com_err(whoami, status, " found.\n");
368 (void) sprintf(buf, "User %s added to list\r", uname);
369 show_text(DISPROW + 3, STARTCOL, buf);
371 currow = DISPROW + 4;
372 show_text(DISPROW + 4, STARTCOL, "Press any Key to continue...");
378 /****************************************************/
381 static char *argv[] = {
382 "delete_member_from_list",
390 show_text(DISPROW, STARTCOL, "Remove yourself from a list\n\r");
391 buf = calloc(LISTMAX, 1);
392 if (Prompt("Enter List Name: ", buf, LISTSIZE) == 1) {
394 argv[1] = strsave(buf);
395 argv[2] = strsave("user");
396 argv[3] = strsave(uname);
397 if (status = sms_query_internal(3, argv, scream, (char *) NULL)) {
399 com_err(whoami, status, " found.\n");
402 (void) sprintf(buf, "User %s deleted from list\r", uname);
403 show_text(DISPROW + 3, STARTCOL, buf);
405 currow = DISPROW + 4;
406 show_text(DISPROW + 4, STARTCOL, "Press any Key to continue...");
412 /****************************************************/
419 nargv[1] = strsave("user");
420 nargv[2] = strsave(uname);
421 buf = calloc(BUFSIZ, 1);
422 (void) sprintf(buf, "%s is on the following lists:\r", uname);
423 show_text(DISPROW, STARTCOL, buf);
424 mvcur(0, 0, currow, STARTCOL);
426 if (status = sms_query("get_lists_of_member", 2, nargv + 1,
427 print_1, (char *) NULL)) {
429 com_err(whoami, status, " in get_lists_of_member");
431 show_text(currow, STARTCOL, "Press any Key to continue...");
437 /****************************************************/
442 show_text(DISPROW, STARTCOL, "This function may take a while...proceed[y]?");
444 if (c == 'n' || c == 'N') {
445 erase_line(DISPROW, STARTCOL);
448 move(DISPROW + 1, STARTCOL);
449 addstr("Processing query...please hold");
451 (void) list_all_groups();
454 /****************************************************/
457 print_1(argc, argv, callback)
459 char *argv[], *callback;
463 /* no newline 'cause Put_message adds one */
464 (void) sprintf(buf, "%s\r", argv[0]);
465 (void) start_display(buf);
470 /****************************************************/
473 print_all(argc, argv, callback)
475 char *argv[], *callback;
482 erase_line(DISPROW + 1, STARTCOL);
483 show_text(DISPROW + 1, STARTCOL, "All mailing lists:");
486 (void) sprintf(buf, "%s\r", argv[0]);
487 (void) start_display(buf);
492 /****************************************************/
497 if (status = sms_query("get_all_visible_maillists", 0, (char **) NULL,
498 print_all, (char *) NULL)) {
500 com_err(whoami, status, " in list_all_groups\n");
504 return (DM_NORMAL); /* HA! */
507 /****************************************************/
515 move(DISPROW, STARTCOL);
516 mvcur(0, 0, DISPROW, STARTCOL);
518 buf = calloc(LISTMAX, 1);
519 if (Prompt("Enter List Name: ", buf, LISTSIZE) == 1) {
520 (void) sprintf(buffer, "The members of list '%s' are:", buf);
521 show_text(DISPROW + 1, STARTCOL, buffer);
523 if (status = sms_query("get_members_of_list", 1, argv,
524 print_2, (char *) NULL)) {
526 com_err(whoami, status, " found.\n");
530 show_text(currow, STARTCOL, "List is empty (no members).");
537 return (DM_NORMAL); /* HA! */
540 /****************************************************/
543 print_2(argc, argv, callback)
545 char *argv[], *callback;
550 (void) sprintf(buf, "%s %s", argv[0], argv[1]);
551 (void) start_display(buf);
556 /****************************************************/
565 buffer = calloc(50, 1);
566 if (currow >= LINES - 2) {
569 mvcur(0, 0, currow, STARTCOL);
571 if (Prompt("--RETURN for more, ctl-c to exit--", buffer, 1) == 0) {
572 Put_message("Flushing query...");
577 currow = DISPROW + 2;
578 (void) sprintf(buffer, "Continued (Page %d)", page);
579 show_text(currow, STARTCOL, buffer);
584 show_text(currow, STARTCOL, buff);
586 show_text(currow, SECONDCOL, buff);
593 /****************************************************/
603 buffer = calloc(50, 1);
605 (void) sprintf(buffer, "End of List. %d Total Members\r", num_members);
606 show_text(currow, STARTCOL, buffer);
608 show_text(currow, STARTCOL, "Press any key to continue...");
614 /****************************************************/
622 mvcur(0, 0, STARTROW, STARTCOL);
624 for (i = 0; i <= menu->num_items - 1; i++) {
625 move(STARTROW + i, STARTCOL);
627 addstr(menu->items[i]);
630 center_text(STARTROW + menu->num_items + 2,
631 "Enter a number, <up arrow>, or <down arrow>.");
633 center_text(STARTROW + menu->num_items + 3,
634 "Press 'q' to exit, <return> to confirm choice.");
636 center_text(STARTROW + menu->num_items + 3,
637 "Press 'q' to exit, 'r' for main menu, <return> to confirm choice.");
640 highlight(main_menu);
643 /****************************************************/
648 main_menu = (MENU *) malloc((unsigned) sizeof(MENU));
649 main_menu->num_items = 7;
650 main_menu->items = (char **) malloc((unsigned) sizeof(char *) * main_menu->num_items);
653 (void) sprintf(buf, "Mail List Program for %s", uname);
654 main_menu->title = strsave(buf);
655 main_menu->items[0] = strsave("1. Show all mailing lists.");
656 main_menu->items[1] = strsave("2. Get all members of a mailing list.");
657 main_menu->items[2] = strsave("3. Display lists of which you are a member.");
658 main_menu->items[3] = strsave("4. Show description of list.");
659 main_menu->items[4] = strsave("5. Add yourself to a mailing list.");
660 main_menu->items[5] = strsave("6. Delete yourself from a mailing list.");
661 main_menu->items[6] = strsave("q. Quit.");
664 /****************************************************/
667 help_menu = (MENU *) malloc((unsigned) sizeof(MENU));
668 help_menu->num_items = 5;
669 help_menu->items = (char **) malloc((unsigned) sizeof(char *) * help_menu->num_items);
671 help_menu->title = strsave("madm is designed as a basic mail list administration program.");
672 help_menu->items[0] = strsave("if you need to perform more advanced list manipulation like");
673 help_menu->items[1] = strsave("adding lists, or changing list characteristics, refer to the");
674 help_menu->items[2] = strsave("program listmaint.");
675 help_menu->items[3] = strsave(" ");
676 help_menu->items[4] = strsave("Press any key to continue.");
679 /****************************************************/
685 if (oldpos[level] != position[level]) {
686 move(STARTROW + oldpos[level] - 1, STARTCOL);
688 addstr(menu->items[oldpos[level] - 1]);
692 move(STARTROW + position[level] - 1, STARTCOL);
694 addstr(menu->items[position[level] - 1]);
700 /****************************************************/
704 move(0, MAX(0, (COLS - strlen(buff)) >> 1));
711 /****************************************************/
712 center_text(row, buff)
716 move(row, MAX(0, (COLS - strlen(buff)) >> 1));
721 /****************************************************/
722 show_text(row, col, buff)
726 mvcur(0, 0, row, col);
731 /****************************************************/
738 buff = calloc((unsigned)COLS, 1);
739 for (i = 0; i <= COLS - 2; i++)
741 buff[i] = 0; /* just to be sure ! */
743 mvcur(0, 0, row, col);
748 /****************************************************/
755 /****************************************************/
765 maxcol = 80; /* limit width */
767 buff = calloc((unsigned)maxcol + 1, 1);
768 for (i = 0; i <= maxcol - 1; i++)
770 buff[i] = 0; /* just to be sure ! */
771 mvcur(0, 0, erase_row, STARTCOL);
773 for (i = erase_row; i <= currow - 1; i++) {
774 printf("%s\n\r", buff);
777 mvcur(erase_row, STARTCOL, STARTROW + oldpos[level] - 1, STARTCOL);
781 /****************************************************/
786 com_err(whoami, status, "\nAn SMS update returned a value -- programmer \
792 /****************************************************/
795 fetch_list_info(list, li)
802 if ((status = sms_query("get_list_info", 1, argv,
803 get_list_info, (char *) NULL)) == 0) {
807 com_err(whoami, status, " found.\n");
814 get_list_info(argc, argv)
820 free(current_li->acl);
821 current_li->acl = strsave(argv[2]);
822 if (current_li->desc)
823 free(current_li->desc);
824 current_li->desc = strsave(argv[3]);
825 if (current_li->expdate)
826 free(current_li->expdate);
827 current_li->expdate = strsave(argv[4]);
834 /****************************************************/
835 /* Prompt the user for input */
837 Prompt(prompt, buf, buflen)
846 printf("%s", prompt);
848 for (p = buf; abs(strlen(p) - strlen(buf)) <= buflen;) {
858 display_menu(main_menu);
864 if (strlen(buf) < 1)/* only \n or \r in buff */
885 if (abs(strlen(p) - strlen(buf)) >= buflen) {
895 (void) putchar(CTL('G'));
909 register char *newstr = malloc((unsigned) strlen(str) + 1);
911 if (newstr == (char *) NULL)
912 return ((char *) NULL);
914 return (strcpy(newstr, str));
921 * c-continued-statement-offset: 4
923 * c-argdecl-indent: 4