| 1 | #include <stdlib.h> |
|---|
| 2 | #include <unistd.h> |
|---|
| 3 | #include <sys/types.h> |
|---|
| 4 | #include <sys/wait.h> |
|---|
| 5 | #include <sys/stat.h> |
|---|
| 6 | #include <string.h> |
|---|
| 7 | #include "owl.h" |
|---|
| 8 | |
|---|
| 9 | static const char fileIdent[] = "$Id$"; |
|---|
| 10 | |
|---|
| 11 | #ifdef HAVE_LIBZEPHYR |
|---|
| 12 | Code_t ZResetAuthentication(); |
|---|
| 13 | #endif |
|---|
| 14 | |
|---|
| 15 | int owl_zephyr_initialize() |
|---|
| 16 | { |
|---|
| 17 | #ifdef HAVE_LIBZEPHYR |
|---|
| 18 | int ret; |
|---|
| 19 | |
|---|
| 20 | if ((ret = ZInitialize()) != ZERR_NONE) { |
|---|
| 21 | com_err("owl",ret,"while initializing"); |
|---|
| 22 | return(1); |
|---|
| 23 | } |
|---|
| 24 | if ((ret = ZOpenPort(NULL)) != ZERR_NONE) { |
|---|
| 25 | com_err("owl",ret,"while opening port"); |
|---|
| 26 | return(1); |
|---|
| 27 | } |
|---|
| 28 | #endif |
|---|
| 29 | return(0); |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | int owl_zephyr_shutdown() |
|---|
| 34 | { |
|---|
| 35 | #ifdef HAVE_LIBZEPHYR |
|---|
| 36 | unsuball(); |
|---|
| 37 | ZClosePort(); |
|---|
| 38 | #endif |
|---|
| 39 | return(0); |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | int owl_zephyr_zpending() |
|---|
| 43 | { |
|---|
| 44 | #ifdef HAVE_LIBZEPHYR |
|---|
| 45 | return(ZPending()); |
|---|
| 46 | #else |
|---|
| 47 | return(0); |
|---|
| 48 | #endif |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | char *owl_zephyr_get_realm() |
|---|
| 52 | { |
|---|
| 53 | #ifdef HAVE_LIBZEPHYR |
|---|
| 54 | return(ZGetRealm()); |
|---|
| 55 | #else |
|---|
| 56 | return(""); |
|---|
| 57 | #endif |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | char *owl_zephyr_get_sender() |
|---|
| 61 | { |
|---|
| 62 | #ifdef HAVE_LIBZEPHYR |
|---|
| 63 | return(ZGetSender()); |
|---|
| 64 | #else |
|---|
| 65 | return(""); |
|---|
| 66 | #endif |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | /* return 0 on success |
|---|
| 70 | * -1 on file error |
|---|
| 71 | * -2 on subscription error |
|---|
| 72 | */ |
|---|
| 73 | int owl_zephyr_loadsubs(char *filename) |
|---|
| 74 | { |
|---|
| 75 | #ifdef HAVE_LIBZEPHYR |
|---|
| 76 | FILE *file; |
|---|
| 77 | char *tmp, *start; |
|---|
| 78 | char buffer[1024], subsfile[1024]; |
|---|
| 79 | ZSubscription_t subs[3001]; |
|---|
| 80 | int count, ret, i; |
|---|
| 81 | struct stat statbuff; |
|---|
| 82 | |
|---|
| 83 | if (filename==NULL) { |
|---|
| 84 | sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".zephyr.subs"); |
|---|
| 85 | } else { |
|---|
| 86 | strcpy(subsfile, filename); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | ret=stat(subsfile, &statbuff); |
|---|
| 90 | if (ret) return(0); |
|---|
| 91 | |
|---|
| 92 | ret=0; |
|---|
| 93 | |
|---|
| 94 | ZResetAuthentication(); |
|---|
| 95 | /* need to redo this to do chunks, not just bail after 3000 */ |
|---|
| 96 | count=0; |
|---|
| 97 | file=fopen(subsfile, "r"); |
|---|
| 98 | if (file) { |
|---|
| 99 | while ( fgets(buffer, 1024, file)!=NULL ) { |
|---|
| 100 | if (buffer[0]=='#' || buffer[0]=='\n' || buffer[0]=='\n') continue; |
|---|
| 101 | |
|---|
| 102 | if (buffer[0]=='-') { |
|---|
| 103 | start=buffer+1; |
|---|
| 104 | } else { |
|---|
| 105 | start=buffer; |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | if (count >= 3000) break; /* also tell the user */ |
|---|
| 109 | |
|---|
| 110 | /* add it to the list of subs */ |
|---|
| 111 | if ((tmp=(char *) strtok(start, ",\n\r"))==NULL) continue; |
|---|
| 112 | subs[count].zsub_class=owl_strdup(tmp); |
|---|
| 113 | if ((tmp=(char *) strtok(NULL, ",\n\r"))==NULL) continue; |
|---|
| 114 | subs[count].zsub_classinst=owl_strdup(tmp); |
|---|
| 115 | if ((tmp=(char *) strtok(NULL, " \t\n\r"))==NULL) continue; |
|---|
| 116 | subs[count].zsub_recipient=owl_strdup(tmp); |
|---|
| 117 | |
|---|
| 118 | /* if it started with '-' then add it to the global punt list */ |
|---|
| 119 | if (buffer[0]=='-') { |
|---|
| 120 | owl_function_zpunt(subs[count].zsub_class, subs[count].zsub_classinst, subs[count].zsub_recipient, 0); |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | count++; |
|---|
| 124 | } |
|---|
| 125 | fclose(file); |
|---|
| 126 | } else { |
|---|
| 127 | count=0; |
|---|
| 128 | ret=-1; |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | /* sub without defaults */ |
|---|
| 132 | if (ZSubscribeToSansDefaults(subs,count,0) != ZERR_NONE) { |
|---|
| 133 | owl_function_error("Error subscribing to zephyr notifications."); |
|---|
| 134 | ret=-2; |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | /* free stuff */ |
|---|
| 138 | for (i=0; i<count; i++) { |
|---|
| 139 | owl_free(subs[i].zsub_class); |
|---|
| 140 | owl_free(subs[i].zsub_classinst); |
|---|
| 141 | owl_free(subs[i].zsub_recipient); |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | return(ret); |
|---|
| 145 | #else |
|---|
| 146 | return(0); |
|---|
| 147 | #endif |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | int owl_zephyr_loaddefaultsubs() |
|---|
| 151 | { |
|---|
| 152 | ZSubscription_t subs[10]; |
|---|
| 153 | |
|---|
| 154 | if (ZSubscribeTo(subs,0,0) != ZERR_NONE) { |
|---|
| 155 | owl_function_error("Error subscribing to default zephyr notifications."); |
|---|
| 156 | return(-1); |
|---|
| 157 | } |
|---|
| 158 | return(0); |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | int owl_zephyr_loadloginsubs(char *filename) |
|---|
| 162 | { |
|---|
| 163 | #ifdef HAVE_LIBZEPHYR |
|---|
| 164 | FILE *file; |
|---|
| 165 | ZSubscription_t subs[3001]; |
|---|
| 166 | char subsfile[1024], buffer[1024]; |
|---|
| 167 | int count, ret, i; |
|---|
| 168 | struct stat statbuff; |
|---|
| 169 | |
|---|
| 170 | if (filename==NULL) { |
|---|
| 171 | sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".anyone"); |
|---|
| 172 | } else { |
|---|
| 173 | strcpy(subsfile, filename); |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | ret=stat(subsfile, &statbuff); |
|---|
| 177 | if (ret) return(0); |
|---|
| 178 | |
|---|
| 179 | ret=0; |
|---|
| 180 | |
|---|
| 181 | ZResetAuthentication(); |
|---|
| 182 | /* need to redo this to do chunks, not just bag out after 3000 */ |
|---|
| 183 | count=0; |
|---|
| 184 | file=fopen(subsfile, "r"); |
|---|
| 185 | if (file) { |
|---|
| 186 | while ( fgets(buffer, 1024, file)!=NULL ) { |
|---|
| 187 | if (buffer[0]=='#' || buffer[0]=='\n' || buffer[0]=='\n') continue; |
|---|
| 188 | |
|---|
| 189 | if (count >= 3000) break; /* also tell the user */ |
|---|
| 190 | |
|---|
| 191 | buffer[strlen(buffer)-1]='\0'; |
|---|
| 192 | subs[count].zsub_class="login"; |
|---|
| 193 | subs[count].zsub_recipient="*"; |
|---|
| 194 | if (strchr(buffer, '@')) { |
|---|
| 195 | subs[count].zsub_classinst=owl_strdup(buffer); |
|---|
| 196 | } else { |
|---|
| 197 | subs[count].zsub_classinst=owl_sprintf("%s@%s", buffer, ZGetRealm()); |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | count++; |
|---|
| 201 | } |
|---|
| 202 | fclose(file); |
|---|
| 203 | } else { |
|---|
| 204 | count=0; |
|---|
| 205 | ret=-1; |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | /* sub with defaults */ |
|---|
| 209 | if (ZSubscribeToSansDefaults(subs,count,0) != ZERR_NONE) { |
|---|
| 210 | owl_function_error("Error subscribing to zephyr notifications."); |
|---|
| 211 | ret=-2; |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | /* free stuff */ |
|---|
| 215 | for (i=0; i<count; i++) { |
|---|
| 216 | owl_free(subs[i].zsub_classinst); |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | return(ret); |
|---|
| 220 | #else |
|---|
| 221 | return(0); |
|---|
| 222 | #endif |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | void unsuball() |
|---|
| 226 | { |
|---|
| 227 | #if HAVE_LIBZEPHYR |
|---|
| 228 | int ret; |
|---|
| 229 | |
|---|
| 230 | ZResetAuthentication(); |
|---|
| 231 | ret=ZCancelSubscriptions(0); |
|---|
| 232 | if (ret != ZERR_NONE) { |
|---|
| 233 | com_err("owl",ret,"while unsubscribing"); |
|---|
| 234 | } |
|---|
| 235 | #endif |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | int owl_zephyr_sub(char *class, char *inst, char *recip) |
|---|
| 239 | { |
|---|
| 240 | #ifdef HAVE_LIBZEPHYR |
|---|
| 241 | ZSubscription_t subs[5]; |
|---|
| 242 | |
|---|
| 243 | subs[0].zsub_class=class; |
|---|
| 244 | subs[0].zsub_classinst=inst; |
|---|
| 245 | subs[0].zsub_recipient=recip; |
|---|
| 246 | |
|---|
| 247 | ZResetAuthentication(); |
|---|
| 248 | if (ZSubscribeTo(subs,1,0) != ZERR_NONE) { |
|---|
| 249 | owl_function_error("Error subbing to <%s,%s,%s>", class, inst, recip); |
|---|
| 250 | return(-2); |
|---|
| 251 | } |
|---|
| 252 | return(0); |
|---|
| 253 | #else |
|---|
| 254 | return(0); |
|---|
| 255 | #endif |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | |
|---|
| 259 | int owl_zephyr_unsub(char *class, char *inst, char *recip) |
|---|
| 260 | { |
|---|
| 261 | #ifdef HAVE_LIBZEPHYR |
|---|
| 262 | ZSubscription_t subs[5]; |
|---|
| 263 | |
|---|
| 264 | subs[0].zsub_class=class; |
|---|
| 265 | subs[0].zsub_classinst=inst; |
|---|
| 266 | subs[0].zsub_recipient=recip; |
|---|
| 267 | |
|---|
| 268 | ZResetAuthentication(); |
|---|
| 269 | if (ZUnsubscribeTo(subs,1,0) != ZERR_NONE) { |
|---|
| 270 | owl_function_error("Error unsubbing from <%s,%s,%s>", class, inst, recip); |
|---|
| 271 | return(-2); |
|---|
| 272 | } |
|---|
| 273 | return(0); |
|---|
| 274 | #else |
|---|
| 275 | return(0); |
|---|
| 276 | #endif |
|---|
| 277 | } |
|---|
| 278 | |
|---|
| 279 | /* return a pointer to the data in the Jth field, (NULL terminated by |
|---|
| 280 | * definition). Caller must free the return. |
|---|
| 281 | */ |
|---|
| 282 | #ifdef HAVE_LIBZEPHYR |
|---|
| 283 | char *owl_zephyr_get_field(ZNotice_t *n, int j) |
|---|
| 284 | { |
|---|
| 285 | int i, count, save; |
|---|
| 286 | char *out; |
|---|
| 287 | |
|---|
| 288 | count=save=0; |
|---|
| 289 | for (i=0; i<n->z_message_len; i++) { |
|---|
| 290 | if (n->z_message[i]=='\0') { |
|---|
| 291 | count++; |
|---|
| 292 | if (count==j) { |
|---|
| 293 | /* just found the end of the field we're looking for */ |
|---|
| 294 | return(owl_strdup(n->z_message+save)); |
|---|
| 295 | } else { |
|---|
| 296 | save=i+1; |
|---|
| 297 | } |
|---|
| 298 | } |
|---|
| 299 | } |
|---|
| 300 | /* catch the last field, which might not be null terminated */ |
|---|
| 301 | if (count==j-1) { |
|---|
| 302 | out=owl_malloc(n->z_message_len-save+5); |
|---|
| 303 | memcpy(out, n->z_message+save, n->z_message_len-save); |
|---|
| 304 | out[n->z_message_len-save]='\0'; |
|---|
| 305 | return(out); |
|---|
| 306 | } |
|---|
| 307 | |
|---|
| 308 | return(owl_strdup("")); |
|---|
| 309 | } |
|---|
| 310 | #else |
|---|
| 311 | char *owl_zephyr_get_field(void *n, int j) |
|---|
| 312 | { |
|---|
| 313 | return(owl_strdup("")); |
|---|
| 314 | } |
|---|
| 315 | #endif |
|---|
| 316 | |
|---|
| 317 | |
|---|
| 318 | #ifdef HAVE_LIBZEPHYR |
|---|
| 319 | int owl_zephyr_get_num_fields(ZNotice_t *n) |
|---|
| 320 | { |
|---|
| 321 | int i, fields; |
|---|
| 322 | |
|---|
| 323 | fields=1; |
|---|
| 324 | for (i=0; i<n->z_message_len; i++) { |
|---|
| 325 | if (n->z_message[i]=='\0') fields++; |
|---|
| 326 | } |
|---|
| 327 | |
|---|
| 328 | return(fields); |
|---|
| 329 | } |
|---|
| 330 | #else |
|---|
| 331 | int owl_zephyr_get_num_fields(void *n) |
|---|
| 332 | { |
|---|
| 333 | return(0); |
|---|
| 334 | } |
|---|
| 335 | #endif |
|---|
| 336 | |
|---|
| 337 | #ifdef HAVE_LIBZEPHYR |
|---|
| 338 | /* return a pointer to the message, place the message length in k |
|---|
| 339 | * caller must free the return |
|---|
| 340 | */ |
|---|
| 341 | char *owl_zephyr_get_message(ZNotice_t *n) |
|---|
| 342 | { |
|---|
| 343 | if (!strcasecmp(n->z_opcode, "ping")) { |
|---|
| 344 | return(owl_strdup("")); |
|---|
| 345 | } |
|---|
| 346 | |
|---|
| 347 | return(owl_zephyr_get_field(n, 2)); |
|---|
| 348 | } |
|---|
| 349 | #endif |
|---|
| 350 | |
|---|
| 351 | #ifdef HAVE_LIBZEPHYR |
|---|
| 352 | char *owl_zephyr_get_zsig(ZNotice_t *n, int *k) |
|---|
| 353 | { |
|---|
| 354 | /* return a pointer to the zsig if there is one */ |
|---|
| 355 | |
|---|
| 356 | if (n->z_message_len==0) { |
|---|
| 357 | *k=0; |
|---|
| 358 | return(""); |
|---|
| 359 | } |
|---|
| 360 | *k=strlen(n->z_message); |
|---|
| 361 | return(n->z_message); |
|---|
| 362 | } |
|---|
| 363 | #else |
|---|
| 364 | char *owl_zephyr_get_zsig(void *n, int *k) |
|---|
| 365 | { |
|---|
| 366 | return(""); |
|---|
| 367 | } |
|---|
| 368 | #endif |
|---|
| 369 | |
|---|
| 370 | int send_zephyr(char *opcode, char *zsig, char *class, char *instance, char *recipient, char *message) |
|---|
| 371 | { |
|---|
| 372 | #ifdef HAVE_LIBZEPHYR |
|---|
| 373 | int ret; |
|---|
| 374 | ZNotice_t notice; |
|---|
| 375 | |
|---|
| 376 | memset(¬ice, 0, sizeof(notice)); |
|---|
| 377 | |
|---|
| 378 | ZResetAuthentication(); |
|---|
| 379 | |
|---|
| 380 | notice.z_kind=ACKED; |
|---|
| 381 | notice.z_port=0; |
|---|
| 382 | notice.z_class=class; |
|---|
| 383 | notice.z_class_inst=instance; |
|---|
| 384 | if (!strcmp(recipient, "*") || !strcmp(recipient, "@")) { |
|---|
| 385 | notice.z_recipient=""; |
|---|
| 386 | } else { |
|---|
| 387 | notice.z_recipient=recipient; |
|---|
| 388 | } |
|---|
| 389 | notice.z_default_format="Class $class, Instance $instance:\nTo: @bold($recipient) at $time $date\nFrom: @bold{$1 <$sender>}\n\n$2"; |
|---|
| 390 | notice.z_sender=NULL; |
|---|
| 391 | if (opcode) notice.z_opcode=opcode; |
|---|
| 392 | |
|---|
| 393 | notice.z_message_len=strlen(zsig)+1+strlen(message); |
|---|
| 394 | notice.z_message=owl_malloc(notice.z_message_len+10); |
|---|
| 395 | strcpy(notice.z_message, zsig); |
|---|
| 396 | memcpy(notice.z_message+strlen(zsig)+1, message, strlen(message)); |
|---|
| 397 | |
|---|
| 398 | /* ret=ZSendNotice(¬ice, ZAUTH); */ |
|---|
| 399 | ret=ZSrvSendNotice(¬ice, ZAUTH, send_zephyr_helper); |
|---|
| 400 | |
|---|
| 401 | /* free then check the return */ |
|---|
| 402 | owl_free(notice.z_message); |
|---|
| 403 | ZFreeNotice(¬ice); |
|---|
| 404 | if (ret!=ZERR_NONE) { |
|---|
| 405 | owl_function_error("Error sending zephyr"); |
|---|
| 406 | return(ret); |
|---|
| 407 | } |
|---|
| 408 | return(0); |
|---|
| 409 | #else |
|---|
| 410 | return(0); |
|---|
| 411 | #endif |
|---|
| 412 | } |
|---|
| 413 | |
|---|
| 414 | #ifdef HAVE_LIBZEPHYR |
|---|
| 415 | Code_t send_zephyr_helper(ZNotice_t *notice, char *buf, int len, int wait) |
|---|
| 416 | { |
|---|
| 417 | return(ZSendPacket(buf, len, 0)); |
|---|
| 418 | } |
|---|
| 419 | #endif |
|---|
| 420 | |
|---|
| 421 | void send_ping(char *to) |
|---|
| 422 | { |
|---|
| 423 | #ifdef HAVE_LIBZEPHYR |
|---|
| 424 | send_zephyr("PING", "", "MESSAGE", "PERSONAL", to, ""); |
|---|
| 425 | #endif |
|---|
| 426 | } |
|---|
| 427 | |
|---|
| 428 | #ifdef HAVE_LIBZEPHYR |
|---|
| 429 | void owl_zephyr_handle_ack(ZNotice_t *retnotice) |
|---|
| 430 | { |
|---|
| 431 | char *tmp; |
|---|
| 432 | |
|---|
| 433 | /* if it's an HMACK ignore it */ |
|---|
| 434 | if (retnotice->z_kind == HMACK) return; |
|---|
| 435 | |
|---|
| 436 | if (retnotice->z_kind == SERVNAK) { |
|---|
| 437 | owl_function_error("Authorization failure sending zephyr"); |
|---|
| 438 | } else if ((retnotice->z_kind != SERVACK) || !retnotice->z_message_len) { |
|---|
| 439 | owl_function_error("Detected server failure while receiving acknowledgement"); |
|---|
| 440 | } else if (!strcmp(retnotice->z_message, ZSRVACK_SENT)) { |
|---|
| 441 | if (!strcasecmp(retnotice->z_opcode, "ping")) { |
|---|
| 442 | return; |
|---|
| 443 | } else if (!strcasecmp(retnotice->z_class, "message") && |
|---|
| 444 | !strcasecmp(retnotice->z_class_inst, "personal")) { |
|---|
| 445 | tmp=short_zuser(retnotice->z_recipient); |
|---|
| 446 | owl_function_makemsg("Message sent to %s.", tmp); |
|---|
| 447 | free(tmp); |
|---|
| 448 | } else { |
|---|
| 449 | owl_function_makemsg("Message sent to -c %s -i %s\n", retnotice->z_class, retnotice->z_class_inst); |
|---|
| 450 | } |
|---|
| 451 | } else if (!strcmp(retnotice->z_message, ZSRVACK_NOTSENT)) { |
|---|
| 452 | if (strcasecmp(retnotice->z_class, "message")) { |
|---|
| 453 | char buff[1024]; |
|---|
| 454 | owl_function_error("No one subscribed to class class %s", retnotice->z_class); |
|---|
| 455 | sprintf(buff, "Could not send message to class %s: no one subscribed.\n", retnotice->z_class); |
|---|
| 456 | owl_function_adminmsg("", buff); |
|---|
| 457 | } else { |
|---|
| 458 | char buff[1024]; |
|---|
| 459 | tmp = short_zuser(retnotice->z_recipient); |
|---|
| 460 | owl_function_error("%s: Not logged in or subscribing to messages.", |
|---|
| 461 | tmp); |
|---|
| 462 | |
|---|
| 463 | sprintf(buff, "Could not send message to %s: not logged in or subscribing to messages.\n", tmp); |
|---|
| 464 | owl_function_adminmsg("", buff); |
|---|
| 465 | if (owl_global_is_logging(&g)) owl_log_outgoing_zephyr_error(tmp, buff); |
|---|
| 466 | owl_free(tmp); |
|---|
| 467 | } |
|---|
| 468 | } else { |
|---|
| 469 | owl_function_error("Internal error on ack (%s)", retnotice->z_message); |
|---|
| 470 | } |
|---|
| 471 | } |
|---|
| 472 | #else |
|---|
| 473 | void owl_zephyr_handle_ack(void *retnotice) |
|---|
| 474 | { |
|---|
| 475 | } |
|---|
| 476 | #endif |
|---|
| 477 | |
|---|
| 478 | #ifdef HAVE_LIBZEPHYR |
|---|
| 479 | int owl_zephyr_notice_is_ack(ZNotice_t *n) |
|---|
| 480 | { |
|---|
| 481 | if (n->z_kind == SERVNAK || n->z_kind == SERVACK || n->z_kind == HMACK) { |
|---|
| 482 | if (!strcasecmp(n->z_class, LOGIN_CLASS)) return(0); |
|---|
| 483 | return(1); |
|---|
| 484 | } |
|---|
| 485 | return(0); |
|---|
| 486 | } |
|---|
| 487 | #else |
|---|
| 488 | int owl_zephyr_notice_is_ack(void *n) |
|---|
| 489 | { |
|---|
| 490 | return(0); |
|---|
| 491 | } |
|---|
| 492 | #endif |
|---|
| 493 | |
|---|
| 494 | void owl_zephyr_zaway(owl_message *m) |
|---|
| 495 | { |
|---|
| 496 | #ifdef HAVE_LIBZEPHYR |
|---|
| 497 | char *tmpbuff, *myuser, *to; |
|---|
| 498 | |
|---|
| 499 | /* bail if it doesn't look like a message we should reply to. Some |
|---|
| 500 | * of this defined by the way zaway(1) works |
|---|
| 501 | */ |
|---|
| 502 | if (strcasecmp(owl_message_get_class(m), "message")) return; |
|---|
| 503 | if (strcasecmp(owl_message_get_recipient(m), ZGetSender())) return; |
|---|
| 504 | if (!strcasecmp(owl_message_get_sender(m), "")) return; |
|---|
| 505 | if (!strcasecmp(owl_message_get_opcode(m), "ping")) return; |
|---|
| 506 | if (!strcasecmp(owl_message_get_opcode(m), "auto")) return; |
|---|
| 507 | if (!strcasecmp(owl_message_get_zsig(m), "Automated reply:")) return; |
|---|
| 508 | if (!strcasecmp(owl_message_get_sender(m), ZGetSender())) return; |
|---|
| 509 | if (owl_message_get_attribute_value(m, "isauto")) return; |
|---|
| 510 | |
|---|
| 511 | if (owl_global_is_smartstrip(&g)) { |
|---|
| 512 | to=owl_zephyr_smartstripped_user(owl_message_get_sender(m)); |
|---|
| 513 | } else { |
|---|
| 514 | to=owl_strdup(owl_message_get_sender(m)); |
|---|
| 515 | } |
|---|
| 516 | |
|---|
| 517 | send_zephyr("", |
|---|
| 518 | "Automated reply:", |
|---|
| 519 | owl_message_get_class(m), |
|---|
| 520 | owl_message_get_instance(m), |
|---|
| 521 | to, |
|---|
| 522 | owl_global_get_zaway_msg(&g)); |
|---|
| 523 | |
|---|
| 524 | myuser=short_zuser(to); |
|---|
| 525 | if (!strcasecmp(owl_message_get_instance(m), "personal")) { |
|---|
| 526 | tmpbuff = owl_sprintf("zwrite %s", myuser); |
|---|
| 527 | } else { |
|---|
| 528 | tmpbuff = owl_sprintf("zwrite -i %s %s", owl_message_get_instance(m), myuser); |
|---|
| 529 | } |
|---|
| 530 | owl_free(myuser); |
|---|
| 531 | owl_free(to); |
|---|
| 532 | |
|---|
| 533 | /* display the message as an admin message in the receive window */ |
|---|
| 534 | owl_function_make_outgoing_zephyr(owl_global_get_zaway_msg(&g), tmpbuff, "Automated reply:"); |
|---|
| 535 | owl_free(tmpbuff); |
|---|
| 536 | #endif |
|---|
| 537 | } |
|---|
| 538 | |
|---|
| 539 | #ifdef HAVE_LIBZEPHYR |
|---|
| 540 | void owl_zephyr_hackaway_cr(ZNotice_t *n) |
|---|
| 541 | { |
|---|
| 542 | /* replace \r's with ' '. Gross-ish */ |
|---|
| 543 | int i; |
|---|
| 544 | |
|---|
| 545 | for (i=0; i<n->z_message_len; i++) { |
|---|
| 546 | if (n->z_message[i]=='\r') { |
|---|
| 547 | n->z_message[i]=' '; |
|---|
| 548 | } |
|---|
| 549 | } |
|---|
| 550 | } |
|---|
| 551 | #endif |
|---|
| 552 | |
|---|
| 553 | void owl_zephyr_zlocate(char *user, char *out, int auth) |
|---|
| 554 | { |
|---|
| 555 | #ifdef HAVE_LIBZEPHYR |
|---|
| 556 | int ret, numlocs; |
|---|
| 557 | int one = 1; |
|---|
| 558 | ZLocations_t locations; |
|---|
| 559 | char *myuser; |
|---|
| 560 | |
|---|
| 561 | strcpy(out, ""); |
|---|
| 562 | ZResetAuthentication(); |
|---|
| 563 | ret=ZLocateUser(user,&numlocs,auth?ZAUTH:ZNOAUTH); |
|---|
| 564 | if (ret != ZERR_NONE) { |
|---|
| 565 | owl_function_error("Error locating user %s", user); |
|---|
| 566 | } |
|---|
| 567 | |
|---|
| 568 | if (numlocs==0) { |
|---|
| 569 | myuser=short_zuser(user); |
|---|
| 570 | sprintf(out, "%s: Hidden or not logged-in\n", myuser); |
|---|
| 571 | owl_free(myuser); |
|---|
| 572 | return; |
|---|
| 573 | } |
|---|
| 574 | |
|---|
| 575 | for (;numlocs;numlocs--) { |
|---|
| 576 | ZGetLocations(&locations,&one); |
|---|
| 577 | myuser=short_zuser(user); |
|---|
| 578 | sprintf(out, "%s%s: %s\t%s\t%s\n", out, myuser, locations.host, locations.tty, locations.time); |
|---|
| 579 | owl_free(myuser); |
|---|
| 580 | } |
|---|
| 581 | #endif |
|---|
| 582 | } |
|---|
| 583 | |
|---|
| 584 | void owl_zephyr_addsub(char *filename, char *class, char *inst, char *recip) |
|---|
| 585 | { |
|---|
| 586 | #ifdef HAVE_LIBZEPHYR |
|---|
| 587 | char *line, subsfile[LINE], buff[LINE]; |
|---|
| 588 | FILE *file; |
|---|
| 589 | |
|---|
| 590 | line=owl_zephyr_makesubline(class, inst, recip); |
|---|
| 591 | |
|---|
| 592 | if (filename==NULL) { |
|---|
| 593 | sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".zephyr.subs"); |
|---|
| 594 | } else { |
|---|
| 595 | strcpy(subsfile, filename); |
|---|
| 596 | } |
|---|
| 597 | |
|---|
| 598 | /* first check if it exists already */ |
|---|
| 599 | file=fopen(subsfile, "r"); |
|---|
| 600 | if (!file) { |
|---|
| 601 | owl_function_error("Error opening file %s", subsfile); |
|---|
| 602 | owl_free(line); |
|---|
| 603 | return; |
|---|
| 604 | } |
|---|
| 605 | while (fgets(buff, LINE, file)!=NULL) { |
|---|
| 606 | if (!strcasecmp(buff, line)) { |
|---|
| 607 | owl_function_error("Subscription already present in %s", subsfile); |
|---|
| 608 | owl_free(line); |
|---|
| 609 | return; |
|---|
| 610 | } |
|---|
| 611 | } |
|---|
| 612 | |
|---|
| 613 | /* if we get here then we didn't find it */ |
|---|
| 614 | fclose(file); |
|---|
| 615 | file=fopen(subsfile, "a"); |
|---|
| 616 | if (!file) { |
|---|
| 617 | owl_function_error("Error opening file %s for writing", subsfile); |
|---|
| 618 | owl_free(line); |
|---|
| 619 | return; |
|---|
| 620 | } |
|---|
| 621 | fputs(line, file); |
|---|
| 622 | fclose(file); |
|---|
| 623 | owl_function_makemsg("Subscription added"); |
|---|
| 624 | |
|---|
| 625 | owl_free(line); |
|---|
| 626 | #endif |
|---|
| 627 | } |
|---|
| 628 | |
|---|
| 629 | void owl_zephyr_delsub(char *filename, char *class, char *inst, char *recip) |
|---|
| 630 | { |
|---|
| 631 | #ifdef HAVE_LIBZEPHYR |
|---|
| 632 | char *line, *subsfile; |
|---|
| 633 | |
|---|
| 634 | line=owl_zephyr_makesubline(class, inst, recip); |
|---|
| 635 | line[strlen(line)-1]='\0'; |
|---|
| 636 | |
|---|
| 637 | if (!filename) { |
|---|
| 638 | subsfile=owl_sprintf("%s/.zephyr.subs", owl_global_get_homedir(&g)); |
|---|
| 639 | } else { |
|---|
| 640 | subsfile=owl_strdup(filename); |
|---|
| 641 | } |
|---|
| 642 | |
|---|
| 643 | owl_util_file_deleteline(subsfile, line, 1); |
|---|
| 644 | owl_free(subsfile); |
|---|
| 645 | owl_free(line); |
|---|
| 646 | owl_function_makemsg("Subscription removed"); |
|---|
| 647 | #endif |
|---|
| 648 | } |
|---|
| 649 | |
|---|
| 650 | /* caller must free the return */ |
|---|
| 651 | char *owl_zephyr_makesubline(char *class, char *inst, char *recip) |
|---|
| 652 | { |
|---|
| 653 | char *out; |
|---|
| 654 | |
|---|
| 655 | out=owl_malloc(strlen(class)+strlen(inst)+strlen(recip)+30); |
|---|
| 656 | sprintf(out, "%s,%s,%s\n", class, inst, !strcmp(recip, "") ? "*" : recip); |
|---|
| 657 | return(out); |
|---|
| 658 | } |
|---|
| 659 | |
|---|
| 660 | |
|---|
| 661 | void owl_zephyr_zlog_in(void) |
|---|
| 662 | { |
|---|
| 663 | #ifdef HAVE_LIBZEPHYR |
|---|
| 664 | char *exposure, *eset; |
|---|
| 665 | int ret; |
|---|
| 666 | |
|---|
| 667 | ZResetAuthentication(); |
|---|
| 668 | |
|---|
| 669 | eset=EXPOSE_REALMVIS; |
|---|
| 670 | exposure=ZGetVariable("exposure"); |
|---|
| 671 | if (exposure==NULL) { |
|---|
| 672 | eset=EXPOSE_REALMVIS; |
|---|
| 673 | } else if (!strcasecmp(exposure,EXPOSE_NONE)) { |
|---|
| 674 | eset = EXPOSE_NONE; |
|---|
| 675 | } else if (!strcasecmp(exposure,EXPOSE_OPSTAFF)) { |
|---|
| 676 | eset = EXPOSE_OPSTAFF; |
|---|
| 677 | } else if (!strcasecmp(exposure,EXPOSE_REALMVIS)) { |
|---|
| 678 | eset = EXPOSE_REALMVIS; |
|---|
| 679 | } else if (!strcasecmp(exposure,EXPOSE_REALMANN)) { |
|---|
| 680 | eset = EXPOSE_REALMANN; |
|---|
| 681 | } else if (!strcasecmp(exposure,EXPOSE_NETVIS)) { |
|---|
| 682 | eset = EXPOSE_NETVIS; |
|---|
| 683 | } else if (!strcasecmp(exposure,EXPOSE_NETANN)) { |
|---|
| 684 | eset = EXPOSE_NETANN; |
|---|
| 685 | } |
|---|
| 686 | |
|---|
| 687 | ret=ZSetLocation(eset); |
|---|
| 688 | if (ret != ZERR_NONE) { |
|---|
| 689 | /* |
|---|
| 690 | char buff[LINE]; |
|---|
| 691 | sprintf(buff, "Error setting location: %s", error_message(ret)); |
|---|
| 692 | owl_function_makemsg(buff); |
|---|
| 693 | */ |
|---|
| 694 | } |
|---|
| 695 | #endif |
|---|
| 696 | } |
|---|
| 697 | |
|---|
| 698 | void owl_zephyr_zlog_out(void) |
|---|
| 699 | { |
|---|
| 700 | #ifdef HAVE_LIBZEPHYR |
|---|
| 701 | int ret; |
|---|
| 702 | |
|---|
| 703 | ZResetAuthentication(); |
|---|
| 704 | ret=ZUnsetLocation(); |
|---|
| 705 | if (ret != ZERR_NONE) { |
|---|
| 706 | /* |
|---|
| 707 | char buff[LINE]; |
|---|
| 708 | sprintf(buff, "Error unsetting location: %s", error_message(ret)); |
|---|
| 709 | owl_function_makemsg(buff); |
|---|
| 710 | */ |
|---|
| 711 | } |
|---|
| 712 | #endif |
|---|
| 713 | } |
|---|
| 714 | |
|---|
| 715 | void owl_zephyr_addbuddy(char *name) |
|---|
| 716 | { |
|---|
| 717 | char *filename; |
|---|
| 718 | FILE *file; |
|---|
| 719 | |
|---|
| 720 | filename=owl_sprintf("%s/.anyone", owl_global_get_homedir(&g)); |
|---|
| 721 | file=fopen(filename, "a"); |
|---|
| 722 | owl_free(filename); |
|---|
| 723 | if (!file) { |
|---|
| 724 | owl_function_error("Error opening zephyr buddy file for append"); |
|---|
| 725 | return; |
|---|
| 726 | } |
|---|
| 727 | fprintf(file, "%s\n", name); |
|---|
| 728 | fclose(file); |
|---|
| 729 | } |
|---|
| 730 | |
|---|
| 731 | void owl_zephyr_delbuddy(char *name) |
|---|
| 732 | { |
|---|
| 733 | char *filename; |
|---|
| 734 | |
|---|
| 735 | filename=owl_sprintf("%s/.anyone", owl_global_get_homedir(&g)); |
|---|
| 736 | owl_util_file_deleteline(filename, name, 0); |
|---|
| 737 | owl_free(filename); |
|---|
| 738 | } |
|---|
| 739 | |
|---|
| 740 | /* return auth string */ |
|---|
| 741 | #ifdef HAVE_LIBZEPHYR |
|---|
| 742 | char *owl_zephyr_get_authstr(ZNotice_t *n) |
|---|
| 743 | { |
|---|
| 744 | |
|---|
| 745 | if (!n) return("UNKNOWN"); |
|---|
| 746 | |
|---|
| 747 | if (n->z_auth == ZAUTH_FAILED) { |
|---|
| 748 | return ("FAILED"); |
|---|
| 749 | } else if (n->z_auth == ZAUTH_NO) { |
|---|
| 750 | return ("NO"); |
|---|
| 751 | } else if (n->z_auth == ZAUTH_YES) { |
|---|
| 752 | return ("YES"); |
|---|
| 753 | } else { |
|---|
| 754 | return ("UNKNOWN"); |
|---|
| 755 | } |
|---|
| 756 | } |
|---|
| 757 | #else |
|---|
| 758 | char *owl_zephyr_get_authstr(void *n) |
|---|
| 759 | { |
|---|
| 760 | return(""); |
|---|
| 761 | } |
|---|
| 762 | #endif |
|---|
| 763 | |
|---|
| 764 | /* Returns a buffer of subscriptions or an error message. Caller must |
|---|
| 765 | * free the return. |
|---|
| 766 | */ |
|---|
| 767 | char *owl_zephyr_getsubs() |
|---|
| 768 | { |
|---|
| 769 | #ifdef HAVE_LIBZEPHYR |
|---|
| 770 | int ret, num, i, one; |
|---|
| 771 | ZSubscription_t sub; |
|---|
| 772 | char *out, *tmpbuff; |
|---|
| 773 | one=1; |
|---|
| 774 | |
|---|
| 775 | ret=ZRetrieveSubscriptions(0, &num); |
|---|
| 776 | if (ret==ZERR_TOOMANYSUBS) { |
|---|
| 777 | return(owl_strdup("Zephyr: too many subscriptions\n")); |
|---|
| 778 | } else if (ret) { |
|---|
| 779 | return(owl_strdup("Zephyr: error retriving subscriptions\n")); |
|---|
| 780 | } |
|---|
| 781 | |
|---|
| 782 | out=owl_malloc(num*500); |
|---|
| 783 | tmpbuff=owl_malloc(num*500); |
|---|
| 784 | strcpy(out, ""); |
|---|
| 785 | for (i=0; i<num; i++) { |
|---|
| 786 | if ((ret = ZGetSubscriptions(&sub, &one)) != ZERR_NONE) { |
|---|
| 787 | owl_free(out); |
|---|
| 788 | owl_free(tmpbuff); |
|---|
| 789 | ZFlushSubscriptions(); |
|---|
| 790 | out=owl_strdup("Error while getting subscriptions\n"); |
|---|
| 791 | return(out); |
|---|
| 792 | } else { |
|---|
| 793 | sprintf(tmpbuff, "<%s,%s,%s>\n%s", sub.zsub_class, sub.zsub_classinst, sub.zsub_recipient, out); |
|---|
| 794 | strcpy(out, tmpbuff); |
|---|
| 795 | } |
|---|
| 796 | } |
|---|
| 797 | |
|---|
| 798 | owl_free(tmpbuff); |
|---|
| 799 | ZFlushSubscriptions(); |
|---|
| 800 | return(out); |
|---|
| 801 | #else |
|---|
| 802 | return(owl_strdup("Zephyr not available")); |
|---|
| 803 | #endif |
|---|
| 804 | } |
|---|
| 805 | |
|---|
| 806 | char *owl_zephyr_get_variable(char *var) |
|---|
| 807 | { |
|---|
| 808 | #ifdef HAVE_LIBZEPHYR |
|---|
| 809 | return(ZGetVariable(var)); |
|---|
| 810 | #else |
|---|
| 811 | return(""); |
|---|
| 812 | #endif |
|---|
| 813 | } |
|---|
| 814 | |
|---|
| 815 | void owl_zephyr_set_locationinfo(char *host, char *val) |
|---|
| 816 | { |
|---|
| 817 | #ifdef HAVE_LIBZEPHYR |
|---|
| 818 | ZInitLocationInfo(host, val); |
|---|
| 819 | #endif |
|---|
| 820 | } |
|---|
| 821 | |
|---|
| 822 | /* Strip a local realm fron the zephyr user name. |
|---|
| 823 | * The caller must free the return |
|---|
| 824 | */ |
|---|
| 825 | char *short_zuser(char *in) |
|---|
| 826 | { |
|---|
| 827 | char *out, *ptr; |
|---|
| 828 | |
|---|
| 829 | out=owl_strdup(in); |
|---|
| 830 | ptr=strchr(out, '@'); |
|---|
| 831 | if (ptr) { |
|---|
| 832 | if (!strcasecmp(ptr+1, owl_zephyr_get_realm())) { |
|---|
| 833 | *ptr='\0'; |
|---|
| 834 | } |
|---|
| 835 | } |
|---|
| 836 | return(out); |
|---|
| 837 | } |
|---|
| 838 | |
|---|
| 839 | /* Append a local realm to the zephyr user name if necessary. |
|---|
| 840 | * The caller must free the return. |
|---|
| 841 | */ |
|---|
| 842 | char *long_zuser(char *in) |
|---|
| 843 | { |
|---|
| 844 | char *ptr; |
|---|
| 845 | |
|---|
| 846 | if (NULL != (ptr=strchr(in, '@'))) { |
|---|
| 847 | return owl_strdup(in); |
|---|
| 848 | } else { |
|---|
| 849 | return owl_sprintf("%s@%s", in, owl_zephyr_get_realm()); |
|---|
| 850 | } |
|---|
| 851 | } |
|---|
| 852 | |
|---|
| 853 | |
|---|
| 854 | /* strip out the instance from a zsender's principal. Preserves the |
|---|
| 855 | * realm if present. daemon.webzephyr is a special case. The |
|---|
| 856 | * caller must free the return |
|---|
| 857 | */ |
|---|
| 858 | char *owl_zephyr_smartstripped_user(char *in) |
|---|
| 859 | { |
|---|
| 860 | char *ptr, *realm, *out; |
|---|
| 861 | |
|---|
| 862 | out=owl_strdup(in); |
|---|
| 863 | |
|---|
| 864 | /* bail immeaditly if we don't have to do any work */ |
|---|
| 865 | ptr=strchr(in, '.'); |
|---|
| 866 | if (!strchr(in, '/') && !ptr) { |
|---|
| 867 | /* no '/' and no '.' */ |
|---|
| 868 | return(out); |
|---|
| 869 | } |
|---|
| 870 | if (ptr && strchr(in, '@') && (ptr > strchr(in, '@'))) { |
|---|
| 871 | /* There's a '.' but it's in the realm */ |
|---|
| 872 | return(out); |
|---|
| 873 | } |
|---|
| 874 | if (!strncasecmp(in, OWL_WEBZEPHYR_PRINCIPAL, strlen(OWL_WEBZEPHYR_PRINCIPAL))) { |
|---|
| 875 | return(out); |
|---|
| 876 | } |
|---|
| 877 | |
|---|
| 878 | /* remove the realm from ptr, but hold on to it */ |
|---|
| 879 | realm=strchr(out, '@'); |
|---|
| 880 | if (realm) realm[0]='\0'; |
|---|
| 881 | |
|---|
| 882 | /* strip */ |
|---|
| 883 | ptr=strchr(out, '.'); |
|---|
| 884 | if (!ptr) ptr=strchr(out, '/'); |
|---|
| 885 | ptr[0]='\0'; |
|---|
| 886 | |
|---|
| 887 | /* reattach the realm if we had one */ |
|---|
| 888 | if (realm) { |
|---|
| 889 | strcat(out, "@"); |
|---|
| 890 | strcat(out, realm+1); |
|---|
| 891 | } |
|---|
| 892 | |
|---|
| 893 | return(out); |
|---|
| 894 | } |
|---|
| 895 | |
|---|
| 896 | /* read the list of users in 'filename' as a .anyone file, and put the |
|---|
| 897 | * names of the zephyr users in the list 'in'. If 'filename' is NULL, |
|---|
| 898 | * use the default .anyone file in the users home directory. Returns |
|---|
| 899 | * -1 on failure, 0 on success. |
|---|
| 900 | */ |
|---|
| 901 | int owl_zephyr_get_anyone_list(owl_list *in, char *filename) |
|---|
| 902 | { |
|---|
| 903 | #ifdef HAVE_LIBZEPHYR |
|---|
| 904 | char *ourfile, *tmp, buff[LINE]; |
|---|
| 905 | FILE *f; |
|---|
| 906 | |
|---|
| 907 | if (filename==NULL) { |
|---|
| 908 | tmp=owl_global_get_homedir(&g); |
|---|
| 909 | ourfile=owl_sprintf("%s/.anyone", owl_global_get_homedir(&g)); |
|---|
| 910 | } else { |
|---|
| 911 | ourfile=owl_strdup(filename); |
|---|
| 912 | } |
|---|
| 913 | |
|---|
| 914 | f=fopen(ourfile, "r"); |
|---|
| 915 | if (!f) { |
|---|
| 916 | owl_function_error("Error opening file %s: %s", ourfile, strerror(errno) ? strerror(errno) : ""); |
|---|
| 917 | owl_free(ourfile); |
|---|
| 918 | return(-1); |
|---|
| 919 | } |
|---|
| 920 | |
|---|
| 921 | while (fgets(buff, LINE, f)!=NULL) { |
|---|
| 922 | /* ignore comments, blank lines etc. */ |
|---|
| 923 | if (buff[0]=='#') continue; |
|---|
| 924 | if (buff[0]=='\n') continue; |
|---|
| 925 | if (buff[0]=='\0') continue; |
|---|
| 926 | |
|---|
| 927 | /* strip the \n */ |
|---|
| 928 | buff[strlen(buff)-1]='\0'; |
|---|
| 929 | |
|---|
| 930 | /* ingore from # on */ |
|---|
| 931 | tmp=strchr(buff, '#'); |
|---|
| 932 | if (tmp) tmp[0]='\0'; |
|---|
| 933 | |
|---|
| 934 | /* ingore from SPC */ |
|---|
| 935 | tmp=strchr(buff, ' '); |
|---|
| 936 | if (tmp) tmp[0]='\0'; |
|---|
| 937 | |
|---|
| 938 | /* stick on the local realm. */ |
|---|
| 939 | if (!strchr(buff, '@')) { |
|---|
| 940 | strcat(buff, "@"); |
|---|
| 941 | strcat(buff, ZGetRealm()); |
|---|
| 942 | } |
|---|
| 943 | owl_list_append_element(in, owl_strdup(buff)); |
|---|
| 944 | } |
|---|
| 945 | fclose(f); |
|---|
| 946 | owl_free(ourfile); |
|---|
| 947 | return(0); |
|---|
| 948 | #else |
|---|
| 949 | return(-1); |
|---|
| 950 | #endif |
|---|
| 951 | } |
|---|