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