[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) { |
---|
| 32 | owl_function_makemsg("Unbalanced quotes"); |
---|
| 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 | } |
---|
| 86 | /* we must already have users */ |
---|
| 87 | if (owl_list_get_size(&(z->recips))<1) { |
---|
| 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) { |
---|
| 97 | z->message=realloc(z->message, strlen(z->message)+strlen(myargv[0])+5); |
---|
| 98 | strcat(z->message, " "); |
---|
| 99 | strcat(z->message, myargv[0]); |
---|
| 100 | myargc--; |
---|
| 101 | myargv++; |
---|
| 102 | } |
---|
| 103 | break; |
---|
[7d4fbcd] | 104 | } else if (!strcmp(myargv[0], "-C")) { |
---|
| 105 | z->cc=1; |
---|
| 106 | myargv++; |
---|
| 107 | myargc--; |
---|
| 108 | } else if (!strcmp(myargv[0], "-n")) { |
---|
| 109 | z->noping=1; |
---|
| 110 | myargv++; |
---|
| 111 | myargc--; |
---|
| 112 | } else { |
---|
| 113 | /* anything unattached is a recipient */ |
---|
| 114 | owl_list_append_element(&(z->recips), strdup(myargv[0])); |
---|
| 115 | myargv++; |
---|
| 116 | myargc--; |
---|
| 117 | } |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | owl_parsefree(argv, argc); |
---|
| 121 | |
---|
| 122 | if (badargs) { |
---|
| 123 | return(-1); |
---|
| 124 | } |
---|
| 125 | |
---|
[ce7db4d] | 126 | /* now deal with defaults */ |
---|
| 127 | if (z->class==NULL) z->class=owl_strdup("message"); |
---|
| 128 | if (z->inst==NULL) z->inst=owl_strdup("personal"); |
---|
| 129 | if (z->realm==NULL) z->realm=owl_strdup(""); |
---|
| 130 | if (z->opcode==NULL) z->opcode=owl_strdup(""); |
---|
| 131 | /* z->message is allowed to stay NULL */ |
---|
| 132 | |
---|
[7d4fbcd] | 133 | if (!strcasecmp(z->class, "message") && |
---|
| 134 | !strcasecmp(z->inst, "personal") && |
---|
| 135 | owl_list_get_size(&(z->recips))==0) { |
---|
| 136 | owl_function_makemsg("You must specify a recipient"); |
---|
| 137 | return(-1); |
---|
| 138 | } |
---|
| 139 | |
---|
[ce7db4d] | 140 | /* get a zsig, if not given */ |
---|
| 141 | if (z->zsig==NULL) { |
---|
| 142 | zsigproc = owl_global_get_zsigproc(&g); |
---|
| 143 | zsigowlvar = owl_global_get_zsig(&g); |
---|
| 144 | zsigzvar = ZGetVariable("zwrite-signature"); |
---|
| 145 | |
---|
| 146 | if (zsigowlvar && *zsigowlvar) { |
---|
| 147 | z->zsig=owl_strdup(zsigowlvar); |
---|
| 148 | } else if (zsigproc && *zsigproc) { |
---|
| 149 | FILE *file; |
---|
| 150 | char buff[LINE], *openline; |
---|
| 151 | |
---|
| 152 | /* simple hack for now to nuke stderr */ |
---|
| 153 | openline=owl_malloc(strlen(zsigproc)+40); |
---|
| 154 | strcpy(openline, zsigproc); |
---|
| 155 | strcat(openline, " 2> /dev/null"); |
---|
| 156 | file=popen(openline, "r"); |
---|
| 157 | owl_free(openline); |
---|
| 158 | if (!file) { |
---|
| 159 | if (zsigzvar && *zsigzvar) { |
---|
| 160 | z->zsig=owl_strdup(zsigzvar); |
---|
| 161 | } |
---|
| 162 | } else { |
---|
| 163 | z->zsig=owl_malloc(LINE*5); |
---|
| 164 | strcpy(z->zsig, ""); |
---|
| 165 | while (fgets(buff, LINE, file)) { /* wrong sizing */ |
---|
| 166 | strcat(z->zsig, buff); |
---|
| 167 | } |
---|
| 168 | pclose(file); |
---|
| 169 | if (z->zsig[strlen(z->zsig)-1]=='\n') { |
---|
| 170 | z->zsig[strlen(z->zsig)-1]='\0'; |
---|
| 171 | } |
---|
[56330ff] | 172 | } |
---|
[ce7db4d] | 173 | } else if (zsigzvar) { |
---|
| 174 | z->zsig=owl_strdup(zsigzvar); |
---|
| 175 | } else if (((pw=getpwuid(getuid()))!=NULL) && (pw->pw_gecos)) { |
---|
| 176 | z->zsig=strdup(pw->pw_gecos); |
---|
| 177 | ptr=strchr(z->zsig, ','); |
---|
| 178 | if (ptr) { |
---|
| 179 | ptr[0]='\0'; |
---|
[56330ff] | 180 | } |
---|
| 181 | } |
---|
| 182 | } |
---|
| 183 | |
---|
[7d4fbcd] | 184 | return(0); |
---|
| 185 | } |
---|
| 186 | |
---|
[ce7db4d] | 187 | void owl_zwrite_send_ping(owl_zwrite *z) |
---|
| 188 | { |
---|
[7d4fbcd] | 189 | int i, j; |
---|
| 190 | char to[LINE]; |
---|
| 191 | |
---|
| 192 | if (z->noping) return; |
---|
| 193 | |
---|
| 194 | if (strcasecmp(z->class, "message") || |
---|
| 195 | strcasecmp(z->inst, "personal")) { |
---|
| 196 | return; |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | /* if there are no recipients we won't send a ping, which |
---|
| 200 | is what we want */ |
---|
| 201 | j=owl_list_get_size(&(z->recips)); |
---|
| 202 | for (i=0; i<j; i++) { |
---|
| 203 | if (strcmp(z->realm, "")) { |
---|
| 204 | sprintf(to, "%s@%s", (char *) owl_list_get_element(&(z->recips), i), z->realm); |
---|
| 205 | } else { |
---|
| 206 | strcpy(to, owl_list_get_element(&(z->recips), i)); |
---|
| 207 | } |
---|
| 208 | send_ping(to); |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | } |
---|
| 212 | |
---|
[ce7db4d] | 213 | void owl_zwrite_set_message(owl_zwrite *z, char *msg) |
---|
| 214 | { |
---|
| 215 | if (z->message) owl_free(z->message); |
---|
| 216 | z->message=owl_strdup(msg); |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | char *owl_zwrite_get_message(owl_zwrite *z) |
---|
| 220 | { |
---|
| 221 | if (z->message) return(z->message); |
---|
| 222 | return(""); |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | int owl_zwrite_is_message_set(owl_zwrite *z) |
---|
| 226 | { |
---|
| 227 | if (z->message) return(1); |
---|
| 228 | return(0); |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | int owl_zwrite_send_message(owl_zwrite *z) |
---|
| 232 | { |
---|
[7d4fbcd] | 233 | int i, j; |
---|
| 234 | char to[LINE]; |
---|
| 235 | |
---|
[ce7db4d] | 236 | if (z->message==NULL) return(-1); |
---|
| 237 | |
---|
[7d4fbcd] | 238 | j=owl_list_get_size(&(z->recips)); |
---|
| 239 | if (j>0) { |
---|
| 240 | char *tmpmsg=NULL; |
---|
| 241 | char toline[LINE]; |
---|
| 242 | |
---|
| 243 | if (z->cc) { |
---|
| 244 | strcpy(toline, "CC: "); |
---|
| 245 | for (i=0; i<j; i++) { |
---|
| 246 | if (strcmp(z->realm, "")) { |
---|
| 247 | sprintf(toline, "%s%s@%s ", toline, (char *) owl_list_get_element(&(z->recips), i), z->realm); |
---|
| 248 | } else { |
---|
| 249 | sprintf(toline, "%s%s ", toline, (char *) owl_list_get_element(&(z->recips), i)); |
---|
| 250 | } |
---|
| 251 | } |
---|
[ce7db4d] | 252 | tmpmsg=owl_malloc(strlen(z->message)+strlen(toline)+30); |
---|
| 253 | sprintf(tmpmsg, "%s\n%s", toline, z->message); |
---|
[7d4fbcd] | 254 | } |
---|
| 255 | |
---|
| 256 | for (i=0; i<j; i++) { |
---|
| 257 | if (strcmp(z->realm, "")) { |
---|
| 258 | sprintf(to, "%s@%s", (char *) owl_list_get_element(&(z->recips), i), z->realm); |
---|
| 259 | } else { |
---|
| 260 | strcpy(to, owl_list_get_element(&(z->recips), i)); |
---|
| 261 | } |
---|
| 262 | if (z->cc) { |
---|
[56330ff] | 263 | send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, tmpmsg); |
---|
[7d4fbcd] | 264 | } else { |
---|
[ce7db4d] | 265 | send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, z->message); |
---|
[7d4fbcd] | 266 | } |
---|
| 267 | } |
---|
| 268 | if (z->cc) { |
---|
| 269 | owl_free(tmpmsg); |
---|
| 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 | |
---|
[ce7db4d] | 307 | char *owl_zwrite_get_realm(owl_zwrite *z) |
---|
| 308 | { |
---|
[7d4fbcd] | 309 | return(z->realm); |
---|
| 310 | } |
---|
| 311 | |
---|
[ce7db4d] | 312 | char *owl_zwrite_get_zsig(owl_zwrite *z) |
---|
| 313 | { |
---|
| 314 | if (z->zsig) return(z->zsig); |
---|
| 315 | return(""); |
---|
[56330ff] | 316 | } |
---|
| 317 | |
---|
[ce7db4d] | 318 | void owl_zwrite_get_recipstr(owl_zwrite *z, char *buff) |
---|
| 319 | { |
---|
[7d4fbcd] | 320 | int i, j; |
---|
| 321 | |
---|
| 322 | strcpy(buff, ""); |
---|
| 323 | j=owl_list_get_size(&(z->recips)); |
---|
| 324 | for (i=0; i<j; i++) { |
---|
| 325 | strcat(buff, owl_list_get_element(&(z->recips), i)); |
---|
| 326 | strcat(buff, " "); |
---|
| 327 | } |
---|
| 328 | buff[strlen(buff)-1]='\0'; |
---|
| 329 | } |
---|
| 330 | |
---|
[ce7db4d] | 331 | int owl_zwrite_get_numrecips(owl_zwrite *z) |
---|
| 332 | { |
---|
[7d4fbcd] | 333 | return(owl_list_get_size(&(z->recips))); |
---|
| 334 | } |
---|
| 335 | |
---|
[ce7db4d] | 336 | char *owl_zwrite_get_recip_n(owl_zwrite *z, int n) |
---|
| 337 | { |
---|
[7d4fbcd] | 338 | return(owl_list_get_element(&(z->recips), n)); |
---|
| 339 | } |
---|
| 340 | |
---|
[ce7db4d] | 341 | int owl_zwrite_is_personal(owl_zwrite *z) |
---|
| 342 | { |
---|
[7d4fbcd] | 343 | /* return true if at least one of the recipients is personal */ |
---|
| 344 | int i, j; |
---|
| 345 | char *foo; |
---|
| 346 | |
---|
| 347 | j=owl_list_get_size(&(z->recips)); |
---|
| 348 | for (i=0; i<j; i++) { |
---|
| 349 | foo=owl_list_get_element(&(z->recips), i); |
---|
| 350 | if (foo[0]!='@') return(1); |
---|
| 351 | } |
---|
| 352 | return(0); |
---|
| 353 | } |
---|
| 354 | |
---|
[ce7db4d] | 355 | void owl_zwrite_free(owl_zwrite *z) |
---|
| 356 | { |
---|
[7d4fbcd] | 357 | owl_list_free_all(&(z->recips), &owl_free); |
---|
[ce7db4d] | 358 | if (z->class) owl_free(z->class); |
---|
| 359 | if (z->inst) owl_free(z->inst); |
---|
| 360 | if (z->opcode) owl_free(z->opcode); |
---|
| 361 | if (z->realm) owl_free(z->realm); |
---|
| 362 | if (z->message) owl_free(z->message); |
---|
| 363 | if (z->zsig) owl_free(z->zsig); |
---|
[7d4fbcd] | 364 | } |
---|