[f1e629d] | 1 | #include <stdio.h> |
---|
| 2 | #include <stdlib.h> |
---|
| 3 | #include <string.h> |
---|
| 4 | #include <sys/types.h> |
---|
| 5 | #include <sys/stat.h> |
---|
| 6 | #include <errno.h> |
---|
[908e388] | 7 | #define OWL_PERL |
---|
[f1e629d] | 8 | #include "owl.h" |
---|
| 9 | |
---|
| 10 | static const char fileIdent[] = "$Id$"; |
---|
| 11 | |
---|
| 12 | extern char *owl_perlwrap_codebuff; |
---|
| 13 | |
---|
| 14 | extern XS(boot_owl); |
---|
[908e388] | 15 | extern XS(boot_DynaLoader); |
---|
[6922edd] | 16 | // extern XS(boot_DBI); |
---|
[f1e629d] | 17 | |
---|
[c3acb0b] | 18 | static void owl_perl_xs_init(pTHX) |
---|
| 19 | { |
---|
[f1e629d] | 20 | char *file = __FILE__; |
---|
| 21 | dXSUB_SYS; |
---|
| 22 | { |
---|
| 23 | newXS("owl::bootstrap", boot_owl, file); |
---|
[908e388] | 24 | newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file); |
---|
[f1e629d] | 25 | } |
---|
| 26 | } |
---|
| 27 | |
---|
[c3acb0b] | 28 | SV *owl_perlconfig_message2hashref(owl_message *m) /*noproto*/ |
---|
| 29 | { |
---|
[f1e629d] | 30 | HV *h; |
---|
| 31 | SV *hr; |
---|
[b0430a6] | 32 | char *ptr, *blessas; |
---|
| 33 | int i, j; |
---|
[f1e629d] | 34 | |
---|
| 35 | if (!m) return &PL_sv_undef; |
---|
| 36 | h = newHV(); |
---|
| 37 | |
---|
| 38 | #define MSG2H(h,field) hv_store(h, #field, strlen(#field), \ |
---|
| 39 | newSVpv(owl_message_get_##field(m),0), 0) |
---|
| 40 | |
---|
| 41 | if (owl_message_is_type_zephyr(m) |
---|
| 42 | && owl_message_is_direction_in(m)) { |
---|
| 43 | /* Handle zephyr-specific fields... */ |
---|
| 44 | AV *av_zfields; |
---|
| 45 | |
---|
| 46 | av_zfields = newAV(); |
---|
| 47 | j=owl_zephyr_get_num_fields(owl_message_get_notice(m)); |
---|
| 48 | for (i=0; i<j; i++) { |
---|
[b0430a6] | 49 | ptr=owl_zephyr_get_field(owl_message_get_notice(m), i+1); |
---|
| 50 | av_push(av_zfields, newSVpvn(ptr, strlen(ptr))); |
---|
| 51 | owl_free(ptr); |
---|
[f1e629d] | 52 | } |
---|
| 53 | hv_store(h, "fields", strlen("fields"), newRV_noinc((SV*)av_zfields), 0); |
---|
| 54 | |
---|
| 55 | hv_store(h, "auth", strlen("auth"), |
---|
| 56 | newSVpv(owl_zephyr_get_authstr(owl_message_get_notice(m)),0),0); |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | MSG2H(h, type); |
---|
| 60 | MSG2H(h, direction); |
---|
| 61 | MSG2H(h, class); |
---|
| 62 | MSG2H(h, instance); |
---|
| 63 | MSG2H(h, sender); |
---|
| 64 | MSG2H(h, realm); |
---|
| 65 | MSG2H(h, recipient); |
---|
| 66 | MSG2H(h, opcode); |
---|
| 67 | MSG2H(h, hostname); |
---|
| 68 | MSG2H(h, body); |
---|
| 69 | MSG2H(h, login); |
---|
| 70 | MSG2H(h, zsig); |
---|
| 71 | MSG2H(h, zwriteline); |
---|
| 72 | if (owl_message_get_header(m)) { |
---|
| 73 | MSG2H(h, header); |
---|
| 74 | } |
---|
| 75 | hv_store(h, "time", strlen("time"), newSVpv(owl_message_get_timestr(m),0),0); |
---|
| 76 | hv_store(h, "id", strlen("id"), newSViv(owl_message_get_id(m)),0); |
---|
| 77 | hv_store(h, "deleted", strlen("deleted"), newSViv(owl_message_is_delete(m)),0); |
---|
[216c734] | 78 | hv_store(h, "private", strlen("private"), newSViv(owl_message_is_private(m)),0); |
---|
[f1e629d] | 79 | |
---|
| 80 | if (owl_message_is_type_zephyr(m)) blessas = "owl::Message::Zephyr"; |
---|
| 81 | else if (owl_message_is_type_aim(m)) blessas = "owl::Message::AIM"; |
---|
| 82 | else if (owl_message_is_type_admin(m)) blessas = "owl::Message::Admin"; |
---|
| 83 | else if (owl_message_is_type_generic(m)) blessas = "owl::Message::Generic"; |
---|
| 84 | else blessas = "owl::Message"; |
---|
| 85 | |
---|
| 86 | hr = sv_2mortal(newRV_noinc((SV*)h)); |
---|
| 87 | return sv_bless(hr, gv_stashpv(blessas,0)); |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | |
---|
[c3acb0b] | 91 | SV *owl_perlconfig_curmessage2hashref(void) /*noproto*/ |
---|
| 92 | { |
---|
[f1e629d] | 93 | int curmsg; |
---|
| 94 | owl_view *v; |
---|
| 95 | v=owl_global_get_current_view(&g); |
---|
| 96 | if (owl_view_get_size(v) < 1) { |
---|
| 97 | return &PL_sv_undef; |
---|
| 98 | } |
---|
| 99 | curmsg=owl_global_get_curmsg(&g); |
---|
| 100 | return owl_perlconfig_message2hashref(owl_view_get_element(v, curmsg)); |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | |
---|
| 104 | /* Calls in a scalar context, passing it a hash reference. |
---|
| 105 | If return value is non-null, caller must free. */ |
---|
[c3acb0b] | 106 | char *owl_perlconfig_call_with_message(char *subname, owl_message *m) |
---|
| 107 | { |
---|
[f1e629d] | 108 | dSP ; |
---|
| 109 | int count, len; |
---|
| 110 | SV *msgref, *srv; |
---|
| 111 | char *out, *preout; |
---|
| 112 | |
---|
| 113 | ENTER ; |
---|
| 114 | SAVETMPS; |
---|
| 115 | |
---|
| 116 | PUSHMARK(SP) ; |
---|
| 117 | msgref = owl_perlconfig_message2hashref(m); |
---|
| 118 | XPUSHs(msgref); |
---|
| 119 | PUTBACK ; |
---|
| 120 | |
---|
| 121 | count = call_pv(subname, G_SCALAR|G_EVAL|G_KEEPERR); |
---|
| 122 | |
---|
| 123 | SPAGAIN ; |
---|
| 124 | |
---|
| 125 | if (SvTRUE(ERRSV)) { |
---|
| 126 | STRLEN n_a; |
---|
[27c3a93] | 127 | owl_function_error("Perl Error: '%s'", SvPV(ERRSV, n_a)); |
---|
| 128 | /* and clear the error */ |
---|
| 129 | sv_setsv (ERRSV, &PL_sv_undef); |
---|
[f1e629d] | 130 | } |
---|
| 131 | |
---|
| 132 | if (count != 1) { |
---|
| 133 | fprintf(stderr, "bad perl! no biscuit! returned wrong count!\n"); |
---|
| 134 | abort(); |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | srv = POPs; |
---|
| 138 | |
---|
| 139 | if (srv) { |
---|
| 140 | preout=SvPV(srv, len); |
---|
| 141 | out = owl_malloc(strlen(preout)+1); |
---|
| 142 | strncpy(out, preout, len); |
---|
| 143 | out[len] = '\0'; |
---|
| 144 | } else { |
---|
| 145 | out = NULL; |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | PUTBACK ; |
---|
| 149 | FREETMPS ; |
---|
| 150 | LEAVE ; |
---|
| 151 | |
---|
| 152 | return out; |
---|
| 153 | } |
---|
| 154 | |
---|
[c3acb0b] | 155 | char *owl_perlconfig_readconfig(char *file) |
---|
| 156 | { |
---|
[4e0f545] | 157 | int ret, fd; |
---|
[f1e629d] | 158 | PerlInterpreter *p; |
---|
| 159 | char filename[1024]; |
---|
| 160 | char *embedding[5]; |
---|
[4e0f545] | 161 | char *err; |
---|
[f1e629d] | 162 | struct stat statbuff; |
---|
| 163 | |
---|
| 164 | if (file==NULL) { |
---|
| 165 | sprintf(filename, "%s/%s", getenv("HOME"), ".owlconf"); |
---|
| 166 | } else { |
---|
| 167 | strcpy(filename, file); |
---|
| 168 | } |
---|
| 169 | embedding[0]=""; |
---|
| 170 | embedding[1]=filename; |
---|
| 171 | embedding[2]=0; |
---|
| 172 | |
---|
| 173 | /* create and initialize interpreter */ |
---|
| 174 | p=perl_alloc(); |
---|
| 175 | owl_global_set_perlinterp(&g, (void*)p); |
---|
| 176 | perl_construct(p); |
---|
| 177 | |
---|
| 178 | owl_global_set_no_have_config(&g); |
---|
| 179 | |
---|
[4e0f545] | 180 | /* Before we let perl have at it, we'll do our own checks on the the |
---|
| 181 | * file to see if it's present, readnable etc. |
---|
| 182 | */ |
---|
| 183 | |
---|
| 184 | /* Not present, start without it */ |
---|
[f1e629d] | 185 | ret=stat(filename, &statbuff); |
---|
| 186 | if (ret) { |
---|
[4e0f545] | 187 | return(NULL); |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | /* present, but stat thinks it's unreadable */ |
---|
| 191 | if (! (statbuff.st_mode & S_IREAD)) { |
---|
| 192 | return(owl_sprintf("%s present but not readable", filename)); |
---|
[f1e629d] | 193 | } |
---|
| 194 | |
---|
[4e0f545] | 195 | /* can we open it? */ |
---|
| 196 | fd=open(filename, O_RDONLY); |
---|
| 197 | if (fd==-1) { |
---|
| 198 | return(owl_sprintf("could not open %s for reading", filename)); |
---|
| 199 | } |
---|
| 200 | close(fd); |
---|
| 201 | |
---|
[f1e629d] | 202 | ret=perl_parse(p, owl_perl_xs_init, 2, embedding, NULL); |
---|
| 203 | if (ret || SvTRUE(ERRSV)) { |
---|
| 204 | STRLEN n_a; |
---|
[4e0f545] | 205 | err=owl_strdup(SvPV(ERRSV, n_a)); |
---|
| 206 | sv_setsv(ERRSV, &PL_sv_undef); /* and clear the error */ |
---|
| 207 | return(err); |
---|
[f1e629d] | 208 | } |
---|
| 209 | |
---|
| 210 | ret=perl_run(p); |
---|
| 211 | if (ret || SvTRUE(ERRSV)) { |
---|
| 212 | STRLEN n_a; |
---|
[4e0f545] | 213 | err=owl_strdup(SvPV(ERRSV, n_a)); |
---|
| 214 | sv_setsv(ERRSV, &PL_sv_undef); /* and clear the error */ |
---|
| 215 | return(err); |
---|
[f1e629d] | 216 | } |
---|
| 217 | |
---|
| 218 | owl_global_set_have_config(&g); |
---|
| 219 | |
---|
| 220 | /* create legacy variables */ |
---|
| 221 | perl_get_sv("owl::id", TRUE); |
---|
| 222 | perl_get_sv("owl::class", TRUE); |
---|
| 223 | perl_get_sv("owl::instance", TRUE); |
---|
| 224 | perl_get_sv("owl::recipient", TRUE); |
---|
| 225 | perl_get_sv("owl::sender", TRUE); |
---|
| 226 | perl_get_sv("owl::realm", TRUE); |
---|
| 227 | perl_get_sv("owl::opcode", TRUE); |
---|
| 228 | perl_get_sv("owl::zsig", TRUE); |
---|
| 229 | perl_get_sv("owl::msg", TRUE); |
---|
| 230 | perl_get_sv("owl::time", TRUE); |
---|
| 231 | perl_get_sv("owl::host", TRUE); |
---|
| 232 | perl_get_av("owl::fields", TRUE); |
---|
| 233 | |
---|
| 234 | perl_eval_pv(owl_perlwrap_codebuff, FALSE); |
---|
| 235 | |
---|
| 236 | if (SvTRUE(ERRSV)) { |
---|
| 237 | STRLEN n_a; |
---|
[4e0f545] | 238 | err=owl_strdup(SvPV(ERRSV, n_a)); |
---|
[27c3a93] | 239 | sv_setsv (ERRSV, &PL_sv_undef); /* and clear the error */ |
---|
[4e0f545] | 240 | return(err); |
---|
[f1e629d] | 241 | } |
---|
| 242 | |
---|
| 243 | /* check if we have the formatting function */ |
---|
| 244 | if (owl_perlconfig_is_function("owl::format_msg")) { |
---|
| 245 | owl_global_set_config_format(&g, 1); |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | return(NULL); |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | /* returns whether or not a function exists */ |
---|
| 252 | int owl_perlconfig_is_function(char *fn) { |
---|
| 253 | if (perl_get_cv(fn, FALSE)) return(1); |
---|
| 254 | else return(0); |
---|
| 255 | } |
---|
| 256 | |
---|
| 257 | /* returns 0 on success */ |
---|
[c3acb0b] | 258 | int owl_perlconfig_get_hashkeys(char *hashname, owl_list *l) |
---|
| 259 | { |
---|
[f1e629d] | 260 | HV *hv; |
---|
| 261 | HE *he; |
---|
| 262 | char *key; |
---|
| 263 | I32 i; |
---|
| 264 | |
---|
| 265 | if (owl_list_create(l)) return(-1); |
---|
| 266 | hv = get_hv(hashname, FALSE); |
---|
| 267 | if (!hv) return(-1); |
---|
| 268 | i = hv_iterinit(hv); |
---|
| 269 | while ((he = hv_iternext(hv))) { |
---|
| 270 | key = hv_iterkey(he, &i); |
---|
| 271 | if (key) { |
---|
| 272 | owl_list_append_element(l, owl_strdup(key)); |
---|
| 273 | } |
---|
| 274 | } |
---|
| 275 | return(0); |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | /* caller is responsible for freeing returned string */ |
---|
[c3acb0b] | 279 | char *owl_perlconfig_execute(char *line) |
---|
| 280 | { |
---|
[f1e629d] | 281 | STRLEN len; |
---|
| 282 | SV *response; |
---|
| 283 | char *out, *preout; |
---|
| 284 | |
---|
| 285 | if (!owl_global_have_config(&g)) return NULL; |
---|
| 286 | |
---|
| 287 | /* execute the subroutine */ |
---|
| 288 | response = perl_eval_pv(line, FALSE); |
---|
| 289 | |
---|
| 290 | if (SvTRUE(ERRSV)) { |
---|
| 291 | STRLEN n_a; |
---|
[27c3a93] | 292 | owl_function_error("Perl Error: '%s'", SvPV(ERRSV, n_a)); |
---|
| 293 | sv_setsv (ERRSV, &PL_sv_undef); /* and clear the error */ |
---|
[f1e629d] | 294 | } |
---|
| 295 | |
---|
| 296 | preout=SvPV(response, len); |
---|
| 297 | /* leave enough space in case we have to add a newline */ |
---|
| 298 | out = owl_malloc(strlen(preout)+2); |
---|
| 299 | strncpy(out, preout, len); |
---|
| 300 | out[len] = '\0'; |
---|
| 301 | if (!strlen(out) || out[strlen(out)-1]!='\n') { |
---|
| 302 | strcat(out, "\n"); |
---|
| 303 | } |
---|
| 304 | |
---|
| 305 | return(out); |
---|
| 306 | } |
---|
| 307 | |
---|
[c3acb0b] | 308 | char *owl_perlconfig_getmsg(owl_message *m, int mode, char *subname) |
---|
| 309 | { |
---|
[f1e629d] | 310 | /* if mode==1 we are doing message formatting. The returned |
---|
| 311 | * formatted message needs to be freed by the caller. |
---|
| 312 | * |
---|
| 313 | * if mode==0 we are just doing the message-has-been-received |
---|
| 314 | * thing. |
---|
| 315 | */ |
---|
| 316 | if (!owl_global_have_config(&g)) return(NULL); |
---|
| 317 | |
---|
| 318 | /* run the procedure corresponding to the mode */ |
---|
| 319 | if (mode==1) { |
---|
| 320 | char *ret = NULL; |
---|
| 321 | ret = owl_perlconfig_call_with_message(subname?subname |
---|
| 322 | :"owl::_format_msg_legacy_wrap", m); |
---|
| 323 | if (!ret) { |
---|
| 324 | ret = owl_sprintf("@b([Perl Message Formatting Failed!])\n"); |
---|
| 325 | } |
---|
| 326 | return ret; |
---|
| 327 | } else { |
---|
| 328 | char *ptr = NULL; |
---|
| 329 | if (owl_perlconfig_is_function("owl::receive_msg")) { |
---|
[c0a90c2] | 330 | ptr = owl_perlconfig_call_with_message(subname?subname |
---|
[f1e629d] | 331 | :"owl::_receive_msg_legacy_wrap", m); |
---|
| 332 | } |
---|
| 333 | if (ptr) owl_free(ptr); |
---|
| 334 | return(NULL); |
---|
| 335 | } |
---|
| 336 | } |
---|
[6922edd] | 337 | |
---|
| 338 | char *owl_perlconfig_perlcmd(owl_cmd *cmd, int argc, char **argv) |
---|
| 339 | { |
---|
| 340 | int i, count; |
---|
| 341 | char * ret = NULL; |
---|
| 342 | STRLEN n_a; |
---|
| 343 | dSP; |
---|
| 344 | |
---|
| 345 | ENTER; |
---|
| 346 | SAVETMPS; |
---|
| 347 | |
---|
| 348 | PUSHMARK(SP); |
---|
| 349 | for(i=0;i<argc;i++) { |
---|
| 350 | XPUSHs(sv_2mortal(newSVpv(argv[i], 0))); |
---|
| 351 | } |
---|
| 352 | PUTBACK; |
---|
| 353 | |
---|
| 354 | count = call_sv(cmd->cmd_perl, G_SCALAR|G_EVAL); |
---|
| 355 | |
---|
| 356 | SPAGAIN; |
---|
| 357 | |
---|
| 358 | if(SvTRUE(ERRSV)) { |
---|
| 359 | owl_function_error("Error: %s", SvPV(ERRSV, n_a)); |
---|
| 360 | POPs; |
---|
| 361 | } else { |
---|
| 362 | if(count != 1) |
---|
| 363 | croak("Perl command %s returned more than one value!", cmd->name); |
---|
| 364 | SV * rv = POPs; |
---|
| 365 | if(SvTRUE(rv)) { |
---|
| 366 | ret = owl_strdup(SvPV(rv, n_a)); |
---|
| 367 | } |
---|
| 368 | } |
---|
| 369 | |
---|
| 370 | FREETMPS; |
---|
| 371 | LEAVE; |
---|
| 372 | |
---|
| 373 | return ret; |
---|
| 374 | } |
---|
| 375 | |
---|
| 376 | void owl_perlconfig_cmd_free(owl_cmd *cmd) |
---|
| 377 | { |
---|
| 378 | SvREFCNT_dec(cmd); |
---|
| 379 | } |
---|
[db8b00b] | 380 | |
---|
| 381 | void owl_perlconfig_edit_callback(owl_editwin *e) |
---|
| 382 | { |
---|
| 383 | SV *cb = (SV*)(e->cbdata); |
---|
| 384 | if(cb == NULL) { |
---|
| 385 | owl_function_error("Perl callback is NULL!"); |
---|
| 386 | } |
---|
| 387 | |
---|
| 388 | dSP; |
---|
| 389 | |
---|
| 390 | ENTER; |
---|
| 391 | SAVETMPS; |
---|
| 392 | |
---|
| 393 | PUSHMARK(SP); |
---|
| 394 | XPUSHs(sv_2mortal(newSVpv(owl_editwin_get_text(e), 0))); |
---|
| 395 | PUTBACK; |
---|
| 396 | |
---|
| 397 | call_sv(cb, G_DISCARD); |
---|
| 398 | |
---|
| 399 | FREETMPS; |
---|
| 400 | LEAVE; |
---|
| 401 | |
---|
| 402 | SvREFCNT_dec(cb); |
---|
| 403 | e->cbdata = NULL; |
---|
| 404 | } |
---|