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