[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, 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 */ |
---|
| 115 | owl_list_append_element(&(z->recips), strdup(myargv[0])); |
---|
| 116 | myargv++; |
---|
| 117 | myargc--; |
---|
| 118 | } |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | owl_parsefree(argv, argc); |
---|
| 122 | |
---|
| 123 | if (badargs) { |
---|
| 124 | return(-1); |
---|
| 125 | } |
---|
| 126 | |
---|
[ce7db4d] | 127 | /* now deal with defaults */ |
---|
| 128 | if (z->class==NULL) z->class=owl_strdup("message"); |
---|
| 129 | if (z->inst==NULL) z->inst=owl_strdup("personal"); |
---|
| 130 | if (z->realm==NULL) z->realm=owl_strdup(""); |
---|
| 131 | if (z->opcode==NULL) z->opcode=owl_strdup(""); |
---|
| 132 | /* z->message is allowed to stay NULL */ |
---|
| 133 | |
---|
[7d4fbcd] | 134 | if (!strcasecmp(z->class, "message") && |
---|
| 135 | !strcasecmp(z->inst, "personal") && |
---|
| 136 | owl_list_get_size(&(z->recips))==0) { |
---|
| 137 | owl_function_makemsg("You must specify a recipient"); |
---|
| 138 | return(-1); |
---|
| 139 | } |
---|
| 140 | |
---|
[ce7db4d] | 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); |
---|
| 145 | zsigzvar = ZGetVariable("zwrite-signature"); |
---|
| 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); |
---|
| 156 | strcat(openline, " 2> /dev/null"); |
---|
| 157 | file=popen(openline, "r"); |
---|
| 158 | owl_free(openline); |
---|
| 159 | if (!file) { |
---|
| 160 | if (zsigzvar && *zsigzvar) { |
---|
| 161 | z->zsig=owl_strdup(zsigzvar); |
---|
| 162 | } |
---|
| 163 | } else { |
---|
| 164 | z->zsig=owl_malloc(LINE*5); |
---|
| 165 | strcpy(z->zsig, ""); |
---|
| 166 | while (fgets(buff, LINE, file)) { /* wrong sizing */ |
---|
| 167 | strcat(z->zsig, buff); |
---|
| 168 | } |
---|
| 169 | pclose(file); |
---|
| 170 | if (z->zsig[strlen(z->zsig)-1]=='\n') { |
---|
| 171 | z->zsig[strlen(z->zsig)-1]='\0'; |
---|
| 172 | } |
---|
[56330ff] | 173 | } |
---|
[ce7db4d] | 174 | } else if (zsigzvar) { |
---|
| 175 | z->zsig=owl_strdup(zsigzvar); |
---|
| 176 | } else if (((pw=getpwuid(getuid()))!=NULL) && (pw->pw_gecos)) { |
---|
| 177 | z->zsig=strdup(pw->pw_gecos); |
---|
| 178 | ptr=strchr(z->zsig, ','); |
---|
| 179 | if (ptr) { |
---|
| 180 | ptr[0]='\0'; |
---|
[56330ff] | 181 | } |
---|
| 182 | } |
---|
| 183 | } |
---|
| 184 | |
---|
[7d4fbcd] | 185 | return(0); |
---|
| 186 | } |
---|
| 187 | |
---|
[ce7db4d] | 188 | void owl_zwrite_send_ping(owl_zwrite *z) |
---|
| 189 | { |
---|
[7d4fbcd] | 190 | int i, j; |
---|
| 191 | char to[LINE]; |
---|
| 192 | |
---|
| 193 | if (z->noping) return; |
---|
| 194 | |
---|
| 195 | if (strcasecmp(z->class, "message") || |
---|
| 196 | strcasecmp(z->inst, "personal")) { |
---|
| 197 | return; |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | /* if there are no recipients we won't send a ping, which |
---|
| 201 | is what we want */ |
---|
| 202 | j=owl_list_get_size(&(z->recips)); |
---|
| 203 | for (i=0; i<j; i++) { |
---|
| 204 | if (strcmp(z->realm, "")) { |
---|
| 205 | sprintf(to, "%s@%s", (char *) owl_list_get_element(&(z->recips), i), z->realm); |
---|
| 206 | } else { |
---|
| 207 | strcpy(to, owl_list_get_element(&(z->recips), i)); |
---|
| 208 | } |
---|
| 209 | send_ping(to); |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | } |
---|
| 213 | |
---|
[ce7db4d] | 214 | void owl_zwrite_set_message(owl_zwrite *z, char *msg) |
---|
| 215 | { |
---|
| 216 | if (z->message) owl_free(z->message); |
---|
| 217 | z->message=owl_strdup(msg); |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | char *owl_zwrite_get_message(owl_zwrite *z) |
---|
| 221 | { |
---|
| 222 | if (z->message) return(z->message); |
---|
| 223 | return(""); |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | int owl_zwrite_is_message_set(owl_zwrite *z) |
---|
| 227 | { |
---|
| 228 | if (z->message) return(1); |
---|
| 229 | return(0); |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | int owl_zwrite_send_message(owl_zwrite *z) |
---|
| 233 | { |
---|
[7d4fbcd] | 234 | int i, j; |
---|
| 235 | char to[LINE]; |
---|
| 236 | |
---|
[ce7db4d] | 237 | if (z->message==NULL) return(-1); |
---|
| 238 | |
---|
[7d4fbcd] | 239 | j=owl_list_get_size(&(z->recips)); |
---|
| 240 | if (j>0) { |
---|
| 241 | char *tmpmsg=NULL; |
---|
| 242 | char toline[LINE]; |
---|
| 243 | |
---|
| 244 | if (z->cc) { |
---|
| 245 | strcpy(toline, "CC: "); |
---|
| 246 | for (i=0; i<j; i++) { |
---|
| 247 | if (strcmp(z->realm, "")) { |
---|
| 248 | sprintf(toline, "%s%s@%s ", toline, (char *) owl_list_get_element(&(z->recips), i), z->realm); |
---|
| 249 | } else { |
---|
| 250 | sprintf(toline, "%s%s ", toline, (char *) owl_list_get_element(&(z->recips), i)); |
---|
| 251 | } |
---|
| 252 | } |
---|
[ce7db4d] | 253 | tmpmsg=owl_malloc(strlen(z->message)+strlen(toline)+30); |
---|
| 254 | sprintf(tmpmsg, "%s\n%s", toline, z->message); |
---|
[7d4fbcd] | 255 | } |
---|
| 256 | |
---|
| 257 | for (i=0; i<j; i++) { |
---|
| 258 | if (strcmp(z->realm, "")) { |
---|
| 259 | sprintf(to, "%s@%s", (char *) owl_list_get_element(&(z->recips), i), z->realm); |
---|
| 260 | } else { |
---|
| 261 | strcpy(to, owl_list_get_element(&(z->recips), i)); |
---|
| 262 | } |
---|
| 263 | if (z->cc) { |
---|
[56330ff] | 264 | send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, tmpmsg); |
---|
[7d4fbcd] | 265 | } else { |
---|
[ce7db4d] | 266 | send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, z->message); |
---|
[7d4fbcd] | 267 | } |
---|
| 268 | } |
---|
| 269 | if (z->cc) { |
---|
| 270 | owl_free(tmpmsg); |
---|
| 271 | } |
---|
| 272 | } else { |
---|
| 273 | sprintf(to, "@%s", z->realm); |
---|
[ce7db4d] | 274 | send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, z->message); |
---|
| 275 | } |
---|
| 276 | return(0); |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | int owl_zwrite_create_and_send_from_line(char *cmd, char *msg) |
---|
| 280 | { |
---|
| 281 | owl_zwrite z; |
---|
| 282 | int rv; |
---|
| 283 | rv=owl_zwrite_create_from_line(&z, cmd); |
---|
| 284 | if (rv) return(rv); |
---|
| 285 | if (!owl_zwrite_is_message_set(&z)) { |
---|
| 286 | owl_zwrite_set_message(&z, msg); |
---|
[7d4fbcd] | 287 | } |
---|
[ce7db4d] | 288 | owl_zwrite_send_message(&z); |
---|
| 289 | owl_zwrite_free(&z); |
---|
| 290 | return(0); |
---|
[7d4fbcd] | 291 | } |
---|
| 292 | |
---|
[ce7db4d] | 293 | char *owl_zwrite_get_class(owl_zwrite *z) |
---|
| 294 | { |
---|
[7d4fbcd] | 295 | return(z->class); |
---|
| 296 | } |
---|
| 297 | |
---|
[ce7db4d] | 298 | char *owl_zwrite_get_instance(owl_zwrite *z) |
---|
| 299 | { |
---|
[7d4fbcd] | 300 | return(z->inst); |
---|
| 301 | } |
---|
| 302 | |
---|
[ce7db4d] | 303 | char *owl_zwrite_get_opcode(owl_zwrite *z) |
---|
| 304 | { |
---|
[4b464a4] | 305 | return(z->opcode); |
---|
| 306 | } |
---|
| 307 | |
---|
[ce7db4d] | 308 | char *owl_zwrite_get_realm(owl_zwrite *z) |
---|
| 309 | { |
---|
[7d4fbcd] | 310 | return(z->realm); |
---|
| 311 | } |
---|
| 312 | |
---|
[ce7db4d] | 313 | char *owl_zwrite_get_zsig(owl_zwrite *z) |
---|
| 314 | { |
---|
| 315 | if (z->zsig) return(z->zsig); |
---|
| 316 | return(""); |
---|
[56330ff] | 317 | } |
---|
| 318 | |
---|
[ce7db4d] | 319 | void owl_zwrite_get_recipstr(owl_zwrite *z, char *buff) |
---|
| 320 | { |
---|
[7d4fbcd] | 321 | int i, j; |
---|
| 322 | |
---|
| 323 | strcpy(buff, ""); |
---|
| 324 | j=owl_list_get_size(&(z->recips)); |
---|
| 325 | for (i=0; i<j; i++) { |
---|
| 326 | strcat(buff, owl_list_get_element(&(z->recips), i)); |
---|
| 327 | strcat(buff, " "); |
---|
| 328 | } |
---|
| 329 | buff[strlen(buff)-1]='\0'; |
---|
| 330 | } |
---|
| 331 | |
---|
[ce7db4d] | 332 | int owl_zwrite_get_numrecips(owl_zwrite *z) |
---|
| 333 | { |
---|
[7d4fbcd] | 334 | return(owl_list_get_size(&(z->recips))); |
---|
| 335 | } |
---|
| 336 | |
---|
[ce7db4d] | 337 | char *owl_zwrite_get_recip_n(owl_zwrite *z, int n) |
---|
| 338 | { |
---|
[7d4fbcd] | 339 | return(owl_list_get_element(&(z->recips), n)); |
---|
| 340 | } |
---|
| 341 | |
---|
[ce7db4d] | 342 | int owl_zwrite_is_personal(owl_zwrite *z) |
---|
| 343 | { |
---|
[7d4fbcd] | 344 | /* return true if at least one of the recipients is personal */ |
---|
| 345 | int i, j; |
---|
| 346 | char *foo; |
---|
| 347 | |
---|
| 348 | j=owl_list_get_size(&(z->recips)); |
---|
| 349 | for (i=0; i<j; i++) { |
---|
| 350 | foo=owl_list_get_element(&(z->recips), i); |
---|
| 351 | if (foo[0]!='@') return(1); |
---|
| 352 | } |
---|
| 353 | return(0); |
---|
| 354 | } |
---|
| 355 | |
---|
[ce7db4d] | 356 | void owl_zwrite_free(owl_zwrite *z) |
---|
| 357 | { |
---|
[7d4fbcd] | 358 | owl_list_free_all(&(z->recips), &owl_free); |
---|
[ce7db4d] | 359 | if (z->class) owl_free(z->class); |
---|
| 360 | if (z->inst) owl_free(z->inst); |
---|
| 361 | if (z->opcode) owl_free(z->opcode); |
---|
| 362 | if (z->realm) owl_free(z->realm); |
---|
| 363 | if (z->message) owl_free(z->message); |
---|
| 364 | if (z->zsig) owl_free(z->zsig); |
---|
[7d4fbcd] | 365 | } |
---|