1 #if (!defined(lint) && !defined(SABER))
2 static char rcsid_module_c[] = "$Header$";
5 /* This is the file lists.c for the SMS Client, which allows a nieve
6 * user to quickly and easily maintain most parts of the SMS database.
7 * It Contains: All list manipulation functions, except delete.
10 * By: Chris D. Peterson
16 * Copyright 1988 by the Massachusetts Institute of Technology.
18 * For further information on copyright and distribution
19 * see the file mit-copyright.h
28 #include "mit-copyright.h"
38 #define DEFAULT_ACTIVE DEFAULT_YES
39 #define DEFAULT_PUBLIC DEFAULT_YES
40 #define DEFAULT_HIDDEN DEFAULT_NO
41 #define DEFAULT_MAILLIST DEFAULT_YES
42 #define DEFAULT_GROUP DEFAULT_NO
43 #define DEFAULT_GID UNIQUE_GID
44 #define DEFAULT_ACE_TYPE "user"
45 #define DEFAULT_ACE_NAME (user)
46 #define DEFAULT_DESCRIPTION DEFAULT_COMMENT
48 /* globals only for this file. */
50 static char current_list[BUFSIZ];
52 /* Function Name: PrintListAce
53 * Description: This function prints the list ace information.
54 * Arguments: info - an info structure.
64 sprintf(buf, "Item: %-20s Name: %s", info[ACE_TYPE],
69 /* Function Name: PrintListInfo
70 * Description: This function Prints out the List info in a coherent form.
71 * Arguments: info - the List info.
82 (void) sprintf(buf, "%20sList: %s", "", info[L_NAME]);
83 (void) Put_message(buf);
84 (void) sprintf(buf, "Description: %s", info[L_DESC]);
85 (void) Put_message(buf);
86 if ( atoi(info[L_MAILLIST]))
87 Put_message("This list is a mailing list.");
89 Put_message("This list is NOT a mailing list.");
90 if ( atoi(info[L_GROUP]) ) {
91 (void) sprintf(buf,"This list is a Group and its ID number is %s",
96 Put_message("This list is NOT a Group.");
98 if (strcmp(info[L_ACE_TYPE],"NONE") == 0)
99 Put_message("This list has no Administrator, how strange?!");
101 sprintf(buf, "The Administrator of this list is the %s: %s",
102 info[L_ACE_TYPE], info[L_ACE_NAME]);
106 (void) sprintf(buf, "This list is: %s, %s, and %s",
107 atoi(info[L_ACTIVE]) ? "active" : "inactive",
108 atoi(info[L_PUBLIC]) ? "public" : "private",
109 atoi(info[L_HIDDEN]) ? "hidden" : "visible");
110 (void) Put_message(buf);
111 sprintf(buf, MOD_FORMAT, info[L_MODBY], info[L_MODTIME], info[L_MODWITH]);
112 (void) Put_message(buf);
115 /* Function Name: GetListInfo
116 * Description: Stores all info about a group of lists in a queue.
117 * Arguments: type - type of info to store.
118 * name - name of the info.
119 * Returns: the first element in the queue.
124 GetListInfo(type, name1, name2)
126 char * name1, *name2;
129 struct qelem * elem = NULL;
135 if ( (status = do_sms_query("get_list_info", 1, args,
136 StoreInfo, (char *) &elem)) != 0) {
137 com_err(program_name, status, " in get_list_info");
143 if ( (status = do_sms_query("get_members_of_list", 1, args,
144 StoreInfo, (char *) &elem)) != 0) {
145 com_err(program_name, status, " in get_members_of_list");
152 if ( (status = do_sms_query("get_lists_of_member", 2, args,
153 StoreInfo, (char *) &elem)) != 0) {
154 com_err(program_name, status, " in get_list_of_members");
161 if ( (status = do_sms_query("get_ace_use", 2, args,
162 StoreInfo, (char *) &elem)) != 0) {
163 com_err(program_name, status, " in get_ace_use");
168 return( QueueTop(elem) );
171 /* Function Name: AskListInfo.
172 * Description: This function askes the user for information about a
173 * machine and saves it into a structure.
174 * Arguments: info - a pointer the the structure to put the
176 * name - add a newname field? (T/F)
177 * Returns: SUB_ERROR or SUB_NORMAL.
181 AskListInfo(info, name)
185 char temp_buf[BUFSIZ], *newname;
188 sprintf(temp_buf,"Setting information of list %s.",info[L_NAME]);
189 Put_message(temp_buf);
193 newname = Strsave(info[L_NAME]);
194 GetValueFromUser("The new name for this list", &newname);
196 GetYesNoValueFromUser("Is this an active list", &info[L_ACTIVE]);
197 GetYesNoValueFromUser("Is this a public list", &info[L_PUBLIC]);
198 GetYesNoValueFromUser("Is this a hidden list", &info[L_HIDDEN]);
199 GetYesNoValueFromUser("Is this a maillist", &info[L_MAILLIST]);
200 GetYesNoValueFromUser("Is this a group", &info[L_GROUP]);
201 if (atoi(info[L_GROUP]))
202 GetValueFromUser("What is the GID for this group.", &info[L_GID]);
204 GetTypeFromUser("What Type of Administrator", "ace_type",&info[L_ACE_TYPE]);
205 if ( (strcmp(info[L_ACE_TYPE], "USER") == 0) ||
206 (strcmp(info[L_ACE_TYPE], "user") == 0) )
207 GetValueFromUser("Who will be the administrator of this list: ",
209 if ( (strcmp(info[L_ACE_TYPE], "LIST") == 0) ||
210 (strcmp(info[L_ACE_TYPE], "list") == 0) )
211 GetValueFromUser("Which group will be the administrator of this list: ",
213 GetValueFromUser("Description: ", &info[L_DESC]);
215 FreeAndClear(&info[L_MODTIME], TRUE);
216 FreeAndClear(&info[L_MODBY], TRUE);
217 FreeAndClear(&info[L_MODWITH], TRUE);
219 * Slide the newname into the #2 slot, this screws up all future references
222 if (name) /* slide the newname into the #2 slot. */
223 SlipInNewName(info, newname);
228 /* -------------- List functions. -------------- */
230 /* Function Name: ShowListInfo.
231 * Description: shows info on a list.
232 * Arguments: argc, argv - name of list in argv[1].
233 * Returns: DM status code.
238 ShowListInfo(argc, argv)
242 struct qelem *top, *list;
244 top = list = GetListInfo(LIST, argv[1], (char *) NULL);
245 while (list != NULL) {
246 PrintListInfo( (char **) list->q_data);
254 /* Function Name: RealUpdateList
255 * Description: performs the actual update of the list.
256 * Arguments: info - all information needed to update the list.
257 * junk - an UNUSED boolean.
263 RealUpdateList(info, junk)
270 args = AskListInfo(info, TRUE);
271 if ( (stat = do_sms_query("update_list", CountArgs(args), args,
272 Scream, (char *) NULL)) != SMS_SUCCESS) {
273 com_err(program_name, stat, " in UpdateList.");
274 Put_message("List ** NOT ** Updated.");
277 Put_message("List successfully updated.");
280 /* Function Name: UpdateList
281 * Description: updates the information on a list.
282 * Arguments: argc, argv - name of list in argv[1].
283 * Returns: DM Status code.
288 UpdateList(argc, argv)
294 top = GetListInfo(LIST, argv[1], (char *) NULL);
295 QueryLoop(top, NullPrint, RealUpdateList, "Update the list");
301 /* Function Name: SetDefaults
302 * Description: sets defaults for AddList function
303 * Arguments: info - the array to add them to.
304 * name - name of the program to add.
305 * Returns: defaults - the default information.
309 SetDefaults(info, name)
313 info[L_NAME] = Strsave(name);
314 info[L_ACTIVE] = Strsave(DEFAULT_ACTIVE);
315 info[L_PUBLIC] = Strsave(DEFAULT_PUBLIC);
316 info[L_HIDDEN] = Strsave(DEFAULT_HIDDEN);
317 info[L_MAILLIST] = Strsave(DEFAULT_MAILLIST);
318 info[L_GROUP] = Strsave(DEFAULT_GROUP);
319 info[L_GID] = Strsave(DEFAULT_GID);
320 info[L_ACE_TYPE] = Strsave(DEFAULT_ACE_TYPE);
321 info[L_ACE_NAME] = Strsave(DEFAULT_ACE_NAME);
322 info[L_DESC] = Strsave(DEFAULT_DESCRIPTION);
323 info[L_MODTIME] = info[L_MODBY] = info[L_MODWITH] = info[L_END] = NULL;
327 /* Function Name: AddList
329 * Arguments: argc, argv - name of list in argv[1].
330 * Returns: SUB_ERROR if list not created.
339 static char *info[MAX_ARGS_SIZE], **add_args;
340 int status, ret_code = SUB_NORMAL;
342 status = do_sms_query("get_list_info", 1, argv + 1, NullFunc,
344 if (status != SMS_NO_MATCH) {
345 if (status == SMS_SUCCESS)
346 Put_message("This list already exists.");
348 com_err(program_name, status, " in AddList.");
352 add_args = AskListInfo(SetDefaults(info,argv[1]), FALSE);
354 if ( (status = do_sms_query("add_list", CountArgs(add_args), add_args,
355 Scream, (char *) NULL)) != SMS_SUCCESS) {
356 com_err(program_name, status, " in AddList.");
357 Put_message("List Not Created.");
358 ret_code = SUB_ERROR;
365 /* Function Name: Instructions
366 * Description: This func prints out instruction on manipulating lists.
368 * Returns: DM Status Code.
374 static char * message[] = {
375 "Listmaint handles the creation, deletion, and updating of lists.",
376 "A list can be a mailing list, a group list, or both.",
377 "The concept behind lists is that a list has an owner",
378 "- administrator - and members.",
379 "The administrator of a list may be another list.",
380 "The members of a list can be users (login names), other lists,",
381 "or address strings.",
382 "You can use certain keys to do the following:",
383 " Refresh the screen - Type ctrl-L.",
384 " Escape from a function - Type ctrl-C.",
385 " Suspend the program (temporarily) - Type ctrl-Z.",
389 return(PrintHelp(message));
392 /*-*-* LISTMAINT UPDATE MENU *-*-*/
394 /* Function Name: ListmaintMemberMenuEntry
395 * Description: entry routine into the listmaint member menu.
396 * Arguments: m - the member menu.
397 * argc, argv - name of the list in argv[1].
403 ListmaintMemberMenuEntry(m, argc, argv)
408 char temp_buf[BUFSIZ];
409 char *list_name = argv[1];
412 if (!ValidName(list_name))
415 if (*argv[0] == 'a') { /* add_list */
416 if (AddList(argc, argv) == SUB_ERROR)
418 (void) sprintf(temp_buf, "List '%s' created. Do you want to %s",
419 list_name, "change its membership (y/n)? ");
420 if (YesNoQuestion(temp_buf, TRUE) != TRUE )
424 /* All we want to know is if it exists. */
425 switch( (stat = do_sms_query("count_members_of_list", 1, argv + 1,
426 NullFunc, (char *) NULL))) {
430 Put_message("This list does not exist.");
433 Put_message("You are not allowed to view this list.");
436 com_err(program_name, stat, " in get_list_info");
440 (void) sprintf(temp_buf,
441 "Change/Display membership of '%s'", list_name);
442 m->m_title = Strsave(temp_buf);
443 strcpy(current_list, list_name);
447 /* Function Name: ListmaintMemberMenuExit
448 * Description: This is the function called when the member menu is
449 * exited, it frees the memory that is storing the name.
450 * Arguments: m - the menu
455 ListmaintMemberMenuExit(m)
459 strcpy(current_list, "");
463 /* Function Name: ListMembersByType
464 * Description: This function lists the users of a list by type.
465 * Arguments: type - the type of the list "USER", "LIST", or "STRING".
467 * NOTE: if type is NULL, all lists members are listed.
471 ListMembersByType(type)
474 char temp_buf[BUFSIZ];
478 args[0] = current_list;
482 if ( (status = do_sms_query("get_members_of_list", CountArgs(args), args,
483 PrintByType, type)) != 0) {
484 com_err(program_name, status, " in ListMembersByType");
489 Put_message("List is empty (no members).");
491 sprintf(temp_buf,"No %s Members",type);
492 Put_message(temp_buf);
497 /* Function Name: ListAllMembers
498 * Description: lists all members of the current list.
506 ListMembersByType(NULL);
510 /* Function Name: ListUserMembers
511 * Description: This function lists all members of a list of type "USER".
513 * Returns: DM_NORMAL.
518 ListMembersByType("USER");
522 /* Function Name: ListListMembers
523 * Description: This function lists all members of a list of type "LIST".
525 * Returns: DM_NORMAL.
530 ListMembersByType("LIST");
534 /* Function Name: ListStringMembers
535 * Description:This function lists all members of a list of type "STRING".
537 * Returns: DM_NORMAL.
542 ListMembersByType("STRING");
546 /* Function Name: GetMemberInfo
547 * Description: This function gets the information needed to
548 * add or delete a user from a list.
549 * Arguments: argc, argv - standard.
550 * action - name of the action to be performed either
552 * ret_argc, ret_argv - the returned value of argc and argv.
553 * Returns: SUB_ERROR or SUB_NORMAL.
557 GetMemberInfo(action, ret_argv)
558 char *action, **ret_argv;
560 char temp_buf[BUFSIZ], ret_buf[BUFSIZ];
562 ret_argv[LM_LIST] = Strsave(current_list);
564 ret_argv[LM_TYPE] = Strsave("user");
565 GetTypeFromUser("Type of member", "member", &ret_argv[LM_TYPE]);
567 sprintf(temp_buf,"Name of %s to %s", ret_argv[LM_TYPE], action);
568 ret_argv[LM_MEMBER] = Strsave(user);
569 GetValueFromUser(temp_buf, &ret_argv[LM_MEMBER]);
570 ret_argv[LM_END] = NULL; /* NULL terminate this list. */
572 if (!ValidName( ret_argv[LM_MEMBER] ) ) {
579 /* Function Name: AddMember
580 * Description: This function adds a member to a list.
582 * Returns: DM_NORMAL.
588 char *args[10], temp_buf[BUFSIZ];
591 if ( GetMemberInfo("add", args) == SUB_ERROR )
594 if ( (status = do_sms_query("add_member_to_list", CountArgs(args), args,
595 Scream, NULL)) != SMS_SUCCESS) {
596 if (status == SMS_EXISTS) {
597 sprintf(temp_buf, "The %s %s is already a member of LIST %s.",
598 args[LM_TYPE], args[LM_MEMBER], args[LM_LIST]);
599 Put_message(temp_buf);
602 com_err(program_name, status, " in AddMember");
609 /* Function Name: DeleteMember
610 * Description: This function deletes a member from a list.
621 if( GetMemberInfo("delete", args) == SUB_ERROR )
624 if (Confirm("Are you sure you want to delete this member?") ) {
625 if (status = do_sms_query("delete_member_from_list", CountArgs(args),
627 com_err(program_name, status, " in DeleteMember");
629 Put_message("Deletion Completed.");
632 Put_message("Deletion has been Aborted.");
638 /* Function Name: InterRemoveItemFromLists
639 * Description: This function allows interactive removal of an item
640 * (user, string, list) for all list that it is on.
642 * Returns: DM_NORMAL.
643 * NOTES: QueryLoop() does not work here because info does not have
644 * enough information in it to delete the member from the list.
648 InterRemoveItemFromLists()
651 char type[BUFSIZ], name[BUFSIZ], *args[10], buf[BUFSIZ];
652 struct qelem *top, *elem;
654 if ( !(PromptWithDefault("Type of member (user, list, string)", type,
658 sprintf(buf, "Name of %s", type);
659 if ( !(PromptWithDefault(buf, name, BUFSIZ, user)) ) {
663 if (!ValidName(name))
666 top = elem = GetListInfo(GLOM, type, name);
668 while(elem != NULL) {
670 char ** info = (char **) elem->q_data;
671 sprintf(line, "Delete %s %s from the list \"%s\" (y/n/q)? ", type,
672 name, info[GLOM_NAME]);
673 switch (YesNoQuitQuestion(line, FALSE)) {
675 Put_message("deleting...");
676 args[DM_LIST] = info[GLOM_NAME];
677 args[DM_TYPE] = type;
678 args[DM_MEMBER] = name;
679 if ( (status = do_sms_query("delete_member_from_list", 3, args,
680 Scream, (char *) NULL)) != 0)
681 /* should probabally check to delete list. */
682 com_err(program_name, status, " in delete_member");
687 Put_message("Aborting...");
697 /*-*-* LIST MENU *-*-*/
699 /* Function Name: ListByMember
700 * Description: This gets all lists that a given member is a member of.
702 * Returns: DM_NORMAL.
708 char buf[BUFSIZ], temp_buf[BUFSIZ], *type, *name, **info;
709 Bool maillist, group;
710 struct qelem *top, *elem;
712 if ( !(PromptWithDefault("Type of member (user, list, string)", buf,
716 /* What we really want is a recursive search */
717 sprintf(temp_buf, "R%s", buf);
718 type = Strsave(temp_buf);
720 sprintf(temp_buf, "Name of %s", buf);
721 if ( !(PromptWithDefault(temp_buf, buf, BUFSIZ, user)) ) {
727 maillist = YesNoQuestion("Show Lists that are Maillists (y/n) ?",
729 group = YesNoQuestion("Show Lists that are Groups (y/n) ?", TRUE);
731 elem = top = GetListInfo(GLOM, type, name);
733 while (elem != NULL) {
734 info = (char**) elem->q_data;
735 if ((maillist == TRUE && !strcmp(info[GLOM_MAILLIST], "1")) ||
736 (group == TRUE && !strcmp(info[GLOM_GROUP], "1")))
737 Put_message(info[GLOM_NAME]);
744 /* Function Name: ListByAdministrator
745 * Description: This function prints all lists which a given user or
748 * Returns: DM_NORMAL.
752 ListByAdministrator()
754 char buf[BUFSIZ], temp_buf[BUFSIZ], *type, *name;
757 if ( !(PromptWithDefault("Type of member (user, list, string)", buf,
761 if ( YesNoQuestion("Do you want a recursive search (y/n)", FALSE) == 1 ) {
762 sprintf(temp_buf, "R%s", buf); /* "USER" to "RUSER" etc. */
763 type = Strsave(temp_buf);
768 sprintf(temp_buf, "Name of %s", buf);
769 if ( !(PromptWithDefault(temp_buf, buf, BUFSIZ, user)) ) {
775 top = GetListInfo(ACE_USE, type, name);
776 Loop(top, PrintListAce);
782 /* Function Name: ListAllGroups
783 * Description: This function lists all visable groups.
785 * Returns: DM_NORMAL.
791 static char * args[] = {
793 "DONTCARE", /* public */
794 "FALSE", /* hidden */
795 "DONTCARE", /* maillist */
799 if (YesNoQuestion("This query will take a while, Do you wish to continue?",
801 if (status = do_sms_query("qualified_get_lists", 5, args,
802 Print, (char *) NULL) != 0)
803 com_err(program_name, status, " in ListAllGroups");
807 /* Function Name: ListAllMailLists
808 * Description: This function lists all visable maillists.
810 * Returns: DM_NORMAL.
816 static char * args[] = {
818 "DONTCARE", /* public */
819 "FALSE", /* hidden */
820 "TRUE", /* maillist */
821 "DONTCARE", /* group. */
824 if (YesNoQuestion("This query will take a while. Do you wish to continue?",
826 if (status = do_sms_query("qualified_get_lists", 5, args,
827 Print, (char *) NULL) != 0)
828 com_err(program_name, status, " in ListAllGroups");
833 /* Function Name: ListAllPublicMailLists
834 * Description: This function lists all public mailing lists.
836 * Returns: DM_NORMAL.
839 ListAllPublicMailLists()
842 static char * args[] = {
845 "FALSE", /* hidden */
846 "TRUE", /* maillist */
847 "DONTCARE", /* group. */
850 if (YesNoQuestion("This query will take a while. Do you wish to continue?",
852 if (status = do_sms_query("qualified_get_lists", 5, args,
853 Print, (char *) NULL) != 0)
854 com_err(program_name, status, " in ListAllGroups");