forked from barnowl/barnowl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
zwrite.c
424 lines (376 loc) · 9.4 KB
/
zwrite.c
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#include "owl.h"
CALLER_OWN owl_zwrite *owl_zwrite_new_from_line(const char *line)
{
owl_zwrite *z = g_slice_new(owl_zwrite);
if (owl_zwrite_create_from_line(z, line) != 0) {
g_slice_free(owl_zwrite, z);
return NULL;
}
return z;
}
CALLER_OWN owl_zwrite *owl_zwrite_new(int argc, const char *const *argv)
{
owl_zwrite *z = g_slice_new(owl_zwrite);
if (owl_zwrite_create(z, argc, argv) != 0) {
g_slice_free(owl_zwrite, z);
return NULL;
}
return z;
}
G_GNUC_WARN_UNUSED_RESULT int owl_zwrite_create_from_line(owl_zwrite *z, const char *line)
{
int argc;
char **argv;
int ret;
/* parse the command line for options */
argv = owl_parseline(line, &argc);
if (argc < 0) {
owl_function_error("Unbalanced quotes in zwrite");
return -1;
}
ret = owl_zwrite_create(z, argc, strs(argv));
g_strfreev(argv);
return ret;
}
G_GNUC_WARN_UNUSED_RESULT int owl_zwrite_create(owl_zwrite *z, int argc, const char *const *argv)
{
int badargs = 0;
char *msg = NULL;
/* start with null entries */
z->cmd=NULL;
z->realm=NULL;
z->class=NULL;
z->inst=NULL;
z->opcode=NULL;
z->zsig=NULL;
z->message=NULL;
z->cc=0;
z->noping=0;
z->recips = g_ptr_array_new();
z->zwriteline = owl_argv_quote(argc, argv);
if (argc && *(argv[0])!='-') {
z->cmd=g_strdup(argv[0]);
argc--;
argv++;
}
while (argc) {
if (!strcmp(argv[0], "-c")) {
if (argc<2) {
badargs=1;
break;
}
z->class=owl_validate_utf8(argv[1]);
argv+=2;
argc-=2;
} else if (!strcmp(argv[0], "-i")) {
if (argc<2) {
badargs=1;
break;
}
z->inst=owl_validate_utf8(argv[1]);
argv+=2;
argc-=2;
} else if (!strcmp(argv[0], "-r")) {
if (argc<2) {
badargs=1;
break;
}
z->realm=owl_validate_utf8(argv[1]);
argv+=2;
argc-=2;
} else if (!strcmp(argv[0], "-s")) {
if (argc<2) {
badargs=1;
break;
}
z->zsig=owl_validate_utf8(argv[1]);
argv+=2;
argc-=2;
} else if (!strcmp(argv[0], "-O")) {
if (argc<2) {
badargs=1;
break;
}
z->opcode=owl_validate_utf8(argv[1]);
argv+=2;
argc-=2;
} else if (!strcmp(argv[0], "-m")) {
if (argc<2) {
badargs=1;
break;
}
/* we must already have users or a class or an instance */
if (z->recips->len < 1 && (!z->class) && (!z->inst)) {
badargs=1;
break;
}
/* Once we have -m, gobble up everything else on the line */
argv++;
argc--;
msg = g_strjoinv(" ", (char**)argv);
break;
} else if (!strcmp(argv[0], "-C")) {
z->cc=1;
argv++;
argc--;
} else if (!strcmp(argv[0], "-n")) {
z->noping=1;
argv++;
argc--;
} else {
/* anything unattached is a recipient */
g_ptr_array_add(z->recips, owl_validate_utf8(argv[0]));
argv++;
argc--;
}
}
if (badargs) {
owl_zwrite_cleanup(z);
return(-1);
}
if (z->class == NULL &&
z->inst == NULL &&
z->recips->len == 0) {
owl_function_error("You must specify a recipient for zwrite");
owl_zwrite_cleanup(z);
return(-1);
}
/* now deal with defaults */
if (z->class==NULL) z->class=g_strdup("message");
if (z->inst==NULL) z->inst=g_strdup("personal");
if (z->realm==NULL) z->realm=g_strdup("");
if (z->opcode==NULL) z->opcode=g_strdup("");
/* z->message is allowed to stay NULL */
if(msg) {
owl_zwrite_set_message(z, msg);
g_free(msg);
}
return(0);
}
void owl_zwrite_populate_zsig(owl_zwrite *z)
{
/* get a zsig, if not given */
if (z->zsig != NULL)
return;
z->zsig = owl_perlconfig_execute(owl_global_get_zsigfunc(&g));
}
void owl_zwrite_send_ping(const owl_zwrite *z)
{
int i;
char *to;
if (z->noping) return;
if (strcasecmp(z->class, "message")) {
return;
}
for (i = 0; i < z->recips->len; i++) {
to = owl_zwrite_get_recip_n_with_realm(z, i);
if (owl_zwrite_recip_is_personal(to))
send_ping(to, z->class, z->inst);
g_free(to);
}
}
/* Set the message with no post-processing*/
void owl_zwrite_set_message_raw(owl_zwrite *z, const char *msg)
{
g_free(z->message);
z->message = owl_validate_utf8(msg);
}
void owl_zwrite_set_message(owl_zwrite *z, const char *msg)
{
int i;
GString *message;
char *tmp = NULL, *tmp2;
g_free(z->message);
if (z->cc && owl_zwrite_is_personal(z)) {
message = g_string_new("CC: ");
for (i = 0; i < z->recips->len; i++) {
tmp = owl_zwrite_get_recip_n_with_realm(z, i);
g_string_append_printf(message, "%s ", tmp);
g_free(tmp);
tmp = NULL;
}
tmp = owl_validate_utf8(msg);
tmp2 = owl_text_expand_tabs(tmp);
g_string_append_printf(message, "\n%s", tmp2);
z->message = g_string_free(message, false);
g_free(tmp);
g_free(tmp2);
} else {
tmp=owl_validate_utf8(msg);
z->message=owl_text_expand_tabs(tmp);
g_free(tmp);
}
}
const char *owl_zwrite_get_message(const owl_zwrite *z)
{
if (z->message) return(z->message);
return("");
}
int owl_zwrite_is_message_set(const owl_zwrite *z)
{
if (z->message) return(1);
return(0);
}
int owl_zwrite_send_message(const owl_zwrite *z)
{
int i, ret = 0;
char *to = NULL;
if (z->message==NULL) return(-1);
if (z->recips->len > 0) {
for (i = 0; i < z->recips->len; i++) {
to = owl_zwrite_get_recip_n_with_realm(z, i);
ret = send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, z->message);
/* Abort on the first error, to match the zwrite binary. */
if (ret != 0)
break;
g_free(to);
to = NULL;
}
} else {
to = g_strdup_printf( "@%s", z->realm);
ret = send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, z->message);
}
g_free(to);
return ret;
}
int owl_zwrite_create_and_send_from_line(const char *cmd, const char *msg)
{
owl_zwrite z;
int rv;
rv = owl_zwrite_create_from_line(&z, cmd);
if (rv != 0) return rv;
if (!owl_zwrite_is_message_set(&z)) {
owl_zwrite_set_message(&z, msg);
}
owl_zwrite_populate_zsig(&z);
owl_zwrite_send_message(&z);
owl_zwrite_cleanup(&z);
return(0);
}
const char *owl_zwrite_get_class(const owl_zwrite *z)
{
return(z->class);
}
const char *owl_zwrite_get_instance(const owl_zwrite *z)
{
return(z->inst);
}
const char *owl_zwrite_get_opcode(const owl_zwrite *z)
{
return(z->opcode);
}
void owl_zwrite_set_opcode(owl_zwrite *z, const char *opcode)
{
g_free(z->opcode);
z->opcode=owl_validate_utf8(opcode);
}
const char *owl_zwrite_get_realm(const owl_zwrite *z)
{
return(z->realm);
}
const char *owl_zwrite_get_zsig(const owl_zwrite *z)
{
if (z->zsig) return(z->zsig);
return("");
}
void owl_zwrite_set_zsig(owl_zwrite *z, const char *zsig)
{
g_free(z->zsig);
z->zsig = g_strdup(zsig);
}
int owl_zwrite_get_numrecips(const owl_zwrite *z)
{
return z->recips->len;
}
const char *owl_zwrite_get_recip_n(const owl_zwrite *z, int n)
{
return z->recips->pdata[n];
}
/* Caller must free the result. */
CALLER_OWN char *owl_zwrite_get_recip_n_with_realm(const owl_zwrite *z, int n)
{
if (z->realm[0]) {
return g_strdup_printf("%s@%s", owl_zwrite_get_recip_n(z, n), z->realm);
} else {
return g_strdup(owl_zwrite_get_recip_n(z, n));
}
}
bool owl_zwrite_recip_is_personal(const char *recipient)
{
return recipient[0] && recipient[0] != '@';
}
bool owl_zwrite_is_personal(const owl_zwrite *z)
{
/* return true if at least one of the recipients is personal */
int i;
for (i = 0; i < z->recips->len; i++)
if (owl_zwrite_recip_is_personal(z->recips->pdata[i]))
return true;
return false;
}
void owl_zwrite_delete(owl_zwrite *z)
{
owl_zwrite_cleanup(z);
g_slice_free(owl_zwrite, z);
}
void owl_zwrite_cleanup(owl_zwrite *z)
{
owl_ptr_array_free(z->recips, g_free);
g_free(z->cmd);
g_free(z->zwriteline);
g_free(z->class);
g_free(z->inst);
g_free(z->opcode);
g_free(z->realm);
g_free(z->message);
g_free(z->zsig);
}
/*
* Returns a zwrite line suitable for replying, specifically the
* message field is stripped out. Result should be freed with
* g_free.
*
* If not a CC, only the recip_index'th user will be replied to.
*/
CALLER_OWN char *owl_zwrite_get_replyline(const owl_zwrite *z, int recip_index)
{
/* Match ordering in zwrite help. */
GString *buf = g_string_new("");
int i;
/* Disturbingly, it is apparently possible to z->cmd to be null if
* owl_zwrite_create_from_line got something starting with -. And we
* can't kill it because this is exported to perl. */
owl_string_append_quoted_arg(buf, z->cmd ? z->cmd : "zwrite");
if (z->noping) {
g_string_append(buf, " -n");
}
if (z->cc) {
g_string_append(buf, " -C");
}
if (strcmp(z->class, "message")) {
g_string_append(buf, " -c ");
owl_string_append_quoted_arg(buf, z->class);
}
if (strcmp(z->inst, "personal")) {
g_string_append(buf, " -i ");
owl_string_append_quoted_arg(buf, z->inst);
}
if (z->realm && z->realm[0] != '\0') {
g_string_append(buf, " -r ");
owl_string_append_quoted_arg(buf, z->realm);
}
if (z->opcode && z->opcode[0] != '\0') {
g_string_append(buf, " -O ");
owl_string_append_quoted_arg(buf, z->opcode);
}
if (z->cc) {
for (i = 0; i < z->recips->len; i++) {
g_string_append_c(buf, ' ');
owl_string_append_quoted_arg(buf, z->recips->pdata[i]);
}
} else if (recip_index < z->recips->len) {
g_string_append_c(buf, ' ');
owl_string_append_quoted_arg(buf, z->recips->pdata[recip_index]);
}
return g_string_free(buf, false);
}