[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> |
---|
| 7 | #include "owl.h" |
---|
| 8 | #include <perl.h> |
---|
| 9 | #include "XSUB.h" |
---|
| 10 | |
---|
| 11 | static const char fileIdent[] = "$Id$"; |
---|
| 12 | |
---|
| 13 | extern char *owl_perlwrap_codebuff; |
---|
| 14 | |
---|
| 15 | extern XS(boot_owl); |
---|
| 16 | |
---|
[c3acb0b] | 17 | static void owl_perl_xs_init(pTHX) |
---|
| 18 | { |
---|
[f1e629d] | 19 | char *file = __FILE__; |
---|
| 20 | dXSUB_SYS; |
---|
| 21 | { |
---|
| 22 | newXS("owl::bootstrap", boot_owl, file); |
---|
| 23 | } |
---|
| 24 | } |
---|
| 25 | |
---|
[c3acb0b] | 26 | SV *owl_perlconfig_message2hashref(owl_message *m) /*noproto*/ |
---|
| 27 | { |
---|
[f1e629d] | 28 | HV *h; |
---|
| 29 | SV *hr; |
---|
[b0430a6] | 30 | char *ptr, *blessas; |
---|
| 31 | int i, j; |
---|
[f1e629d] | 32 | |
---|
| 33 | if (!m) return &PL_sv_undef; |
---|
| 34 | h = newHV(); |
---|
| 35 | |
---|
| 36 | #define MSG2H(h,field) hv_store(h, #field, strlen(#field), \ |
---|
| 37 | newSVpv(owl_message_get_##field(m),0), 0) |
---|
| 38 | |
---|
| 39 | if (owl_message_is_type_zephyr(m) |
---|
| 40 | && owl_message_is_direction_in(m)) { |
---|
| 41 | /* Handle zephyr-specific fields... */ |
---|
| 42 | AV *av_zfields; |
---|
| 43 | |
---|
| 44 | av_zfields = newAV(); |
---|
| 45 | j=owl_zephyr_get_num_fields(owl_message_get_notice(m)); |
---|
| 46 | for (i=0; i<j; i++) { |
---|
[b0430a6] | 47 | ptr=owl_zephyr_get_field(owl_message_get_notice(m), i+1); |
---|
| 48 | av_push(av_zfields, newSVpvn(ptr, strlen(ptr))); |
---|
| 49 | owl_free(ptr); |
---|
[f1e629d] | 50 | } |
---|
| 51 | hv_store(h, "fields", strlen("fields"), newRV_noinc((SV*)av_zfields), 0); |
---|
| 52 | |
---|
| 53 | hv_store(h, "auth", strlen("auth"), |
---|
| 54 | newSVpv(owl_zephyr_get_authstr(owl_message_get_notice(m)),0),0); |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | MSG2H(h, type); |
---|
| 58 | MSG2H(h, direction); |
---|
| 59 | MSG2H(h, class); |
---|
| 60 | MSG2H(h, instance); |
---|
| 61 | MSG2H(h, sender); |
---|
| 62 | MSG2H(h, realm); |
---|
| 63 | MSG2H(h, recipient); |
---|
| 64 | MSG2H(h, opcode); |
---|
| 65 | MSG2H(h, hostname); |
---|
| 66 | MSG2H(h, body); |
---|
| 67 | MSG2H(h, login); |
---|
| 68 | MSG2H(h, zsig); |
---|
| 69 | MSG2H(h, zwriteline); |
---|
| 70 | if (owl_message_get_header(m)) { |
---|
| 71 | MSG2H(h, header); |
---|
| 72 | } |
---|
| 73 | hv_store(h, "time", strlen("time"), newSVpv(owl_message_get_timestr(m),0),0); |
---|
| 74 | hv_store(h, "id", strlen("id"), newSViv(owl_message_get_id(m)),0); |
---|
| 75 | hv_store(h, "deleted", strlen("deleted"), newSViv(owl_message_is_delete(m)),0); |
---|
| 76 | |
---|
| 77 | if (owl_message_is_type_zephyr(m)) blessas = "owl::Message::Zephyr"; |
---|
| 78 | else if (owl_message_is_type_aim(m)) blessas = "owl::Message::AIM"; |
---|
| 79 | else if (owl_message_is_type_admin(m)) blessas = "owl::Message::Admin"; |
---|
| 80 | else if (owl_message_is_type_generic(m)) blessas = "owl::Message::Generic"; |
---|
| 81 | else blessas = "owl::Message"; |
---|
| 82 | |
---|
| 83 | hr = sv_2mortal(newRV_noinc((SV*)h)); |
---|
| 84 | return sv_bless(hr, gv_stashpv(blessas,0)); |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | |
---|
[c3acb0b] | 88 | SV *owl_perlconfig_curmessage2hashref(void) /*noproto*/ |
---|
| 89 | { |
---|
[f1e629d] | 90 | int curmsg; |
---|
| 91 | owl_view *v; |
---|
| 92 | v=owl_global_get_current_view(&g); |
---|
| 93 | if (owl_view_get_size(v) < 1) { |
---|
| 94 | return &PL_sv_undef; |
---|
| 95 | } |
---|
| 96 | curmsg=owl_global_get_curmsg(&g); |
---|
| 97 | return owl_perlconfig_message2hashref(owl_view_get_element(v, curmsg)); |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | |
---|
| 101 | /* Calls in a scalar context, passing it a hash reference. |
---|
| 102 | If return value is non-null, caller must free. */ |
---|
[c3acb0b] | 103 | char *owl_perlconfig_call_with_message(char *subname, owl_message *m) |
---|
| 104 | { |
---|
[f1e629d] | 105 | dSP ; |
---|
| 106 | int count, len; |
---|
| 107 | SV *msgref, *srv; |
---|
| 108 | char *out, *preout; |
---|
| 109 | |
---|
| 110 | ENTER ; |
---|
| 111 | SAVETMPS; |
---|
| 112 | |
---|
| 113 | PUSHMARK(SP) ; |
---|
| 114 | msgref = owl_perlconfig_message2hashref(m); |
---|
| 115 | XPUSHs(msgref); |
---|
| 116 | PUTBACK ; |
---|
| 117 | |
---|
| 118 | count = call_pv(subname, G_SCALAR|G_EVAL|G_KEEPERR); |
---|
| 119 | |
---|
| 120 | SPAGAIN ; |
---|
| 121 | |
---|
| 122 | if (SvTRUE(ERRSV)) { |
---|
| 123 | STRLEN n_a; |
---|
[27c3a93] | 124 | owl_function_error("Perl Error: '%s'", SvPV(ERRSV, n_a)); |
---|
| 125 | /* and clear the error */ |
---|
| 126 | sv_setsv (ERRSV, &PL_sv_undef); |
---|
[f1e629d] | 127 | } |
---|
| 128 | |
---|
| 129 | if (count != 1) { |
---|
| 130 | fprintf(stderr, "bad perl! no biscuit! returned wrong count!\n"); |
---|
| 131 | abort(); |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | srv = POPs; |
---|
| 135 | |
---|
| 136 | if (srv) { |
---|
| 137 | preout=SvPV(srv, len); |
---|
| 138 | out = owl_malloc(strlen(preout)+1); |
---|
| 139 | strncpy(out, preout, len); |
---|
| 140 | out[len] = '\0'; |
---|
| 141 | } else { |
---|
| 142 | out = NULL; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | PUTBACK ; |
---|
| 146 | FREETMPS ; |
---|
| 147 | LEAVE ; |
---|
| 148 | |
---|
| 149 | return out; |
---|
| 150 | } |
---|
| 151 | |
---|
[c3acb0b] | 152 | char *owl_perlconfig_readconfig(char *file) |
---|
| 153 | { |
---|
[f1e629d] | 154 | int ret; |
---|
| 155 | PerlInterpreter *p; |
---|
| 156 | char filename[1024]; |
---|
| 157 | char *embedding[5]; |
---|
| 158 | struct stat statbuff; |
---|
| 159 | |
---|
| 160 | if (file==NULL) { |
---|
| 161 | sprintf(filename, "%s/%s", getenv("HOME"), ".owlconf"); |
---|
| 162 | } else { |
---|
| 163 | strcpy(filename, file); |
---|
| 164 | } |
---|
| 165 | embedding[0]=""; |
---|
| 166 | embedding[1]=filename; |
---|
| 167 | embedding[2]=0; |
---|
| 168 | |
---|
| 169 | /* create and initialize interpreter */ |
---|
| 170 | p=perl_alloc(); |
---|
| 171 | owl_global_set_perlinterp(&g, (void*)p); |
---|
| 172 | perl_construct(p); |
---|
| 173 | |
---|
| 174 | owl_global_set_no_have_config(&g); |
---|
| 175 | |
---|
| 176 | ret=stat(filename, &statbuff); |
---|
| 177 | if (ret) { |
---|
| 178 | return NULL; |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | ret=perl_parse(p, owl_perl_xs_init, 2, embedding, NULL); |
---|
| 182 | if (ret || SvTRUE(ERRSV)) { |
---|
| 183 | STRLEN n_a; |
---|
[27c3a93] | 184 | char *err; |
---|
| 185 | err = owl_strdup(SvPV(ERRSV, n_a)); |
---|
| 186 | sv_setsv (ERRSV, &PL_sv_undef); /* and clear the error */ |
---|
| 187 | return err; |
---|
[f1e629d] | 188 | } |
---|
| 189 | |
---|
| 190 | ret=perl_run(p); |
---|
| 191 | if (ret || SvTRUE(ERRSV)) { |
---|
| 192 | STRLEN n_a; |
---|
[27c3a93] | 193 | char *err; |
---|
| 194 | err = owl_strdup(SvPV(ERRSV, n_a)); |
---|
| 195 | sv_setsv (ERRSV, &PL_sv_undef); /* and clear the error */ |
---|
| 196 | return err; |
---|
[f1e629d] | 197 | } |
---|
| 198 | |
---|
| 199 | owl_global_set_have_config(&g); |
---|
| 200 | |
---|
| 201 | /* create legacy variables */ |
---|
| 202 | perl_get_sv("owl::id", TRUE); |
---|
| 203 | perl_get_sv("owl::class", TRUE); |
---|
| 204 | perl_get_sv("owl::instance", TRUE); |
---|
| 205 | perl_get_sv("owl::recipient", TRUE); |
---|
| 206 | perl_get_sv("owl::sender", TRUE); |
---|
| 207 | perl_get_sv("owl::realm", TRUE); |
---|
| 208 | perl_get_sv("owl::opcode", TRUE); |
---|
| 209 | perl_get_sv("owl::zsig", TRUE); |
---|
| 210 | perl_get_sv("owl::msg", TRUE); |
---|
| 211 | perl_get_sv("owl::time", TRUE); |
---|
| 212 | perl_get_sv("owl::host", TRUE); |
---|
| 213 | perl_get_av("owl::fields", TRUE); |
---|
| 214 | |
---|
| 215 | perl_eval_pv(owl_perlwrap_codebuff, FALSE); |
---|
| 216 | |
---|
| 217 | if (SvTRUE(ERRSV)) { |
---|
| 218 | STRLEN n_a; |
---|
[27c3a93] | 219 | char *err; |
---|
| 220 | err = owl_strdup(SvPV(ERRSV, n_a)); |
---|
| 221 | sv_setsv (ERRSV, &PL_sv_undef); /* and clear the error */ |
---|
| 222 | return err; |
---|
[f1e629d] | 223 | } |
---|
| 224 | |
---|
| 225 | /* check if we have the formatting function */ |
---|
| 226 | if (owl_perlconfig_is_function("owl::format_msg")) { |
---|
| 227 | owl_global_set_config_format(&g, 1); |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | return(NULL); |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | /* returns whether or not a function exists */ |
---|
| 234 | int owl_perlconfig_is_function(char *fn) { |
---|
| 235 | if (perl_get_cv(fn, FALSE)) return(1); |
---|
| 236 | else return(0); |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | /* returns 0 on success */ |
---|
[c3acb0b] | 240 | int owl_perlconfig_get_hashkeys(char *hashname, owl_list *l) |
---|
| 241 | { |
---|
[f1e629d] | 242 | HV *hv; |
---|
| 243 | HE *he; |
---|
| 244 | char *key; |
---|
| 245 | I32 i; |
---|
| 246 | |
---|
| 247 | if (owl_list_create(l)) return(-1); |
---|
| 248 | hv = get_hv(hashname, FALSE); |
---|
| 249 | if (!hv) return(-1); |
---|
| 250 | i = hv_iterinit(hv); |
---|
| 251 | while ((he = hv_iternext(hv))) { |
---|
| 252 | key = hv_iterkey(he, &i); |
---|
| 253 | if (key) { |
---|
| 254 | owl_list_append_element(l, owl_strdup(key)); |
---|
| 255 | } |
---|
| 256 | } |
---|
| 257 | return(0); |
---|
| 258 | } |
---|
| 259 | |
---|
| 260 | /* caller is responsible for freeing returned string */ |
---|
[c3acb0b] | 261 | char *owl_perlconfig_execute(char *line) |
---|
| 262 | { |
---|
[f1e629d] | 263 | STRLEN len; |
---|
| 264 | SV *response; |
---|
| 265 | char *out, *preout; |
---|
| 266 | |
---|
| 267 | if (!owl_global_have_config(&g)) return NULL; |
---|
| 268 | |
---|
| 269 | /* execute the subroutine */ |
---|
| 270 | response = perl_eval_pv(line, FALSE); |
---|
| 271 | |
---|
| 272 | if (SvTRUE(ERRSV)) { |
---|
| 273 | STRLEN n_a; |
---|
[27c3a93] | 274 | owl_function_error("Perl Error: '%s'", SvPV(ERRSV, n_a)); |
---|
| 275 | sv_setsv (ERRSV, &PL_sv_undef); /* and clear the error */ |
---|
[f1e629d] | 276 | } |
---|
| 277 | |
---|
| 278 | preout=SvPV(response, len); |
---|
| 279 | /* leave enough space in case we have to add a newline */ |
---|
| 280 | out = owl_malloc(strlen(preout)+2); |
---|
| 281 | strncpy(out, preout, len); |
---|
| 282 | out[len] = '\0'; |
---|
| 283 | if (!strlen(out) || out[strlen(out)-1]!='\n') { |
---|
| 284 | strcat(out, "\n"); |
---|
| 285 | } |
---|
| 286 | |
---|
| 287 | return(out); |
---|
| 288 | } |
---|
| 289 | |
---|
[c3acb0b] | 290 | char *owl_perlconfig_getmsg(owl_message *m, int mode, char *subname) |
---|
| 291 | { |
---|
[f1e629d] | 292 | /* if mode==1 we are doing message formatting. The returned |
---|
| 293 | * formatted message needs to be freed by the caller. |
---|
| 294 | * |
---|
| 295 | * if mode==0 we are just doing the message-has-been-received |
---|
| 296 | * thing. |
---|
| 297 | */ |
---|
| 298 | if (!owl_global_have_config(&g)) return(NULL); |
---|
| 299 | |
---|
| 300 | /* run the procedure corresponding to the mode */ |
---|
| 301 | if (mode==1) { |
---|
| 302 | char *ret = NULL; |
---|
| 303 | ret = owl_perlconfig_call_with_message(subname?subname |
---|
| 304 | :"owl::_format_msg_legacy_wrap", m); |
---|
| 305 | if (!ret) { |
---|
| 306 | ret = owl_sprintf("@b([Perl Message Formatting Failed!])\n"); |
---|
| 307 | } |
---|
| 308 | return ret; |
---|
| 309 | } else { |
---|
| 310 | char *ptr = NULL; |
---|
| 311 | if (owl_perlconfig_is_function("owl::receive_msg")) { |
---|
| 312 | owl_perlconfig_call_with_message(subname?subname |
---|
| 313 | :"owl::_receive_msg_legacy_wrap", m); |
---|
| 314 | } |
---|
| 315 | if (ptr) owl_free(ptr); |
---|
| 316 | return(NULL); |
---|
| 317 | } |
---|
| 318 | } |
---|