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
27 #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.
65 sprintf(buf, "Item Administered: %-20s Name: %s", info[ACE_TYPE],
70 /* Function Name: PrintListInfo
71 * Description: This function Prints out the List info in a coherent form.
72 * Arguments: info - the List info.
83 (void) sprintf(buf, "%20sList: %s", "", info[L_NAME]);
84 (void) Put_message(buf);
85 (void) sprintf(buf, "Description: %s", info[L_DESC]);
86 (void) Put_message(buf);
87 if ( atoi(info[L_MAILLIST]))
88 Put_message("This list is a mailing list.");
90 Put_message("This list is NOT a mailing list.");
91 if ( atoi(info[L_GROUP]) ) {
92 (void) sprintf(buf,"This list is Group and its ID number is %s",
97 Put_message("This list is NOT a Group.");
99 if (strcmp(info[L_ACE_TYPE],"NONE") == 0)
100 Put_message("This list has no Administrator, how strange?!");
102 sprintf(buf, "The Administrator of this list is the %s: %s",
103 info[L_ACE_TYPE], info[L_ACE_NAME]);
107 (void) sprintf(buf, "This list is: %s, %s, and %s",
108 atoi(info[L_ACTIVE]) ? "active" : "inactive",
109 atoi(info[L_PUBLIC]) ? "public" : "private",
110 atoi(info[L_HIDDEN]) ? "hidden" : "visible");
111 (void) Put_message(buf);
112 sprintf(buf, MOD_FORMAT, info[L_MODBY], info[L_MODTIME], info[L_MODWITH]);
113 (void) Put_message(buf);
116 /* Function Name: GetListInfo
117 * Description: Stores all info about a group of lists in a queue.
118 * Arguments: type - type of info to store.
119 * name - name of the info.
120 * Returns: the first element in the queue.
125 GetListInfo(type, name1, name2)
127 char * name1, *name2;
130 struct qelem * elem = NULL;
136 if ( (status = sms_query("get_list_info", 1, args,
137 StoreInfo, (char *) &elem)) != 0) {
138 com_err(program_name, status, " in get_list_info");
144 if ( (status = sms_query("get_members_of_list", 1, args,
145 StoreInfo, (char *) &elem)) != 0) {
146 com_err(program_name, status, " in get_members_of_list");
153 if ( (status = sms_query("get_lists_of_member", 2, args,
154 StoreInfo, (char *) &elem)) != 0) {
155 com_err(program_name, status, " in get_list_of_members");
162 if ( (status = sms_query("get_ace_use", 2, args,
163 StoreInfo, (char *) &elem)) != 0) {
164 com_err(program_name, status, " in get_ace_use");
169 return( QueueTop(elem) );
172 /* Function Name: AskListInfo.
173 * Description: This function askes the user for information about a
174 * machine and saves it into a structure.
175 * Arguments: info - a pointer the the structure to put the
177 * name - add a newname field? (T/F)
178 * Returns: SUB_ERROR or SUB_NORMAL.
182 AskListInfo(info, name)
186 char temp_buf[BUFSIZ], *newname;
189 sprintf(temp_buf,"Setting information of list %s.",info[L_NAME]);
190 Put_message(temp_buf);
194 newname = Strsave(info[L_NAME]);
195 GetValueFromUser("The new name for this list", &newname);
197 GetYesNoValueFromUser("Is this an active list", &info[L_ACTIVE]);
198 GetYesNoValueFromUser("Is this a public list", &info[L_PUBLIC]);
199 GetYesNoValueFromUser("Is this a hidden list", &info[L_HIDDEN]);
200 GetYesNoValueFromUser("Is this a maillist", &info[L_MAILLIST]);
201 GetYesNoValueFromUser("Is this a group", &info[L_GROUP]);
202 if (atoi(info[L_GROUP]))
203 GetValueFromUser("What is the GID for this group.", &info[L_GID]);
205 GetTypeFromUser("What Type of Administrator", "ace_type",&info[L_ACE_TYPE]);
206 if ( (strcmp(info[L_ACE_TYPE], "USER") == 0) ||
207 (strcmp(info[L_ACE_TYPE], "user") == 0) )
208 GetValueFromUser("Who will be the administrator of this list: ",
210 if ( (strcmp(info[L_ACE_TYPE], "LIST") == 0) ||
211 (strcmp(info[L_ACE_TYPE], "list") == 0) )
212 GetValueFromUser("Which group will be the administrator of this list: ",
214 GetValueFromUser("Description: ", &info[L_DESC]);
216 FreeAndClear(&info[L_MODTIME], TRUE);
217 FreeAndClear(&info[L_MODBY], TRUE);
218 FreeAndClear(&info[L_MODWITH], TRUE);
220 * Slide the newname into the #2 slot, this screws up all future references
223 if (name) /* slide the newname into the #2 slot. */
224 SlipInNewName(info, newname);
229 /* -------------- List functions. -------------- */
231 /* Function Name: ShowListInfo.
232 * Description: shows info on a list.
233 * Arguments: argc, argv - name of list in argv[1].
234 * Returns: DM status code.
239 ShowListInfo(argc, argv)
243 struct qelem *top, *list;
245 top = list = GetListInfo(LIST, argv[1], (char *) NULL);
246 while (list != NULL) {
247 PrintListInfo( (char **) list->q_data);
255 /* Function Name: RealUpdateList
256 * Description: performs the actual update of the list.
257 * Arguments: info - all information needed to update the list.
258 * junk - an UNUSED boolean.
264 RealUpdateList(info, junk)
271 args = AskListInfo(info, TRUE);
272 if ( (stat = sms_query("update_list", CountArgs(args), args,
273 Scream, (char *) NULL)) != SMS_SUCCESS) {
274 com_err(program_name, stat, " in UpdateList.");
275 Put_message("List ** NOT ** Updated.");
278 Put_message("List successfully updated.");
281 /* Function Name: UpdateList
282 * Description: updates the information on a list.
283 * Arguments: argc, argv - name of list in argv[1].
284 * Returns: DM Status code.
289 UpdateList(argc, argv)
295 top = GetListInfo(LIST, argv[1], (char *) NULL);
296 QueryLoop(top, NullPrint, RealUpdateList, "Update the list");
302 /* Function Name: SetDefaults
303 * Description: sets defaults for AddList function
304 * Arguments: info - the array to add them to.
305 * name - name of the program to add.
306 * Returns: defaults - the default information.
310 SetDefaults(info, name)
314 info[L_NAME] = Strsave(name);
315 info[L_ACTIVE] = Strsave(DEFAULT_ACTIVE);
316 info[L_PUBLIC] = Strsave(DEFAULT_PUBLIC);
317 info[L_HIDDEN] = Strsave(DEFAULT_HIDDEN);
318 info[L_MAILLIST] = Strsave(DEFAULT_MAILLIST);
319 info[L_GROUP] = Strsave(DEFAULT_GROUP);
320 info[L_GID] = Strsave(DEFAULT_GID);
321 info[L_ACE_TYPE] = Strsave(DEFAULT_ACE_TYPE);
322 info[L_ACE_NAME] = Strsave(DEFAULT_ACE_NAME);
323 info[L_DESC] = Strsave(DEFAULT_DESCRIPTION);
324 info[L_MODTIME] = info[L_MODBY] = info[L_MODWITH] = info[L_END] = NULL;
328 /* Function Name: AddList
330 * Arguments: argc, argv - name of list in argv[1].
331 * Returns: SUB_ERROR if list not created.
340 static char *info[MAX_ARGS_SIZE], **add_args;
341 int status, ret_code = SUB_NORMAL;
343 status = sms_query("get_list_info", 1, argv + 1, NullFunc,
345 if (status != SMS_NO_MATCH) {
346 if (status == SMS_SUCCESS)
347 Put_message("This list already exists.");
349 com_err(program_name, status, " in AddList.");
353 add_args = AskListInfo(SetDefaults(info,argv[1]), FALSE);
355 if ( (status = sms_query("add_list", CountArgs(add_args), add_args,
356 Scream, (char *) NULL)) != SMS_SUCCESS) {
357 com_err(program_name, status, " in AddList.");
358 Put_message("List Not Created.");
359 ret_code = SUB_ERROR;
366 /* Function Name: Instructions
367 * Description: This func prints out instruction on manipulating lists.
369 * Returns: DM Status Code.
375 static char * message[] = {
376 "Listmaint handles the creation, deletion, and updating of lists.",
377 "A list can be a mailing list, a group list, or both.",
378 "The concept behind lists is that a list has an owner",
379 "- administrator - and members.",
380 "The administrator of a list may be another list.",
381 "The members of a list can be users (login names), other lists,",
382 "or address strings.",
383 "You can use certain keys to do the following:",
384 " Refresh the screen - Type ctrl-L.",
385 " Escape from a function - Type ctrl-C.",
386 " Suspend the program (temporarily) - Type ctrl-Z.",
390 return(PrintHelp(message));
393 /*-*-* LISTMAINT UPDATE MENU *-*-*/
395 /* Function Name: ListmaintMemberMenuEntry
396 * Description: entry routine into the listmaint member menu.
397 * Arguments: m - the member menu.
398 * argc, argv - name of the list in argv[1].
404 ListmaintMemberMenuEntry(m, argc, argv)
409 char temp_buf[BUFSIZ];
410 char *list_name = argv[1];
413 if (!ValidName(list_name))
416 if (*argv[0] == 'a') { /* add_list */
417 if (AddList(argc, argv) == SUB_ERROR)
419 (void) sprintf(temp_buf, "List '%s' created. Do you want to %s",
420 list_name, "change its membership (y/n)? ");
421 if (YesNoQuestion(temp_buf, TRUE) != TRUE )
425 /* All we want to know is if it exists. */
426 switch( (stat = sms_query("count_members_of_list", 1, argv + 1,
427 NullFunc, (char *) NULL))) {
431 Put_message("This list does not exist.");
434 Put_message("You are not allowed to view this list.");
437 com_err(program_name, stat, " in get_list_info");
441 (void) sprintf(temp_buf,
442 "Change/Display membership of '%s'", list_name);
443 m->m_title = Strsave(temp_buf);
444 strcpy(current_list, list_name);
448 /* Function Name: ListmaintMemberMenuExit
449 * Description: This is the function called when the member menu is
450 * exited, it frees the memory that is storing the name.
451 * Arguments: m - the menu
456 ListmaintMemberMenuExit(m)
460 strcpy(current_list, "");
464 /* Function Name: ListMembersByType
465 * Description: This function lists the users of a list by type.
466 * Arguments: type - the type of the list "USER", "LIST", or "STRING".
468 * NOTE: if type is NULL, all lists members are listed.
472 ListMembersByType(type)
475 char temp_buf[BUFSIZ];
479 args[0] = current_list;
483 if ( (status = sms_query("get_members_of_list", CountArgs(args), args,
484 PrintByType, type)) != 0)
485 com_err(program_name, status, " in ListMembersByType");
488 Put_message("List is empty (no members).");
490 sprintf(temp_buf,"No %s Members",type);
491 Put_message(temp_buf);
496 /* Function Name: ListAllMembers
497 * Description: lists all members of the current list.
505 ListMembersByType(NULL);
509 /* Function Name: ListUserMembers
510 * Description: This function lists all members of a list of type "USER".
512 * Returns: DM_NORMAL.
517 ListMembersByType("USER");
521 /* Function Name: ListListMembers
522 * Description: This function lists all members of a list of type "LIST".
524 * Returns: DM_NORMAL.
529 ListMembersByType("LIST");
533 /* Function Name: ListStringMembers
534 * Description:This function lists all members of a list of type "STRING".
536 * Returns: DM_NORMAL.
541 ListMembersByType("STRING");
545 /* Function Name: GetMemberInfo
546 * Description: This function gets the information needed to
547 * add or delete a user from a list.
548 * Arguments: argc, argv - standard.
549 * action - name of the action to be performed either
551 * ret_argc, ret_argv - the returned value of argc and argv.
552 * Returns: SUB_ERROR or SUB_NORMAL.
556 GetMemberInfo(action, ret_argv)
557 char *action, **ret_argv;
559 char temp_buf[BUFSIZ], ret_buf[BUFSIZ];
561 ret_argv[LM_LIST] = Strsave(current_list);
563 ret_argv[LM_TYPE] = Strsave("user");
564 GetTypeFromUser("Type of member", "member", &ret_argv[LM_TYPE]);
566 sprintf(temp_buf,"Name of %s to %s", ret_argv[LM_TYPE], action);
567 ret_argv[LM_MEMBER] = Strsave(user);
568 GetValueFromUser(temp_buf, &ret_argv[LM_MEMBER]);
569 ret_argv[LM_END] = NULL; /* NULL terminate this list. */
571 if (!ValidName( ret_argv[LM_MEMBER] ) ) {
578 /* Function Name: AddMember
579 * Description: This function adds a member to a list.
581 * Returns: DM_NORMAL.
587 char *args[10], temp_buf[BUFSIZ];
590 if ( GetMemberInfo("add", args) == SUB_ERROR )
593 if ( (status = sms_query("add_member_to_list", CountArgs(args), args,
594 Scream, NULL)) != SMS_SUCCESS) {
595 if (status == SMS_EXISTS) {
596 sprintf(temp_buf, "The %s %s is already a member of LIST %s.",
597 args[LM_TYPE], args[LM_MEMBER], args[LM_LIST]);
598 Put_message(temp_buf);
601 com_err(program_name, status, " in AddMember");
608 /* Function Name: DeleteMember
609 * Description: This function deletes a member from a list.
620 if( GetMemberInfo("delete", args) == SUB_ERROR )
623 if (Confirm("Are you sure you want to delete this member?") ) {
624 if (status = sms_query("delete_member_from_list", CountArgs(args),
626 com_err(program_name, status, " in DeleteMember");
628 Put_message("Deletion Completed.");
631 Put_message("Deletion has been Aborted.");
637 /* Function Name: InterRemoveItemFromLists
638 * Description: This function allows interactive removal of an item
639 * (user, string, list) for all list that it is on.
641 * Returns: DM_NORMAL.
642 * NOTES: QueryLoop() does not work here because info does not have
643 * enough information in it to delete the member from the list.
647 InterRemoveItemFromLists()
650 char type[BUFSIZ], name[BUFSIZ], *args[10], buf[BUFSIZ];
651 struct qelem *top, *elem;
653 if ( !(PromptWithDefault("Type of member (user, list, string)", type,
657 sprintf(buf, "Name of %s", type);
658 if ( !(PromptWithDefault(buf, name, BUFSIZ, user)) ) {
662 if (!ValidName(name))
665 top = elem = GetListInfo(GLOM, type, name);
667 while(elem != NULL) {
669 char ** info = (char **) elem->q_data;
670 sprintf(line, "Delete %s %s from the list \"%s\" (y/n/q)? ", type,
671 name, info[GLOM_NAME]);
672 switch (YesNoQuitQuestion(line, FALSE)) {
674 Put_message("deleting...");
675 args[DM_LIST] = info[GLOM_NAME];
676 args[DM_TYPE] = type;
677 args[DM_MEMBER] = name;
678 if ( (status = sms_query("delete_member_from_list", 3, args,
679 Scream, (char *) NULL)) != 0)
680 /* should probabally check to delete list. */
681 com_err(program_name, status, " in delete_member");
686 Put_message("Aborting...");
696 /*-*-* LIST MENU *-*-*/
698 /* Function Name: ListByMember
699 * Description: This gets all lists that a given member is a member of.
701 * Returns: DM_NORMAL.
707 char buf[BUFSIZ], temp_buf[BUFSIZ], *type, *name, **info;
708 Bool maillist, group;
709 struct qelem *top, *elem;
711 if ( !(PromptWithDefault("Type of member (user, list, string)", buf,
715 /* What we really want is a recursive search */
716 sprintf(temp_buf, "R%s", buf);
717 type = Strsave(temp_buf);
719 sprintf(temp_buf, "Name of %s", buf);
720 if ( !(PromptWithDefault(temp_buf, buf, BUFSIZ, user)) ) {
726 maillist = YesNoQuestion("Show only Lists that are Maillists (y/n) ?",
728 group = YesNoQuestion("Show only Lists that are Groups (y/n) ?", TRUE);
730 elem = top = GetListInfo(GLOM, type, name);
732 while (elem != NULL) {
733 info = (char**) elem->q_data;
734 if (maillist != TRUE || !strcmp(info[GLOM_MAILLIST], "1"))
735 if (group != TRUE || !strcmp(info[GLOM_GROUP], "1"))
736 Put_message(info[GLOM_NAME]);
743 /* Function Name: ListByAdministrator
744 * Description: This function prints all lists which a given user or
747 * Returns: DM_NORMAL.
751 ListByAdministrator()
753 char buf[BUFSIZ], temp_buf[BUFSIZ], *type, *name;
756 if ( !(PromptWithDefault("Type of member (user, list, string)", buf,
760 if ( YesNoQuestion("Do you want a recursive search (y/n)", FALSE) == 1 ) {
761 sprintf(temp_buf, "R%s", buf); /* "USER" to "RUSER" etc. */
762 type = Strsave(temp_buf);
767 sprintf(temp_buf, "Name of %s", buf);
768 if ( !(PromptWithDefault(temp_buf, buf, BUFSIZ, user)) ) {
774 top = GetListInfo(ACE_USE, type, name);
775 Loop(top, PrintListAce);
781 /* Function Name: ListAllGroups
782 * Description: This function lists all visable groups.
784 * Returns: DM_NORMAL.
790 static char * args[] = {
792 "DONTCARE", /* public */
793 "FALSE", /* hidden */
794 "DONTCARE", /* maillist */
798 if (YesNoQuestion("This query will take a while, Do you wish to continue?",
800 if (status = sms_query("qualified_get_lists", 5, args,
801 Print, (char *) NULL) != 0)
802 com_err(program_name, status, " in ListAllGroups");
806 /* Function Name: ListAllMailLists
807 * Description: This function lists all visable maillists.
809 * Returns: DM_NORMAL.
815 static char * args[] = {
817 "DONTCARE", /* public */
818 "FALSE", /* hidden */
819 "TRUE", /* maillist */
820 "DONTCARE", /* group. */
823 if (YesNoQuestion("This query will take a while. Do you wish to continue?",
825 if (status = sms_query("qualified_get_lists", 5, args,
826 Print, (char *) NULL) != 0)
827 com_err(program_name, status, " in ListAllGroups");
832 /* Function Name: ListAllPublicMailLists
833 * Description: This function lists all public mailing lists.
835 * Returns: DM_NORMAL.
838 ListAllPublicMailLists()
841 static char * args[] = {
844 "FALSE", /* hidden */
845 "TRUE", /* maillist */
846 "DONTCARE", /* group. */
849 if (YesNoQuestion("This query will take a while. Do you wish to continue?",
851 if (status = sms_query("qualified_get_lists", 5, args,
852 Print, (char *) NULL) != 0)
853 com_err(program_name, status, " in ListAllGroups");
862 * c-continued-statement-offset: 4
864 * c-argdecl-indent: 4