[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> |
---|
| 9 | |
---|
| 10 | int owl_readconfig(char *file) { |
---|
| 11 | int ret; |
---|
| 12 | PerlInterpreter *p; |
---|
| 13 | char buff[1024], filename[1024]; |
---|
| 14 | char *embedding[5]; |
---|
| 15 | struct stat statbuff; |
---|
| 16 | |
---|
| 17 | if (file==NULL) { |
---|
| 18 | sprintf(filename, "%s/%s", getenv("HOME"), ".owlconf"); |
---|
| 19 | } else { |
---|
| 20 | strcpy(filename, file); |
---|
| 21 | } |
---|
| 22 | |
---|
| 23 | embedding[0]=""; |
---|
| 24 | embedding[1]=filename; |
---|
| 25 | embedding[2]=0; |
---|
| 26 | |
---|
| 27 | /* create and initialize interpreter */ |
---|
| 28 | p=perl_alloc(); |
---|
| 29 | owl_global_set_perlinterp(&g, (void*)p); |
---|
| 30 | perl_construct(p); |
---|
| 31 | |
---|
| 32 | owl_global_set_no_have_config(&g); |
---|
| 33 | |
---|
| 34 | ret=stat(filename, &statbuff); |
---|
| 35 | if (ret) { |
---|
| 36 | return(0); |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | ret=perl_parse(p, NULL, 2, embedding, NULL); |
---|
| 40 | if (ret) return(-1); |
---|
| 41 | |
---|
| 42 | ret=perl_run(p); |
---|
| 43 | if (ret) return(-1); |
---|
| 44 | |
---|
| 45 | owl_global_set_have_config(&g); |
---|
| 46 | |
---|
| 47 | /* create variables */ |
---|
| 48 | perl_get_sv("owl::id", TRUE); |
---|
| 49 | perl_get_sv("owl::class", TRUE); |
---|
| 50 | perl_get_sv("owl::instance", TRUE); |
---|
| 51 | perl_get_sv("owl::recipient", TRUE); |
---|
| 52 | perl_get_sv("owl::sender", TRUE); |
---|
| 53 | perl_get_sv("owl::realm", TRUE); |
---|
| 54 | perl_get_sv("owl::opcode", TRUE); |
---|
| 55 | perl_get_sv("owl::zsig", TRUE); |
---|
| 56 | perl_get_sv("owl::msg", TRUE); |
---|
| 57 | perl_get_sv("owl::time", TRUE); |
---|
| 58 | perl_get_sv("owl::host", TRUE); |
---|
| 59 | perl_get_av("owl::fields", TRUE); |
---|
| 60 | |
---|
| 61 | /* load in owl_command() */ |
---|
| 62 | strcpy(buff, "sub owl::command { \n"); |
---|
| 63 | strcat(buff, " my $command = shift; \n"); |
---|
| 64 | strcat(buff, " push @owl::commands, $command; \n"); |
---|
| 65 | strcat(buff, "} \n"); |
---|
| 66 | perl_eval_pv(buff, FALSE); |
---|
| 67 | |
---|
| 68 | |
---|
| 69 | /* check if we have the formatting function */ |
---|
| 70 | if (perl_get_cv("owl::format_msg", FALSE)) { |
---|
| 71 | owl_global_set_config_format(&g, 1); |
---|
| 72 | } |
---|
| 73 | return(0); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | |
---|
| 77 | /* caller is responsible for freeing returned string */ |
---|
| 78 | char *owl_config_execute(char *line) { |
---|
| 79 | STRLEN n_a; |
---|
| 80 | SV *command, *response; |
---|
| 81 | AV *commands; |
---|
| 82 | int numcommands, i; |
---|
| 83 | char *out, *preout; |
---|
| 84 | |
---|
| 85 | if (!owl_global_have_config(&g)) return NULL; |
---|
| 86 | |
---|
| 87 | /* execute the subroutine */ |
---|
| 88 | response = perl_eval_pv(line, FALSE); |
---|
| 89 | |
---|
| 90 | preout=SvPV(response, n_a); |
---|
| 91 | /* leave enough space in case we have to add a newline */ |
---|
| 92 | out = owl_malloc(strlen(preout)+2); |
---|
| 93 | strcpy(out, preout); |
---|
| 94 | if (!strlen(out) || out[strlen(out)-1]!='\n') { |
---|
| 95 | strcat(out, "\n"); |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | /* execute all the commands, cleaning up the arrays as we go */ |
---|
| 99 | commands=perl_get_av("owl::commands", TRUE); |
---|
| 100 | numcommands=av_len(commands)+1; |
---|
| 101 | for (i=0; i<numcommands; i++) { |
---|
| 102 | command=av_shift(commands); |
---|
| 103 | owl_function_command_norv(SvPV(command, n_a)); |
---|
| 104 | } |
---|
| 105 | av_undef(commands); |
---|
| 106 | |
---|
| 107 | return(out); |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | char *owl_config_getmsg(owl_message *m, int mode) { |
---|
| 111 | /* if mode==1 we are doing message formatting. The returned |
---|
| 112 | * formatted message needs to be freed by the caller. |
---|
| 113 | * |
---|
| 114 | * if mode==0 we are just doing the message-has-been-received |
---|
| 115 | * thing. |
---|
| 116 | */ |
---|
| 117 | |
---|
| 118 | int i, j, len; |
---|
| 119 | char *ptr, *ptr2; |
---|
| 120 | ZNotice_t *n; |
---|
| 121 | |
---|
| 122 | if (!owl_global_have_config(&g)) return(""); |
---|
| 123 | |
---|
| 124 | /* set owl::msg */ |
---|
| 125 | n=owl_message_get_notice(m); |
---|
| 126 | ptr=owl_zephyr_get_message(n, &len); |
---|
| 127 | ptr2=owl_malloc(len+20); |
---|
| 128 | memcpy(ptr2, ptr, len); |
---|
| 129 | ptr2[len]='\0'; |
---|
| 130 | if (ptr2[len-1]!='\n') { |
---|
| 131 | strcat(ptr2, "\n"); |
---|
| 132 | } |
---|
| 133 | sv_setpv(perl_get_sv("owl::msg", TRUE), ptr2); |
---|
| 134 | owl_free(ptr2); |
---|
| 135 | |
---|
| 136 | /* set owl::zsig */ |
---|
| 137 | ptr=owl_zephyr_get_zsig(n, &len); |
---|
| 138 | if (len>0) { |
---|
| 139 | ptr2=owl_malloc(len+20); |
---|
| 140 | memcpy(ptr2, ptr, len); |
---|
| 141 | ptr2[len]='\0'; |
---|
| 142 | if (ptr2[len-1]=='\n') { /* do we really need this? */ |
---|
| 143 | ptr2[len-1]='\0'; |
---|
| 144 | } |
---|
| 145 | sv_setpv(perl_get_sv("owl::zsig", TRUE), ptr2); |
---|
| 146 | owl_free(ptr2); |
---|
| 147 | } else { |
---|
| 148 | sv_setpv(perl_get_sv("owl::zsig", TRUE), ""); |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | /* set owl::type */ |
---|
| 152 | if (owl_message_is_zephyr(m)) { |
---|
| 153 | sv_setpv(perl_get_sv("owl::type", TRUE), "zephyr"); |
---|
| 154 | } else if (owl_message_is_admin(m)) { |
---|
| 155 | sv_setpv(perl_get_sv("owl::type", TRUE), "admin"); |
---|
| 156 | } else { |
---|
| 157 | sv_setpv(perl_get_sv("owl::type", TRUE), "unknown"); |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | /* set everything else */ |
---|
| 161 | sv_setpv(perl_get_sv("owl::class", TRUE), owl_message_get_class(m)); |
---|
| 162 | sv_setpv(perl_get_sv("owl::instance", TRUE), owl_message_get_instance(m)); |
---|
| 163 | sv_setpv(perl_get_sv("owl::sender", TRUE), owl_message_get_sender(m)); |
---|
| 164 | sv_setpv(perl_get_sv("owl::realm", TRUE), owl_message_get_realm(m)); |
---|
| 165 | sv_setpv(perl_get_sv("owl::recipient", TRUE), owl_message_get_recipient(m)); |
---|
| 166 | sv_setpv(perl_get_sv("owl::opcode", TRUE), owl_message_get_opcode(m)); |
---|
| 167 | sv_setpv(perl_get_sv("owl::time", TRUE), owl_message_get_timestr(m)); |
---|
| 168 | sv_setpv(perl_get_sv("owl::host", TRUE), owl_message_get_hostname(m)); |
---|
| 169 | sv_setiv(perl_get_sv("owl::id", TRUE), owl_message_get_id(m)); |
---|
| 170 | |
---|
| 171 | /* free old @fields ? */ |
---|
| 172 | /* I don't think I need to do this, but ask marc to make sure */ |
---|
| 173 | /* |
---|
| 174 | j=av_len(perl_get_av("fields", TRUE)); |
---|
| 175 | for (i=0; i<j; i++) { |
---|
| 176 | tmpsv=av_pop(perl_get_av("fields", TRUE)); |
---|
| 177 | SvREFCNT_dec(tmpsv); |
---|
| 178 | } |
---|
| 179 | */ |
---|
| 180 | |
---|
| 181 | /* set owl::fields */ |
---|
| 182 | av_clear(perl_get_av("owl::fields", TRUE)); |
---|
| 183 | j=owl_zephyr_get_num_fields(n); |
---|
| 184 | for (i=0; i<j; i++) { |
---|
| 185 | ptr=owl_zephyr_get_field(n, i+1, &len); |
---|
| 186 | ptr2=owl_malloc(len+10); |
---|
| 187 | memcpy(ptr2, ptr, len); |
---|
| 188 | ptr2[len]='\0'; |
---|
| 189 | av_push(perl_get_av("owl::fields", TRUE), newSVpvn(ptr2, len)); |
---|
| 190 | owl_free(ptr2); |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | /* for backwards compatibilty, because I'm an idiot */ |
---|
| 194 | av_clear(perl_get_av("fields", TRUE)); |
---|
| 195 | j=owl_zephyr_get_num_fields(n); |
---|
| 196 | for (i=0; i<j; i++) { |
---|
| 197 | ptr=owl_zephyr_get_field(n, i+1, &len); |
---|
| 198 | ptr2=owl_malloc(len+10); |
---|
| 199 | memcpy(ptr2, ptr, len); |
---|
| 200 | ptr2[len]='\0'; |
---|
| 201 | av_push(perl_get_av("fields", TRUE), newSVpvn(ptr2, len)); |
---|
| 202 | owl_free(ptr2); |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | /* run the procedure corresponding to the mode */ |
---|
| 206 | if (mode==1) { |
---|
| 207 | return(owl_config_execute("owl::format_msg();")); |
---|
| 208 | } else { |
---|
| 209 | ptr=owl_config_execute("owl::receive_msg();"); |
---|
| 210 | if (ptr) owl_free(ptr); |
---|
| 211 | return(NULL); |
---|
| 212 | } |
---|
| 213 | } |
---|