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