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