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