| 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 | #ifdef HAVE_LIBZEPHYR |
|---|
| 10 | static GList *deferred_subs = NULL; |
|---|
| 11 | |
|---|
| 12 | typedef struct _owl_sub_list { /* noproto */ |
|---|
| 13 | ZSubscription_t *subs; |
|---|
| 14 | int nsubs; |
|---|
| 15 | } owl_sub_list; |
|---|
| 16 | |
|---|
| 17 | Code_t ZResetAuthentication(); |
|---|
| 18 | #endif |
|---|
| 19 | |
|---|
| 20 | #define HM_SVC_FALLBACK htons((unsigned short) 2104) |
|---|
| 21 | |
|---|
| 22 | #ifdef HAVE_LIBZEPHYR |
|---|
| 23 | void owl_zephyr_initialize() |
|---|
| 24 | { |
|---|
| 25 | int ret; |
|---|
| 26 | struct servent *sp; |
|---|
| 27 | struct sockaddr_in sin; |
|---|
| 28 | ZNotice_t req; |
|---|
| 29 | Code_t code; |
|---|
| 30 | owl_dispatch *dispatch; |
|---|
| 31 | |
|---|
| 32 | /* |
|---|
| 33 | * Code modified from libzephyr's ZhmStat.c |
|---|
| 34 | * |
|---|
| 35 | * Modified to add the fd to our select loop, rather than hanging |
|---|
| 36 | * until we get an ack. |
|---|
| 37 | */ |
|---|
| 38 | |
|---|
| 39 | if ((ret = ZOpenPort(NULL)) != ZERR_NONE) { |
|---|
| 40 | owl_function_error("Error opening Zephyr port: %s", error_message(ret)); |
|---|
| 41 | return; |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | (void) memset(&sin, 0, sizeof(struct sockaddr_in)); |
|---|
| 45 | |
|---|
| 46 | sp = getservbyname(HM_SVCNAME, "udp"); |
|---|
| 47 | |
|---|
| 48 | sin.sin_port = (sp) ? sp->s_port : HM_SVC_FALLBACK; |
|---|
| 49 | sin.sin_family = AF_INET; |
|---|
| 50 | |
|---|
| 51 | sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
|---|
| 52 | |
|---|
| 53 | (void) memset(&req, 0, sizeof(req)); |
|---|
| 54 | req.z_kind = STAT; |
|---|
| 55 | req.z_port = 0; |
|---|
| 56 | req.z_class = zstr(HM_STAT_CLASS); |
|---|
| 57 | req.z_class_inst = zstr(HM_STAT_CLIENT); |
|---|
| 58 | req.z_opcode = zstr(HM_GIMMESTATS); |
|---|
| 59 | req.z_sender = zstr(""); |
|---|
| 60 | req.z_recipient = zstr(""); |
|---|
| 61 | req.z_default_format = zstr(""); |
|---|
| 62 | req.z_message_len = 0; |
|---|
| 63 | |
|---|
| 64 | if ((code = ZSetDestAddr(&sin)) != ZERR_NONE) { |
|---|
| 65 | owl_function_error("Initializing Zephyr: %s", error_message(code)); |
|---|
| 66 | return; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | if ((code = ZSendNotice(&req, ZNOAUTH)) != ZERR_NONE) { |
|---|
| 70 | owl_function_error("Initializing Zephyr: %s", error_message(code)); |
|---|
| 71 | return; |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | dispatch = owl_malloc(sizeof(*dispatch)); |
|---|
| 75 | dispatch->fd = ZGetFD(); |
|---|
| 76 | dispatch->cfunc = owl_zephyr_finish_initialization; |
|---|
| 77 | dispatch->destroy = (void(*)(owl_dispatch*))owl_free; |
|---|
| 78 | |
|---|
| 79 | owl_select_add_dispatch(dispatch); |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | void owl_zephyr_finish_initialization(owl_dispatch *d) { |
|---|
| 83 | Code_t code; |
|---|
| 84 | |
|---|
| 85 | owl_select_remove_dispatch(d->fd); |
|---|
| 86 | |
|---|
| 87 | ZClosePort(); |
|---|
| 88 | |
|---|
| 89 | if ((code = ZInitialize()) != ZERR_NONE) { |
|---|
| 90 | owl_function_error("Initializing Zephyr: %s", error_message(code)); |
|---|
| 91 | return; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | if ((code = ZOpenPort(NULL)) != ZERR_NONE) { |
|---|
| 95 | owl_function_error("Initializing Zephyr: %s", error_message(code)); |
|---|
| 96 | return; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | d = owl_malloc(sizeof(owl_dispatch)); |
|---|
| 100 | d->fd = ZGetFD(); |
|---|
| 101 | d->cfunc = &owl_zephyr_process_events; |
|---|
| 102 | d->destroy = NULL; |
|---|
| 103 | owl_select_add_dispatch(d); |
|---|
| 104 | owl_global_set_havezephyr(&g); |
|---|
| 105 | |
|---|
| 106 | if(g.load_initial_subs) { |
|---|
| 107 | owl_zephyr_load_initial_subs(); |
|---|
| 108 | } |
|---|
| 109 | while(deferred_subs != NULL) { |
|---|
| 110 | owl_sub_list *subs = deferred_subs->data; |
|---|
| 111 | owl_function_debugmsg("Loading %d deferred subs.", subs->nsubs); |
|---|
| 112 | owl_zephyr_loadsubs_helper(subs->subs, subs->nsubs); |
|---|
| 113 | deferred_subs = g_list_delete_link(deferred_subs, deferred_subs); |
|---|
| 114 | owl_free(subs); |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | /* zlog in if we need to */ |
|---|
| 118 | if (owl_global_is_startuplogin(&g)) { |
|---|
| 119 | owl_function_debugmsg("startup: doing zlog in"); |
|---|
| 120 | owl_zephyr_zlog_in(); |
|---|
| 121 | } |
|---|
| 122 | /* check pseudo-logins if we need to */ |
|---|
| 123 | if (owl_global_is_pseudologins(&g)) { |
|---|
| 124 | owl_function_debugmsg("startup: checking pseudo-logins"); |
|---|
| 125 | owl_function_zephyr_buddy_check(0); |
|---|
| 126 | } |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | void owl_zephyr_load_initial_subs() { |
|---|
| 130 | int ret_sd, ret_bd, ret_u; |
|---|
| 131 | |
|---|
| 132 | owl_function_debugmsg("startup: loading initial zephyr subs"); |
|---|
| 133 | |
|---|
| 134 | /* load default subscriptions */ |
|---|
| 135 | ret_sd = owl_zephyr_loaddefaultsubs(); |
|---|
| 136 | |
|---|
| 137 | /* load Barnowl default subscriptions */ |
|---|
| 138 | ret_bd = owl_zephyr_loadbarnowldefaultsubs(); |
|---|
| 139 | |
|---|
| 140 | /* load subscriptions from subs file */ |
|---|
| 141 | ret_u = owl_zephyr_loadsubs(NULL, 0); |
|---|
| 142 | |
|---|
| 143 | if (ret_sd || ret_bd || ret_u) { |
|---|
| 144 | owl_function_error("Error loading zephyr subscriptions"); |
|---|
| 145 | } else if (ret_u!=-1) { |
|---|
| 146 | owl_global_add_userclue(&g, OWL_USERCLUE_CLASSES); |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | /* load login subscriptions */ |
|---|
| 150 | if (owl_global_is_loginsubs(&g)) { |
|---|
| 151 | owl_function_debugmsg("startup: loading login subs"); |
|---|
| 152 | owl_function_loadloginsubs(NULL); |
|---|
| 153 | } |
|---|
| 154 | } |
|---|
| 155 | #else |
|---|
| 156 | void owl_zephyr_initialize() |
|---|
| 157 | { |
|---|
| 158 | } |
|---|
| 159 | #endif |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | int owl_zephyr_shutdown() |
|---|
| 163 | { |
|---|
| 164 | #ifdef HAVE_LIBZEPHYR |
|---|
| 165 | if(owl_global_is_havezephyr(&g)) { |
|---|
| 166 | unsuball(); |
|---|
| 167 | ZClosePort(); |
|---|
| 168 | } |
|---|
| 169 | #endif |
|---|
| 170 | return(0); |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | int owl_zephyr_zpending() |
|---|
| 174 | { |
|---|
| 175 | #ifdef HAVE_LIBZEPHYR |
|---|
| 176 | if(owl_global_is_havezephyr(&g)) |
|---|
| 177 | return(ZPending()); |
|---|
| 178 | else |
|---|
| 179 | return 0; |
|---|
| 180 | #else |
|---|
| 181 | return(0); |
|---|
| 182 | #endif |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | const char *owl_zephyr_get_realm() |
|---|
| 186 | { |
|---|
| 187 | #ifdef HAVE_LIBZEPHYR |
|---|
| 188 | return(ZGetRealm()); |
|---|
| 189 | #else |
|---|
| 190 | return(""); |
|---|
| 191 | #endif |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | const char *owl_zephyr_get_sender() |
|---|
| 195 | { |
|---|
| 196 | #ifdef HAVE_LIBZEPHYR |
|---|
| 197 | return(ZGetSender()); |
|---|
| 198 | #else |
|---|
| 199 | return(""); |
|---|
| 200 | #endif |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | #ifdef HAVE_LIBZEPHYR |
|---|
| 204 | int owl_zephyr_loadsubs_helper(ZSubscription_t subs[], int count) |
|---|
| 205 | { |
|---|
| 206 | int ret = 0; |
|---|
| 207 | if (owl_global_is_havezephyr(&g)) { |
|---|
| 208 | int i; |
|---|
| 209 | /* sub without defaults */ |
|---|
| 210 | if (ZSubscribeToSansDefaults(subs,count,0) != ZERR_NONE) { |
|---|
| 211 | owl_function_error("Error subscribing to zephyr notifications."); |
|---|
| 212 | ret=-2; |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | /* free stuff */ |
|---|
| 216 | for (i=0; i<count; i++) { |
|---|
| 217 | owl_free(subs[i].zsub_class); |
|---|
| 218 | owl_free(subs[i].zsub_classinst); |
|---|
| 219 | owl_free(subs[i].zsub_recipient); |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | owl_free(subs); |
|---|
| 223 | } else { |
|---|
| 224 | owl_sub_list *s = owl_malloc(sizeof(owl_sub_list)); |
|---|
| 225 | s->subs = subs; |
|---|
| 226 | s->nsubs = count; |
|---|
| 227 | deferred_subs = g_list_append(deferred_subs, s); |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | return ret; |
|---|
| 231 | } |
|---|
| 232 | #endif |
|---|
| 233 | |
|---|
| 234 | /* Load zephyr subscriptions from 'filename'. If 'filename' is NULL, |
|---|
| 235 | * the default file $HOME/.zephyr.subs will be used. |
|---|
| 236 | * |
|---|
| 237 | * Returns 0 on success. If the file does not exist, return -1 if |
|---|
| 238 | * 'error_on_nofile' is 1, otherwise return 0. Return -1 if the file |
|---|
| 239 | * exists but can not be read. Return -2 if there is a failure from |
|---|
| 240 | * zephyr to load the subscriptions. |
|---|
| 241 | */ |
|---|
| 242 | int owl_zephyr_loadsubs(const char *filename, int error_on_nofile) |
|---|
| 243 | { |
|---|
| 244 | #ifdef HAVE_LIBZEPHYR |
|---|
| 245 | FILE *file; |
|---|
| 246 | char *tmp, *start; |
|---|
| 247 | char buffer[1024], subsfile[1024]; |
|---|
| 248 | ZSubscription_t *subs; |
|---|
| 249 | int subSize = 1024; |
|---|
| 250 | int count, ret; |
|---|
| 251 | struct stat statbuff; |
|---|
| 252 | |
|---|
| 253 | subs = owl_malloc(sizeof(ZSubscription_t) * subSize); |
|---|
| 254 | if (filename==NULL) { |
|---|
| 255 | sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".zephyr.subs"); |
|---|
| 256 | } else { |
|---|
| 257 | strcpy(subsfile, filename); |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | ret=stat(subsfile, &statbuff); |
|---|
| 261 | if (ret) { |
|---|
| 262 | if (error_on_nofile==1) return(-1); |
|---|
| 263 | return(0); |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | ZResetAuthentication(); |
|---|
| 267 | count=0; |
|---|
| 268 | file=fopen(subsfile, "r"); |
|---|
| 269 | if (!file) return(-1); |
|---|
| 270 | while ( fgets(buffer, 1024, file)!=NULL ) { |
|---|
| 271 | if (buffer[0]=='#' || buffer[0]=='\n' || buffer[0]=='\n') continue; |
|---|
| 272 | |
|---|
| 273 | if (buffer[0]=='-') { |
|---|
| 274 | start=buffer+1; |
|---|
| 275 | } else { |
|---|
| 276 | start=buffer; |
|---|
| 277 | } |
|---|
| 278 | |
|---|
| 279 | if (count >= subSize) { |
|---|
| 280 | subSize *= 2; |
|---|
| 281 | subs = owl_realloc(subs, sizeof(ZSubscription_t) * subSize); |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | /* add it to the list of subs */ |
|---|
| 285 | if ((tmp=strtok(start, ",\n\r"))==NULL) continue; |
|---|
| 286 | subs[count].zsub_class=owl_strdup(tmp); |
|---|
| 287 | if ((tmp=strtok(NULL, ",\n\r"))==NULL) continue; |
|---|
| 288 | subs[count].zsub_classinst=owl_strdup(tmp); |
|---|
| 289 | if ((tmp=strtok(NULL, " \t\n\r"))==NULL) continue; |
|---|
| 290 | subs[count].zsub_recipient=owl_strdup(tmp); |
|---|
| 291 | |
|---|
| 292 | /* if it started with '-' then add it to the global punt list, and |
|---|
| 293 | * remove it from the list of subs. */ |
|---|
| 294 | if (buffer[0]=='-') { |
|---|
| 295 | owl_function_zpunt(subs[count].zsub_class, subs[count].zsub_classinst, subs[count].zsub_recipient, 0); |
|---|
| 296 | owl_free(subs[count].zsub_class); |
|---|
| 297 | owl_free(subs[count].zsub_classinst); |
|---|
| 298 | owl_free(subs[count].zsub_recipient); |
|---|
| 299 | } |
|---|
| 300 | else { |
|---|
| 301 | count++; |
|---|
| 302 | } |
|---|
| 303 | } |
|---|
| 304 | fclose(file); |
|---|
| 305 | |
|---|
| 306 | ret = owl_zephyr_loadsubs_helper(subs, count); |
|---|
| 307 | return(ret); |
|---|
| 308 | #else |
|---|
| 309 | return(0); |
|---|
| 310 | #endif |
|---|
| 311 | } |
|---|
| 312 | |
|---|
| 313 | /* Load default Barnowl subscriptions |
|---|
| 314 | * |
|---|
| 315 | * Returns 0 on success. |
|---|
| 316 | * Return -2 if there is a failure from zephyr to load the subscriptions. |
|---|
| 317 | */ |
|---|
| 318 | int owl_zephyr_loadbarnowldefaultsubs() |
|---|
| 319 | { |
|---|
| 320 | #ifdef HAVE_LIBZEPHYR |
|---|
| 321 | ZSubscription_t *subs; |
|---|
| 322 | int subSize = 10; /* Max Barnowl default subs we allow */ |
|---|
| 323 | int count, ret; |
|---|
| 324 | |
|---|
| 325 | subs = owl_malloc(sizeof(ZSubscription_t) * subSize); |
|---|
| 326 | ret = 0; |
|---|
| 327 | ZResetAuthentication(); |
|---|
| 328 | count=0; |
|---|
| 329 | |
|---|
| 330 | subs[count].zsub_class=owl_strdup("message"); |
|---|
| 331 | subs[count].zsub_classinst=owl_strdup("*"); |
|---|
| 332 | subs[count].zsub_recipient=owl_strdup("%me%"); |
|---|
| 333 | count++; |
|---|
| 334 | |
|---|
| 335 | ret = owl_zephyr_loadsubs_helper(subs, count); |
|---|
| 336 | return(ret); |
|---|
| 337 | #else |
|---|
| 338 | return(0); |
|---|
| 339 | #endif |
|---|
| 340 | } |
|---|
| 341 | |
|---|
| 342 | int owl_zephyr_loaddefaultsubs() |
|---|
| 343 | { |
|---|
| 344 | #ifdef HAVE_LIBZEPHYR |
|---|
| 345 | ZSubscription_t subs[10]; |
|---|
| 346 | |
|---|
| 347 | if (ZSubscribeTo(subs,0,0) != ZERR_NONE) { |
|---|
| 348 | owl_function_error("Error subscribing to default zephyr notifications."); |
|---|
| 349 | return(-1); |
|---|
| 350 | } |
|---|
| 351 | return(0); |
|---|
| 352 | #else |
|---|
| 353 | return(0); |
|---|
| 354 | #endif |
|---|
| 355 | } |
|---|
| 356 | |
|---|
| 357 | int owl_zephyr_loadloginsubs(const char *filename) |
|---|
| 358 | { |
|---|
| 359 | #ifdef HAVE_LIBZEPHYR |
|---|
| 360 | FILE *file; |
|---|
| 361 | ZSubscription_t *subs; |
|---|
| 362 | int numSubs = 100; |
|---|
| 363 | char subsfile[1024], buffer[1024]; |
|---|
| 364 | int count, ret; |
|---|
| 365 | struct stat statbuff; |
|---|
| 366 | |
|---|
| 367 | subs = owl_malloc(numSubs * sizeof(ZSubscription_t)); |
|---|
| 368 | |
|---|
| 369 | if (filename==NULL) { |
|---|
| 370 | sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".anyone"); |
|---|
| 371 | } else { |
|---|
| 372 | strcpy(subsfile, filename); |
|---|
| 373 | } |
|---|
| 374 | |
|---|
| 375 | ret=stat(subsfile, &statbuff); |
|---|
| 376 | if (ret) return(0); |
|---|
| 377 | |
|---|
| 378 | ret=0; |
|---|
| 379 | |
|---|
| 380 | ZResetAuthentication(); |
|---|
| 381 | count=0; |
|---|
| 382 | file=fopen(subsfile, "r"); |
|---|
| 383 | if (file) { |
|---|
| 384 | while ( fgets(buffer, 1024, file)!=NULL ) { |
|---|
| 385 | if (buffer[0] == '\0' || buffer[0] == '#' || buffer[0] == '\n') continue; |
|---|
| 386 | |
|---|
| 387 | if (count == numSubs) { |
|---|
| 388 | numSubs *= 2; |
|---|
| 389 | subs = owl_realloc(subs, numSubs * sizeof(ZSubscription_t)); |
|---|
| 390 | } |
|---|
| 391 | |
|---|
| 392 | if (buffer[strlen(buffer) - 1] == '\n') |
|---|
| 393 | buffer[strlen(buffer) - 1] = '\0'; |
|---|
| 394 | subs[count].zsub_class=owl_strdup("login"); |
|---|
| 395 | subs[count].zsub_recipient=owl_strdup("*"); |
|---|
| 396 | if (strchr(buffer, '@')) { |
|---|
| 397 | subs[count].zsub_classinst=owl_strdup(buffer); |
|---|
| 398 | } else { |
|---|
| 399 | subs[count].zsub_classinst=owl_sprintf("%s@%s", buffer, ZGetRealm()); |
|---|
| 400 | } |
|---|
| 401 | |
|---|
| 402 | count++; |
|---|
| 403 | } |
|---|
| 404 | fclose(file); |
|---|
| 405 | } else { |
|---|
| 406 | count=0; |
|---|
| 407 | ret=-1; |
|---|
| 408 | } |
|---|
| 409 | |
|---|
| 410 | ret = owl_zephyr_loadsubs_helper(subs, count); |
|---|
| 411 | return(ret); |
|---|
| 412 | #else |
|---|
| 413 | return(0); |
|---|
| 414 | #endif |
|---|
| 415 | } |
|---|
| 416 | |
|---|
| 417 | void unsuball() |
|---|
| 418 | { |
|---|
| 419 | #if HAVE_LIBZEPHYR |
|---|
| 420 | int ret; |
|---|
| 421 | |
|---|
| 422 | ZResetAuthentication(); |
|---|
| 423 | ret=ZCancelSubscriptions(0); |
|---|
| 424 | if (ret != ZERR_NONE) { |
|---|
| 425 | com_err("owl",ret,"while unsubscribing"); |
|---|
| 426 | } |
|---|
| 427 | #endif |
|---|
| 428 | } |
|---|
| 429 | |
|---|
| 430 | int owl_zephyr_sub(const char *class, const char *inst, const char *recip) |
|---|
| 431 | { |
|---|
| 432 | #ifdef HAVE_LIBZEPHYR |
|---|
| 433 | ZSubscription_t subs[5]; |
|---|
| 434 | |
|---|
| 435 | subs[0].zsub_class=zstr(class); |
|---|
| 436 | subs[0].zsub_classinst=zstr(inst); |
|---|
| 437 | subs[0].zsub_recipient=zstr(recip); |
|---|
| 438 | |
|---|
| 439 | ZResetAuthentication(); |
|---|
| 440 | if (ZSubscribeTo(subs,1,0) != ZERR_NONE) { |
|---|
| 441 | owl_function_error("Error subbing to <%s,%s,%s>", class, inst, recip); |
|---|
| 442 | return(-2); |
|---|
| 443 | } |
|---|
| 444 | return(0); |
|---|
| 445 | #else |
|---|
| 446 | return(0); |
|---|
| 447 | #endif |
|---|
| 448 | } |
|---|
| 449 | |
|---|
| 450 | |
|---|
| 451 | int owl_zephyr_unsub(const char *class, const char *inst, const char *recip) |
|---|
| 452 | { |
|---|
| 453 | #ifdef HAVE_LIBZEPHYR |
|---|
| 454 | ZSubscription_t subs[5]; |
|---|
| 455 | |
|---|
| 456 | subs[0].zsub_class=zstr(class); |
|---|
| 457 | subs[0].zsub_classinst=zstr(inst); |
|---|
| 458 | subs[0].zsub_recipient=zstr(recip); |
|---|
| 459 | |
|---|
| 460 | ZResetAuthentication(); |
|---|
| 461 | if (ZUnsubscribeTo(subs,1,0) != ZERR_NONE) { |
|---|
| 462 | owl_function_error("Error unsubbing from <%s,%s,%s>", class, inst, recip); |
|---|
| 463 | return(-2); |
|---|
| 464 | } |
|---|
| 465 | return(0); |
|---|
| 466 | #else |
|---|
| 467 | return(0); |
|---|
| 468 | #endif |
|---|
| 469 | } |
|---|
| 470 | |
|---|
| 471 | /* return a pointer to the data in the Jth field, (NULL terminated by |
|---|
| 472 | * definition). Caller must free the return. |
|---|
| 473 | */ |
|---|
| 474 | #ifdef HAVE_LIBZEPHYR |
|---|
| 475 | char *owl_zephyr_get_field(const ZNotice_t *n, int j) |
|---|
| 476 | { |
|---|
| 477 | int i, count, save; |
|---|
| 478 | char *out; |
|---|
| 479 | |
|---|
| 480 | /* If there's no message here, just run along now */ |
|---|
| 481 | if (n->z_message_len == 0) |
|---|
| 482 | return(owl_strdup("")); |
|---|
| 483 | |
|---|
| 484 | count=save=0; |
|---|
| 485 | for (i=0; i<n->z_message_len; i++) { |
|---|
| 486 | if (n->z_message[i]=='\0') { |
|---|
| 487 | count++; |
|---|
| 488 | if (count==j) { |
|---|
| 489 | /* just found the end of the field we're looking for */ |
|---|
| 490 | return(owl_strdup(n->z_message+save)); |
|---|
| 491 | } else { |
|---|
| 492 | save=i+1; |
|---|
| 493 | } |
|---|
| 494 | } |
|---|
| 495 | } |
|---|
| 496 | /* catch the last field, which might not be null terminated */ |
|---|
| 497 | if (count==j-1) { |
|---|
| 498 | out=owl_malloc(n->z_message_len-save+5); |
|---|
| 499 | memcpy(out, n->z_message+save, n->z_message_len-save); |
|---|
| 500 | out[n->z_message_len-save]='\0'; |
|---|
| 501 | return(out); |
|---|
| 502 | } |
|---|
| 503 | |
|---|
| 504 | return(owl_strdup("")); |
|---|
| 505 | } |
|---|
| 506 | |
|---|
| 507 | char *owl_zephyr_get_field_as_utf8(const ZNotice_t *n, int j) |
|---|
| 508 | { |
|---|
| 509 | int i, count, save; |
|---|
| 510 | |
|---|
| 511 | /* If there's no message here, just run along now */ |
|---|
| 512 | if (n->z_message_len == 0) |
|---|
| 513 | return(owl_strdup("")); |
|---|
| 514 | |
|---|
| 515 | count=save=0; |
|---|
| 516 | for (i = 0; i < n->z_message_len; i++) { |
|---|
| 517 | if (n->z_message[i]=='\0') { |
|---|
| 518 | count++; |
|---|
| 519 | if (count == j) { |
|---|
| 520 | /* just found the end of the field we're looking for */ |
|---|
| 521 | return(owl_validate_or_convert(n->z_message + save)); |
|---|
| 522 | } else { |
|---|
| 523 | save = i + 1; |
|---|
| 524 | } |
|---|
| 525 | } |
|---|
| 526 | } |
|---|
| 527 | /* catch the last field, which might not be null terminated */ |
|---|
| 528 | if (count == j - 1) { |
|---|
| 529 | char *tmp, *out; |
|---|
| 530 | tmp = owl_malloc(n->z_message_len-save+5); |
|---|
| 531 | memcpy(tmp, n->z_message+save, n->z_message_len-save); |
|---|
| 532 | tmp[n->z_message_len-save]='\0'; |
|---|
| 533 | out = owl_validate_or_convert(tmp); |
|---|
| 534 | owl_free(tmp); |
|---|
| 535 | return out; |
|---|
| 536 | } |
|---|
| 537 | |
|---|
| 538 | return(owl_strdup("")); |
|---|
| 539 | } |
|---|
| 540 | #else |
|---|
| 541 | char *owl_zephyr_get_field(void *n, int j) |
|---|
| 542 | { |
|---|
| 543 | return(owl_strdup("")); |
|---|
| 544 | } |
|---|
| 545 | char *owl_zephyr_get_field_as_utf8(void *n, int j) |
|---|
| 546 | { |
|---|
| 547 | return owl_zephyr_get_field(n, j); |
|---|
| 548 | } |
|---|
| 549 | #endif |
|---|
| 550 | |
|---|
| 551 | |
|---|
| 552 | #ifdef HAVE_LIBZEPHYR |
|---|
| 553 | int owl_zephyr_get_num_fields(const ZNotice_t *n) |
|---|
| 554 | { |
|---|
| 555 | int i, fields; |
|---|
| 556 | |
|---|
| 557 | if(n->z_message_len == 0) |
|---|
| 558 | return 0; |
|---|
| 559 | |
|---|
| 560 | fields=1; |
|---|
| 561 | for (i=0; i<n->z_message_len; i++) { |
|---|
| 562 | if (n->z_message[i]=='\0') fields++; |
|---|
| 563 | } |
|---|
| 564 | |
|---|
| 565 | return(fields); |
|---|
| 566 | } |
|---|
| 567 | #else |
|---|
| 568 | int owl_zephyr_get_num_fields(const void *n) |
|---|
| 569 | { |
|---|
| 570 | return(0); |
|---|
| 571 | } |
|---|
| 572 | #endif |
|---|
| 573 | |
|---|
| 574 | #ifdef HAVE_LIBZEPHYR |
|---|
| 575 | /* return a pointer to the message, place the message length in k |
|---|
| 576 | * caller must free the return |
|---|
| 577 | */ |
|---|
| 578 | char *owl_zephyr_get_message(const ZNotice_t *n, const owl_message *m) |
|---|
| 579 | { |
|---|
| 580 | /* don't let ping messages have a body */ |
|---|
| 581 | if (!strcasecmp(n->z_opcode, "ping")) { |
|---|
| 582 | return(owl_strdup("")); |
|---|
| 583 | } |
|---|
| 584 | |
|---|
| 585 | /* deal with MIT NOC messages */ |
|---|
| 586 | if (!strcasecmp(n->z_default_format, "@center(@bold(NOC Message))\n\n@bold(Sender:) $1 <$sender>\n@bold(Time: ) $time\n\n@italic($opcode service on $instance $3.) $4\n")) { |
|---|
| 587 | char *msg, *field3, *field4; |
|---|
| 588 | |
|---|
| 589 | field3 = owl_zephyr_get_field(n, 3); |
|---|
| 590 | field4 = owl_zephyr_get_field(n, 4); |
|---|
| 591 | |
|---|
| 592 | msg = owl_sprintf("%s service on %s %s\n%s", n->z_opcode, n->z_class_inst, field3, field4); |
|---|
| 593 | owl_free(field3); |
|---|
| 594 | owl_free(field4); |
|---|
| 595 | if (msg) { |
|---|
| 596 | return msg; |
|---|
| 597 | } |
|---|
| 598 | } |
|---|
| 599 | /* deal with MIT Discuss messages */ |
|---|
| 600 | else if (!strcasecmp(n->z_default_format, "New transaction [$1] entered in $2\nFrom: $3 ($5)\nSubject: $4") || |
|---|
| 601 | !strcasecmp(n->z_default_format, "New transaction [$1] entered in $2\nFrom: $3\nSubject: $4")) { |
|---|
| 602 | char *msg, *field1, *field2, *field3, *field4, *field5; |
|---|
| 603 | |
|---|
| 604 | field1 = owl_zephyr_get_field(n, 1); |
|---|
| 605 | field2 = owl_zephyr_get_field(n, 2); |
|---|
| 606 | field3 = owl_zephyr_get_field(n, 3); |
|---|
| 607 | field4 = owl_zephyr_get_field(n, 4); |
|---|
| 608 | field5 = owl_zephyr_get_field(n, 5); |
|---|
| 609 | |
|---|
| 610 | msg = owl_sprintf("New transaction [%s] entered in %s\nFrom: %s (%s)\nSubject: %s", field1, field2, field3, field5, field4); |
|---|
| 611 | owl_free(field1); |
|---|
| 612 | owl_free(field2); |
|---|
| 613 | owl_free(field3); |
|---|
| 614 | owl_free(field4); |
|---|
| 615 | owl_free(field5); |
|---|
| 616 | if (msg) { |
|---|
| 617 | return msg; |
|---|
| 618 | } |
|---|
| 619 | } |
|---|
| 620 | /* deal with MIT Moira messages */ |
|---|
| 621 | else if (!strcasecmp(n->z_default_format, "MOIRA $instance on $fromhost:\n $message\n")) { |
|---|
| 622 | char *msg, *field1; |
|---|
| 623 | |
|---|
| 624 | field1 = owl_zephyr_get_field(n, 1); |
|---|
| 625 | |
|---|
| 626 | msg = owl_sprintf("MOIRA %s on %s: %s", n->z_class_inst, owl_message_get_hostname(m), field1); |
|---|
| 627 | owl_free(field1); |
|---|
| 628 | if (msg) { |
|---|
| 629 | return msg; |
|---|
| 630 | } |
|---|
| 631 | } |
|---|
| 632 | |
|---|
| 633 | if (owl_zephyr_get_num_fields(n) == 1) { |
|---|
| 634 | return(owl_zephyr_get_field(n, 1)); |
|---|
| 635 | } |
|---|
| 636 | else { |
|---|
| 637 | return(owl_zephyr_get_field(n, 2)); |
|---|
| 638 | } |
|---|
| 639 | } |
|---|
| 640 | #endif |
|---|
| 641 | |
|---|
| 642 | #ifdef HAVE_LIBZEPHYR |
|---|
| 643 | const char *owl_zephyr_get_zsig(const ZNotice_t *n, int *k) |
|---|
| 644 | { |
|---|
| 645 | /* return a pointer to the zsig if there is one */ |
|---|
| 646 | |
|---|
| 647 | /* message length 0? No zsig */ |
|---|
| 648 | if (n->z_message_len==0) { |
|---|
| 649 | *k=0; |
|---|
| 650 | return(""); |
|---|
| 651 | } |
|---|
| 652 | |
|---|
| 653 | /* If there's only one field, no zsig */ |
|---|
| 654 | if (owl_zephyr_get_num_fields(n) == 1) { |
|---|
| 655 | *k=0; |
|---|
| 656 | return(""); |
|---|
| 657 | } |
|---|
| 658 | |
|---|
| 659 | /* Everything else is field 1 */ |
|---|
| 660 | *k=strlen(n->z_message); |
|---|
| 661 | return(n->z_message); |
|---|
| 662 | } |
|---|
| 663 | #else |
|---|
| 664 | const char *owl_zephyr_get_zsig(const void *n, int *k) |
|---|
| 665 | { |
|---|
| 666 | return(""); |
|---|
| 667 | } |
|---|
| 668 | #endif |
|---|
| 669 | |
|---|
| 670 | int send_zephyr(const char *opcode, const char *zsig, const char *class, const char *instance, const char *recipient, const char *message) |
|---|
| 671 | { |
|---|
| 672 | #ifdef HAVE_LIBZEPHYR |
|---|
| 673 | int ret; |
|---|
| 674 | ZNotice_t notice; |
|---|
| 675 | |
|---|
| 676 | memset(¬ice, 0, sizeof(notice)); |
|---|
| 677 | |
|---|
| 678 | ZResetAuthentication(); |
|---|
| 679 | |
|---|
| 680 | if (!zsig) zsig=""; |
|---|
| 681 | |
|---|
| 682 | notice.z_kind=ACKED; |
|---|
| 683 | notice.z_port=0; |
|---|
| 684 | notice.z_class=zstr(class); |
|---|
| 685 | notice.z_class_inst=zstr(instance); |
|---|
| 686 | notice.z_sender=NULL; |
|---|
| 687 | if (!strcmp(recipient, "*") || !strcmp(recipient, "@")) { |
|---|
| 688 | notice.z_recipient=zstr(""); |
|---|
| 689 | if (*owl_global_get_zsender(&g)) |
|---|
| 690 | notice.z_sender=zstr(owl_global_get_zsender(&g)); |
|---|
| 691 | } else { |
|---|
| 692 | notice.z_recipient=zstr(recipient); |
|---|
| 693 | } |
|---|
| 694 | notice.z_default_format=zstr("Class $class, Instance $instance:\nTo: @bold($recipient) at $time $date\nFrom: @bold{$1 <$sender>}\n\n$2"); |
|---|
| 695 | if (opcode) notice.z_opcode=zstr(opcode); |
|---|
| 696 | |
|---|
| 697 | notice.z_message_len=strlen(zsig)+1+strlen(message); |
|---|
| 698 | notice.z_message=owl_malloc(notice.z_message_len+10); |
|---|
| 699 | strcpy(notice.z_message, zsig); |
|---|
| 700 | memcpy(notice.z_message+strlen(zsig)+1, message, strlen(message)); |
|---|
| 701 | |
|---|
| 702 | /* ret=ZSendNotice(¬ice, ZAUTH); */ |
|---|
| 703 | ret=ZSrvSendNotice(¬ice, ZAUTH, send_zephyr_helper); |
|---|
| 704 | |
|---|
| 705 | /* free then check the return */ |
|---|
| 706 | owl_free(notice.z_message); |
|---|
| 707 | ZFreeNotice(¬ice); |
|---|
| 708 | if (ret!=ZERR_NONE) { |
|---|
| 709 | owl_function_error("Error sending zephyr"); |
|---|
| 710 | return(ret); |
|---|
| 711 | } |
|---|
| 712 | return(0); |
|---|
| 713 | #else |
|---|
| 714 | return(0); |
|---|
| 715 | #endif |
|---|
| 716 | } |
|---|
| 717 | |
|---|
| 718 | #ifdef HAVE_LIBZEPHYR |
|---|
| 719 | Code_t send_zephyr_helper(ZNotice_t *notice, char *buf, int len, int wait) |
|---|
| 720 | { |
|---|
| 721 | return(ZSendPacket(buf, len, 0)); |
|---|
| 722 | } |
|---|
| 723 | #endif |
|---|
| 724 | |
|---|
| 725 | void send_ping(const char *to, const char *zclass, const char *zinstance) |
|---|
| 726 | { |
|---|
| 727 | #ifdef HAVE_LIBZEPHYR |
|---|
| 728 | send_zephyr("PING", "", zclass, zinstance, to, ""); |
|---|
| 729 | #endif |
|---|
| 730 | } |
|---|
| 731 | |
|---|
| 732 | #ifdef HAVE_LIBZEPHYR |
|---|
| 733 | void owl_zephyr_handle_ack(const ZNotice_t *retnotice) |
|---|
| 734 | { |
|---|
| 735 | char *tmp; |
|---|
| 736 | |
|---|
| 737 | /* if it's an HMACK ignore it */ |
|---|
| 738 | if (retnotice->z_kind == HMACK) return; |
|---|
| 739 | |
|---|
| 740 | if (retnotice->z_kind == SERVNAK) { |
|---|
| 741 | owl_function_error("Authorization failure sending zephyr"); |
|---|
| 742 | } else if ((retnotice->z_kind != SERVACK) || !retnotice->z_message_len) { |
|---|
| 743 | owl_function_error("Detected server failure while receiving acknowledgement"); |
|---|
| 744 | } else if (!strcmp(retnotice->z_message, ZSRVACK_SENT)) { |
|---|
| 745 | if (!strcasecmp(retnotice->z_opcode, "ping")) { |
|---|
| 746 | return; |
|---|
| 747 | } else { |
|---|
| 748 | if (strcasecmp(retnotice->z_recipient, "")) |
|---|
| 749 | { /* personal */ |
|---|
| 750 | tmp=short_zuser(retnotice->z_recipient); |
|---|
| 751 | if(!strcasecmp(retnotice->z_class, "message") && |
|---|
| 752 | !strcasecmp(retnotice->z_class_inst, "personal")) { |
|---|
| 753 | owl_function_makemsg("Message sent to %s.", tmp); |
|---|
| 754 | } else if(!strcasecmp(retnotice->z_class, "message")) { /* instanced, but not classed, personal */ |
|---|
| 755 | owl_function_makemsg("Message sent to %s on -i %s\n", tmp, retnotice->z_class_inst); |
|---|
| 756 | } else { /* classed personal */ |
|---|
| 757 | owl_function_makemsg("Message sent to %s on -c %s -i %s\n", tmp, retnotice->z_class, retnotice->z_class_inst); |
|---|
| 758 | } |
|---|
| 759 | owl_free(tmp); |
|---|
| 760 | } else { |
|---|
| 761 | /* class / instance message */ |
|---|
| 762 | owl_function_makemsg("Message sent to -c %s -i %s\n", retnotice->z_class, retnotice->z_class_inst); |
|---|
| 763 | } |
|---|
| 764 | } |
|---|
| 765 | } else if (!strcmp(retnotice->z_message, ZSRVACK_NOTSENT)) { |
|---|
| 766 | #define BUFFLEN 1024 |
|---|
| 767 | if (retnotice->z_recipient == NULL |
|---|
| 768 | || *retnotice->z_recipient == 0 |
|---|
| 769 | || *retnotice->z_recipient == '@') { |
|---|
| 770 | char buff[BUFFLEN]; |
|---|
| 771 | owl_function_error("No one subscribed to class %s", retnotice->z_class); |
|---|
| 772 | snprintf(buff, BUFFLEN, "Could not send message to class %s: no one subscribed.\n", retnotice->z_class); |
|---|
| 773 | owl_function_adminmsg("", buff); |
|---|
| 774 | } else { |
|---|
| 775 | char buff[BUFFLEN]; |
|---|
| 776 | owl_zwrite zw; |
|---|
| 777 | char *realm; |
|---|
| 778 | |
|---|
| 779 | tmp = short_zuser(retnotice->z_recipient); |
|---|
| 780 | owl_function_error("%s: Not logged in or subscribing.", tmp); |
|---|
| 781 | /* |
|---|
| 782 | * These error messages are often over 80 chars, but users who want to |
|---|
| 783 | * see the whole thing can scroll to the side, and for those with wide |
|---|
| 784 | * terminals or who don't care, not splitting saves a line in the UI |
|---|
| 785 | */ |
|---|
| 786 | if(strcasecmp(retnotice->z_class, "message")) { |
|---|
| 787 | snprintf(buff, BUFFLEN, |
|---|
| 788 | "Could not send message to %s: " |
|---|
| 789 | "not logged in or subscribing to class %s, instance %s.\n", |
|---|
| 790 | tmp, |
|---|
| 791 | retnotice->z_class, |
|---|
| 792 | retnotice->z_class_inst); |
|---|
| 793 | } else if(strcasecmp(retnotice->z_class_inst, "personal")) { |
|---|
| 794 | snprintf(buff, BUFFLEN, |
|---|
| 795 | "Could not send message to %s: " |
|---|
| 796 | "not logged in or subscribing to instance %s.\n", |
|---|
| 797 | tmp, |
|---|
| 798 | retnotice->z_class_inst); |
|---|
| 799 | } else { |
|---|
| 800 | snprintf(buff, BUFFLEN, |
|---|
| 801 | "Could not send message to %s: " |
|---|
| 802 | "not logged in or subscribing to messages.\n", |
|---|
| 803 | tmp); |
|---|
| 804 | } |
|---|
| 805 | owl_function_adminmsg("", buff); |
|---|
| 806 | |
|---|
| 807 | memset(&zw, 0, sizeof(zw)); |
|---|
| 808 | zw.class = owl_strdup(retnotice->z_class); |
|---|
| 809 | zw.inst = owl_strdup(retnotice->z_class_inst); |
|---|
| 810 | realm = strchr(retnotice->z_recipient, '@'); |
|---|
| 811 | if(realm) { |
|---|
| 812 | zw.realm = owl_strdup(realm + 1); |
|---|
| 813 | } else { |
|---|
| 814 | zw.realm = owl_strdup(owl_zephyr_get_realm()); |
|---|
| 815 | } |
|---|
| 816 | zw.opcode = owl_strdup(retnotice->z_opcode); |
|---|
| 817 | zw.zsig = owl_strdup(""); |
|---|
| 818 | owl_list_create(&(zw.recips)); |
|---|
| 819 | owl_list_append_element(&(zw.recips), owl_strdup(tmp)); |
|---|
| 820 | |
|---|
| 821 | owl_log_outgoing_zephyr_error(&zw, buff); |
|---|
| 822 | |
|---|
| 823 | owl_zwrite_free(&zw); |
|---|
| 824 | owl_free(tmp); |
|---|
| 825 | } |
|---|
| 826 | } else { |
|---|
| 827 | owl_function_error("Internal error on ack (%s)", retnotice->z_message); |
|---|
| 828 | } |
|---|
| 829 | } |
|---|
| 830 | #else |
|---|
| 831 | void owl_zephyr_handle_ack(const void *retnotice) |
|---|
| 832 | { |
|---|
| 833 | } |
|---|
| 834 | #endif |
|---|
| 835 | |
|---|
| 836 | #ifdef HAVE_LIBZEPHYR |
|---|
| 837 | int owl_zephyr_notice_is_ack(const ZNotice_t *n) |
|---|
| 838 | { |
|---|
| 839 | if (n->z_kind == SERVNAK || n->z_kind == SERVACK || n->z_kind == HMACK) { |
|---|
| 840 | if (!strcasecmp(n->z_class, LOGIN_CLASS)) return(0); |
|---|
| 841 | return(1); |
|---|
| 842 | } |
|---|
| 843 | return(0); |
|---|
| 844 | } |
|---|
| 845 | #else |
|---|
| 846 | int owl_zephyr_notice_is_ack(const void *n) |
|---|
| 847 | { |
|---|
| 848 | return(0); |
|---|
| 849 | } |
|---|
| 850 | #endif |
|---|
| 851 | |
|---|
| 852 | void owl_zephyr_zaway(const owl_message *m) |
|---|
| 853 | { |
|---|
| 854 | #ifdef HAVE_LIBZEPHYR |
|---|
| 855 | char *tmpbuff, *myuser, *to; |
|---|
| 856 | owl_message *mout; |
|---|
| 857 | |
|---|
| 858 | /* bail if it doesn't look like a message we should reply to. Some |
|---|
| 859 | * of this defined by the way zaway(1) works |
|---|
| 860 | */ |
|---|
| 861 | if (strcasecmp(owl_message_get_class(m), "message")) return; |
|---|
| 862 | if (strcasecmp(owl_message_get_recipient(m), ZGetSender())) return; |
|---|
| 863 | if (!strcasecmp(owl_message_get_sender(m), "")) return; |
|---|
| 864 | if (!strcasecmp(owl_message_get_opcode(m), "ping")) return; |
|---|
| 865 | if (!strcasecmp(owl_message_get_opcode(m), "auto")) return; |
|---|
| 866 | if (!strcasecmp(owl_message_get_zsig(m), "Automated reply:")) return; |
|---|
| 867 | if (!strcasecmp(owl_message_get_sender(m), ZGetSender())) return; |
|---|
| 868 | if (owl_message_get_attribute_value(m, "isauto")) return; |
|---|
| 869 | |
|---|
| 870 | if (owl_global_is_smartstrip(&g)) { |
|---|
| 871 | to=owl_zephyr_smartstripped_user(owl_message_get_sender(m)); |
|---|
| 872 | } else { |
|---|
| 873 | to=owl_strdup(owl_message_get_sender(m)); |
|---|
| 874 | } |
|---|
| 875 | |
|---|
| 876 | send_zephyr("", |
|---|
| 877 | "Automated reply:", |
|---|
| 878 | owl_message_get_class(m), |
|---|
| 879 | owl_message_get_instance(m), |
|---|
| 880 | to, |
|---|
| 881 | owl_global_get_zaway_msg(&g)); |
|---|
| 882 | |
|---|
| 883 | myuser=short_zuser(to); |
|---|
| 884 | if (!strcasecmp(owl_message_get_instance(m), "personal")) { |
|---|
| 885 | tmpbuff = owl_sprintf("zwrite %s", myuser); |
|---|
| 886 | } else { |
|---|
| 887 | tmpbuff = owl_sprintf("zwrite -i %s %s", owl_message_get_instance(m), myuser); |
|---|
| 888 | } |
|---|
| 889 | owl_free(myuser); |
|---|
| 890 | owl_free(to); |
|---|
| 891 | |
|---|
| 892 | /* display the message as an admin message in the receive window */ |
|---|
| 893 | mout=owl_function_make_outgoing_zephyr(owl_global_get_zaway_msg(&g), tmpbuff, "Automated reply:"); |
|---|
| 894 | owl_global_messagequeue_addmsg(&g, mout); |
|---|
| 895 | owl_free(tmpbuff); |
|---|
| 896 | #endif |
|---|
| 897 | } |
|---|
| 898 | |
|---|
| 899 | #ifdef HAVE_LIBZEPHYR |
|---|
| 900 | void owl_zephyr_hackaway_cr(ZNotice_t *n) |
|---|
| 901 | { |
|---|
| 902 | /* replace \r's with ' '. Gross-ish */ |
|---|
| 903 | int i; |
|---|
| 904 | |
|---|
| 905 | for (i=0; i<n->z_message_len; i++) { |
|---|
| 906 | if (n->z_message[i]=='\r') { |
|---|
| 907 | n->z_message[i]=' '; |
|---|
| 908 | } |
|---|
| 909 | } |
|---|
| 910 | } |
|---|
| 911 | #endif |
|---|
| 912 | |
|---|
| 913 | void owl_zephyr_zlocate(const char *user, char *out, int auth) |
|---|
| 914 | { |
|---|
| 915 | #ifdef HAVE_LIBZEPHYR |
|---|
| 916 | int ret, numlocs; |
|---|
| 917 | int one = 1; |
|---|
| 918 | ZLocations_t locations; |
|---|
| 919 | char *myuser; |
|---|
| 920 | |
|---|
| 921 | strcpy(out, ""); |
|---|
| 922 | ZResetAuthentication(); |
|---|
| 923 | ret=ZLocateUser(zstr(user),&numlocs,auth?ZAUTH:ZNOAUTH); |
|---|
| 924 | if (ret != ZERR_NONE) { |
|---|
| 925 | sprintf(out, "Error locating user %s\n", user); |
|---|
| 926 | return; |
|---|
| 927 | } |
|---|
| 928 | |
|---|
| 929 | if (numlocs==0) { |
|---|
| 930 | myuser=short_zuser(user); |
|---|
| 931 | sprintf(out, "%s: Hidden or not logged in\n", myuser); |
|---|
| 932 | owl_free(myuser); |
|---|
| 933 | return; |
|---|
| 934 | } |
|---|
| 935 | |
|---|
| 936 | for (;numlocs;numlocs--) { |
|---|
| 937 | ZGetLocations(&locations,&one); |
|---|
| 938 | myuser=short_zuser(user); |
|---|
| 939 | sprintf(out + strlen(out), "%s: %s\t%s\t%s\n", myuser, |
|---|
| 940 | locations.host ? locations.host : "?", |
|---|
| 941 | locations.tty ? locations.tty : "?", |
|---|
| 942 | locations.time ? locations.time : "?"); |
|---|
| 943 | owl_free(myuser); |
|---|
| 944 | } |
|---|
| 945 | #endif |
|---|
| 946 | } |
|---|
| 947 | |
|---|
| 948 | void owl_zephyr_addsub(const char *filename, const char *class, const char *inst, const char *recip) |
|---|
| 949 | { |
|---|
| 950 | #ifdef HAVE_LIBZEPHYR |
|---|
| 951 | char *line, subsfile[LINE], buff[LINE]; |
|---|
| 952 | FILE *file; |
|---|
| 953 | |
|---|
| 954 | line=owl_zephyr_makesubline(class, inst, recip); |
|---|
| 955 | |
|---|
| 956 | if (filename==NULL) { |
|---|
| 957 | sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".zephyr.subs"); |
|---|
| 958 | } else { |
|---|
| 959 | strcpy(subsfile, filename); |
|---|
| 960 | } |
|---|
| 961 | |
|---|
| 962 | /* if the file already exists, check to see if the sub is already there */ |
|---|
| 963 | file=fopen(subsfile, "r"); |
|---|
| 964 | if (file) { |
|---|
| 965 | while (fgets(buff, LINE, file)!=NULL) { |
|---|
| 966 | if (!strcasecmp(buff, line)) { |
|---|
| 967 | owl_function_error("Subscription already present in %s", subsfile); |
|---|
| 968 | owl_free(line); |
|---|
| 969 | fclose(file); |
|---|
| 970 | return; |
|---|
| 971 | } |
|---|
| 972 | } |
|---|
| 973 | fclose(file); |
|---|
| 974 | } |
|---|
| 975 | |
|---|
| 976 | /* if we get here then we didn't find it */ |
|---|
| 977 | file=fopen(subsfile, "a"); |
|---|
| 978 | if (!file) { |
|---|
| 979 | owl_function_error("Error opening file %s for writing", subsfile); |
|---|
| 980 | owl_free(line); |
|---|
| 981 | return; |
|---|
| 982 | } |
|---|
| 983 | fputs(line, file); |
|---|
| 984 | fclose(file); |
|---|
| 985 | owl_function_makemsg("Subscription added"); |
|---|
| 986 | |
|---|
| 987 | owl_free(line); |
|---|
| 988 | #endif |
|---|
| 989 | } |
|---|
| 990 | |
|---|
| 991 | void owl_zephyr_delsub(const char *filename, const char *class, const char *inst, const char *recip) |
|---|
| 992 | { |
|---|
| 993 | #ifdef HAVE_LIBZEPHYR |
|---|
| 994 | char *line, *subsfile; |
|---|
| 995 | |
|---|
| 996 | line=owl_zephyr_makesubline(class, inst, recip); |
|---|
| 997 | line[strlen(line)-1]='\0'; |
|---|
| 998 | |
|---|
| 999 | if (!filename) { |
|---|
| 1000 | subsfile=owl_sprintf("%s/.zephyr.subs", owl_global_get_homedir(&g)); |
|---|
| 1001 | } else { |
|---|
| 1002 | subsfile=owl_strdup(filename); |
|---|
| 1003 | } |
|---|
| 1004 | |
|---|
| 1005 | owl_util_file_deleteline(subsfile, line, 1); |
|---|
| 1006 | owl_free(subsfile); |
|---|
| 1007 | owl_free(line); |
|---|
| 1008 | owl_function_makemsg("Subscription removed"); |
|---|
| 1009 | #endif |
|---|
| 1010 | } |
|---|
| 1011 | |
|---|
| 1012 | /* caller must free the return */ |
|---|
| 1013 | char *owl_zephyr_makesubline(const char *class, const char *inst, const char *recip) |
|---|
| 1014 | { |
|---|
| 1015 | return owl_sprintf("%s,%s,%s\n", class, inst, !strcmp(recip, "") ? "*" : recip); |
|---|
| 1016 | } |
|---|
| 1017 | |
|---|
| 1018 | |
|---|
| 1019 | void owl_zephyr_zlog_in(void) |
|---|
| 1020 | { |
|---|
| 1021 | #ifdef HAVE_LIBZEPHYR |
|---|
| 1022 | const char *exposure, *eset; |
|---|
| 1023 | int ret; |
|---|
| 1024 | |
|---|
| 1025 | ZResetAuthentication(); |
|---|
| 1026 | |
|---|
| 1027 | eset=EXPOSE_REALMVIS; |
|---|
| 1028 | exposure=ZGetVariable(zstr("exposure")); |
|---|
| 1029 | if (exposure==NULL) { |
|---|
| 1030 | eset=EXPOSE_REALMVIS; |
|---|
| 1031 | } else if (!strcasecmp(exposure,EXPOSE_NONE)) { |
|---|
| 1032 | eset = EXPOSE_NONE; |
|---|
| 1033 | } else if (!strcasecmp(exposure,EXPOSE_OPSTAFF)) { |
|---|
| 1034 | eset = EXPOSE_OPSTAFF; |
|---|
| 1035 | } else if (!strcasecmp(exposure,EXPOSE_REALMVIS)) { |
|---|
| 1036 | eset = EXPOSE_REALMVIS; |
|---|
| 1037 | } else if (!strcasecmp(exposure,EXPOSE_REALMANN)) { |
|---|
| 1038 | eset = EXPOSE_REALMANN; |
|---|
| 1039 | } else if (!strcasecmp(exposure,EXPOSE_NETVIS)) { |
|---|
| 1040 | eset = EXPOSE_NETVIS; |
|---|
| 1041 | } else if (!strcasecmp(exposure,EXPOSE_NETANN)) { |
|---|
| 1042 | eset = EXPOSE_NETANN; |
|---|
| 1043 | } |
|---|
| 1044 | |
|---|
| 1045 | ret=ZSetLocation(zstr(eset)); |
|---|
| 1046 | if (ret != ZERR_NONE) { |
|---|
| 1047 | /* |
|---|
| 1048 | char buff[LINE]; |
|---|
| 1049 | sprintf(buff, "Error setting location: %s", error_message(ret)); |
|---|
| 1050 | owl_function_makemsg(buff); |
|---|
| 1051 | */ |
|---|
| 1052 | } |
|---|
| 1053 | #endif |
|---|
| 1054 | } |
|---|
| 1055 | |
|---|
| 1056 | void owl_zephyr_zlog_out(void) |
|---|
| 1057 | { |
|---|
| 1058 | #ifdef HAVE_LIBZEPHYR |
|---|
| 1059 | int ret; |
|---|
| 1060 | |
|---|
| 1061 | ZResetAuthentication(); |
|---|
| 1062 | ret=ZUnsetLocation(); |
|---|
| 1063 | if (ret != ZERR_NONE) { |
|---|
| 1064 | /* |
|---|
| 1065 | char buff[LINE]; |
|---|
| 1066 | sprintf(buff, "Error unsetting location: %s", error_message(ret)); |
|---|
| 1067 | owl_function_makemsg(buff); |
|---|
| 1068 | */ |
|---|
| 1069 | } |
|---|
| 1070 | #endif |
|---|
| 1071 | } |
|---|
| 1072 | |
|---|
| 1073 | void owl_zephyr_addbuddy(const char *name) |
|---|
| 1074 | { |
|---|
| 1075 | char *filename; |
|---|
| 1076 | FILE *file; |
|---|
| 1077 | |
|---|
| 1078 | filename=owl_sprintf("%s/.anyone", owl_global_get_homedir(&g)); |
|---|
| 1079 | file=fopen(filename, "a"); |
|---|
| 1080 | owl_free(filename); |
|---|
| 1081 | if (!file) { |
|---|
| 1082 | owl_function_error("Error opening zephyr buddy file for append"); |
|---|
| 1083 | return; |
|---|
| 1084 | } |
|---|
| 1085 | fprintf(file, "%s\n", name); |
|---|
| 1086 | fclose(file); |
|---|
| 1087 | } |
|---|
| 1088 | |
|---|
| 1089 | void owl_zephyr_delbuddy(const char *name) |
|---|
| 1090 | { |
|---|
| 1091 | char *filename; |
|---|
| 1092 | |
|---|
| 1093 | filename=owl_sprintf("%s/.anyone", owl_global_get_homedir(&g)); |
|---|
| 1094 | owl_util_file_deleteline(filename, name, 0); |
|---|
| 1095 | owl_free(filename); |
|---|
| 1096 | } |
|---|
| 1097 | |
|---|
| 1098 | /* return auth string */ |
|---|
| 1099 | #ifdef HAVE_LIBZEPHYR |
|---|
| 1100 | const char *owl_zephyr_get_authstr(const ZNotice_t *n) |
|---|
| 1101 | { |
|---|
| 1102 | |
|---|
| 1103 | if (!n) return("UNKNOWN"); |
|---|
| 1104 | |
|---|
| 1105 | if (n->z_auth == ZAUTH_FAILED) { |
|---|
| 1106 | return ("FAILED"); |
|---|
| 1107 | } else if (n->z_auth == ZAUTH_NO) { |
|---|
| 1108 | return ("NO"); |
|---|
| 1109 | } else if (n->z_auth == ZAUTH_YES) { |
|---|
| 1110 | return ("YES"); |
|---|
| 1111 | } else { |
|---|
| 1112 | return ("UNKNOWN"); |
|---|
| 1113 | } |
|---|
| 1114 | } |
|---|
| 1115 | #else |
|---|
| 1116 | const char *owl_zephyr_get_authstr(const void *n) |
|---|
| 1117 | { |
|---|
| 1118 | return(""); |
|---|
| 1119 | } |
|---|
| 1120 | #endif |
|---|
| 1121 | |
|---|
| 1122 | /* Returns a buffer of subscriptions or an error message. Caller must |
|---|
| 1123 | * free the return. |
|---|
| 1124 | */ |
|---|
| 1125 | char *owl_zephyr_getsubs() |
|---|
| 1126 | { |
|---|
| 1127 | #ifdef HAVE_LIBZEPHYR |
|---|
| 1128 | int ret, num, i, one; |
|---|
| 1129 | int buffsize; |
|---|
| 1130 | ZSubscription_t sub; |
|---|
| 1131 | char *out; |
|---|
| 1132 | one=1; |
|---|
| 1133 | |
|---|
| 1134 | ret=ZRetrieveSubscriptions(0, &num); |
|---|
| 1135 | if (ret==ZERR_TOOMANYSUBS) { |
|---|
| 1136 | return(owl_strdup("Zephyr: too many subscriptions\n")); |
|---|
| 1137 | } else if (ret || (num <= 0)) { |
|---|
| 1138 | return(owl_strdup("Zephyr: error retriving subscriptions\n")); |
|---|
| 1139 | } |
|---|
| 1140 | |
|---|
| 1141 | buffsize = (num + 1) * 50; |
|---|
| 1142 | out=owl_malloc(buffsize); |
|---|
| 1143 | strcpy(out, ""); |
|---|
| 1144 | for (i=0; i<num; i++) { |
|---|
| 1145 | if ((ret = ZGetSubscriptions(&sub, &one)) != ZERR_NONE) { |
|---|
| 1146 | owl_free(out); |
|---|
| 1147 | ZFlushSubscriptions(); |
|---|
| 1148 | out=owl_strdup("Error while getting subscriptions\n"); |
|---|
| 1149 | return(out); |
|---|
| 1150 | } else { |
|---|
| 1151 | int tmpbufflen; |
|---|
| 1152 | char *tmpbuff; |
|---|
| 1153 | tmpbuff = owl_sprintf("<%s,%s,%s>\n%s", sub.zsub_class, sub.zsub_classinst, sub.zsub_recipient, out); |
|---|
| 1154 | tmpbufflen = strlen(tmpbuff) + 1; |
|---|
| 1155 | if (tmpbufflen > buffsize) { |
|---|
| 1156 | char *out2; |
|---|
| 1157 | buffsize = tmpbufflen * 2; |
|---|
| 1158 | out2 = owl_realloc(out, buffsize); |
|---|
| 1159 | if (out2 == NULL) { |
|---|
| 1160 | owl_free(out); |
|---|
| 1161 | owl_free(tmpbuff); |
|---|
| 1162 | ZFlushSubscriptions(); |
|---|
| 1163 | out=owl_strdup("Realloc error while getting subscriptions\n"); |
|---|
| 1164 | return(out); |
|---|
| 1165 | } |
|---|
| 1166 | out = out2; |
|---|
| 1167 | } |
|---|
| 1168 | strcpy(out, tmpbuff); |
|---|
| 1169 | owl_free(tmpbuff); |
|---|
| 1170 | } |
|---|
| 1171 | } |
|---|
| 1172 | |
|---|
| 1173 | ZFlushSubscriptions(); |
|---|
| 1174 | return(out); |
|---|
| 1175 | #else |
|---|
| 1176 | return(owl_strdup("Zephyr not available")); |
|---|
| 1177 | #endif |
|---|
| 1178 | } |
|---|
| 1179 | |
|---|
| 1180 | const char *owl_zephyr_get_variable(const char *var) |
|---|
| 1181 | { |
|---|
| 1182 | #ifdef HAVE_LIBZEPHYR |
|---|
| 1183 | return(ZGetVariable(zstr(var))); |
|---|
| 1184 | #else |
|---|
| 1185 | return(""); |
|---|
| 1186 | #endif |
|---|
| 1187 | } |
|---|
| 1188 | |
|---|
| 1189 | void owl_zephyr_set_locationinfo(const char *host, const char *val) |
|---|
| 1190 | { |
|---|
| 1191 | #ifdef HAVE_LIBZEPHYR |
|---|
| 1192 | ZInitLocationInfo(zstr(host), zstr(val)); |
|---|
| 1193 | #endif |
|---|
| 1194 | } |
|---|
| 1195 | |
|---|
| 1196 | /* Strip a local realm fron the zephyr user name. |
|---|
| 1197 | * The caller must free the return |
|---|
| 1198 | */ |
|---|
| 1199 | char *short_zuser(const char *in) |
|---|
| 1200 | { |
|---|
| 1201 | char *out, *ptr; |
|---|
| 1202 | |
|---|
| 1203 | out=owl_strdup(in); |
|---|
| 1204 | ptr=strchr(out, '@'); |
|---|
| 1205 | if (ptr) { |
|---|
| 1206 | if (!strcasecmp(ptr+1, owl_zephyr_get_realm())) { |
|---|
| 1207 | *ptr='\0'; |
|---|
| 1208 | } |
|---|
| 1209 | } |
|---|
| 1210 | return(out); |
|---|
| 1211 | } |
|---|
| 1212 | |
|---|
| 1213 | /* Append a local realm to the zephyr user name if necessary. |
|---|
| 1214 | * The caller must free the return. |
|---|
| 1215 | */ |
|---|
| 1216 | char *long_zuser(const char *in) |
|---|
| 1217 | { |
|---|
| 1218 | if (strchr(in, '@')) { |
|---|
| 1219 | return(owl_strdup(in)); |
|---|
| 1220 | } |
|---|
| 1221 | return(owl_sprintf("%s@%s", in, owl_zephyr_get_realm())); |
|---|
| 1222 | } |
|---|
| 1223 | |
|---|
| 1224 | /* strip out the instance from a zsender's principal. Preserves the |
|---|
| 1225 | * realm if present. daemon.webzephyr is a special case. The |
|---|
| 1226 | * caller must free the return |
|---|
| 1227 | */ |
|---|
| 1228 | char *owl_zephyr_smartstripped_user(const char *in) |
|---|
| 1229 | { |
|---|
| 1230 | char *ptr, *realm, *out; |
|---|
| 1231 | |
|---|
| 1232 | out=owl_strdup(in); |
|---|
| 1233 | |
|---|
| 1234 | /* bail immeaditly if we don't have to do any work */ |
|---|
| 1235 | ptr=strchr(out, '.'); |
|---|
| 1236 | if (!strchr(out, '/') && !ptr) { |
|---|
| 1237 | /* no '/' and no '.' */ |
|---|
| 1238 | return(out); |
|---|
| 1239 | } |
|---|
| 1240 | if (ptr && strchr(out, '@') && (ptr > strchr(out, '@'))) { |
|---|
| 1241 | /* There's a '.' but it's in the realm */ |
|---|
| 1242 | return(out); |
|---|
| 1243 | } |
|---|
| 1244 | if (!strncasecmp(out, OWL_WEBZEPHYR_PRINCIPAL, strlen(OWL_WEBZEPHYR_PRINCIPAL))) { |
|---|
| 1245 | return(out); |
|---|
| 1246 | } |
|---|
| 1247 | |
|---|
| 1248 | /* remove the realm from out, but hold on to it */ |
|---|
| 1249 | realm=strchr(out, '@'); |
|---|
| 1250 | if (realm) realm[0]='\0'; |
|---|
| 1251 | |
|---|
| 1252 | /* strip */ |
|---|
| 1253 | ptr=strchr(out, '.'); |
|---|
| 1254 | if (!ptr) ptr=strchr(out, '/'); |
|---|
| 1255 | ptr[0]='\0'; |
|---|
| 1256 | |
|---|
| 1257 | /* reattach the realm if we had one */ |
|---|
| 1258 | if (realm) { |
|---|
| 1259 | strcat(out, "@"); |
|---|
| 1260 | strcat(out, realm+1); |
|---|
| 1261 | } |
|---|
| 1262 | |
|---|
| 1263 | return(out); |
|---|
| 1264 | } |
|---|
| 1265 | |
|---|
| 1266 | /* read the list of users in 'filename' as a .anyone file, and put the |
|---|
| 1267 | * names of the zephyr users in the list 'in'. If 'filename' is NULL, |
|---|
| 1268 | * use the default .anyone file in the users home directory. Returns |
|---|
| 1269 | * -1 on failure, 0 on success. |
|---|
| 1270 | */ |
|---|
| 1271 | int owl_zephyr_get_anyone_list(owl_list *in, const char *filename) |
|---|
| 1272 | { |
|---|
| 1273 | #ifdef HAVE_LIBZEPHYR |
|---|
| 1274 | char *ourfile, *tmp, buff[LINE]; |
|---|
| 1275 | FILE *f; |
|---|
| 1276 | |
|---|
| 1277 | if (filename==NULL) { |
|---|
| 1278 | ourfile=owl_sprintf("%s/.anyone", owl_global_get_homedir(&g)); |
|---|
| 1279 | } else { |
|---|
| 1280 | ourfile=owl_strdup(filename); |
|---|
| 1281 | } |
|---|
| 1282 | |
|---|
| 1283 | f=fopen(ourfile, "r"); |
|---|
| 1284 | if (!f) { |
|---|
| 1285 | owl_function_error("Error opening file %s: %s", ourfile, strerror(errno) ? strerror(errno) : ""); |
|---|
| 1286 | owl_free(ourfile); |
|---|
| 1287 | return(-1); |
|---|
| 1288 | } |
|---|
| 1289 | |
|---|
| 1290 | while (fgets(buff, LINE, f)!=NULL) { |
|---|
| 1291 | /* ignore comments, blank lines etc. */ |
|---|
| 1292 | if (buff[0]=='#') continue; |
|---|
| 1293 | if (buff[0]=='\n') continue; |
|---|
| 1294 | if (buff[0]=='\0') continue; |
|---|
| 1295 | |
|---|
| 1296 | /* strip the \n */ |
|---|
| 1297 | buff[strlen(buff)-1]='\0'; |
|---|
| 1298 | |
|---|
| 1299 | /* ingore from # on */ |
|---|
| 1300 | tmp=strchr(buff, '#'); |
|---|
| 1301 | if (tmp) tmp[0]='\0'; |
|---|
| 1302 | |
|---|
| 1303 | /* ingore from SPC */ |
|---|
| 1304 | tmp=strchr(buff, ' '); |
|---|
| 1305 | if (tmp) tmp[0]='\0'; |
|---|
| 1306 | |
|---|
| 1307 | /* stick on the local realm. */ |
|---|
| 1308 | if (!strchr(buff, '@')) { |
|---|
| 1309 | strcat(buff, "@"); |
|---|
| 1310 | strcat(buff, ZGetRealm()); |
|---|
| 1311 | } |
|---|
| 1312 | owl_list_append_element(in, owl_strdup(buff)); |
|---|
| 1313 | } |
|---|
| 1314 | fclose(f); |
|---|
| 1315 | owl_free(ourfile); |
|---|
| 1316 | return(0); |
|---|
| 1317 | #else |
|---|
| 1318 | return(-1); |
|---|
| 1319 | #endif |
|---|
| 1320 | } |
|---|
| 1321 | |
|---|
| 1322 | #ifdef HAVE_LIBZEPHYR |
|---|
| 1323 | void owl_zephyr_process_events(owl_dispatch *d) { |
|---|
| 1324 | int zpendcount=0; |
|---|
| 1325 | ZNotice_t notice; |
|---|
| 1326 | struct sockaddr_in from; |
|---|
| 1327 | owl_message *m=NULL; |
|---|
| 1328 | |
|---|
| 1329 | while(owl_zephyr_zpending() && zpendcount < 20) { |
|---|
| 1330 | if (owl_zephyr_zpending()) { |
|---|
| 1331 | ZReceiveNotice(¬ice, &from); |
|---|
| 1332 | zpendcount++; |
|---|
| 1333 | |
|---|
| 1334 | /* is this an ack from a zephyr we sent? */ |
|---|
| 1335 | if (owl_zephyr_notice_is_ack(¬ice)) { |
|---|
| 1336 | owl_zephyr_handle_ack(¬ice); |
|---|
| 1337 | continue; |
|---|
| 1338 | } |
|---|
| 1339 | |
|---|
| 1340 | /* if it's a ping and we're not viewing pings then skip it */ |
|---|
| 1341 | if (!owl_global_is_rxping(&g) && !strcasecmp(notice.z_opcode, "ping")) { |
|---|
| 1342 | continue; |
|---|
| 1343 | } |
|---|
| 1344 | |
|---|
| 1345 | /* create the new message */ |
|---|
| 1346 | m=owl_malloc(sizeof(owl_message)); |
|---|
| 1347 | owl_message_create_from_znotice(m, ¬ice); |
|---|
| 1348 | |
|---|
| 1349 | owl_global_messagequeue_addmsg(&g, m); |
|---|
| 1350 | } |
|---|
| 1351 | } |
|---|
| 1352 | } |
|---|
| 1353 | |
|---|
| 1354 | #else |
|---|
| 1355 | void owl_zephyr_process_events(owl_dispatch *d) { |
|---|
| 1356 | |
|---|
| 1357 | } |
|---|
| 1358 | #endif |
|---|