[7d4fbcd] | 1 | #include "owl.h" |
---|
| 2 | |
---|
[d953ede] | 3 | CALLER_OWN owl_zwrite *owl_zwrite_new_from_line(const char *line) |
---|
[987cf3f] | 4 | { |
---|
[96828e4] | 5 | owl_zwrite *z = g_new(owl_zwrite, 1); |
---|
[987cf3f] | 6 | if (owl_zwrite_create_from_line(z, line) < 0) { |
---|
| 7 | owl_zwrite_delete(z); |
---|
| 8 | return NULL; |
---|
| 9 | } |
---|
| 10 | return z; |
---|
| 11 | } |
---|
| 12 | |
---|
[6329cd5] | 13 | CALLER_OWN owl_zwrite *owl_zwrite_new(int argc, const char *const *argv) |
---|
| 14 | { |
---|
| 15 | owl_zwrite *z = g_new(owl_zwrite, 1); |
---|
| 16 | if (owl_zwrite_create(z, argc, argv) < 0) { |
---|
| 17 | owl_zwrite_delete(z); |
---|
| 18 | return NULL; |
---|
| 19 | } |
---|
| 20 | return z; |
---|
| 21 | } |
---|
| 22 | |
---|
[d427f08] | 23 | G_GNUC_WARN_UNUSED_RESULT int owl_zwrite_create_from_line(owl_zwrite *z, const char *line) |
---|
[ce7db4d] | 24 | { |
---|
[6329cd5] | 25 | int argc; |
---|
[65b2173] | 26 | char **argv; |
---|
[6329cd5] | 27 | int ret; |
---|
[7d4fbcd] | 28 | |
---|
[6329cd5] | 29 | /* parse the command line for options */ |
---|
| 30 | argv = owl_parseline(line, &argc); |
---|
| 31 | if (argc < 0) { |
---|
| 32 | owl_function_error("Unbalanced quotes in zwrite"); |
---|
| 33 | return -1; |
---|
| 34 | } |
---|
| 35 | ret = owl_zwrite_create(z, argc, strs(argv)); |
---|
| 36 | g_strfreev(argv); |
---|
| 37 | return ret; |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | G_GNUC_WARN_UNUSED_RESULT int owl_zwrite_create(owl_zwrite *z, int argc, const char *const *argv) |
---|
| 41 | { |
---|
| 42 | int badargs = 0; |
---|
| 43 | char *msg = NULL; |
---|
[7d4fbcd] | 44 | |
---|
[69f89c7] | 45 | /* start with null entries */ |
---|
[987cf3f] | 46 | z->cmd=NULL; |
---|
[ce7db4d] | 47 | z->realm=NULL; |
---|
| 48 | z->class=NULL; |
---|
| 49 | z->inst=NULL; |
---|
| 50 | z->opcode=NULL; |
---|
| 51 | z->zsig=NULL; |
---|
| 52 | z->message=NULL; |
---|
[7d4fbcd] | 53 | z->cc=0; |
---|
| 54 | z->noping=0; |
---|
[12294d2] | 55 | z->recips = g_ptr_array_new(); |
---|
[6329cd5] | 56 | z->zwriteline = owl_argv_quote(argc, argv); |
---|
[7d4fbcd] | 57 | |
---|
[6329cd5] | 58 | if (argc && *(argv[0])!='-') { |
---|
| 59 | z->cmd=g_strdup(argv[0]); |
---|
| 60 | argc--; |
---|
| 61 | argv++; |
---|
[e1c4636] | 62 | } |
---|
[6329cd5] | 63 | while (argc) { |
---|
| 64 | if (!strcmp(argv[0], "-c")) { |
---|
| 65 | if (argc<2) { |
---|
[7d4fbcd] | 66 | badargs=1; |
---|
| 67 | break; |
---|
| 68 | } |
---|
[6329cd5] | 69 | z->class=owl_validate_utf8(argv[1]); |
---|
| 70 | argv+=2; |
---|
| 71 | argc-=2; |
---|
| 72 | } else if (!strcmp(argv[0], "-i")) { |
---|
| 73 | if (argc<2) { |
---|
[7d4fbcd] | 74 | badargs=1; |
---|
| 75 | break; |
---|
| 76 | } |
---|
[6329cd5] | 77 | z->inst=owl_validate_utf8(argv[1]); |
---|
| 78 | argv+=2; |
---|
| 79 | argc-=2; |
---|
| 80 | } else if (!strcmp(argv[0], "-r")) { |
---|
| 81 | if (argc<2) { |
---|
[7d4fbcd] | 82 | badargs=1; |
---|
| 83 | break; |
---|
| 84 | } |
---|
[6329cd5] | 85 | z->realm=owl_validate_utf8(argv[1]); |
---|
| 86 | argv+=2; |
---|
| 87 | argc-=2; |
---|
| 88 | } else if (!strcmp(argv[0], "-s")) { |
---|
| 89 | if (argc<2) { |
---|
[ce7db4d] | 90 | badargs=1; |
---|
| 91 | break; |
---|
| 92 | } |
---|
[6329cd5] | 93 | z->zsig=owl_validate_utf8(argv[1]); |
---|
| 94 | argv+=2; |
---|
| 95 | argc-=2; |
---|
| 96 | } else if (!strcmp(argv[0], "-O")) { |
---|
| 97 | if (argc<2) { |
---|
[7d4fbcd] | 98 | badargs=1; |
---|
| 99 | break; |
---|
| 100 | } |
---|
[6329cd5] | 101 | z->opcode=owl_validate_utf8(argv[1]); |
---|
| 102 | argv+=2; |
---|
| 103 | argc-=2; |
---|
| 104 | } else if (!strcmp(argv[0], "-m")) { |
---|
| 105 | if (argc<2) { |
---|
[ce7db4d] | 106 | badargs=1; |
---|
| 107 | break; |
---|
| 108 | } |
---|
[69f89c7] | 109 | /* we must already have users or a class or an instance */ |
---|
[12294d2] | 110 | if (z->recips->len < 1 && (!z->class) && (!z->inst)) { |
---|
[ce7db4d] | 111 | badargs=1; |
---|
| 112 | break; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | /* Once we have -m, gobble up everything else on the line */ |
---|
[6329cd5] | 116 | argv++; |
---|
| 117 | argc--; |
---|
| 118 | msg = g_strjoinv(" ", (char**)argv); |
---|
[ce7db4d] | 119 | break; |
---|
[6329cd5] | 120 | } else if (!strcmp(argv[0], "-C")) { |
---|
[7d4fbcd] | 121 | z->cc=1; |
---|
[6329cd5] | 122 | argv++; |
---|
| 123 | argc--; |
---|
| 124 | } else if (!strcmp(argv[0], "-n")) { |
---|
[7d4fbcd] | 125 | z->noping=1; |
---|
[6329cd5] | 126 | argv++; |
---|
| 127 | argc--; |
---|
[7d4fbcd] | 128 | } else { |
---|
| 129 | /* anything unattached is a recipient */ |
---|
[6329cd5] | 130 | g_ptr_array_add(z->recips, owl_validate_utf8(argv[0])); |
---|
| 131 | argv++; |
---|
| 132 | argc--; |
---|
[7d4fbcd] | 133 | } |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | if (badargs) { |
---|
| 137 | return(-1); |
---|
| 138 | } |
---|
| 139 | |
---|
[1fe100c] | 140 | if (z->class == NULL && |
---|
| 141 | z->inst == NULL && |
---|
[12294d2] | 142 | z->recips->len == 0) { |
---|
[1fe100c] | 143 | owl_function_error("You must specify a recipient for zwrite"); |
---|
| 144 | return(-1); |
---|
| 145 | } |
---|
| 146 | |
---|
[ce7db4d] | 147 | /* now deal with defaults */ |
---|
[d4927a7] | 148 | if (z->class==NULL) z->class=g_strdup("message"); |
---|
| 149 | if (z->inst==NULL) z->inst=g_strdup("personal"); |
---|
| 150 | if (z->realm==NULL) z->realm=g_strdup(""); |
---|
| 151 | if (z->opcode==NULL) z->opcode=g_strdup(""); |
---|
[ce7db4d] | 152 | /* z->message is allowed to stay NULL */ |
---|
[a52d13a] | 153 | |
---|
| 154 | if(msg) { |
---|
| 155 | owl_zwrite_set_message(z, msg); |
---|
[ddbbcffa] | 156 | g_free(msg); |
---|
[a52d13a] | 157 | } |
---|
| 158 | |
---|
[3f3ee61] | 159 | return(0); |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | void owl_zwrite_populate_zsig(owl_zwrite *z) |
---|
| 163 | { |
---|
[ce7db4d] | 164 | /* get a zsig, if not given */ |
---|
[de3f641] | 165 | if (z->zsig != NULL) |
---|
| 166 | return; |
---|
[ce7db4d] | 167 | |
---|
[de3f641] | 168 | z->zsig = owl_perlconfig_execute(owl_global_get_zsigfunc(&g)); |
---|
[7d4fbcd] | 169 | } |
---|
| 170 | |
---|
[a352029b] | 171 | void owl_zwrite_send_ping(const owl_zwrite *z) |
---|
[ce7db4d] | 172 | { |
---|
[12294d2] | 173 | int i; |
---|
[44a61ac] | 174 | char *to; |
---|
[7d4fbcd] | 175 | |
---|
| 176 | if (z->noping) return; |
---|
| 177 | |
---|
[3ef779b] | 178 | if (strcasecmp(z->class, "message")) { |
---|
[7d4fbcd] | 179 | return; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | /* if there are no recipients we won't send a ping, which |
---|
| 183 | is what we want */ |
---|
[12294d2] | 184 | for (i = 0; i < z->recips->len; i++) { |
---|
[3f52e14] | 185 | to = owl_zwrite_get_recip_n_with_realm(z, i); |
---|
[3ef779b] | 186 | send_ping(to, z->class, z->inst); |
---|
[ddbbcffa] | 187 | g_free(to); |
---|
[7d4fbcd] | 188 | } |
---|
| 189 | |
---|
| 190 | } |
---|
| 191 | |
---|
[7bfc613] | 192 | /* Set the message with no post-processing*/ |
---|
| 193 | void owl_zwrite_set_message_raw(owl_zwrite *z, const char *msg) |
---|
| 194 | { |
---|
[3b8a563] | 195 | g_free(z->message); |
---|
[7bfc613] | 196 | z->message = owl_validate_utf8(msg); |
---|
| 197 | } |
---|
| 198 | |
---|
[e19eb97] | 199 | void owl_zwrite_set_message(owl_zwrite *z, const char *msg) |
---|
[ce7db4d] | 200 | { |
---|
[12294d2] | 201 | int i; |
---|
[3f52e14] | 202 | GString *message; |
---|
[a52d13a] | 203 | char *tmp = NULL, *tmp2; |
---|
[db2dd3d] | 204 | |
---|
[3b8a563] | 205 | g_free(z->message); |
---|
[db2dd3d] | 206 | |
---|
[12294d2] | 207 | if (z->recips->len > 0 && z->cc) { |
---|
[3f52e14] | 208 | message = g_string_new("CC: "); |
---|
[12294d2] | 209 | for (i = 0; i < z->recips->len; i++) { |
---|
[3f52e14] | 210 | tmp = owl_zwrite_get_recip_n_with_realm(z, i); |
---|
| 211 | g_string_append_printf(message, "%s ", tmp); |
---|
[ddbbcffa] | 212 | g_free(tmp); |
---|
[3538bc8] | 213 | tmp = NULL; |
---|
[db2dd3d] | 214 | } |
---|
[4b17a6c] | 215 | tmp = owl_validate_utf8(msg); |
---|
[a52d13a] | 216 | tmp2 = owl_text_expand_tabs(tmp); |
---|
[3f52e14] | 217 | g_string_append_printf(message, "\n%s", tmp2); |
---|
| 218 | z->message = g_string_free(message, false); |
---|
[ddbbcffa] | 219 | g_free(tmp); |
---|
| 220 | g_free(tmp2); |
---|
[db2dd3d] | 221 | } else { |
---|
[a52d13a] | 222 | tmp=owl_validate_utf8(msg); |
---|
| 223 | z->message=owl_text_expand_tabs(tmp); |
---|
[ddbbcffa] | 224 | g_free(tmp); |
---|
[db2dd3d] | 225 | } |
---|
[ce7db4d] | 226 | } |
---|
| 227 | |
---|
[a352029b] | 228 | const char *owl_zwrite_get_message(const owl_zwrite *z) |
---|
[ce7db4d] | 229 | { |
---|
| 230 | if (z->message) return(z->message); |
---|
| 231 | return(""); |
---|
| 232 | } |
---|
| 233 | |
---|
[a352029b] | 234 | int owl_zwrite_is_message_set(const owl_zwrite *z) |
---|
[ce7db4d] | 235 | { |
---|
| 236 | if (z->message) return(1); |
---|
| 237 | return(0); |
---|
| 238 | } |
---|
| 239 | |
---|
[a352029b] | 240 | int owl_zwrite_send_message(const owl_zwrite *z) |
---|
[ce7db4d] | 241 | { |
---|
[12294d2] | 242 | int i, ret = 0; |
---|
[823671c] | 243 | char *to = NULL; |
---|
[7d4fbcd] | 244 | |
---|
[ce7db4d] | 245 | if (z->message==NULL) return(-1); |
---|
| 246 | |
---|
[12294d2] | 247 | if (z->recips->len > 0) { |
---|
| 248 | for (i = 0; i < z->recips->len; i++) { |
---|
[3f52e14] | 249 | to = owl_zwrite_get_recip_n_with_realm(z, i); |
---|
[0743696] | 250 | ret = send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, z->message); |
---|
| 251 | /* Abort on the first error, to match the zwrite binary. */ |
---|
| 252 | if (ret != 0) |
---|
| 253 | break; |
---|
[ddbbcffa] | 254 | g_free(to); |
---|
[823671c] | 255 | to = NULL; |
---|
[7d4fbcd] | 256 | } |
---|
| 257 | } else { |
---|
[3472845] | 258 | to = g_strdup_printf( "@%s", z->realm); |
---|
[0743696] | 259 | ret = send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, z->message); |
---|
[ce7db4d] | 260 | } |
---|
[ddbbcffa] | 261 | g_free(to); |
---|
[0743696] | 262 | return ret; |
---|
[ce7db4d] | 263 | } |
---|
| 264 | |
---|
[e19eb97] | 265 | int owl_zwrite_create_and_send_from_line(const char *cmd, const char *msg) |
---|
[ce7db4d] | 266 | { |
---|
| 267 | owl_zwrite z; |
---|
| 268 | int rv; |
---|
| 269 | rv=owl_zwrite_create_from_line(&z, cmd); |
---|
| 270 | if (rv) return(rv); |
---|
| 271 | if (!owl_zwrite_is_message_set(&z)) { |
---|
| 272 | owl_zwrite_set_message(&z, msg); |
---|
[7d4fbcd] | 273 | } |
---|
[3f3ee61] | 274 | owl_zwrite_populate_zsig(&z); |
---|
[ce7db4d] | 275 | owl_zwrite_send_message(&z); |
---|
[c230bc1] | 276 | owl_zwrite_cleanup(&z); |
---|
[ce7db4d] | 277 | return(0); |
---|
[7d4fbcd] | 278 | } |
---|
| 279 | |
---|
[a352029b] | 280 | const char *owl_zwrite_get_class(const owl_zwrite *z) |
---|
[ce7db4d] | 281 | { |
---|
[7d4fbcd] | 282 | return(z->class); |
---|
| 283 | } |
---|
| 284 | |
---|
[a352029b] | 285 | const char *owl_zwrite_get_instance(const owl_zwrite *z) |
---|
[ce7db4d] | 286 | { |
---|
[7d4fbcd] | 287 | return(z->inst); |
---|
| 288 | } |
---|
| 289 | |
---|
[a352029b] | 290 | const char *owl_zwrite_get_opcode(const owl_zwrite *z) |
---|
[ce7db4d] | 291 | { |
---|
[4b464a4] | 292 | return(z->opcode); |
---|
| 293 | } |
---|
| 294 | |
---|
[e19eb97] | 295 | void owl_zwrite_set_opcode(owl_zwrite *z, const char *opcode) |
---|
[9ceee9d] | 296 | { |
---|
[3b8a563] | 297 | g_free(z->opcode); |
---|
[4b17a6c] | 298 | z->opcode=owl_validate_utf8(opcode); |
---|
[9ceee9d] | 299 | } |
---|
| 300 | |
---|
[a352029b] | 301 | const char *owl_zwrite_get_realm(const owl_zwrite *z) |
---|
[ce7db4d] | 302 | { |
---|
[7d4fbcd] | 303 | return(z->realm); |
---|
| 304 | } |
---|
| 305 | |
---|
[a352029b] | 306 | const char *owl_zwrite_get_zsig(const owl_zwrite *z) |
---|
[ce7db4d] | 307 | { |
---|
| 308 | if (z->zsig) return(z->zsig); |
---|
| 309 | return(""); |
---|
[56330ff] | 310 | } |
---|
| 311 | |
---|
[24ccc01] | 312 | void owl_zwrite_set_zsig(owl_zwrite *z, const char *zsig) |
---|
| 313 | { |
---|
[3b8a563] | 314 | g_free(z->zsig); |
---|
[d4927a7] | 315 | z->zsig = g_strdup(zsig); |
---|
[24ccc01] | 316 | } |
---|
| 317 | |
---|
[a352029b] | 318 | int owl_zwrite_get_numrecips(const owl_zwrite *z) |
---|
[ce7db4d] | 319 | { |
---|
[12294d2] | 320 | return z->recips->len; |
---|
[7d4fbcd] | 321 | } |
---|
| 322 | |
---|
[a352029b] | 323 | const char *owl_zwrite_get_recip_n(const owl_zwrite *z, int n) |
---|
[ce7db4d] | 324 | { |
---|
[12294d2] | 325 | return z->recips->pdata[n]; |
---|
[7d4fbcd] | 326 | } |
---|
| 327 | |
---|
[3f52e14] | 328 | /* Caller must free the result. */ |
---|
[6829afc] | 329 | CALLER_OWN char *owl_zwrite_get_recip_n_with_realm(const owl_zwrite *z, int n) |
---|
[3f52e14] | 330 | { |
---|
| 331 | if (z->realm[0]) { |
---|
| 332 | return g_strdup_printf("%s@%s", owl_zwrite_get_recip_n(z, n), z->realm); |
---|
| 333 | } else { |
---|
| 334 | return g_strdup(owl_zwrite_get_recip_n(z, n)); |
---|
| 335 | } |
---|
| 336 | } |
---|
| 337 | |
---|
[a352029b] | 338 | int owl_zwrite_is_personal(const owl_zwrite *z) |
---|
[ce7db4d] | 339 | { |
---|
[7d4fbcd] | 340 | /* return true if at least one of the recipients is personal */ |
---|
[12294d2] | 341 | int i; |
---|
| 342 | char *recip; |
---|
[7d4fbcd] | 343 | |
---|
[12294d2] | 344 | for (i = 0; i < z->recips->len; i++) { |
---|
| 345 | recip = z->recips->pdata[i]; |
---|
| 346 | if (recip[0] != '@') return 1; |
---|
[7d4fbcd] | 347 | } |
---|
| 348 | return(0); |
---|
| 349 | } |
---|
[3f3ee61] | 350 | |
---|
[987cf3f] | 351 | void owl_zwrite_delete(owl_zwrite *z) |
---|
| 352 | { |
---|
| 353 | owl_zwrite_cleanup(z); |
---|
[ddbbcffa] | 354 | g_free(z); |
---|
[987cf3f] | 355 | } |
---|
| 356 | |
---|
[c230bc1] | 357 | void owl_zwrite_cleanup(owl_zwrite *z) |
---|
[ce7db4d] | 358 | { |
---|
[3cdd6d2] | 359 | owl_ptr_array_free(z->recips, g_free); |
---|
[3b8a563] | 360 | g_free(z->cmd); |
---|
| 361 | g_free(z->zwriteline); |
---|
| 362 | g_free(z->class); |
---|
| 363 | g_free(z->inst); |
---|
| 364 | g_free(z->opcode); |
---|
| 365 | g_free(z->realm); |
---|
| 366 | g_free(z->message); |
---|
| 367 | g_free(z->zsig); |
---|
[7d4fbcd] | 368 | } |
---|
[719119de] | 369 | |
---|
| 370 | /* |
---|
| 371 | * Returns a zwrite line suitable for replying, specifically the |
---|
| 372 | * message field is stripped out. Result should be freed with |
---|
[ddbbcffa] | 373 | * g_free. |
---|
[a5b5d00] | 374 | * |
---|
| 375 | * If not a CC, only the recip_index'th user will be replied to. |
---|
[719119de] | 376 | */ |
---|
[6829afc] | 377 | CALLER_OWN char *owl_zwrite_get_replyline(const owl_zwrite *z, int recip_index) |
---|
[719119de] | 378 | { |
---|
| 379 | /* Match ordering in zwrite help. */ |
---|
| 380 | GString *buf = g_string_new(""); |
---|
| 381 | int i; |
---|
| 382 | |
---|
| 383 | /* Disturbingly, it is apparently possible to z->cmd to be null if |
---|
| 384 | * owl_zwrite_create_from_line got something starting with -. And we |
---|
| 385 | * can't kill it because this is exported to perl. */ |
---|
| 386 | owl_string_append_quoted_arg(buf, z->cmd ? z->cmd : "zwrite"); |
---|
| 387 | if (z->noping) { |
---|
| 388 | g_string_append(buf, " -n"); |
---|
| 389 | } |
---|
| 390 | if (z->cc) { |
---|
| 391 | g_string_append(buf, " -C"); |
---|
| 392 | } |
---|
| 393 | if (strcmp(z->class, "message")) { |
---|
| 394 | g_string_append(buf, " -c "); |
---|
| 395 | owl_string_append_quoted_arg(buf, z->class); |
---|
| 396 | } |
---|
| 397 | if (strcmp(z->inst, "personal")) { |
---|
| 398 | g_string_append(buf, " -i "); |
---|
| 399 | owl_string_append_quoted_arg(buf, z->inst); |
---|
| 400 | } |
---|
| 401 | if (z->realm && z->realm[0] != '\0') { |
---|
| 402 | g_string_append(buf, " -r "); |
---|
| 403 | owl_string_append_quoted_arg(buf, z->realm); |
---|
| 404 | } |
---|
| 405 | if (z->opcode && z->opcode[0] != '\0') { |
---|
| 406 | g_string_append(buf, " -O "); |
---|
| 407 | owl_string_append_quoted_arg(buf, z->opcode); |
---|
| 408 | } |
---|
[a5b5d00] | 409 | if (z->cc) { |
---|
[12294d2] | 410 | for (i = 0; i < z->recips->len; i++) { |
---|
[a5b5d00] | 411 | g_string_append_c(buf, ' '); |
---|
[12294d2] | 412 | owl_string_append_quoted_arg(buf, z->recips->pdata[i]); |
---|
[a5b5d00] | 413 | } |
---|
[12294d2] | 414 | } else if (recip_index < z->recips->len) { |
---|
[719119de] | 415 | g_string_append_c(buf, ' '); |
---|
[12294d2] | 416 | owl_string_append_quoted_arg(buf, z->recips->pdata[recip_index]); |
---|
[719119de] | 417 | } |
---|
| 418 | |
---|
| 419 | return g_string_free(buf, false); |
---|
| 420 | } |
---|