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