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