aboutsummaryrefslogtreecommitdiffstats
path: root/add-contact.c
blob: 9a72bacce8602db21c315be8588116838c333246 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* See LICENSE file for copyright and license details. */
#include "common.h"

USAGE("[contact-id]");


int
main(int argc, char *argv[])
{
	struct passwd *user;
	struct libcontacts_contact contact;
	const char *id = NULL;
	char *path;
	int fd;
	size_t i;

	NOFLAGS(argc > 1);

	if (argc) {
		id = argv[0];
		if (!id[0])
			usage();
		for (i = 0; id[i]; i++) {
			if (id[i] == '/')
				eprintf("contact-id cannot contain /\n");
			else if (!islower(id[i]) && !isdigit(id[i]) && (i && id[i] != '.') && id[i] != '-')
				if (id[i] != '~' || id[i + 1])
					weprintf("it is recommended to only have [a-z0-9-] in contact-id\n");
		}
		if (id[i - 1] == '~')
			eprintf("contact-id cannot end with ~\n");
		if (id[0] == '.' && strcmp(id, ".me") && strcmp(id, ".nobody"))
			weprintf("it is recommended to only have [a-z0-9-] in contact-id\n");
	}

	errno = 0;
	user = getpwuid(getuid());
	if (!user)
		eprintf("getpwuid: %s\n", errno ? strerror(errno) : "user does not exist");
	if (!user->pw_dir || !*user->pw_dir)
		eprintf("user does not have a home directory\n");
	if (access(user->pw_dir, R_OK | W_OK | X_OK))
		eprintf("access %s R_OK|W_OK|X_OK:", user->pw_dir);

	if (id) {
		path = libcontacts_get_path(id, user);
		if (!path)
			eprintf("libcontacts_get_path %s:", id);
		fd = open(path, O_WRONLY | O_CREAT | O_EXCL, 0666);
		if (fd < 0)
			eprintf("open %s O_WRONLY|O_CREAT|O_EXCL 0666:", path);
		if (close(fd))
			eprintf("close %s:", path);
		free(path);
	} else {
		memset(&contact, 0, sizeof(contact));
		if (libcontacts_save_contact(&contact, user))
			eprintf("libcontacts_save_contact:");
		printf("%s\n", contact.id);
		if (fflush(stdout) || ferror(stdout) || fclose(stdout))
			eprintf("printf:");
		free(contact.id);
	}

	return 0;
}