[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 | |
---|
[e19eb97] | 7 | int owl_zwrite_create_from_line(owl_zwrite *z, const char *line) |
---|
[ce7db4d] | 8 | { |
---|
[a52d13a] | 9 | int argc, badargs, myargc, i, len; |
---|
[65b2173] | 10 | char **argv; |
---|
[e19eb97] | 11 | const char *const *myargv; |
---|
[a52d13a] | 12 | char *msg = NULL; |
---|
[7d4fbcd] | 13 | |
---|
| 14 | badargs=0; |
---|
| 15 | |
---|
[69f89c7] | 16 | /* start with null entries */ |
---|
[ce7db4d] | 17 | z->realm=NULL; |
---|
| 18 | z->class=NULL; |
---|
| 19 | z->inst=NULL; |
---|
| 20 | z->opcode=NULL; |
---|
| 21 | z->zsig=NULL; |
---|
| 22 | z->message=NULL; |
---|
[7d4fbcd] | 23 | z->cc=0; |
---|
| 24 | z->noping=0; |
---|
| 25 | owl_list_create(&(z->recips)); |
---|
| 26 | |
---|
| 27 | /* parse the command line for options */ |
---|
[c529ac8] | 28 | argv=owl_parseline(line, &argc); |
---|
| 29 | myargv=strs(argv); |
---|
[7d4fbcd] | 30 | if (argc<0) { |
---|
[836ea3a3] | 31 | owl_function_error("Unbalanced quotes in zwrite"); |
---|
[7d4fbcd] | 32 | return(-1); |
---|
| 33 | } |
---|
| 34 | myargc=argc; |
---|
[e1c4636] | 35 | if (myargc && *(myargv[0])!='-') { |
---|
| 36 | myargc--; |
---|
| 37 | myargv++; |
---|
| 38 | } |
---|
[7d4fbcd] | 39 | while (myargc) { |
---|
| 40 | if (!strcmp(myargv[0], "-c")) { |
---|
| 41 | if (myargc<2) { |
---|
| 42 | badargs=1; |
---|
| 43 | break; |
---|
| 44 | } |
---|
[4b17a6c] | 45 | z->class=owl_validate_utf8(myargv[1]); |
---|
[7d4fbcd] | 46 | myargv+=2; |
---|
| 47 | myargc-=2; |
---|
| 48 | } else if (!strcmp(myargv[0], "-i")) { |
---|
| 49 | if (myargc<2) { |
---|
| 50 | badargs=1; |
---|
| 51 | break; |
---|
| 52 | } |
---|
[4b17a6c] | 53 | z->inst=owl_validate_utf8(myargv[1]); |
---|
[7d4fbcd] | 54 | myargv+=2; |
---|
| 55 | myargc-=2; |
---|
| 56 | } else if (!strcmp(myargv[0], "-r")) { |
---|
| 57 | if (myargc<2) { |
---|
| 58 | badargs=1; |
---|
| 59 | break; |
---|
| 60 | } |
---|
[4b17a6c] | 61 | z->realm=owl_validate_utf8(myargv[1]); |
---|
[ce7db4d] | 62 | myargv+=2; |
---|
| 63 | myargc-=2; |
---|
| 64 | } else if (!strcmp(myargv[0], "-s")) { |
---|
| 65 | if (myargc<2) { |
---|
| 66 | badargs=1; |
---|
| 67 | break; |
---|
| 68 | } |
---|
[4b17a6c] | 69 | z->zsig=owl_validate_utf8(myargv[1]); |
---|
[7d4fbcd] | 70 | myargv+=2; |
---|
| 71 | myargc-=2; |
---|
| 72 | } else if (!strcmp(myargv[0], "-O")) { |
---|
| 73 | if (myargc<2) { |
---|
| 74 | badargs=1; |
---|
| 75 | break; |
---|
| 76 | } |
---|
[4b17a6c] | 77 | z->opcode=owl_validate_utf8(myargv[1]); |
---|
[7d4fbcd] | 78 | myargv+=2; |
---|
| 79 | myargc-=2; |
---|
[ce7db4d] | 80 | } else if (!strcmp(myargv[0], "-m")) { |
---|
| 81 | if (myargc<2) { |
---|
| 82 | badargs=1; |
---|
| 83 | break; |
---|
| 84 | } |
---|
[69f89c7] | 85 | /* we must already have users or a class or an instance */ |
---|
[e016fc2] | 86 | if (owl_list_get_size(&(z->recips))<1 && (!z->class) && (!z->inst)) { |
---|
[ce7db4d] | 87 | badargs=1; |
---|
| 88 | break; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | /* Once we have -m, gobble up everything else on the line */ |
---|
| 92 | myargv++; |
---|
| 93 | myargc--; |
---|
[a52d13a] | 94 | len = 0; |
---|
| 95 | for (i=0;i<myargc;i++) { |
---|
| 96 | len += strlen(myargv[i-1]) + 1; |
---|
| 97 | } |
---|
| 98 | msg = owl_malloc(len); |
---|
| 99 | msg[0] = '\0'; |
---|
[ce7db4d] | 100 | while (myargc) { |
---|
[a52d13a] | 101 | strcat(msg, myargv[0]); |
---|
| 102 | strcat(msg, " "); |
---|
| 103 | myargc--; |
---|
| 104 | myargv++; |
---|
[ce7db4d] | 105 | } |
---|
[a52d13a] | 106 | msg[strlen(msg)-1] = '\0'; |
---|
[ce7db4d] | 107 | break; |
---|
[7d4fbcd] | 108 | } else if (!strcmp(myargv[0], "-C")) { |
---|
| 109 | z->cc=1; |
---|
| 110 | myargv++; |
---|
| 111 | myargc--; |
---|
| 112 | } else if (!strcmp(myargv[0], "-n")) { |
---|
| 113 | z->noping=1; |
---|
| 114 | myargv++; |
---|
| 115 | myargc--; |
---|
| 116 | } else { |
---|
| 117 | /* anything unattached is a recipient */ |
---|
[4b17a6c] | 118 | owl_list_append_element(&(z->recips), owl_validate_utf8(myargv[0])); |
---|
[7d4fbcd] | 119 | myargv++; |
---|
| 120 | myargc--; |
---|
| 121 | } |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | owl_parsefree(argv, argc); |
---|
| 125 | |
---|
| 126 | if (badargs) { |
---|
| 127 | return(-1); |
---|
| 128 | } |
---|
| 129 | |
---|
[1fe100c] | 130 | if (z->class == NULL && |
---|
| 131 | z->inst == NULL && |
---|
| 132 | owl_list_get_size(&(z->recips))==0) { |
---|
| 133 | owl_function_error("You must specify a recipient for zwrite"); |
---|
| 134 | return(-1); |
---|
| 135 | } |
---|
| 136 | |
---|
[ce7db4d] | 137 | /* now deal with defaults */ |
---|
| 138 | if (z->class==NULL) z->class=owl_strdup("message"); |
---|
| 139 | if (z->inst==NULL) z->inst=owl_strdup("personal"); |
---|
| 140 | if (z->realm==NULL) z->realm=owl_strdup(""); |
---|
| 141 | if (z->opcode==NULL) z->opcode=owl_strdup(""); |
---|
| 142 | /* z->message is allowed to stay NULL */ |
---|
[a52d13a] | 143 | |
---|
| 144 | if(msg) { |
---|
| 145 | owl_zwrite_set_message(z, msg); |
---|
| 146 | owl_free(msg); |
---|
| 147 | } |
---|
| 148 | |
---|
[3f3ee61] | 149 | return(0); |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | void owl_zwrite_populate_zsig(owl_zwrite *z) |
---|
| 153 | { |
---|
[e19eb97] | 154 | const char *zsigproc, *zsigowlvar, *zsigzvar; |
---|
[65b2173] | 155 | char *ptr; |
---|
[3f3ee61] | 156 | struct passwd *pw; |
---|
| 157 | |
---|
[ce7db4d] | 158 | /* get a zsig, if not given */ |
---|
| 159 | if (z->zsig==NULL) { |
---|
| 160 | zsigproc = owl_global_get_zsigproc(&g); |
---|
| 161 | zsigowlvar = owl_global_get_zsig(&g); |
---|
[09489b89] | 162 | zsigzvar = owl_zephyr_get_variable("zwrite-signature"); |
---|
[ce7db4d] | 163 | |
---|
| 164 | if (zsigowlvar && *zsigowlvar) { |
---|
[4b17a6c] | 165 | z->zsig=owl_validate_utf8(zsigowlvar); |
---|
[ce7db4d] | 166 | } else if (zsigproc && *zsigproc) { |
---|
| 167 | FILE *file; |
---|
| 168 | char buff[LINE], *openline; |
---|
| 169 | |
---|
| 170 | /* simple hack for now to nuke stderr */ |
---|
| 171 | openline=owl_malloc(strlen(zsigproc)+40); |
---|
| 172 | strcpy(openline, zsigproc); |
---|
[2a2bb60] | 173 | #if !(OWL_STDERR_REDIR) |
---|
[ce7db4d] | 174 | strcat(openline, " 2> /dev/null"); |
---|
[2a2bb60] | 175 | #endif |
---|
[ce7db4d] | 176 | file=popen(openline, "r"); |
---|
| 177 | owl_free(openline); |
---|
| 178 | if (!file) { |
---|
| 179 | if (zsigzvar && *zsigzvar) { |
---|
[4b17a6c] | 180 | z->zsig=owl_validate_utf8(zsigzvar); |
---|
[ce7db4d] | 181 | } |
---|
| 182 | } else { |
---|
| 183 | z->zsig=owl_malloc(LINE*5); |
---|
| 184 | strcpy(z->zsig, ""); |
---|
| 185 | while (fgets(buff, LINE, file)) { /* wrong sizing */ |
---|
| 186 | strcat(z->zsig, buff); |
---|
| 187 | } |
---|
| 188 | pclose(file); |
---|
| 189 | if (z->zsig[strlen(z->zsig)-1]=='\n') { |
---|
| 190 | z->zsig[strlen(z->zsig)-1]='\0'; |
---|
| 191 | } |
---|
[56330ff] | 192 | } |
---|
[ce7db4d] | 193 | } else if (zsigzvar) { |
---|
[4b17a6c] | 194 | z->zsig=owl_validate_utf8(zsigzvar); |
---|
[ce7db4d] | 195 | } else if (((pw=getpwuid(getuid()))!=NULL) && (pw->pw_gecos)) { |
---|
[4b17a6c] | 196 | z->zsig=owl_validate_utf8(pw->pw_gecos); |
---|
[ce7db4d] | 197 | ptr=strchr(z->zsig, ','); |
---|
| 198 | if (ptr) { |
---|
| 199 | ptr[0]='\0'; |
---|
[56330ff] | 200 | } |
---|
| 201 | } |
---|
| 202 | } |
---|
[7d4fbcd] | 203 | } |
---|
| 204 | |
---|
[a352029b] | 205 | void owl_zwrite_send_ping(const owl_zwrite *z) |
---|
[ce7db4d] | 206 | { |
---|
[7d4fbcd] | 207 | int i, j; |
---|
[44a61ac] | 208 | char *to; |
---|
[7d4fbcd] | 209 | |
---|
| 210 | if (z->noping) return; |
---|
| 211 | |
---|
[3ef779b] | 212 | if (strcasecmp(z->class, "message")) { |
---|
[7d4fbcd] | 213 | return; |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | /* if there are no recipients we won't send a ping, which |
---|
| 217 | is what we want */ |
---|
| 218 | j=owl_list_get_size(&(z->recips)); |
---|
| 219 | for (i=0; i<j; i++) { |
---|
| 220 | if (strcmp(z->realm, "")) { |
---|
[e19eb97] | 221 | to = owl_sprintf("%s@%s", (const char *) owl_list_get_element(&(z->recips), i), z->realm); |
---|
[7d4fbcd] | 222 | } else { |
---|
[44a61ac] | 223 | to = owl_strdup(owl_list_get_element(&(z->recips), i)); |
---|
[7d4fbcd] | 224 | } |
---|
[3ef779b] | 225 | send_ping(to, z->class, z->inst); |
---|
[44a61ac] | 226 | owl_free(to); |
---|
[7d4fbcd] | 227 | } |
---|
| 228 | |
---|
| 229 | } |
---|
| 230 | |
---|
[e19eb97] | 231 | void owl_zwrite_set_message(owl_zwrite *z, const char *msg) |
---|
[ce7db4d] | 232 | { |
---|
[db2dd3d] | 233 | int i, j; |
---|
[3538bc8] | 234 | char *toline = NULL; |
---|
[a52d13a] | 235 | char *tmp = NULL, *tmp2; |
---|
[db2dd3d] | 236 | |
---|
[ce7db4d] | 237 | if (z->message) owl_free(z->message); |
---|
[db2dd3d] | 238 | |
---|
| 239 | j=owl_list_get_size(&(z->recips)); |
---|
| 240 | if (j>0 && z->cc) { |
---|
[3538bc8] | 241 | toline = owl_strdup( "CC: "); |
---|
[db2dd3d] | 242 | for (i=0; i<j; i++) { |
---|
[3538bc8] | 243 | tmp = toline; |
---|
[db2dd3d] | 244 | if (strcmp(z->realm, "")) { |
---|
[e19eb97] | 245 | toline = owl_sprintf( "%s%s@%s ", toline, (const char *) owl_list_get_element(&(z->recips), i), z->realm); |
---|
[db2dd3d] | 246 | } else { |
---|
[e19eb97] | 247 | toline = owl_sprintf( "%s%s ", toline, (const char *) owl_list_get_element(&(z->recips), i)); |
---|
[db2dd3d] | 248 | } |
---|
[3538bc8] | 249 | owl_free(tmp); |
---|
| 250 | tmp = NULL; |
---|
[db2dd3d] | 251 | } |
---|
[4b17a6c] | 252 | tmp = owl_validate_utf8(msg); |
---|
[a52d13a] | 253 | tmp2 = owl_text_expand_tabs(tmp); |
---|
| 254 | z->message=owl_sprintf("%s\n%s", toline, tmp2); |
---|
[3538bc8] | 255 | owl_free(toline); |
---|
[a52d13a] | 256 | owl_free(tmp); |
---|
| 257 | owl_free(tmp2); |
---|
[db2dd3d] | 258 | } else { |
---|
[a52d13a] | 259 | tmp=owl_validate_utf8(msg); |
---|
| 260 | z->message=owl_text_expand_tabs(tmp); |
---|
| 261 | owl_free(tmp); |
---|
[db2dd3d] | 262 | } |
---|
[ce7db4d] | 263 | } |
---|
| 264 | |
---|
[a352029b] | 265 | const char *owl_zwrite_get_message(const owl_zwrite *z) |
---|
[ce7db4d] | 266 | { |
---|
| 267 | if (z->message) return(z->message); |
---|
| 268 | return(""); |
---|
| 269 | } |
---|
| 270 | |
---|
[a352029b] | 271 | int owl_zwrite_is_message_set(const owl_zwrite *z) |
---|
[ce7db4d] | 272 | { |
---|
| 273 | if (z->message) return(1); |
---|
| 274 | return(0); |
---|
| 275 | } |
---|
| 276 | |
---|
[a352029b] | 277 | int owl_zwrite_send_message(const owl_zwrite *z) |
---|
[ce7db4d] | 278 | { |
---|
[7d4fbcd] | 279 | int i, j; |
---|
[823671c] | 280 | char *to = NULL; |
---|
[7d4fbcd] | 281 | |
---|
[ce7db4d] | 282 | if (z->message==NULL) return(-1); |
---|
| 283 | |
---|
[7d4fbcd] | 284 | j=owl_list_get_size(&(z->recips)); |
---|
| 285 | if (j>0) { |
---|
| 286 | for (i=0; i<j; i++) { |
---|
| 287 | if (strcmp(z->realm, "")) { |
---|
[e19eb97] | 288 | to = owl_sprintf("%s@%s", (const char *) owl_list_get_element(&(z->recips), i), z->realm); |
---|
[7d4fbcd] | 289 | } else { |
---|
[823671c] | 290 | to = owl_strdup( owl_list_get_element(&(z->recips), i)); |
---|
[7d4fbcd] | 291 | } |
---|
[db2dd3d] | 292 | send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, z->message); |
---|
[823671c] | 293 | owl_free(to); |
---|
| 294 | to = NULL; |
---|
[7d4fbcd] | 295 | } |
---|
| 296 | } else { |
---|
[823671c] | 297 | to = owl_sprintf( "@%s", z->realm); |
---|
[ce7db4d] | 298 | send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, z->message); |
---|
| 299 | } |
---|
[823671c] | 300 | owl_free(to); |
---|
[ce7db4d] | 301 | return(0); |
---|
| 302 | } |
---|
| 303 | |
---|
[e19eb97] | 304 | int owl_zwrite_create_and_send_from_line(const char *cmd, const char *msg) |
---|
[ce7db4d] | 305 | { |
---|
| 306 | owl_zwrite z; |
---|
| 307 | int rv; |
---|
| 308 | rv=owl_zwrite_create_from_line(&z, cmd); |
---|
| 309 | if (rv) return(rv); |
---|
| 310 | if (!owl_zwrite_is_message_set(&z)) { |
---|
| 311 | owl_zwrite_set_message(&z, msg); |
---|
[7d4fbcd] | 312 | } |
---|
[3f3ee61] | 313 | owl_zwrite_populate_zsig(&z); |
---|
[ce7db4d] | 314 | owl_zwrite_send_message(&z); |
---|
| 315 | owl_zwrite_free(&z); |
---|
| 316 | return(0); |
---|
[7d4fbcd] | 317 | } |
---|
| 318 | |
---|
[a352029b] | 319 | const char *owl_zwrite_get_class(const owl_zwrite *z) |
---|
[ce7db4d] | 320 | { |
---|
[7d4fbcd] | 321 | return(z->class); |
---|
| 322 | } |
---|
| 323 | |
---|
[a352029b] | 324 | const char *owl_zwrite_get_instance(const owl_zwrite *z) |
---|
[ce7db4d] | 325 | { |
---|
[7d4fbcd] | 326 | return(z->inst); |
---|
| 327 | } |
---|
| 328 | |
---|
[a352029b] | 329 | const char *owl_zwrite_get_opcode(const owl_zwrite *z) |
---|
[ce7db4d] | 330 | { |
---|
[4b464a4] | 331 | return(z->opcode); |
---|
| 332 | } |
---|
| 333 | |
---|
[e19eb97] | 334 | void owl_zwrite_set_opcode(owl_zwrite *z, const char *opcode) |
---|
[9ceee9d] | 335 | { |
---|
| 336 | if (z->opcode) owl_free(z->opcode); |
---|
[4b17a6c] | 337 | z->opcode=owl_validate_utf8(opcode); |
---|
[9ceee9d] | 338 | } |
---|
| 339 | |
---|
[a352029b] | 340 | const char *owl_zwrite_get_realm(const owl_zwrite *z) |
---|
[ce7db4d] | 341 | { |
---|
[7d4fbcd] | 342 | return(z->realm); |
---|
| 343 | } |
---|
| 344 | |
---|
[a352029b] | 345 | const char *owl_zwrite_get_zsig(const owl_zwrite *z) |
---|
[ce7db4d] | 346 | { |
---|
| 347 | if (z->zsig) return(z->zsig); |
---|
| 348 | return(""); |
---|
[56330ff] | 349 | } |
---|
| 350 | |
---|
[a352029b] | 351 | void owl_zwrite_get_recipstr(const owl_zwrite *z, char *buff) |
---|
[ce7db4d] | 352 | { |
---|
[7d4fbcd] | 353 | int i, j; |
---|
| 354 | |
---|
| 355 | strcpy(buff, ""); |
---|
| 356 | j=owl_list_get_size(&(z->recips)); |
---|
| 357 | for (i=0; i<j; i++) { |
---|
| 358 | strcat(buff, owl_list_get_element(&(z->recips), i)); |
---|
| 359 | strcat(buff, " "); |
---|
| 360 | } |
---|
| 361 | buff[strlen(buff)-1]='\0'; |
---|
| 362 | } |
---|
| 363 | |
---|
[a352029b] | 364 | int owl_zwrite_get_numrecips(const owl_zwrite *z) |
---|
[ce7db4d] | 365 | { |
---|
[7d4fbcd] | 366 | return(owl_list_get_size(&(z->recips))); |
---|
| 367 | } |
---|
| 368 | |
---|
[a352029b] | 369 | const char *owl_zwrite_get_recip_n(const owl_zwrite *z, int n) |
---|
[ce7db4d] | 370 | { |
---|
[7d4fbcd] | 371 | return(owl_list_get_element(&(z->recips), n)); |
---|
| 372 | } |
---|
| 373 | |
---|
[a352029b] | 374 | int owl_zwrite_is_personal(const owl_zwrite *z) |
---|
[ce7db4d] | 375 | { |
---|
[7d4fbcd] | 376 | /* return true if at least one of the recipients is personal */ |
---|
| 377 | int i, j; |
---|
| 378 | char *foo; |
---|
| 379 | |
---|
| 380 | j=owl_list_get_size(&(z->recips)); |
---|
| 381 | for (i=0; i<j; i++) { |
---|
| 382 | foo=owl_list_get_element(&(z->recips), i); |
---|
| 383 | if (foo[0]!='@') return(1); |
---|
| 384 | } |
---|
| 385 | return(0); |
---|
| 386 | } |
---|
[3f3ee61] | 387 | |
---|
[ce7db4d] | 388 | void owl_zwrite_free(owl_zwrite *z) |
---|
| 389 | { |
---|
[7d4fbcd] | 390 | owl_list_free_all(&(z->recips), &owl_free); |
---|
[ce7db4d] | 391 | if (z->class) owl_free(z->class); |
---|
| 392 | if (z->inst) owl_free(z->inst); |
---|
| 393 | if (z->opcode) owl_free(z->opcode); |
---|
| 394 | if (z->realm) owl_free(z->realm); |
---|
| 395 | if (z->message) owl_free(z->message); |
---|
| 396 | if (z->zsig) owl_free(z->zsig); |
---|
[7d4fbcd] | 397 | } |
---|