3 * This is the file lists.c for the Moira Client, which allows users
4 * to quickly and easily maintain most parts of the Moira database.
5 * It Contains: All list manipulation functions, except delete.
8 * By: Chris D. Peterson
10 * Copyright (C) 1988-1998 by the Massachusetts Institute of Technology.
11 * For copying and distribution information, please see the file
15 #include <mit-copyright.h>
17 #include <moira_site.h>
28 struct mqelem *GetListInfo(int type, char *name1, char *name2);
29 char **AskListInfo(char **info, Bool name);
30 int AddList(int argc, char **argv);
31 void ListMembersByType(char *type, int tags);
32 int GetMemberInfo(char *action, char **ret_argv);
39 #define DEFAULT_ACTIVE DEFAULT_YES
40 #define DEFAULT_PUBLIC DEFAULT_YES
41 #define DEFAULT_HIDDEN DEFAULT_NO
42 #define DEFAULT_MAILLIST DEFAULT_YES
43 #define DEFAULT_GROUP DEFAULT_NO
44 #define DEFAULT_GID UNIQUE_GID
45 #define DEFAULT_ACE_TYPE "user"
46 #define DEFAULT_ACE_NAME (user)
47 #define DEFAULT_DESCRIPTION DEFAULT_COMMENT
49 /* globals only for this file. */
51 static char current_list[BUFSIZ];
54 /* Function Name: PrintListAce
55 * Description: This function prints the list ace information.
56 * Arguments: info - an info structure.
60 static void PrintListAce(char **info)
64 sprintf(buf, "Item: %-20s Name: %s", info[ACE_TYPE], info[ACE_NAME]);
68 /* Function Name: PrintListInfo
69 * Description: This function Prints out the List info in a coherent form.
70 * Arguments: info - the List info.
74 static void PrintListInfo(char **info)
79 sprintf(buf, "%20sList: %s", "", info[L_NAME]);
81 sprintf(buf, "Description: %s", info[L_DESC]);
83 if (atoi(info[L_MAILLIST]))
84 Put_message("This list is a mailing list.");
86 Put_message("This list is NOT a mailing list.");
87 if (atoi(info[L_GROUP]))
89 sprintf(buf, "This list is a Group and its ID number is %s",
94 Put_message("This list is NOT a Group.");
96 if (!strcmp(info[L_ACE_TYPE], "NONE"))
97 Put_message("This list has no Administrator, how strange?!");
100 sprintf(buf, "The Administrator of this list is the %s: %s",
101 info[L_ACE_TYPE], info[L_ACE_NAME]);
105 sprintf(buf, "This list is: %s, %s, and %s",
106 atoi(info[L_ACTIVE]) ? "active" : "inactive",
107 atoi(info[L_PUBLIC]) ? "public" : "private",
108 atoi(info[L_HIDDEN]) ? "hidden" : "visible");
110 sprintf(buf, MOD_FORMAT, info[L_MODBY], info[L_MODTIME], info[L_MODWITH]);
114 /* Function Name: GetListInfo
115 * Description: Stores all info about a group of lists in a queue.
116 * Arguments: type - type of info to store.
117 * name - name of the info.
118 * Returns: the first element in the queue.
121 struct mqelem *GetListInfo(int type, char *name1, char *name2)
124 struct mqelem *elem = NULL;
131 if ((status = do_mr_query("get_list_info", 1, args, StoreInfo, &elem)))
133 com_err(program_name, status, " in get_list_info");
139 if ((status = do_mr_query("get_members_of_list", 1, args,
142 com_err(program_name, status, " in get_members_of_list");
149 if ((status = do_mr_query("get_lists_of_member", 2, args,
152 com_err(program_name, status, " in get_list_of_members");
159 if ((status = do_mr_query("get_ace_use", 2, args, StoreInfo, &elem)))
161 com_err(program_name, status, " in get_ace_use");
166 return QueueTop(elem);
169 /* Function Name: AskListInfo.
170 * Description: This function askes the user for information about a
171 * machine and saves it into a structure.
172 * Arguments: info - a pointer the the structure to put the
174 * name - add a newname field? (T/F)
175 * Returns: SUB_ERROR or SUB_NORMAL.
178 char **AskListInfo(char **info, Bool name)
180 char temp_buf[BUFSIZ], *newname;
183 sprintf(temp_buf, "Setting information of list %s.", info[L_NAME]);
184 Put_message(temp_buf);
191 newname = strdup(info[L_NAME]);
192 if (GetValueFromUser("The new name for this list", &newname) ==
195 if (ValidName(newname))
199 if (GetYesNoValueFromUser("Is this an active list", &info[L_ACTIVE]) ==
202 if (GetYesNoValueFromUser("Is this a public list", &info[L_PUBLIC]) ==
205 if (GetYesNoValueFromUser("Is this a hidden list", &info[L_HIDDEN]) ==
208 if (GetYesNoValueFromUser("Is this a maillist", &info[L_MAILLIST]) ==
211 if (GetYesNoValueFromUser("Is this a group", &info[L_GROUP]) == SUB_ERROR)
213 if (atoi(info[L_GROUP]))
215 if (GetValueFromUser("What is the GID for this group.", &info[L_GID]) ==
220 if (GetTypeFromUser("What Type of Administrator", "ace_type",
221 &info[L_ACE_TYPE]) == SUB_ERROR)
223 if (strcasecmp(info[L_ACE_TYPE], "NONE") &&
224 strcasecmp(info[L_ACE_TYPE], "none"))
226 sprintf(temp_buf, "Which %s will be the administrator of this list: ",
228 if (GetValueFromUser(temp_buf, &info[L_ACE_NAME]) == SUB_ERROR)
231 if (GetValueFromUser("Description: ", &info[L_DESC]) == SUB_ERROR)
234 FreeAndClear(&info[L_MODTIME], TRUE);
235 FreeAndClear(&info[L_MODBY], TRUE);
236 FreeAndClear(&info[L_MODWITH], TRUE);
238 * Slide the newname into the #2 slot, this screws up all future references
241 if (name) /* slide the newname into the #2 slot. */
242 SlipInNewName(info, newname);
247 /* -------------- List functions. -------------- */
249 /* Function Name: ShowListInfo.
250 * Description: shows info on a list.
251 * Arguments: argc, argv - name of list in argv[1].
252 * Returns: DM status code.
255 int ShowListInfo(int argc, char **argv)
257 struct mqelem *top, *list;
259 top = list = GetListInfo(LIST, argv[1], NULL);
262 PrintListInfo(list->q_data);
270 /* Function Name: RealUpdateList
271 * Description: performs the actual update of the list.
272 * Arguments: info - all information needed to update the list.
273 * junk - an UNUSED boolean.
277 static void RealUpdateList(char **info, Bool junk)
281 struct mqelem *elem = NULL;
283 if (!(args = AskListInfo(info, TRUE)))
285 Put_message("Aborted.");
290 * If the new list name is less than 8 characters, make sure it doesn't
291 * collide with a username.
293 if ((strlen(args[2]) <= 8) &&
294 do_mr_query("get_user_account_by_login", 1, args + 1,
295 StoreInfo, &elem) != MR_NO_MATCH)
299 sprintf(buf, "\nA user by the name `%s' already exists in the database.",
302 Loop(QueueTop(elem), FreeInfo);
304 if (YesNoQuestion("Do you still want to rename this list to that name",
307 Put_message("List ** NOT ** Updated.");
312 if ((stat = do_mr_query("update_list", CountArgs(args), args,
313 NULL, NULL)) != MR_SUCCESS)
315 com_err(program_name, stat, " in UpdateList.");
316 Put_message("List ** NOT ** Updated.");
319 Put_message("List successfully updated.");
322 /* Function Name: UpdateList
323 * Description: updates the information on a list.
324 * Arguments: argc, argv - name of list in argv[1].
325 * Returns: DM Status code.
328 int UpdateList(int argc, char **argv)
332 top = GetListInfo(LIST, argv[1], (char *) NULL);
333 QueryLoop(top, NullPrint, RealUpdateList, "Update the list");
339 /* Function Name: SetDefaults
340 * Description: sets defaults for AddList function
341 * Arguments: info - the array to add them to.
342 * name - name of the program to add.
343 * Returns: defaults - the default information.
346 static char **SetDefaults(char **info, char *name)
348 info[L_NAME] = strdup(name);
349 info[L_ACTIVE] = strdup(DEFAULT_ACTIVE);
350 info[L_PUBLIC] = strdup(DEFAULT_PUBLIC);
351 info[L_HIDDEN] = strdup(DEFAULT_HIDDEN);
352 info[L_MAILLIST] = strdup(DEFAULT_MAILLIST);
353 info[L_GROUP] = strdup(DEFAULT_GROUP);
354 info[L_GID] = strdup(DEFAULT_GID);
355 info[L_ACE_TYPE] = strdup(DEFAULT_ACE_TYPE);
356 info[L_ACE_NAME] = strdup(DEFAULT_ACE_NAME);
357 info[L_DESC] = strdup(DEFAULT_DESCRIPTION);
358 info[L_MODTIME] = info[L_MODBY] = info[L_MODWITH] = info[L_END] = NULL;
362 /* Function Name: AddList
364 * Arguments: argc, argv - name of list in argv[1].
365 * Returns: SUB_ERROR if list not created.
368 int AddList(int argc, char **argv)
370 static char *info[MAX_ARGS_SIZE], **add_args;
371 int status, ret_code = SUB_NORMAL;
372 struct mqelem *elem = NULL;
374 if (!ValidName(argv[1]))
376 status = do_mr_query("get_list_info", 1, argv + 1, NULL, NULL);
377 if (status != MR_NO_MATCH)
379 if (status == MR_SUCCESS)
380 Put_message("This list already exists.");
382 com_err(program_name, status, " in AddList.");
387 * If the listname is less than 8 characters, make sure it doesn't
388 * collide with a username.
390 if ((strlen(argv[1]) <= 8) &&
391 do_mr_query("get_user_account_by_login", 1, argv + 1,
392 StoreInfo, &elem) != MR_NO_MATCH)
396 sprintf(buf, "\nA user by the name `%s' already exists in the database.",
399 Loop(QueueTop(elem), FreeInfo);
401 if (YesNoQuestion("Create a list with the same name", FALSE) != TRUE)
405 if (!(add_args = AskListInfo(SetDefaults(info, argv[1]), FALSE)))
407 Put_message("Aborted.");
411 if ((status = do_mr_query("add_list", CountArgs(add_args), add_args,
412 NULL, NULL)) != MR_SUCCESS)
414 com_err(program_name, status, " in AddList.");
415 Put_message("List Not Created.");
416 ret_code = SUB_ERROR;
423 /* Function Name: Instructions
424 * Description: This func prints out instruction on manipulating lists.
426 * Returns: DM Status Code.
429 int ListHelp(int argc, char **argv)
431 static char *message[] = {
432 "Listmaint handles the creation, deletion, and updating of lists.",
433 "A list can be a mailing list, a group list, or both.",
434 "The concept behind lists is that a list has an owner",
435 "- administrator - and members.",
436 "The administrator of a list may be another list.",
437 "The members of a list can be users (login names), other lists,",
438 "or address strings.",
439 "You can use certain keys to do the following:",
440 " Refresh the screen - Type ctrl-L.",
441 " Escape from a function - Type ctrl-C.",
442 " Suspend the program (temporarily) - Type ctrl-Z.",
446 return PrintHelp(message);
449 /*-*-* LISTMAINT UPDATE MENU *-*-*/
451 /* Function Name: ListmaintMemberMenuEntry
452 * Description: entry routine into the listmaint member menu.
453 * Arguments: m - the member menu.
454 * argc, argv - name of the list in argv[1].
458 int ListmaintMemberMenuEntry(Menu *m, int argc, char **argv)
460 char temp_buf[BUFSIZ];
461 char *list_name = argv[1];
464 if (!ValidName(list_name))
469 if (AddList(argc, argv) == SUB_ERROR)
471 sprintf(temp_buf, "List '%s' created. Do you want to %s", list_name,
472 "change its membership (y/n)? ");
473 if (YesNoQuestion(temp_buf, TRUE) != TRUE)
477 /* All we want to know is if it exists. */
478 switch ((stat = do_mr_query("count_members_of_list", 1, argv + 1,
484 Put_message("This list does not exist.");
487 Put_message("You are not allowed to view this list.");
490 com_err(program_name, stat, " in get_list_info");
494 sprintf(temp_buf, "Change/Display membership of '%s'", list_name);
495 m->m_title = strdup(temp_buf);
496 strcpy(current_list, list_name);
500 /* Function Name: ListmaintMemberMenuExit
501 * Description: This is the function called when the member menu is
502 * exited, it frees the memory that is storing the name.
503 * Arguments: m - the menu
507 int ListmaintMemberMenuExit(Menu *m)
510 strcpy(current_list, "");
514 /* Function Name: ListMembersByType
515 * Description: This function lists the users of a list by type.
516 * Arguments: type - the type of the list "USER", "LIST", or "STRING".
517 * tags - whether or not to display tags
519 * NOTE: if type is NULL, all lists members are listed.
522 void ListMembersByType(char *type, int tags)
524 char temp_buf[BUFSIZ];
528 args[0] = current_list;
532 if ((status = do_mr_query(tags ? "get_tagged_members_of_list" :
533 "get_members_of_list", CountArgs(args),
534 args, PrintByType, type)))
535 com_err(program_name, status, " in ListMembersByType");
539 Put_message("List is empty (no members).");
542 sprintf(temp_buf, "No %s Members", type);
543 Put_message(temp_buf);
548 /* Function Name: ListAllMembers
549 * Description: lists all members of the current list.
554 int ListAllMembers(int argc, char **argv)
556 ListMembersByType(NULL, 0);
560 /* Function Name: ListUserMembers
561 * Description: This function lists all members of a list of type "USER".
563 * Returns: DM_NORMAL.
566 int ListUserMembers(int argc, char **argv)
568 ListMembersByType("USER", 0);
572 /* Function Name: ListListMembers
573 * Description: This function lists all members of a list of type "LIST".
575 * Returns: DM_NORMAL.
578 int ListListMembers(int argc, char **argv)
580 ListMembersByType("LIST", 0);
584 /* Function Name: ListStringMembers
585 * Description:This function lists all members of a list of type "STRING".
587 * Returns: DM_NORMAL.
590 int ListStringMembers(int argc, char **argv)
592 ListMembersByType("STRING", 0);
596 /* Function Name: GetMemberInfo
597 * Description: This function gets the information needed to
598 * add or delete a user from a list.
599 * Arguments: argc, argv - standard.
600 * action - name of the action to be performed either
602 * ret_argc, ret_argv - the returned value of argc and argv.
603 * Returns: SUB_ERROR or SUB_NORMAL.
606 int GetMemberInfo(char *action, char **ret_argv)
608 char temp_buf[BUFSIZ];
610 ret_argv[LM_LIST] = strdup(current_list);
612 ret_argv[LM_TYPE] = strdup("user");
613 if (GetTypeFromUser("Type of member", "member", &ret_argv[LM_TYPE]) ==
617 sprintf(temp_buf, "Name of %s to %s", ret_argv[LM_TYPE], action);
618 ret_argv[LM_MEMBER] = strdup(user);
619 if (GetValueFromUser(temp_buf, &ret_argv[LM_MEMBER]) == SUB_ERROR)
621 ret_argv[LM_END] = NULL; /* NULL terminate this list. */
623 if (strcasecmp(ret_argv[LM_TYPE], "string") &&
624 !ValidName(ret_argv[LM_MEMBER]))
632 /* Function Name: AddMember
633 * Description: This function adds a member to a list.
635 * Returns: DM_NORMAL.
638 int AddMember(int argc, char **argv)
640 char *args[10], temp_buf[BUFSIZ], *p;
642 struct mqelem *mailhubs, *elem;
644 if (GetMemberInfo("add", args) == SUB_ERROR)
647 if (!strcmp(args[LM_TYPE], "STRING"))
649 if ((p = strchr(args[LM_MEMBER], '@')))
651 char *host = canonicalize_hostname(strdup(++p));
652 mailhubs = GetTypeValues("mailhub");
653 for (elem = mailhubs; elem; elem = elem->q_forw)
655 if (!strcasecmp(host, elem->q_data))
658 host = strdup(args[LM_MEMBER]);
660 sprintf(temp_buf, "String \"%s\" should be USER or LIST "
661 "\"%s\" because it is a local name.", host,
663 Put_message(temp_buf);
671 else if (!strchr(args[LM_MEMBER], '!'))
673 Put_message("Member which is not a foreign mail address "
674 "should not be type STRING.");
679 if ((status = do_mr_query("add_member_to_list", CountArgs(args), args,
680 NULL, NULL)) != MR_SUCCESS)
682 if (status == MR_EXISTS)
684 sprintf(temp_buf, "The %s %s is already a member of LIST %s.",
685 args[LM_TYPE], args[LM_MEMBER], args[LM_LIST]);
686 Put_message(temp_buf);
689 com_err(program_name, status, " in AddMember");
696 /* Function Name: DeleteMember
697 * Description: This function deletes a member from a list.
702 int DeleteMember(int argc, char **argv)
707 if (GetMemberInfo("delete", args) == SUB_ERROR)
710 if (Confirm("Are you sure you want to delete this member?"))
712 if ((status = do_mr_query("delete_member_from_list", CountArgs(args),
714 com_err(program_name, status, " in DeleteMember");
716 Put_message("Deletion Completed.");
719 Put_message("Deletion has been Aborted.");
725 /* Function Name: TagMember
726 * Description: Add a tag to a list member
731 int TagMember(int argc, char **argv)
735 char temp_buf[BUFSIZ];
737 if (GetMemberInfo("tag", args) == SUB_ERROR)
740 args[LM_TAG] = strdup("");
741 if (GetValueFromUser("Tag" , &args[LM_TAG]) == SUB_ERROR)
743 Put_message("Aborted.");
746 args[LM_TAG_END] = NULL; /* NULL terminate this list. */
748 if ((status = do_mr_query("tag_member_of_list", CountArgs(args),
750 com_err(program_name, status, " in TagMember");
756 /* Function Name: ListAllMembers
757 * Description: lists all members of the current list.
762 int ListMembersWithTags(int argc, char **argv)
764 ListMembersByType(NULL, 1);
768 /* Function Name: InterRemoveItemFromLists
769 * Description: This function allows interactive removal of an item
770 * (user, string, list) for all list that it is on.
772 * Returns: DM_NORMAL.
773 * NOTES: QueryLoop() does not work here because info does not have
774 * enough information in it to delete the member from the list.
777 int InterRemoveItemFromLists(int argc, char **argv)
780 char *type, *name, *args[10], buf[BUFSIZ];
781 struct mqelem *top, *elem;
783 type = strdup("USER");
784 if (GetTypeFromUser("Type of member", "member", &type) == SUB_ERROR)
787 sprintf(buf, "Name of %s", type);
789 if (GetValueFromUser(buf, &name) == SUB_ERROR)
792 if (!ValidName(name))
795 top = elem = GetListInfo(GLOM, type, name);
800 char **info = elem->q_data;
801 sprintf(line, "Delete %s %s from the list \"%s\" (y/n/q)? ", type,
802 name, info[GLOM_NAME]);
803 switch (YesNoQuitQuestion(line, FALSE))
806 Put_message("deleting...");
807 args[DM_LIST] = info[GLOM_NAME];
808 args[DM_TYPE] = type;
809 args[DM_MEMBER] = name;
810 if ((status = do_mr_query("delete_member_from_list", 3, args,
813 /* should probabally check to delete list. */
814 com_err(program_name, status, " in delete_member");
820 Put_message("Aborting...");
830 /*-*-* LIST MENU *-*-*/
832 /* Function Name: ListByMember
833 * Description: This gets all lists that a given member is a member of.
835 * Returns: DM_NORMAL.
838 int ListByMember(int argc, char **argv)
840 char buf[BUFSIZ], temp_buf[BUFSIZ], *type, *name, **info;
841 Bool maillist, group;
842 struct mqelem *top, *elem;
844 type = strdup("USER");
845 if (GetTypeFromUser("Type of member", "member", &type) == SUB_ERROR)
848 sprintf(buf, "Name of %s", type);
850 if (GetValueFromUser(buf, &name) == SUB_ERROR)
853 /* What we really want is a recursive search */
854 sprintf(temp_buf, "R%s", type);
856 type = strdup(temp_buf);
858 if ((maillist = YesNoQuestion("Show Lists that are Maillists (y/n) ?",
861 if ((group = YesNoQuestion("Show Lists that are Groups (y/n) ?",
865 elem = top = GetListInfo(GLOM, type, name);
870 if ((maillist == TRUE && !strcmp(info[GLOM_MAILLIST], "1")) ||
871 (group == TRUE && !strcmp(info[GLOM_GROUP], "1")))
872 Put_message(info[GLOM_NAME]);
879 /* Function Name: ListByAdministrator
880 * Description: This function prints all lists which a given user or
883 * Returns: DM_NORMAL.
886 int ListByAdministrator(int argc, char **argv)
888 char buf[BUFSIZ], temp_buf[BUFSIZ], *type, *name;
891 type = strdup("USER");
892 if (GetTypeFromUser("Type of member", "member", &type) == SUB_ERROR)
895 sprintf(buf, "Name of %s", type);
897 if (GetValueFromUser(buf, &name) == SUB_ERROR)
900 switch (YesNoQuestion("Do you want a recursive search (y/n)", FALSE))
903 sprintf(temp_buf, "R%s", type); /* "USER" to "RUSER" etc. */
905 type = strdup(temp_buf);
913 top = GetListInfo(ACE_USE, type, name);
914 Loop(top, PrintListAce);
920 /* Function Name: ListAllPublicMailLists
921 * Description: This function lists all public mailing lists.
923 * Returns: DM_NORMAL.
926 int ListAllPublicMailLists(int argc, char **argv)
929 static char *args[] = {
932 "FALSE", /* hidden */
933 "TRUE", /* maillist */
934 "DONTCARE", /* group. */
937 if (YesNoQuestion("This query will take a while. Do you wish to continue?",
940 if ((status = do_mr_query("qualified_get_lists", 5, args,
941 Print, NULL)) != MR_SUCCESS)
942 com_err(program_name, status, " in ListAllGroups");