aboutsummaryrefslogtreecommitdiffstats
path: root/set-contact-numbers.c
blob: f75f9f53dc45b3fe105ad4aed80c48e19c3d5186 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* See LICENSE file for copyright and license details. */
#include "common.h"

USAGE("[-C old-context] [-c new-context] [-N old-number] [-n new-number] [-F | -f] [-M | -m] [-u] contact-id");


int
main(int argc, char *argv[])
{
	int set_facsimile = -1, set_mobile = -1, add = 1, edit = 0, remove = 0;
	const char *context, *number, *lookup_context, *lookup_number;
	struct passwd *user;
	struct libcontacts_contact contact;
	struct libcontacts_number **r, **w;
	size_t i;

	ARGBEGIN {
	case 'C':
		add = 0;
		if (lookup_context)
			usage();
		lookup_context = ARG();
		break;
	case 'c':
		edit = 1;
		if (context)
			usage();
		context = ARG();
		break;
	case 'N':
		add = 0;
		if (lookup_number)
			usage();
		lookup_number = ARG();
		break;
	case 'n':
		edit = 1;
		if (number)
			usage();
		number = ARG();
		break;
	case 'F':
		if (set_facsimile >= 0)
			return 0;
		set_facsimile = 0;
		break;
	case 'f':
		if (set_facsimile >= 0)
			return 0;
		set_facsimile = 1;
		break;
	case 'M':
		if (set_mobile >= 0)
			return 0;
		set_mobile = 0;
		break;
	case 'm':
		if (set_mobile >= 0)
			return 0;
		set_mobile = 1;
		break;
	case 'u':
		remove = 1;
		break;
	default:
		usage();
	} ARGEND;

	if (remove == edit) {
		if (edit)
			eprintf("-u cannot be combined with -cn\n");
		eprintf("at least one of -cnu is required\n");
	}

	if (add)
		edit = 0;

	if (argc != 0 || !*argv[0] || strchr(argv[0], '/'))
		usage();

	errno = 0;
	user = getpwuid(getuid());
	if (!user)
		eprintf("getpwuid: %s\n", errno ? strerror(errno) : "user does not exist");

	if (libcontacts_load_contact(argv[0], &contact, user))
		eprintf("libcontacts_load_contact %s: %s\n", argv[0], errno ? strerror(errno) : "contact file is malformatted");

	i = 0;
	if ((edit || remove) && contact.numbers) {
		for (r = contact.numbers; *r; r++) {
			if (lookup_context && strcmpnul((*r)->context, lookup_context))
				continue;
			if (lookup_number && strcmpnul((*r)->context, lookup_number))
				continue;
			break;
		}
		if (!edit) {
			libcontacts_number_destroy(*r);
			free(*r);
			for (w = r++; (*w++ = *r++););
		} else if (*r) {
			if (context) {
				free(contact.numbers[i]->context);
				contact.numbers[i]->context = estrdup(context);
			}
			if (number) {
				free(contact.numbers[i]->number);
				contact.numbers[i]->number = estrdup(number);
			}
			if (set_mobile >= 0)
				contact.numbers[i]->is_mobile = set_mobile;
			if (set_facsimile >= 0)
				contact.numbers[i]->is_facsimile = set_facsimile;
		} else {
			libcontacts_contact_destroy(&contact);
			return 0;
		}
	} else if (!edit && !remove) {
		if (contact.numbers)
			for (i = 0; contact.numbers[i]; i++);
		contact.numbers = erealloc(contact.numbers, (i + 2) * sizeof(*contact.numbers));
		contact.numbers[i + 1] = NULL;
		contact.numbers[i] = ecalloc(1, sizeof(**contact.numbers));
		contact.numbers[i]->context = estrdup(argv[1]);
		contact.numbers[i]->number = estrdup(argv[2]);
		contact.numbers[i]->is_mobile = set_mobile > 0;
		contact.numbers[i]->is_facsimile = set_facsimile > 0;
	}

	if (libcontacts_save_contact(&contact, user))
		eprintf("libcontacts_save_contact %s:", argv[0]);
	libcontacts_contact_destroy(&contact);

	return 0;
}