3 * Simple add-me-to/remove-me-from list client
5 * mailmaint.c - pjlevine - 20 August 1987
7 * Copyright (C) 1988-1998 by the Massachusetts Institute of Technology.
8 * For copying and distribution information, please see the file
12 #include <mit-copyright.h>
14 #include <moira_site.h>
37 #define INPUT_MASK 0xff
41 #define getchar() _getch()
45 static void DELETE_A_CHAR(void)
53 #define INPUT_MASK 0x7f
54 #define DELETE_A_CHAR() printf("\b \b");
64 #define CTL(ch) ((ch) & 037)
68 #define MAX(A, B) ((A) > (B) ? (A) : (B))
70 char *whoami; /* should not be static, for logging package */
73 typedef struct list_info {
87 static char *ascbuff = {"0123456789"};
88 static List_info *current_li = (List_info *) NULL;
90 typedef struct _menu {
96 MENU *main_menu, *help_menu;
98 int position[2], oldpos[2];
99 int level, found_some, currow, page, num_members;
100 int moreflg, toggle, first_time;
103 void get_main_input(void);
104 void show_list_info(void);
105 void display_buff(char *buf);
106 void start_display_buff(char *buff);
107 void add_member(void);
108 void delete_member(void);
109 void list_by_member(void);
111 static int print_1(int argc, char *argv[], void *callback);
112 static int print_all(int argc, char *argv[], void *callback);
113 void list_all_groups(void);
114 void list_members(void);
115 static int print_2(int argc, char *argv[], void *callback);
116 void start_display(char *buff);
117 void end_display(void);
118 void display_menu(MENU *menu);
119 void pack_main_menu(void);
120 void pack_help_menu(void);
121 void free_menu(MENU* menu);
122 void highlight(MENU *menu);
123 void title(char *buff);
124 void center_text(int row, char *buff);
125 void show_text(int row, int col, char *buff);
126 void erase_line(int row, int col);
128 void clrwin(int erase_row);
129 static int fetch_list_info(char *list, List_info *li);
130 static int get_list_info(int argc, char **argv, void *hint);
131 int Prompt(char *prompt, char *buf, int buflen, int crok);
132 void menu_err_hook(const char *who, long code, const char *fmt, va_list args);
134 /* This crock is because the original code was very broken and this makes
135 * it work. Someday, we should abandon the code or fix it right.
137 #define mvcur(oy, ox, ny, nx) move(ny, nx)
139 /****************************************************/
141 int main(int argc, char *argv[])
143 void (*old_hook)(const char *, long, const char *, va_list);
144 int use_menu = 1, k_errno;
147 if ((whoami = strrchr(argv[0], '/')) == NULL)
151 if (!(current_li = malloc(sizeof(List_info))))
153 sprintf(buf, ": allocating list info");
158 current_li->acl_type = NULL;
159 current_li->acl_name = NULL;
160 current_li->desc = NULL;
161 current_li->modtime = NULL;
162 current_li->modby = NULL;
163 current_li->modwith = NULL;
166 username = mrcl_krb_user();
170 if (mrcl_connect(NULL, "mailmaint", 2, 1))
174 if ((LINES < 24) || (COLS < 60))
176 display_buff("Display window too small.\n\n");
177 sprintf(buf, "Current window parameters are (%d lines, %d columns)\n",
180 display_buff("Please resize your window\n");
181 display_buff("to at least 24 lines and 60 columns.\n");
187 old_hook = set_com_err_hook(menu_err_hook);
188 position[0] = oldpos[0] = 1;
193 display_menu(main_menu);
197 set_com_err_hook(old_hook);
199 free_menu(main_menu);
200 free_menu(help_menu);
202 if (current_li->acl_type)
203 free(current_li->acl_type);
204 if (current_li->acl_name)
205 free(current_li->acl_name);
206 if (current_li->desc)
207 free(current_li->desc);
208 if (current_li->modtime)
209 free(current_li->modtime);
210 if (current_li->modby)
211 free(current_li->modby);
212 if (current_li->modwith)
213 free(current_li->modwith);
221 com_err(whoami, status, buf);
225 /****************************************************/
226 void get_main_input(void)
233 oldpos[level] = position[level];
235 currow = DISPROW + 2;
237 toggle = num_members = moreflg = 0;
238 c = getchar() & INPUT_MASK; /* mask parity bit */
239 if (c == '\r' || c == '\n')
241 if (position[level] == 7)
244 c = ascbuff[position[level]];
249 case 'L' & 037: /* clear screen */
250 display_menu(main_menu);
255 highlight(main_menu);
262 case '1': /* show all lists */
267 case '2': /* get all members of a list */
272 case '3': /* display list which user is a recipient */
277 case '4': /* show description */
282 case '5': /* add to list */
287 case '6': /* delete */
293 case 27: /* escape */
294 c = getchar() & INPUT_MASK;
297 c = getchar() & INPUT_MASK;
298 if (c == 65) /* up arrow */
301 if (!position[level])
306 if (c == 66) /* down arrow */
309 if (position[level] > 7)
316 c = getchar() & INPUT_MASK;
317 if (c == 0x48) /* up arrow */
320 if (!position[level])
325 if (c == 0x50) /* down arrow */
328 if (position[level] > 7)
338 highlight(main_menu);
342 /****************************************************/
343 void show_list_info(void)
347 show_text(DISPROW, STARTCOL, "Show information about a list.\n");
348 buf = calloc(1024, 1);
349 if (Prompt("Enter List Name: ", buf, LISTSIZE, 1) == 1)
352 if (fetch_list_info(buf, current_li) == 0)
354 sprintf(buf, "Description: %s\n", current_li->desc);
355 if (strlen(buf) > 60)
358 show_text(currow, STARTCOL, buf);
360 sprintf(buf, "List Administrator: %s %s",
361 current_li->acl_type, current_li->acl_name);
362 show_text(currow, STARTCOL, buf);
364 sprintf(buf, "Modified on %s by user %s with %s",
365 current_li->modtime, current_li->modby,
366 current_li->modwith);
367 show_text(currow, STARTCOL, buf);
372 show_text(currow, STARTCOL, "mailmaint: No such list found.");
375 show_text(currow, STARTCOL, "Press any Key to continue...");
382 /****************************************************/
383 void display_buff(char *buf)
386 char *printbuf = NULL;
393 printbuf = calloc(maxcol, 1);
395 for (i = 0; i <= len; i++)
397 printbuf[cnt] = buf[i];
401 start_display_buff(printbuf);
404 printbuf = calloc(maxcol, 1);
407 if (len % maxcol != 0)
409 start_display_buff(printbuf);
416 /****************************************************/
417 void start_display_buff(char *buff)
424 if (currow >= LINES - 2)
428 mvcur(0, 0, currow, STARTCOL);
430 if (Prompt("--RETURN for more, ctl-c to exit--", buffer, 1, 0) == 0)
432 erase_line(currow, STARTCOL);
433 show_text(currow, STARTCOL, "Flushing query...");
438 currow = DISPROW + 2;
439 show_text(currow, STARTCOL, "continued");
442 show_text(currow, STARTCOL, buff);
447 /****************************************************/
448 void add_member(void)
453 show_text(DISPROW, STARTCOL, "Add yourself to a list\n");
454 buf = calloc(LISTMAX, 1);
455 if (Prompt("Enter List Name: ", buf, LISTSIZE, 1) == 1)
458 argv[0] = strdup(buf);
459 argv[1] = strdup("user");
460 argv[2] = strdup(username);
461 if ((status = mr_query("add_member_to_list", 3, argv, NULL, NULL)))
464 com_err(whoami, status, " found.\n");
468 sprintf(buf, "User %s added to list\n", username);
469 show_text(DISPROW + 3, STARTCOL, buf);
471 currow = DISPROW + 4;
472 show_text(DISPROW + 4, STARTCOL, "Press any Key to continue...");
479 /****************************************************/
480 void delete_member(void)
485 show_text(DISPROW, STARTCOL, "Remove yourself from a list\n");
486 buf = calloc(LISTMAX, 1);
487 if (Prompt("Enter List Name: ", buf, LISTSIZE, 1) == 1)
490 argv[0] = strdup(buf);
491 argv[1] = strdup("user");
492 argv[2] = strdup(username);
493 if ((status = mr_query("delete_member_from_list", 3, argv, NULL, NULL)))
496 com_err(whoami, status, " found.\n");
500 sprintf(buf, "User %s deleted from list\n", username);
501 show_text(DISPROW + 3, STARTCOL, buf);
503 currow = DISPROW + 4;
504 show_text(DISPROW + 4, STARTCOL, "Press any Key to continue...");
514 /****************************************************/
515 void list_by_member(void)
520 nargv[1] = strdup("ruser");
521 nargv[2] = strdup(username);
522 buf = calloc(BUFSIZ, 1);
523 sprintf(buf, "%s is on the following lists:\n", username);
524 show_text(DISPROW, STARTCOL, buf);
525 mvcur(0, 0, currow, STARTCOL);
527 if ((status = mr_query("get_lists_of_member", 2, nargv + 1, print_1, NULL)))
530 com_err(whoami, status, " in get_lists_of_member");
533 show_text(currow, STARTCOL, "Press any Key to continue...");
539 /****************************************************/
544 show_text(DISPROW, STARTCOL, "This function may take a while... proceed? [n] ");
545 c = getchar() & INPUT_MASK;
546 if (c == 'y' || c == 'Y')
548 move(DISPROW + 1, STARTCOL);
549 addstr("Processing query...please hold");
554 erase_line(DISPROW, STARTCOL);
557 /****************************************************/
558 static int print_1(int argc, char *argv[], void *callback)
562 /* no newline 'cause display_buff adds one */
563 sprintf(buf, "%s\n", argv[0]);
569 /****************************************************/
570 static int print_all(int argc, char *argv[], void *callback)
578 erase_line(DISPROW + 1, STARTCOL);
579 show_text(DISPROW + 1, STARTCOL, "All mailing lists:");
582 sprintf(buf, "%s\n", argv[0]);
588 /****************************************************/
589 void list_all_groups(void)
592 argv[0] = argv[1] = argv[3] = "true";
593 argv[4] = "dontcare";
596 if ((status = mr_query("qualified_get_lists", 5, argv, print_all, NULL)))
599 com_err(whoami, status, " in list_all_groups\n");
604 /****************************************************/
605 void list_members(void)
612 move(DISPROW, STARTCOL);
613 mvcur(0, 0, DISPROW, STARTCOL);
615 buf = calloc(LISTMAX, 1);
616 if (Prompt("Enter List Name: ", buf, LISTSIZE, 1) == 1)
618 sprintf(buffer, "The members of list '%s' are:", buf);
619 show_text(DISPROW + 1, STARTCOL, buffer);
621 if ((status = mr_query("get_members_of_list", 1, argv, print_2, NULL)))
624 com_err(whoami, status, " found.\n");
629 show_text(currow, STARTCOL, "List is empty (no members).");
631 show_text(currow, STARTCOL, "Press any key to continue...");
644 /****************************************************/
645 static int print_2(int argc, char *argv[], void *callback)
650 sprintf(buf, "%s %s", argv[0], argv[1]);
656 /****************************************************/
657 void start_display(char *buff)
660 int secondcol; /* where to start the second column of text */
662 secondcol = (COLS / 2); /* 1/2 was accross the screen */
666 buffer = calloc(50, 1);
667 if (currow >= LINES - 2)
670 mvcur(0, 0, currow, STARTCOL);
672 if (Prompt("--RETURN for more, ctl-c to exit--", buffer, 1, 0) == 0)
674 erase_line(currow, STARTCOL);
675 show_text(currow, STARTCOL, "Flushing query...");
680 currow = DISPROW + 2;
681 sprintf(buffer, "Continued (Page %d)", page);
682 show_text(currow, STARTCOL, buffer);
687 show_text(currow, STARTCOL, buff);
690 erase_line(currow, secondcol - 1); /* in case the 1st col is too long */
691 show_text(currow, secondcol, buff);
699 /****************************************************/
700 void end_display(void)
710 buffer = calloc(50, 1);
712 sprintf(buffer, "End of List. %d Total Members\n", num_members - 1);
713 show_text(currow, STARTCOL, buffer);
715 show_text(currow, STARTCOL, "Press any key to continue...");
721 /****************************************************/
722 void display_menu(MENU *menu)
728 mvcur(0, 0, STARTROW, STARTCOL);
730 for (i = 0; i <= menu->num_items - 1; i++)
732 move(STARTROW + i, STARTCOL);
734 addstr(menu->items[i]);
737 center_text(STARTROW + menu->num_items + 2,
738 "Enter a number, <up arrow>, or <down arrow>.");
741 center_text(STARTROW + menu->num_items + 3,
742 "Press 'q' to exit, <return> to confirm choice.");
746 center_text(STARTROW + menu->num_items + 3,
747 "Press 'q' to exit, 'r' for main menu, "
748 "<return> to confirm choice.");
752 highlight(main_menu);
755 /****************************************************/
756 void pack_main_menu(void)
760 main_menu = malloc(sizeof(MENU));
761 main_menu->num_items = 7;
762 main_menu->items = malloc(sizeof(char *) * main_menu->num_items);
765 sprintf(buf, "Mail List Program for %s", username);
766 main_menu->title = strdup(buf);
767 main_menu->items[0] = strdup("1. Show all public mailing lists.");
768 main_menu->items[1] = strdup("2. Get all members of a mailing list.");
769 main_menu->items[2] = strdup("3. Display lists of which you are a member.");
770 main_menu->items[3] = strdup("4. Show description of list.");
771 main_menu->items[4] = strdup("5. Add yourself to a mailing list.");
772 main_menu->items[5] = strdup("6. Delete yourself from a mailing list.");
773 main_menu->items[6] = strdup("q. Quit.");
777 /****************************************************/
778 void pack_help_menu(void)
780 help_menu = malloc(sizeof(MENU));
781 help_menu->num_items = 5;
782 help_menu->items = malloc(sizeof(char *) * help_menu->num_items);
784 help_menu->title = strdup("mailmaint is designed as a basic mail list administration program.");
785 help_menu->items[0] = strdup("if you need to perform more advanced list manipulation like");
786 help_menu->items[1] = strdup("adding lists, or changing list characteristics, refer to the");
787 help_menu->items[2] = strdup("program listmaint.");
788 help_menu->items[3] = strdup(" ");
789 help_menu->items[4] = strdup("Press any key to continue.");
792 /****************************************************/
793 void free_menu(MENU* menu)
796 for (i = 0; i < menu->num_items; i++)
797 free(menu->items[i]);
803 /****************************************************/
804 void highlight(MENU *menu)
806 if (oldpos[level] != position[level])
808 move(STARTROW + oldpos[level] - 1, STARTCOL);
810 addstr(menu->items[oldpos[level] - 1]);
814 move(STARTROW + position[level] - 1, STARTCOL);
816 addstr(menu->items[position[level] - 1]);
822 /****************************************************/
823 void title(char *buff)
825 move(0, MAX(0, (COLS - (int)strlen(buff)) >> 1));
832 /****************************************************/
833 void center_text(int row, char *buff)
835 move(row, MAX(0, (COLS - (int)strlen(buff)) >> 1));
840 /****************************************************/
841 void show_text(int row, int col, char *buff)
843 mvcur(0, 0, row, col);
848 /****************************************************/
849 void erase_line(int row, int col)
854 buff = calloc(COLS, 1);
855 for (i = 0; i <= COLS - 2; i++)
857 buff[i] = 0; /* just to be sure ! */
859 mvcur(0, 0, row, col);
862 free(buff); /* close mem. leak */
865 /****************************************************/
872 /****************************************************/
873 void clrwin(int erase_row)
881 buff = calloc(maxcol + 1, 1);
882 for (i = 0; i <= maxcol - 1; i++)
884 buff[i] = 0; /* just to be sure ! */
885 mvcur(0, 0, erase_row, STARTCOL);
887 for (i = erase_row; i <= currow - 1; i++)
890 mvcur(erase_row, STARTCOL, STARTROW + oldpos[level] - 1, STARTCOL);
895 /****************************************************/
896 static int fetch_list_info(char *list, List_info *li)
901 return mr_query("get_list_info", 1, argv, get_list_info, NULL);
904 static int get_list_info(int argc, char **argv, void *hint)
906 if (current_li->acl_type)
907 free(current_li->acl_type);
908 current_li->acl_type = strdup(argv[7]);
909 if (current_li->acl_name)
910 free(current_li->acl_name);
911 current_li->acl_name = strdup(argv[8]);
912 if (current_li->desc)
913 free(current_li->desc);
914 current_li->desc = strdup(argv[9]);
915 if (current_li->modtime)
916 free(current_li->modtime);
917 current_li->modtime = strdup(argv[10]);
918 if (current_li->modby)
919 free(current_li->modby);
920 current_li->modby = strdup(argv[11]);
921 if (current_li->modwith)
922 free(current_li->modwith);
923 current_li->modwith = strdup(argv[12]);
928 /****************************************************/
929 /* Prompt the user for input */
930 int Prompt(char *prompt, char *buf, int buflen, int crok)
938 for (p = buf; abs(strlen(p) - strlen(buf)) <= buflen;)
941 c = getchar() & INPUT_MASK;
950 display_menu(main_menu);
957 if (strlen(buf) < 1) /* only \n or \r in buff */
978 c = getchar() & INPUT_MASK;
983 if (abs(strlen(p) - strlen(buf)) >= buflen)
1003 * Hook function to cause error messages to be printed through
1004 * curses instead of around it.
1007 void menu_err_hook(const char *who, long code, const char *fmt, va_list args)
1009 char buf[BUFSIZ], *cp;
1012 for (cp = buf; *cp; cp++)
1018 strcpy(cp, error_message(code));
1022 vsprintf(cp, fmt, args);