[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 | } |
---|
[ce7db4d] | 46 | z->class=owl_strdup(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 | } |
---|
[ce7db4d] | 54 | z->inst=owl_strdup(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 | } |
---|
[ce7db4d] | 62 | z->realm=owl_strdup(myargv[1]); |
---|
| 63 | myargv+=2; |
---|
| 64 | myargc-=2; |
---|
| 65 | } else if (!strcmp(myargv[0], "-s")) { |
---|
| 66 | if (myargc<2) { |
---|
| 67 | badargs=1; |
---|
| 68 | break; |
---|
| 69 | } |
---|
| 70 | z->zsig=owl_strdup(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 | } |
---|
[ce7db4d] | 78 | z->opcode=owl_strdup(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--; |
---|
| 95 | z->message=owl_strdup(""); |
---|
| 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 */ |
---|
[34509d5] | 115 | owl_list_append_element(&(z->recips), owl_strdup(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) { |
---|
| 148 | z->zsig=owl_strdup(zsigowlvar); |
---|
| 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) { |
---|
| 163 | z->zsig=owl_strdup(zsigzvar); |
---|
| 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) { |
---|
| 177 | z->zsig=owl_strdup(zsigzvar); |
---|
| 178 | } else if (((pw=getpwuid(getuid()))!=NULL) && (pw->pw_gecos)) { |
---|
[34509d5] | 179 | z->zsig=owl_strdup(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]; |
---|
| 220 | |
---|
[ce7db4d] | 221 | if (z->message) owl_free(z->message); |
---|
[db2dd3d] | 222 | |
---|
| 223 | j=owl_list_get_size(&(z->recips)); |
---|
| 224 | if (j>0 && z->cc) { |
---|
| 225 | strcpy(toline, "CC: "); |
---|
| 226 | for (i=0; i<j; i++) { |
---|
| 227 | if (strcmp(z->realm, "")) { |
---|
| 228 | sprintf(toline, "%s%s@%s ", toline, (char *) owl_list_get_element(&(z->recips), i), z->realm); |
---|
| 229 | } else { |
---|
| 230 | sprintf(toline, "%s%s ", toline, (char *) owl_list_get_element(&(z->recips), i)); |
---|
| 231 | } |
---|
| 232 | } |
---|
| 233 | z->message=owl_sprintf("%s\n%s", toline, msg); |
---|
| 234 | } else { |
---|
| 235 | z->message=owl_strdup(msg); |
---|
| 236 | } |
---|
[ce7db4d] | 237 | } |
---|
| 238 | |
---|
| 239 | char *owl_zwrite_get_message(owl_zwrite *z) |
---|
| 240 | { |
---|
| 241 | if (z->message) return(z->message); |
---|
| 242 | return(""); |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | int owl_zwrite_is_message_set(owl_zwrite *z) |
---|
| 246 | { |
---|
| 247 | if (z->message) return(1); |
---|
| 248 | return(0); |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | int owl_zwrite_send_message(owl_zwrite *z) |
---|
| 252 | { |
---|
[7d4fbcd] | 253 | int i, j; |
---|
| 254 | char to[LINE]; |
---|
| 255 | |
---|
[ce7db4d] | 256 | if (z->message==NULL) return(-1); |
---|
| 257 | |
---|
[7d4fbcd] | 258 | j=owl_list_get_size(&(z->recips)); |
---|
| 259 | if (j>0) { |
---|
| 260 | for (i=0; i<j; i++) { |
---|
| 261 | if (strcmp(z->realm, "")) { |
---|
| 262 | sprintf(to, "%s@%s", (char *) owl_list_get_element(&(z->recips), i), z->realm); |
---|
| 263 | } else { |
---|
| 264 | strcpy(to, owl_list_get_element(&(z->recips), i)); |
---|
| 265 | } |
---|
[db2dd3d] | 266 | send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, z->message); |
---|
[7d4fbcd] | 267 | } |
---|
| 268 | } else { |
---|
| 269 | sprintf(to, "@%s", z->realm); |
---|
[ce7db4d] | 270 | send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, z->message); |
---|
| 271 | } |
---|
| 272 | return(0); |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | int owl_zwrite_create_and_send_from_line(char *cmd, char *msg) |
---|
| 276 | { |
---|
| 277 | owl_zwrite z; |
---|
| 278 | int rv; |
---|
| 279 | rv=owl_zwrite_create_from_line(&z, cmd); |
---|
| 280 | if (rv) return(rv); |
---|
| 281 | if (!owl_zwrite_is_message_set(&z)) { |
---|
| 282 | owl_zwrite_set_message(&z, msg); |
---|
[7d4fbcd] | 283 | } |
---|
[ce7db4d] | 284 | owl_zwrite_send_message(&z); |
---|
| 285 | owl_zwrite_free(&z); |
---|
| 286 | return(0); |
---|
[7d4fbcd] | 287 | } |
---|
| 288 | |
---|
[ce7db4d] | 289 | char *owl_zwrite_get_class(owl_zwrite *z) |
---|
| 290 | { |
---|
[7d4fbcd] | 291 | return(z->class); |
---|
| 292 | } |
---|
| 293 | |
---|
[ce7db4d] | 294 | char *owl_zwrite_get_instance(owl_zwrite *z) |
---|
| 295 | { |
---|
[7d4fbcd] | 296 | return(z->inst); |
---|
| 297 | } |
---|
| 298 | |
---|
[ce7db4d] | 299 | char *owl_zwrite_get_opcode(owl_zwrite *z) |
---|
| 300 | { |
---|
[4b464a4] | 301 | return(z->opcode); |
---|
| 302 | } |
---|
| 303 | |
---|
[9ceee9d] | 304 | void owl_zwrite_set_opcode(owl_zwrite *z, char *opcode) |
---|
| 305 | { |
---|
| 306 | if (z->opcode) owl_free(z->opcode); |
---|
| 307 | z->opcode=owl_strdup(opcode); |
---|
| 308 | } |
---|
| 309 | |
---|
[ce7db4d] | 310 | char *owl_zwrite_get_realm(owl_zwrite *z) |
---|
| 311 | { |
---|
[7d4fbcd] | 312 | return(z->realm); |
---|
| 313 | } |
---|
| 314 | |
---|
[ce7db4d] | 315 | char *owl_zwrite_get_zsig(owl_zwrite *z) |
---|
| 316 | { |
---|
| 317 | if (z->zsig) return(z->zsig); |
---|
| 318 | return(""); |
---|
[56330ff] | 319 | } |
---|
| 320 | |
---|
[ce7db4d] | 321 | void owl_zwrite_get_recipstr(owl_zwrite *z, char *buff) |
---|
| 322 | { |
---|
[7d4fbcd] | 323 | int i, j; |
---|
| 324 | |
---|
| 325 | strcpy(buff, ""); |
---|
| 326 | j=owl_list_get_size(&(z->recips)); |
---|
| 327 | for (i=0; i<j; i++) { |
---|
| 328 | strcat(buff, owl_list_get_element(&(z->recips), i)); |
---|
| 329 | strcat(buff, " "); |
---|
| 330 | } |
---|
| 331 | buff[strlen(buff)-1]='\0'; |
---|
| 332 | } |
---|
| 333 | |
---|
[ce7db4d] | 334 | int owl_zwrite_get_numrecips(owl_zwrite *z) |
---|
| 335 | { |
---|
[7d4fbcd] | 336 | return(owl_list_get_size(&(z->recips))); |
---|
| 337 | } |
---|
| 338 | |
---|
[ce7db4d] | 339 | char *owl_zwrite_get_recip_n(owl_zwrite *z, int n) |
---|
| 340 | { |
---|
[7d4fbcd] | 341 | return(owl_list_get_element(&(z->recips), n)); |
---|
| 342 | } |
---|
| 343 | |
---|
[ce7db4d] | 344 | int owl_zwrite_is_personal(owl_zwrite *z) |
---|
| 345 | { |
---|
[7d4fbcd] | 346 | /* return true if at least one of the recipients is personal */ |
---|
| 347 | int i, j; |
---|
| 348 | char *foo; |
---|
| 349 | |
---|
| 350 | j=owl_list_get_size(&(z->recips)); |
---|
| 351 | for (i=0; i<j; i++) { |
---|
| 352 | foo=owl_list_get_element(&(z->recips), i); |
---|
| 353 | if (foo[0]!='@') return(1); |
---|
| 354 | } |
---|
| 355 | return(0); |
---|
| 356 | } |
---|
| 357 | |
---|
[ce7db4d] | 358 | void owl_zwrite_free(owl_zwrite *z) |
---|
| 359 | { |
---|
[7d4fbcd] | 360 | owl_list_free_all(&(z->recips), &owl_free); |
---|
[ce7db4d] | 361 | if (z->class) owl_free(z->class); |
---|
| 362 | if (z->inst) owl_free(z->inst); |
---|
| 363 | if (z->opcode) owl_free(z->opcode); |
---|
| 364 | if (z->realm) owl_free(z->realm); |
---|
| 365 | if (z->message) owl_free(z->message); |
---|
| 366 | if (z->zsig) owl_free(z->zsig); |
---|
[7d4fbcd] | 367 | } |
---|