aboutsummaryrefslogtreecommitdiffstats
path: root/set-contact-blocks.c
blob: 31e6626d66530d43a5fa59b0301a537039858521 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* See LICENSE file for copyright and license details. */
#include "common.h"

USAGE("[-a ask-at | -A ask-at] [-s service | -S service] [-t type | -T type] "
      "[-u unblock-at | -U unblock-at] [-y style | -Y style] contact-id");


int
main(int argc, char *argv[])
{
	int edit_srv = 0, edit_type = 0, edit_style = 0, edit_ask = 0, edit_ublk = 0;
	int edit = 0, explicit = 1;
	enum libcontacts_block_type shadow_block = LIBCONTACTS_BLOCK_IGNORE;
	time_t soft_unblock = 0, hard_unblock = 0;
	const char *srv = NULL, *type = NULL, *style = NULL, *ask = NULL, *ublk = NULL;
	struct passwd *user;
	struct libcontacts_contact contact;
	char *p;
	size_t i;

	ARGBEGIN {
	case 'A':
		edit_ask = 1;
		edit = 1;
		/* fall through */
	case 'a':
		if (ask)
			usage();
		ask = ARG();
		if (!isdigit(*ask))
			usage();
		soft_unblock = (time_t)strtoumax(ask, &p, 10);
		if (errno || *p)
			usage();
		break;
	case 'S':
		edit_srv = 1;
		edit = 1;
		/* fall through */
	case 's':
		if (srv)
			usage();
		srv = ARG();
		break;
	case 'T':
		edit_type = 1;
		edit = 1;
		/* fall through */
	case 't':
		if (type)
			usage();
		type = ARG();
		break;
	case 'U':
		edit_ublk = 1;
		edit = 1;
		/* fall through */
	case 'u':
		if (ublk)
			usage();
		ublk = ARG();
		if (!isdigit(*ublk))
			usage();
		hard_unblock = (time_t)strtoumax(ublk, &p, 10);
		if (errno || *p)
			usage();
		break;
	case 'Y':
		edit_style = 1;
		edit = 1;
		/* fall through */
	case 'y':
		if (style)
			usage();
		style = ARG();
		break;
	default:
		usage();
	} ARGEND;

	if ((!srv || edit_srv) && (!type || edit_type) && (!style || edit_style) &&
	    (!ask || edit_ask) && (!ublk || edit_ublk))
		edit = 0;

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

	if (type) {
		if (!strcmp(type, "explicit"))
			explicit = 1;
		else if (!strcmp(type, "shadow"))
			explicit = 0;
		else
			eprintf("value of -%c shall be either \"explicit\" or \"shadow\"\n", edit_type ? 'T' : 't');
	}

	if (style) {
		if (!strcmp(style, "silent"))
			shadow_block = LIBCONTACTS_SILENT;
		else if (!strcmp(style, "as-off"))
			shadow_block = LIBCONTACTS_BLOCK_OFF;
		else if (!strcmp(style, "as-busy"))
			shadow_block = LIBCONTACTS_BLOCK_BUSY;
		else if (!strcmp(style, "ignore"))
			shadow_block = LIBCONTACTS_BLOCK_IGNORE;
		else if (edit_style)
			eprintf("value of -Y shall be either \"silent\", \"as-off\", \"as-busy\", or \"ignore\"\n");
		else
			eprintf("value of -y shall be either \"silent\", \"as-off\", \"as-busy\", or \"ignore\"\n");
	}

	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");

	if (edit && contact.blocks) {
		for (i = 0; contact.blocks[i]; i++) {
			if (srv && !edit_srv && strcmpnul(contact.blocks[i]->service, srv))
				continue;
			if (type && !edit_type && contact.blocks[i]->explicit != explicit)
				continue;
			if (style && !edit_style && contact.blocks[i]->shadow_block != shadow_block)
				continue;
			if (ask && !edit_ask && contact.blocks[i]->soft_unblock != soft_unblock)
				continue;
			if (ublk && !edit_ublk && contact.blocks[i]->hard_unblock != hard_unblock)
				continue;
			if (edit_srv) {
				free(contact.blocks[i]->service);
				contact.blocks[i]->service = estrdup(srv);
			}
			if (edit_type)
				contact.blocks[i]->explicit = explicit;
			if (edit_style)
				contact.blocks[i]->shadow_block = shadow_block;
			if (edit_ask)
				contact.blocks[i]->soft_unblock = soft_unblock;
			if (edit_ublk)
				contact.blocks[i]->hard_unblock = hard_unblock;
		}
	} else if (!edit) {
		i = 0;
		if (contact.blocks)
			for (; contact.blocks[i]; i++);
		contact.blocks = erealloc(contact.blocks, (i + 2) * sizeof(*contact.blocks));
		contact.blocks[i + 1] = NULL;
		contact.blocks[i] = ecalloc(1, sizeof(**contact.emails));
		contact.blocks[i]->service      = estrdup(srv ? srv : ".global");
		contact.blocks[i]->explicit     = explicit;
		contact.blocks[i]->shadow_block = shadow_block;
		contact.blocks[i]->soft_unblock = soft_unblock;
		contact.blocks[i]->hard_unblock = hard_unblock;
	}

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

	return 0;
}