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);
194 newname = Strsave(info[L_NAME]);
195 GetValueFromUser("The new name for this list", &newname);
196 if (ValidName(newname))
200 GetYesNoValueFromUser("Is this an active list", &info[L_ACTIVE]);
201 GetYesNoValueFromUser("Is this a public list", &info[L_PUBLIC]);
202 GetYesNoValueFromUser("Is this a hidden list", &info[L_HIDDEN]);
203 GetYesNoValueFromUser("Is this a maillist", &info[L_MAILLIST]);
204 GetYesNoValueFromUser("Is this a group", &info[L_GROUP]);
205 if (atoi(info[L_GROUP]))
206 GetValueFromUser("What is the GID for this group.", &info[L_GID]);
208 GetTypeFromUser("What Type of Administrator", "ace_type",&info[L_ACE_TYPE]);
209 if ((strcasecmp(info[L_ACE_TYPE], "NONE") != 0) &&
210 (strcasecmp(info[L_ACE_TYPE], "none") != 0)) {
211 sprintf(temp_buf, "Which %s will be the administrator of this list: ",
213 GetValueFromUser(temp_buf, &info[L_ACE_NAME]);
215 GetValueFromUser("Description: ", &info[L_DESC]);
217 FreeAndClear(&info[L_MODTIME], TRUE);
218 FreeAndClear(&info[L_MODBY], TRUE);
219 FreeAndClear(&info[L_MODWITH], TRUE);
221 * Slide the newname into the #2 slot, this screws up all future references
224 if (name) /* slide the newname into the #2 slot. */
225 SlipInNewName(info, newname);
230 /* -------------- List functions. -------------- */
232 /* Function Name: ShowListInfo.
233 * Description: shows info on a list.
234 * Arguments: argc, argv - name of list in argv[1].
235 * Returns: DM status code.
240 ShowListInfo(argc, argv)
244 struct qelem *top, *list;
246 top = list = GetListInfo(LIST, argv[1], (char *) NULL);
247 while (list != NULL) {
248 PrintListInfo( (char **) list->q_data);
256 /* Function Name: RealUpdateList
257 * Description: performs the actual update of the list.
258 * Arguments: info - all information needed to update the list.
259 * junk - an UNUSED boolean.
265 RealUpdateList(info, junk)
272 args = AskListInfo(info, TRUE);
273 if ( (stat = do_sms_query("update_list", CountArgs(args), args,
274 Scream, (char *) NULL)) != SMS_SUCCESS) {
275 com_err(program_name, stat, " in UpdateList.");
276 Put_message("List ** NOT ** Updated.");
279 Put_message("List successfully updated.");
282 /* Function Name: UpdateList
283 * Description: updates the information on a list.
284 * Arguments: argc, argv - name of list in argv[1].
285 * Returns: DM Status code.
290 UpdateList(argc, argv)
296 top = GetListInfo(LIST, argv[1], (char *) NULL);
297 QueryLoop(top, NullPrint, RealUpdateList, "Update the list");
303 /* Function Name: SetDefaults
304 * Description: sets defaults for AddList function
305 * Arguments: info - the array to add them to.
306 * name - name of the program to add.
307 * Returns: defaults - the default information.
311 SetDefaults(info, name)
315 info[L_NAME] = Strsave(name);
316 info[L_ACTIVE] = Strsave(DEFAULT_ACTIVE);
317 info[L_PUBLIC] = Strsave(DEFAULT_PUBLIC);
318 info[L_HIDDEN] = Strsave(DEFAULT_HIDDEN);
319 info[L_MAILLIST] = Strsave(DEFAULT_MAILLIST);
320 info[L_GROUP] = Strsave(DEFAULT_GROUP);
321 info[L_GID] = Strsave(DEFAULT_GID);
322 info[L_ACE_TYPE] = Strsave(DEFAULT_ACE_TYPE);
323 info[L_ACE_NAME] = Strsave(DEFAULT_ACE_NAME);
324 info[L_DESC] = Strsave(DEFAULT_DESCRIPTION);
325 info[L_MODTIME] = info[L_MODBY] = info[L_MODWITH] = info[L_END] = NULL;
329 /* Function Name: AddList
331 * Arguments: argc, argv - name of list in argv[1].
332 * Returns: SUB_ERROR if list not created.
341 static char *info[MAX_ARGS_SIZE], **add_args;
342 int status, ret_code = SUB_NORMAL;
344 if (!ValidName(argv[1]))
346 status = do_sms_query("get_list_info", 1, argv + 1, NullFunc,
348 if (status != SMS_NO_MATCH) {
349 if (status == SMS_SUCCESS)
350 Put_message("This list already exists.");
352 com_err(program_name, status, " in AddList.");
356 add_args = AskListInfo(SetDefaults(info,argv[1]), FALSE);
358 if ( (status = do_sms_query("add_list", CountArgs(add_args), add_args,
359 Scream, (char *) NULL)) != SMS_SUCCESS) {
360 com_err(program_name, status, " in AddList.");
361 Put_message("List Not Created.");
362 ret_code = SUB_ERROR;
369 /* Function Name: Instructions
370 * Description: This func prints out instruction on manipulating lists.
372 * Returns: DM Status Code.
378 static char * message[] = {
379 "Listmaint handles the creation, deletion, and updating of lists.",
380 "A list can be a mailing list, a group list, or both.",
381 "The concept behind lists is that a list has an owner",
382 "- administrator - and members.",
383 "The administrator of a list may be another list.",
384 "The members of a list can be users (login names), other lists,",
385 "or address strings.",
386 "You can use certain keys to do the following:",
387 " Refresh the screen - Type ctrl-L.",
388 " Escape from a function - Type ctrl-C.",
389 " Suspend the program (temporarily) - Type ctrl-Z.",
393 return(PrintHelp(message));
396 /*-*-* LISTMAINT UPDATE MENU *-*-*/
398 /* Function Name: ListmaintMemberMenuEntry
399 * Description: entry routine into the listmaint member menu.
400 * Arguments: m - the member menu.
401 * argc, argv - name of the list in argv[1].
407 ListmaintMemberMenuEntry(m, argc, argv)
412 char temp_buf[BUFSIZ];
413 char *list_name = argv[1];
416 if (!ValidName(list_name))
419 if (*argv[0] == 'a') { /* add_list */
420 if (AddList(argc, argv) == SUB_ERROR)
422 (void) sprintf(temp_buf, "List '%s' created. Do you want to %s",
423 list_name, "change its membership (y/n)? ");
424 if (YesNoQuestion(temp_buf, TRUE) != TRUE )
428 /* All we want to know is if it exists. */
429 switch( (stat = do_sms_query("count_members_of_list", 1, argv + 1,
430 NullFunc, (char *) NULL))) {
434 Put_message("This list does not exist.");
437 Put_message("You are not allowed to view this list.");
440 com_err(program_name, stat, " in get_list_info");
444 (void) sprintf(temp_buf,
445 "Change/Display membership of '%s'", list_name);
446 m->m_title = Strsave(temp_buf);
447 strcpy(current_list, list_name);
451 /* Function Name: ListmaintMemberMenuExit
452 * Description: This is the function called when the member menu is
453 * exited, it frees the memory that is storing the name.
454 * Arguments: m - the menu
459 ListmaintMemberMenuExit(m)
463 strcpy(current_list, "");
467 /* Function Name: ListMembersByType
468 * Description: This function lists the users of a list by type.
469 * Arguments: type - the type of the list "USER", "LIST", or "STRING".
471 * NOTE: if type is NULL, all lists members are listed.
475 ListMembersByType(type)
478 char temp_buf[BUFSIZ];
482 args[0] = current_list;
486 if ( (status = do_sms_query("get_members_of_list", CountArgs(args), args,
487 PrintByType, type)) != 0) {
488 com_err(program_name, status, " in ListMembersByType");
493 Put_message("List is empty (no members).");
495 sprintf(temp_buf,"No %s Members",type);
496 Put_message(temp_buf);
501 /* Function Name: ListAllMembers
502 * Description: lists all members of the current list.
510 ListMembersByType(NULL);
514 /* Function Name: ListUserMembers
515 * Description: This function lists all members of a list of type "USER".
517 * Returns: DM_NORMAL.
522 ListMembersByType("USER");
526 /* Function Name: ListListMembers
527 * Description: This function lists all members of a list of type "LIST".
529 * Returns: DM_NORMAL.
534 ListMembersByType("LIST");
538 /* Function Name: ListStringMembers
539 * Description:This function lists all members of a list of type "STRING".
541 * Returns: DM_NORMAL.
546 ListMembersByType("STRING");
550 /* Function Name: GetMemberInfo
551 * Description: This function gets the information needed to
552 * add or delete a user from a list.
553 * Arguments: argc, argv - standard.
554 * action - name of the action to be performed either
556 * ret_argc, ret_argv - the returned value of argc and argv.
557 * Returns: SUB_ERROR or SUB_NORMAL.
561 GetMemberInfo(action, ret_argv)
562 char *action, **ret_argv;
564 char temp_buf[BUFSIZ], ret_buf[BUFSIZ];
566 ret_argv[LM_LIST] = Strsave(current_list);
568 ret_argv[LM_TYPE] = Strsave("user");
569 GetTypeFromUser("Type of member", "member", &ret_argv[LM_TYPE]);
571 sprintf(temp_buf,"Name of %s to %s", ret_argv[LM_TYPE], action);
572 ret_argv[LM_MEMBER] = Strsave(user);
573 GetValueFromUser(temp_buf, &ret_argv[LM_MEMBER]);
574 ret_argv[LM_END] = NULL; /* NULL terminate this list. */
576 if (strcasecmp(ret_argv[LM_TYPE], "string") &&
577 !ValidName( ret_argv[LM_MEMBER] ) ) {
584 /* Function Name: AddMember
585 * Description: This function adds a member to a list.
587 * Returns: DM_NORMAL.
593 char *args[10], temp_buf[BUFSIZ];
596 if ( GetMemberInfo("add", args) == SUB_ERROR )
599 if ( (status = do_sms_query("add_member_to_list", CountArgs(args), args,
600 Scream, NULL)) != SMS_SUCCESS) {
601 if (status == SMS_EXISTS) {
602 sprintf(temp_buf, "The %s %s is already a member of LIST %s.",
603 args[LM_TYPE], args[LM_MEMBER], args[LM_LIST]);
604 Put_message(temp_buf);
607 com_err(program_name, status, " in AddMember");
614 /* Function Name: DeleteMember
615 * Description: This function deletes a member from a list.
626 if( GetMemberInfo("delete", args) == SUB_ERROR )
629 if (Confirm("Are you sure you want to delete this member?") ) {
630 if (status = do_sms_query("delete_member_from_list", CountArgs(args),
632 com_err(program_name, status, " in DeleteMember");
634 Put_message("Deletion Completed.");
637 Put_message("Deletion has been Aborted.");
643 /* Function Name: InterRemoveItemFromLists
644 * Description: This function allows interactive removal of an item
645 * (user, string, list) for all list that it is on.
647 * Returns: DM_NORMAL.
648 * NOTES: QueryLoop() does not work here because info does not have
649 * enough information in it to delete the member from the list.
653 InterRemoveItemFromLists()
656 char type[BUFSIZ], name[BUFSIZ], *args[10], buf[BUFSIZ];
657 struct qelem *top, *elem;
659 if ( !(PromptWithDefault("Type of member (user, list, string)", type,
663 sprintf(buf, "Name of %s", type);
664 if ( !(PromptWithDefault(buf, name, BUFSIZ, user)) ) {
668 if (!ValidName(name))
671 top = elem = GetListInfo(GLOM, type, name);
673 while(elem != NULL) {
675 char ** info = (char **) elem->q_data;
676 sprintf(line, "Delete %s %s from the list \"%s\" (y/n/q)? ", type,
677 name, info[GLOM_NAME]);
678 switch (YesNoQuitQuestion(line, FALSE)) {
680 Put_message("deleting...");
681 args[DM_LIST] = info[GLOM_NAME];
682 args[DM_TYPE] = type;
683 args[DM_MEMBER] = name;
684 if ( (status = do_sms_query("delete_member_from_list", 3, args,
685 Scream, (char *) NULL)) != 0)
686 /* should probabally check to delete list. */
687 com_err(program_name, status, " in delete_member");
692 Put_message("Aborting...");
702 /*-*-* LIST MENU *-*-*/
704 /* Function Name: ListByMember
705 * Description: This gets all lists that a given member is a member of.
707 * Returns: DM_NORMAL.
713 char buf[BUFSIZ], temp_buf[BUFSIZ], *type, *name, **info;
714 Bool maillist, group;
715 struct qelem *top, *elem;
717 if ( !(PromptWithDefault("Type of member (user, list, string)", buf,
721 /* What we really want is a recursive search */
722 sprintf(temp_buf, "R%s", buf);
723 type = Strsave(temp_buf);
725 sprintf(temp_buf, "Name of %s", buf);
726 if ( !(PromptWithDefault(temp_buf, buf, BUFSIZ, user)) ) {
732 maillist = YesNoQuestion("Show Lists that are Maillists (y/n) ?",
734 group = YesNoQuestion("Show Lists that are Groups (y/n) ?", TRUE);
736 elem = top = GetListInfo(GLOM, type, name);
738 while (elem != NULL) {
739 info = (char**) elem->q_data;
740 if ((maillist == TRUE && !strcmp(info[GLOM_MAILLIST], "1")) ||
741 (group == TRUE && !strcmp(info[GLOM_GROUP], "1")))
742 Put_message(info[GLOM_NAME]);
749 /* Function Name: ListByAdministrator
750 * Description: This function prints all lists which a given user or
753 * Returns: DM_NORMAL.
757 ListByAdministrator()
759 char buf[BUFSIZ], temp_buf[BUFSIZ], *type, *name;
762 if ( !(PromptWithDefault("Type of member (user, list, string)", buf,
766 if ( YesNoQuestion("Do you want a recursive search (y/n)", FALSE) == 1 ) {
767 sprintf(temp_buf, "R%s", buf); /* "USER" to "RUSER" etc. */
768 type = Strsave(temp_buf);
773 sprintf(temp_buf, "Name of %s", buf);
774 if ( !(PromptWithDefault(temp_buf, buf, BUFSIZ, user)) ) {
780 top = GetListInfo(ACE_USE, type, name);
781 Loop(top, PrintListAce);
787 /* Function Name: ListAllGroups
788 * Description: This function lists all visable groups.
790 * Returns: DM_NORMAL.
796 static char * args[] = {
798 "DONTCARE", /* public */
799 "FALSE", /* hidden */
800 "DONTCARE", /* maillist */
804 if (YesNoQuestion("This query will take a while, Do you wish to continue?",
806 if (status = do_sms_query("qualified_get_lists", 5, args,
807 Print, (char *) NULL) != 0)
808 com_err(program_name, status, " in ListAllGroups");
812 /* Function Name: ListAllMailLists
813 * Description: This function lists all visable maillists.
815 * Returns: DM_NORMAL.
821 static char * args[] = {
823 "DONTCARE", /* public */
824 "FALSE", /* hidden */
825 "TRUE", /* maillist */
826 "DONTCARE", /* group. */
829 if (YesNoQuestion("This query will take a while. Do you wish to continue?",
831 if (status = do_sms_query("qualified_get_lists", 5, args,
832 Print, (char *) NULL) != 0)
833 com_err(program_name, status, " in ListAllGroups");
838 /* Function Name: ListAllPublicMailLists
839 * Description: This function lists all public mailing lists.
841 * Returns: DM_NORMAL.
844 ListAllPublicMailLists()
847 static char * args[] = {
850 "FALSE", /* hidden */
851 "TRUE", /* maillist */
852 "DONTCARE", /* group. */
855 if (YesNoQuestion("This query will take a while. Do you wish to continue?",
857 if (status = do_sms_query("qualified_get_lists", 5, args,
858 Print, (char *) NULL) != 0)
859 com_err(program_name, status, " in ListAllGroups");