source: readconfig.c @ 1aee7d9

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