[7d4fbcd] | 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> |
---|
[67103d4] | 9 | #include "XSUB.h" |
---|
[7d4fbcd] | 10 | |
---|
[1aee7d9] | 11 | static const char fileIdent[] = "$Id$"; |
---|
| 12 | |
---|
[67103d4] | 13 | extern XS(boot_owl); |
---|
[8ee73f8d] | 14 | |
---|
| 15 | static void owl_perl_xs_init(pTHX) { |
---|
| 16 | char *file = __FILE__; |
---|
| 17 | dXSUB_SYS; |
---|
| 18 | { |
---|
| 19 | newXS("owl::bootstrap", boot_owl, file); |
---|
| 20 | } |
---|
| 21 | } |
---|
| 22 | |
---|
| 23 | |
---|
[7d4fbcd] | 24 | int owl_readconfig(char *file) { |
---|
| 25 | int ret; |
---|
| 26 | PerlInterpreter *p; |
---|
[8ee73f8d] | 27 | char *codebuff, filename[1024]; |
---|
[7d4fbcd] | 28 | char *embedding[5]; |
---|
| 29 | struct stat statbuff; |
---|
| 30 | |
---|
| 31 | if (file==NULL) { |
---|
| 32 | sprintf(filename, "%s/%s", getenv("HOME"), ".owlconf"); |
---|
| 33 | } else { |
---|
| 34 | strcpy(filename, file); |
---|
| 35 | } |
---|
| 36 | embedding[0]=""; |
---|
| 37 | embedding[1]=filename; |
---|
| 38 | embedding[2]=0; |
---|
| 39 | |
---|
| 40 | /* create and initialize interpreter */ |
---|
| 41 | p=perl_alloc(); |
---|
| 42 | owl_global_set_perlinterp(&g, (void*)p); |
---|
| 43 | perl_construct(p); |
---|
| 44 | |
---|
[ac70242] | 45 | /* start by assuming no config */ |
---|
[7d4fbcd] | 46 | owl_global_set_no_have_config(&g); |
---|
| 47 | |
---|
| 48 | ret=stat(filename, &statbuff); |
---|
| 49 | if (ret) { |
---|
| 50 | return(0); |
---|
| 51 | } |
---|
| 52 | |
---|
[ac70242] | 53 | /* This is the CMU code for reading a site owlconf. Not sure if I |
---|
| 54 | want it yet */ |
---|
| 55 | /* |
---|
| 56 | ret=stat(filename, &statbuff); |
---|
| 57 | if (ret) { |
---|
| 58 | sprintf(filename, "%s/%s", DATADIR, "owl/owlconf"); |
---|
| 59 | ret=stat(filename, &statbuff); |
---|
| 60 | if (ret) { |
---|
| 61 | return(0); |
---|
| 62 | } |
---|
| 63 | } |
---|
| 64 | */ |
---|
| 65 | |
---|
[8ee73f8d] | 66 | ret=perl_parse(p, owl_perl_xs_init, 2, embedding, NULL); |
---|
[7d4fbcd] | 67 | if (ret) return(-1); |
---|
| 68 | ret=perl_run(p); |
---|
| 69 | if (ret) return(-1); |
---|
| 70 | |
---|
[ac70242] | 71 | /* if we get this far, we have a good config file */ |
---|
[7d4fbcd] | 72 | owl_global_set_have_config(&g); |
---|
| 73 | |
---|
| 74 | /* create variables */ |
---|
| 75 | perl_get_sv("owl::id", TRUE); |
---|
[b45293f] | 76 | perl_get_sv("owl::type", TRUE); |
---|
| 77 | perl_get_sv("owl::direction", TRUE); |
---|
[7d4fbcd] | 78 | perl_get_sv("owl::class", TRUE); |
---|
| 79 | perl_get_sv("owl::instance", TRUE); |
---|
| 80 | perl_get_sv("owl::recipient", TRUE); |
---|
| 81 | perl_get_sv("owl::sender", TRUE); |
---|
| 82 | perl_get_sv("owl::realm", TRUE); |
---|
| 83 | perl_get_sv("owl::opcode", TRUE); |
---|
| 84 | perl_get_sv("owl::zsig", TRUE); |
---|
| 85 | perl_get_sv("owl::msg", TRUE); |
---|
| 86 | perl_get_sv("owl::time", TRUE); |
---|
| 87 | perl_get_sv("owl::host", TRUE); |
---|
[0c502e9] | 88 | perl_get_sv("owl::login", TRUE); |
---|
[290f290] | 89 | perl_get_sv("owl::auth", TRUE); |
---|
[7d4fbcd] | 90 | perl_get_av("owl::fields", TRUE); |
---|
| 91 | |
---|
[6794f72] | 92 | /* perl bootstrapping code */ |
---|
[8ee73f8d] | 93 | codebuff = |
---|
| 94 | " \n" |
---|
| 95 | "package owl; \n" |
---|
| 96 | " \n" |
---|
| 97 | "bootstrap owl 0.01; \n" |
---|
| 98 | " \n" |
---|
| 99 | "package main; \n"; |
---|
| 100 | |
---|
| 101 | perl_eval_pv(codebuff, FALSE); |
---|
[7d4fbcd] | 102 | |
---|
| 103 | |
---|
| 104 | /* check if we have the formatting function */ |
---|
| 105 | if (perl_get_cv("owl::format_msg", FALSE)) { |
---|
| 106 | owl_global_set_config_format(&g, 1); |
---|
| 107 | } |
---|
| 108 | return(0); |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | |
---|
| 112 | /* caller is responsible for freeing returned string */ |
---|
| 113 | char *owl_config_execute(char *line) { |
---|
[8ee73f8d] | 114 | STRLEN len; |
---|
| 115 | SV *response; |
---|
[7d4fbcd] | 116 | char *out, *preout; |
---|
| 117 | |
---|
| 118 | if (!owl_global_have_config(&g)) return NULL; |
---|
| 119 | |
---|
| 120 | /* execute the subroutine */ |
---|
| 121 | response = perl_eval_pv(line, FALSE); |
---|
| 122 | |
---|
[8ee73f8d] | 123 | preout=SvPV(response, len); |
---|
[7d4fbcd] | 124 | /* leave enough space in case we have to add a newline */ |
---|
| 125 | out = owl_malloc(strlen(preout)+2); |
---|
[8ee73f8d] | 126 | strncpy(out, preout, len); |
---|
| 127 | out[len] = '\0'; |
---|
[7d4fbcd] | 128 | if (!strlen(out) || out[strlen(out)-1]!='\n') { |
---|
| 129 | strcat(out, "\n"); |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | return(out); |
---|
| 133 | } |
---|
| 134 | |
---|
[bd3f232] | 135 | char *owl_config_getmsg(owl_message *m, char *funcname) { |
---|
[7d4fbcd] | 136 | int i, j, len; |
---|
| 137 | char *ptr, *ptr2; |
---|
[bd3f232] | 138 | char runstr[LINE]; |
---|
[7d4fbcd] | 139 | |
---|
| 140 | if (!owl_global_have_config(&g)) return(""); |
---|
| 141 | |
---|
| 142 | /* set owl::msg */ |
---|
[b45293f] | 143 | ptr=owl_message_get_body(m); |
---|
| 144 | len=strlen(ptr); |
---|
[7d4fbcd] | 145 | ptr2=owl_malloc(len+20); |
---|
| 146 | memcpy(ptr2, ptr, len); |
---|
| 147 | ptr2[len]='\0'; |
---|
| 148 | if (ptr2[len-1]!='\n') { |
---|
| 149 | strcat(ptr2, "\n"); |
---|
| 150 | } |
---|
| 151 | sv_setpv(perl_get_sv("owl::msg", TRUE), ptr2); |
---|
| 152 | owl_free(ptr2); |
---|
| 153 | |
---|
| 154 | /* set owl::zsig */ |
---|
[b45293f] | 155 | ptr=owl_message_get_zsig(m); |
---|
| 156 | len=strlen(ptr); |
---|
[7d4fbcd] | 157 | if (len>0) { |
---|
| 158 | ptr2=owl_malloc(len+20); |
---|
| 159 | memcpy(ptr2, ptr, len); |
---|
| 160 | ptr2[len]='\0'; |
---|
| 161 | if (ptr2[len-1]=='\n') { /* do we really need this? */ |
---|
| 162 | ptr2[len-1]='\0'; |
---|
| 163 | } |
---|
| 164 | sv_setpv(perl_get_sv("owl::zsig", TRUE), ptr2); |
---|
| 165 | owl_free(ptr2); |
---|
| 166 | } else { |
---|
| 167 | sv_setpv(perl_get_sv("owl::zsig", TRUE), ""); |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | /* set owl::type */ |
---|
[d559df9] | 171 | sv_setpv(perl_get_sv("owl::type", TRUE), owl_message_type_to_string(m)); |
---|
[7d4fbcd] | 172 | |
---|
[9381782] | 173 | |
---|
| 174 | /* set owl::auth default */ |
---|
| 175 | sv_setpv(perl_get_sv("owl::auth", TRUE), "YES"); |
---|
| 176 | |
---|
[b45293f] | 177 | /* set owl::direction */ |
---|
| 178 | if (owl_message_is_direction_in(m)) { |
---|
| 179 | sv_setpv(perl_get_sv("owl::direction", TRUE), "in"); |
---|
[9381782] | 180 | |
---|
| 181 | /* Change auth value for in messages */ |
---|
| 182 | sv_setpv(perl_get_sv("owl::auth", TRUE), owl_zephyr_get_authstr(owl_message_get_notice(m))); |
---|
| 183 | |
---|
[b45293f] | 184 | } else if (owl_message_is_direction_out(m)) { |
---|
| 185 | sv_setpv(perl_get_sv("owl::direction", TRUE), "out"); |
---|
| 186 | } else if (owl_message_is_direction_none(m)) { |
---|
| 187 | sv_setpv(perl_get_sv("owl::direction", TRUE), "none"); |
---|
| 188 | } else { |
---|
| 189 | sv_setpv(perl_get_sv("owl::direction", TRUE), "unknown"); |
---|
| 190 | } |
---|
| 191 | |
---|
[0c502e9] | 192 | /* set owl::login */ |
---|
| 193 | if (owl_message_is_login(m)) { |
---|
| 194 | sv_setpv(perl_get_sv("owl::login", TRUE), "login"); |
---|
| 195 | } else if (owl_message_is_logout(m)) { |
---|
| 196 | sv_setpv(perl_get_sv("owl::login", TRUE), "logout"); |
---|
| 197 | } else { |
---|
| 198 | sv_setpv(perl_get_sv("owl::login", TRUE), "none"); |
---|
| 199 | } |
---|
| 200 | |
---|
[7d4fbcd] | 201 | /* set everything else */ |
---|
| 202 | sv_setpv(perl_get_sv("owl::class", TRUE), owl_message_get_class(m)); |
---|
| 203 | sv_setpv(perl_get_sv("owl::instance", TRUE), owl_message_get_instance(m)); |
---|
| 204 | sv_setpv(perl_get_sv("owl::sender", TRUE), owl_message_get_sender(m)); |
---|
| 205 | sv_setpv(perl_get_sv("owl::realm", TRUE), owl_message_get_realm(m)); |
---|
| 206 | sv_setpv(perl_get_sv("owl::recipient", TRUE), owl_message_get_recipient(m)); |
---|
| 207 | sv_setpv(perl_get_sv("owl::opcode", TRUE), owl_message_get_opcode(m)); |
---|
| 208 | sv_setpv(perl_get_sv("owl::time", TRUE), owl_message_get_timestr(m)); |
---|
| 209 | sv_setpv(perl_get_sv("owl::host", TRUE), owl_message_get_hostname(m)); |
---|
| 210 | sv_setiv(perl_get_sv("owl::id", TRUE), owl_message_get_id(m)); |
---|
| 211 | |
---|
| 212 | /* free old @fields ? */ |
---|
| 213 | /* I don't think I need to do this, but ask marc to make sure */ |
---|
| 214 | /* |
---|
| 215 | j=av_len(perl_get_av("fields", TRUE)); |
---|
| 216 | for (i=0; i<j; i++) { |
---|
| 217 | tmpsv=av_pop(perl_get_av("fields", TRUE)); |
---|
| 218 | SvREFCNT_dec(tmpsv); |
---|
| 219 | } |
---|
| 220 | */ |
---|
| 221 | |
---|
| 222 | /* set owl::fields */ |
---|
[b45293f] | 223 | if (owl_message_is_type_zephyr(m) && owl_message_is_direction_in(m)) { |
---|
| 224 | av_clear(perl_get_av("owl::fields", TRUE)); |
---|
| 225 | j=owl_zephyr_get_num_fields(owl_message_get_notice(m)); |
---|
| 226 | for (i=0; i<j; i++) { |
---|
| 227 | ptr=owl_zephyr_get_field(owl_message_get_notice(m), i+1, &len); |
---|
| 228 | ptr2=owl_malloc(len+10); |
---|
| 229 | memcpy(ptr2, ptr, len); |
---|
| 230 | ptr2[len]='\0'; |
---|
| 231 | av_push(perl_get_av("owl::fields", TRUE), newSVpvn(ptr2, len)); |
---|
| 232 | owl_free(ptr2); |
---|
| 233 | } |
---|
[7d4fbcd] | 234 | } |
---|
| 235 | |
---|
| 236 | /* for backwards compatibilty, because I'm an idiot */ |
---|
[b45293f] | 237 | if (owl_message_is_type_zephyr(m) && owl_message_is_direction_in(m)) { |
---|
| 238 | av_clear(perl_get_av("fields", TRUE)); |
---|
| 239 | j=owl_zephyr_get_num_fields(owl_message_get_notice(m)); |
---|
| 240 | for (i=0; i<j; i++) { |
---|
| 241 | ptr=owl_zephyr_get_field(owl_message_get_notice(m), i+1, &len); |
---|
| 242 | ptr2=owl_malloc(len+10); |
---|
| 243 | memcpy(ptr2, ptr, len); |
---|
| 244 | ptr2[len]='\0'; |
---|
| 245 | av_push(perl_get_av("fields", TRUE), newSVpvn(ptr2, len)); |
---|
| 246 | owl_free(ptr2); |
---|
| 247 | } |
---|
[7d4fbcd] | 248 | } |
---|
| 249 | |
---|
| 250 | /* run the procedure corresponding to the mode */ |
---|
[bd3f232] | 251 | sprintf(runstr, "%s();", funcname); |
---|
| 252 | return(owl_config_execute(runstr)); |
---|
[7d4fbcd] | 253 | } |
---|
[8ee73f8d] | 254 | |
---|