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