[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 | |
---|
[987cf3f] | 7 | owl_zwrite *owl_zwrite_new(const char *line) |
---|
| 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 | |
---|
[e19eb97] | 17 | 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; |
---|
| 36 | owl_list_create(&(z->recips)); |
---|
[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 */ |
---|
[e016fc2] | 99 | if (owl_list_get_size(&(z->recips))<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 */ |
---|
[4b17a6c] | 119 | owl_list_append_element(&(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 && |
---|
| 133 | owl_list_get_size(&(z->recips))==0) { |
---|
| 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 | { |
---|
[7d4fbcd] | 164 | int i, j; |
---|
[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 */ |
---|
| 175 | j=owl_list_get_size(&(z->recips)); |
---|
| 176 | for (i=0; i<j; i++) { |
---|
[3f52e14] | 177 | to = owl_zwrite_get_recip_n_with_realm(z, i); |
---|
[3ef779b] | 178 | send_ping(to, z->class, z->inst); |
---|
[ddbbcffa] | 179 | g_free(to); |
---|
[7d4fbcd] | 180 | } |
---|
| 181 | |
---|
| 182 | } |
---|
| 183 | |
---|
[7bfc613] | 184 | /* Set the message with no post-processing*/ |
---|
| 185 | void owl_zwrite_set_message_raw(owl_zwrite *z, const char *msg) |
---|
| 186 | { |
---|
[ddbbcffa] | 187 | if (z->message) g_free(z->message); |
---|
[7bfc613] | 188 | z->message = owl_validate_utf8(msg); |
---|
| 189 | } |
---|
| 190 | |
---|
[e19eb97] | 191 | void owl_zwrite_set_message(owl_zwrite *z, const char *msg) |
---|
[ce7db4d] | 192 | { |
---|
[db2dd3d] | 193 | int i, j; |
---|
[3f52e14] | 194 | GString *message; |
---|
[a52d13a] | 195 | char *tmp = NULL, *tmp2; |
---|
[db2dd3d] | 196 | |
---|
[ddbbcffa] | 197 | if (z->message) g_free(z->message); |
---|
[db2dd3d] | 198 | |
---|
| 199 | j=owl_list_get_size(&(z->recips)); |
---|
| 200 | if (j>0 && z->cc) { |
---|
[3f52e14] | 201 | message = g_string_new("CC: "); |
---|
[db2dd3d] | 202 | for (i=0; i<j; i++) { |
---|
[3f52e14] | 203 | tmp = owl_zwrite_get_recip_n_with_realm(z, i); |
---|
| 204 | g_string_append_printf(message, "%s ", tmp); |
---|
[ddbbcffa] | 205 | g_free(tmp); |
---|
[3538bc8] | 206 | tmp = NULL; |
---|
[db2dd3d] | 207 | } |
---|
[4b17a6c] | 208 | tmp = owl_validate_utf8(msg); |
---|
[a52d13a] | 209 | tmp2 = owl_text_expand_tabs(tmp); |
---|
[3f52e14] | 210 | g_string_append_printf(message, "\n%s", tmp2); |
---|
| 211 | z->message = g_string_free(message, false); |
---|
[ddbbcffa] | 212 | g_free(tmp); |
---|
| 213 | g_free(tmp2); |
---|
[db2dd3d] | 214 | } else { |
---|
[a52d13a] | 215 | tmp=owl_validate_utf8(msg); |
---|
| 216 | z->message=owl_text_expand_tabs(tmp); |
---|
[ddbbcffa] | 217 | g_free(tmp); |
---|
[db2dd3d] | 218 | } |
---|
[ce7db4d] | 219 | } |
---|
| 220 | |
---|
[a352029b] | 221 | const char *owl_zwrite_get_message(const owl_zwrite *z) |
---|
[ce7db4d] | 222 | { |
---|
| 223 | if (z->message) return(z->message); |
---|
| 224 | return(""); |
---|
| 225 | } |
---|
| 226 | |
---|
[a352029b] | 227 | int owl_zwrite_is_message_set(const owl_zwrite *z) |
---|
[ce7db4d] | 228 | { |
---|
| 229 | if (z->message) return(1); |
---|
| 230 | return(0); |
---|
| 231 | } |
---|
| 232 | |
---|
[a352029b] | 233 | int owl_zwrite_send_message(const owl_zwrite *z) |
---|
[ce7db4d] | 234 | { |
---|
[0743696] | 235 | int i, j, ret = 0; |
---|
[823671c] | 236 | char *to = NULL; |
---|
[7d4fbcd] | 237 | |
---|
[ce7db4d] | 238 | if (z->message==NULL) return(-1); |
---|
| 239 | |
---|
[7d4fbcd] | 240 | j=owl_list_get_size(&(z->recips)); |
---|
| 241 | if (j>0) { |
---|
| 242 | for (i=0; i<j; i++) { |
---|
[3f52e14] | 243 | to = owl_zwrite_get_recip_n_with_realm(z, i); |
---|
[0743696] | 244 | ret = send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, z->message); |
---|
| 245 | /* Abort on the first error, to match the zwrite binary. */ |
---|
| 246 | if (ret != 0) |
---|
| 247 | break; |
---|
[ddbbcffa] | 248 | g_free(to); |
---|
[823671c] | 249 | to = NULL; |
---|
[7d4fbcd] | 250 | } |
---|
| 251 | } else { |
---|
[3472845] | 252 | to = g_strdup_printf( "@%s", z->realm); |
---|
[0743696] | 253 | ret = send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, z->message); |
---|
[ce7db4d] | 254 | } |
---|
[ddbbcffa] | 255 | g_free(to); |
---|
[0743696] | 256 | return ret; |
---|
[ce7db4d] | 257 | } |
---|
| 258 | |
---|
[e19eb97] | 259 | int owl_zwrite_create_and_send_from_line(const char *cmd, const char *msg) |
---|
[ce7db4d] | 260 | { |
---|
| 261 | owl_zwrite z; |
---|
| 262 | int rv; |
---|
| 263 | rv=owl_zwrite_create_from_line(&z, cmd); |
---|
| 264 | if (rv) return(rv); |
---|
| 265 | if (!owl_zwrite_is_message_set(&z)) { |
---|
| 266 | owl_zwrite_set_message(&z, msg); |
---|
[7d4fbcd] | 267 | } |
---|
[3f3ee61] | 268 | owl_zwrite_populate_zsig(&z); |
---|
[ce7db4d] | 269 | owl_zwrite_send_message(&z); |
---|
[c230bc1] | 270 | owl_zwrite_cleanup(&z); |
---|
[ce7db4d] | 271 | return(0); |
---|
[7d4fbcd] | 272 | } |
---|
| 273 | |
---|
[a352029b] | 274 | const char *owl_zwrite_get_class(const owl_zwrite *z) |
---|
[ce7db4d] | 275 | { |
---|
[7d4fbcd] | 276 | return(z->class); |
---|
| 277 | } |
---|
| 278 | |
---|
[a352029b] | 279 | const char *owl_zwrite_get_instance(const owl_zwrite *z) |
---|
[ce7db4d] | 280 | { |
---|
[7d4fbcd] | 281 | return(z->inst); |
---|
| 282 | } |
---|
| 283 | |
---|
[a352029b] | 284 | const char *owl_zwrite_get_opcode(const owl_zwrite *z) |
---|
[ce7db4d] | 285 | { |
---|
[4b464a4] | 286 | return(z->opcode); |
---|
| 287 | } |
---|
| 288 | |
---|
[e19eb97] | 289 | void owl_zwrite_set_opcode(owl_zwrite *z, const char *opcode) |
---|
[9ceee9d] | 290 | { |
---|
[ddbbcffa] | 291 | if (z->opcode) g_free(z->opcode); |
---|
[4b17a6c] | 292 | z->opcode=owl_validate_utf8(opcode); |
---|
[9ceee9d] | 293 | } |
---|
| 294 | |
---|
[a352029b] | 295 | const char *owl_zwrite_get_realm(const owl_zwrite *z) |
---|
[ce7db4d] | 296 | { |
---|
[7d4fbcd] | 297 | return(z->realm); |
---|
| 298 | } |
---|
| 299 | |
---|
[a352029b] | 300 | const char *owl_zwrite_get_zsig(const owl_zwrite *z) |
---|
[ce7db4d] | 301 | { |
---|
| 302 | if (z->zsig) return(z->zsig); |
---|
| 303 | return(""); |
---|
[56330ff] | 304 | } |
---|
| 305 | |
---|
[24ccc01] | 306 | void owl_zwrite_set_zsig(owl_zwrite *z, const char *zsig) |
---|
| 307 | { |
---|
[ddbbcffa] | 308 | if(z->zsig) g_free(z->zsig); |
---|
[d4927a7] | 309 | z->zsig = g_strdup(zsig); |
---|
[24ccc01] | 310 | } |
---|
| 311 | |
---|
[a352029b] | 312 | int owl_zwrite_get_numrecips(const owl_zwrite *z) |
---|
[ce7db4d] | 313 | { |
---|
[7d4fbcd] | 314 | return(owl_list_get_size(&(z->recips))); |
---|
| 315 | } |
---|
| 316 | |
---|
[a352029b] | 317 | const char *owl_zwrite_get_recip_n(const owl_zwrite *z, int n) |
---|
[ce7db4d] | 318 | { |
---|
[7d4fbcd] | 319 | return(owl_list_get_element(&(z->recips), n)); |
---|
| 320 | } |
---|
| 321 | |
---|
[3f52e14] | 322 | /* Caller must free the result. */ |
---|
| 323 | char *owl_zwrite_get_recip_n_with_realm(const owl_zwrite *z, int n) |
---|
| 324 | { |
---|
| 325 | if (z->realm[0]) { |
---|
| 326 | return g_strdup_printf("%s@%s", owl_zwrite_get_recip_n(z, n), z->realm); |
---|
| 327 | } else { |
---|
| 328 | return g_strdup(owl_zwrite_get_recip_n(z, n)); |
---|
| 329 | } |
---|
| 330 | } |
---|
| 331 | |
---|
[a352029b] | 332 | int owl_zwrite_is_personal(const owl_zwrite *z) |
---|
[ce7db4d] | 333 | { |
---|
[7d4fbcd] | 334 | /* return true if at least one of the recipients is personal */ |
---|
| 335 | int i, j; |
---|
| 336 | char *foo; |
---|
| 337 | |
---|
| 338 | j=owl_list_get_size(&(z->recips)); |
---|
| 339 | for (i=0; i<j; i++) { |
---|
| 340 | foo=owl_list_get_element(&(z->recips), i); |
---|
| 341 | if (foo[0]!='@') return(1); |
---|
| 342 | } |
---|
| 343 | return(0); |
---|
| 344 | } |
---|
[3f3ee61] | 345 | |
---|
[987cf3f] | 346 | void owl_zwrite_delete(owl_zwrite *z) |
---|
| 347 | { |
---|
| 348 | owl_zwrite_cleanup(z); |
---|
[ddbbcffa] | 349 | g_free(z); |
---|
[987cf3f] | 350 | } |
---|
| 351 | |
---|
[c230bc1] | 352 | void owl_zwrite_cleanup(owl_zwrite *z) |
---|
[ce7db4d] | 353 | { |
---|
[ddbbcffa] | 354 | owl_list_cleanup(&(z->recips), &g_free); |
---|
| 355 | if (z->cmd) g_free(z->cmd); |
---|
| 356 | if (z->zwriteline) g_free(z->zwriteline); |
---|
| 357 | if (z->class) g_free(z->class); |
---|
| 358 | if (z->inst) g_free(z->inst); |
---|
| 359 | if (z->opcode) g_free(z->opcode); |
---|
| 360 | if (z->realm) g_free(z->realm); |
---|
| 361 | if (z->message) g_free(z->message); |
---|
| 362 | if (z->zsig) g_free(z->zsig); |
---|
[7d4fbcd] | 363 | } |
---|
[719119de] | 364 | |
---|
| 365 | /* |
---|
| 366 | * Returns a zwrite line suitable for replying, specifically the |
---|
| 367 | * message field is stripped out. Result should be freed with |
---|
[ddbbcffa] | 368 | * g_free. |
---|
[a5b5d00] | 369 | * |
---|
| 370 | * If not a CC, only the recip_index'th user will be replied to. |
---|
[719119de] | 371 | */ |
---|
[a5b5d00] | 372 | char *owl_zwrite_get_replyline(const owl_zwrite *z, int recip_index) |
---|
[719119de] | 373 | { |
---|
| 374 | /* Match ordering in zwrite help. */ |
---|
| 375 | GString *buf = g_string_new(""); |
---|
| 376 | int i; |
---|
| 377 | |
---|
| 378 | /* Disturbingly, it is apparently possible to z->cmd to be null if |
---|
| 379 | * owl_zwrite_create_from_line got something starting with -. And we |
---|
| 380 | * can't kill it because this is exported to perl. */ |
---|
| 381 | owl_string_append_quoted_arg(buf, z->cmd ? z->cmd : "zwrite"); |
---|
| 382 | if (z->noping) { |
---|
| 383 | g_string_append(buf, " -n"); |
---|
| 384 | } |
---|
| 385 | if (z->cc) { |
---|
| 386 | g_string_append(buf, " -C"); |
---|
| 387 | } |
---|
| 388 | if (strcmp(z->class, "message")) { |
---|
| 389 | g_string_append(buf, " -c "); |
---|
| 390 | owl_string_append_quoted_arg(buf, z->class); |
---|
| 391 | } |
---|
| 392 | if (strcmp(z->inst, "personal")) { |
---|
| 393 | g_string_append(buf, " -i "); |
---|
| 394 | owl_string_append_quoted_arg(buf, z->inst); |
---|
| 395 | } |
---|
| 396 | if (z->realm && z->realm[0] != '\0') { |
---|
| 397 | g_string_append(buf, " -r "); |
---|
| 398 | owl_string_append_quoted_arg(buf, z->realm); |
---|
| 399 | } |
---|
| 400 | if (z->opcode && z->opcode[0] != '\0') { |
---|
| 401 | g_string_append(buf, " -O "); |
---|
| 402 | owl_string_append_quoted_arg(buf, z->opcode); |
---|
| 403 | } |
---|
[a5b5d00] | 404 | if (z->cc) { |
---|
| 405 | for (i = 0; i < owl_list_get_size(&(z->recips)); i++) { |
---|
| 406 | g_string_append_c(buf, ' '); |
---|
| 407 | owl_string_append_quoted_arg(buf, owl_list_get_element(&(z->recips), i)); |
---|
| 408 | } |
---|
| 409 | } else if (recip_index < owl_list_get_size(&(z->recips))) { |
---|
[719119de] | 410 | g_string_append_c(buf, ' '); |
---|
[a5b5d00] | 411 | owl_string_append_quoted_arg(buf, owl_list_get_element(&(z->recips), recip_index)); |
---|
[719119de] | 412 | } |
---|
| 413 | |
---|
| 414 | return g_string_free(buf, false); |
---|
| 415 | } |
---|