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