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