20 #define INT_TO_STRING_LEN ((sizeof(int)*4*CHAR_BIT)/10 + 6)
28 void __attribute__ ((noreturn))
29 bye(int code, char* msg, ...)
33 if (current_body->tmpfile)
34 unlink(current_body->tmpfile);
38 vfprintf(stderr, msg, args);
46 set_modif_flag(int var, int flag)
48 var_tab[var].modif = flag;
52 get_modif_flag(int var)
54 return var_tab[var].modif;
67 if ((var_tab[var].value != NULL))
68 free_string(var_tab[var].value);
69 var_tab[var].modif = 0;
73 set_var(int var, char* value)
76 var_tab[var].modif = 1;
77 var_tab[var].value = xstrdup(value);
81 set_cur_mail_length_var(int len, struct list* hash)
86 pv = get_var_struct(INT_VAR_MAIL_LEN, VAR_INTERN, hash);
88 if (! pv) /* user is not interested */
91 buf = xmalloc(INT_TO_STRING_LEN);
92 sprintf(buf,"%d", len);
93 set_var(pv->varcode, buf);
98 set_exit_code_var(int code, struct list* hash)
103 pv = get_var_struct(INT_VAR_LAST_EXIT, VAR_INTERN, hash);
105 if (! pv) /* user is not interested */
108 buf = xmalloc(INT_TO_STRING_LEN);
109 sprintf(buf,"%d", code);
110 set_var(pv->varcode, buf);
115 set_last_pipe_var(char* buf, struct list* hash)
119 pv = get_var_struct(INT_VAR_LAST_EXIT, VAR_INTERN, hash);
121 if (! pv) /* user is not interested */
123 set_var(pv->varcode, buf);
130 return xstrdup(const_tab[-var]);
131 return xstrdup(var_tab[var].value);
135 regex_cmp(char* s, char* r)
140 int ovector[OVECCOUNT];
142 brum = pcre_compile(r,0,&error,&erroroffset,NULL);
146 int res = pcre_exec(brum,NULL,s,strlen(s),0,0,ovector,OVECCOUNT);
152 #define UPPER(a) ((a) >> 8)
153 #define LOWER(a) ((a) & 0xFF)
156 xcat(char* left, char* right)
158 char* res = xmalloc(strlen(left) + strlen(right) + 1);
170 fold(const char* value, int taken)
174 int len = strlen(value);
175 int newlines[len / 78 + 5];
181 if (len + taken <= 78)
182 return xstrdup(value);
183 for(i = 0; i < len; i++) {
184 if (value[i] == ' ' || value[i]=='\t')
189 newlines[newl++] = curr_ws;
191 while(value[i] && (value[i] == '\t'
199 ret = xmalloc(2*newl + len + 1);
200 for (i = 0; i < len; i++) {
201 if (newl_done == newl) {
202 strcpy(ret + pos, value + i);
205 if(i != newlines[newl_done])
206 ret[pos++] = value[i];
217 unfold(const char* u)
223 new = xmalloc(strlen(u)+1);
226 #define IS_WHITE(c) ((c) == '\t' || (c)==' ' || c=='\n')
228 while (IS_WHITE(*pu))
233 while (IS_WHITE(*pu))
246 modify_headers(struct list* headers, struct list* hash)
254 LIST_FOREACH(p, headers){
255 pv = get_var_struct(p->name, VAR_HEADER, hash);
258 u = unfold(p->value);
259 value = get_var(pv->varcode);
260 if (strcmp(u, value)){
261 set_modif_flag(pv->varcode, 0);
262 free_string(p->value);
263 p->value = fold(value,
264 strlen(p->name) + 2);
271 for (i = 0; i < HASHSIZE; i++){
272 LIST_FOREACH(pv, hash + i){
273 if (pv->type == VAR_HEADER &&
274 get_modif_flag(pv->varcode)){
276 set_modif_flag(pv->varcode, 0);
277 p = xmalloc(sizeof(struct hlist));
278 p->name = xstrdup(pv->name);
279 p->value = get_var(pv->varcode);
281 list_add_last(headers, &p->car);
288 copy_headers(struct list* orig)
290 struct list* new = xmalloc(sizeof(struct list));
291 struct hlist* po, *pn;
295 LIST_FOREACH(po, orig){
296 pn = xmalloc(sizeof(struct hlist));
297 pn->name = xstrdup(po->name);
298 pn->value = xstrdup(po->value);
301 list_add_last(new, &pn->car);
308 prepare_email(struct list* hash)
312 modify_headers(current_headers, hash);
313 em.headers = copy_headers(current_headers);
314 em.body_len = current_body->body_len;
315 em.fd = current_body->fd;
316 if (current_body->body) {
317 em.body = xmalloc(em.body_len);
318 memcpy(em.body, current_body->body, em.body_len);
321 em.tmpfile = xstrdup(current_body->tmpfile);
329 destroy_headers(struct list* h)
333 while ((p = list_del_last(h))) {
342 destroy_body(struct email* b)
353 destroy_email(struct email em)
355 destroy_headers(em.headers);
360 do_string_ternary_op(struct code* p)
362 char* l = get_var(p->u.tpop.l);
363 char* r = get_var(p->u.tpop.r);
368 result = xmalloc(INT_TO_STRING_LEN);
369 sprintf(result, "%d", regex_cmp(l, r));
372 result = xmalloc(INT_TO_STRING_LEN);
373 sprintf(result, "%d", !regex_cmp(l, r));
382 set_var(p->u.tpop.res, result);
386 do_num_ternary_op(struct code* p)
389 char* result = xmalloc(INT_TO_STRING_LEN);
391 sscanf(get_var(p->u.tpop.l),"%d", &l);
392 sscanf(get_var(p->u.tpop.r),"%d", &r);
420 res = ((l || r) && !(l && r));
438 sprintf(result, "%d", res);
439 set_var(p->u.tpop.res, result);
445 char* val = get_var(var);
452 sscanf(val, "%d", &v);
458 deliver(char* where, int copy, struct list* hash)
461 struct email em = prepare_email(hash);
463 res = deliver_local_email(where, &em);
469 bye(EX_TEMPFAIL, "Cannot save mail to mailbox %s: %m", where);
476 send_mail(char* where, int copy, struct list* hash)
481 struct email em = prepare_email(hash);
487 if ((pid = fork()) < 0)
494 res = execl("/usr/lib/sendmail", "sendmail", where, NULL);
497 write_email_to_fd(pd[1], &em);
504 bye(EX_TEMPFAIL, "Cannot forward e-mail: %m");
510 static struct procstat*
511 pipe_to(char* program, struct email* em)
516 struct pollfd pfd[2];
517 char* name = xstrdup("/tmp/umpf.XXXXXX");
522 int fd = mkstemp(name);
523 struct procstat* tmp = xmalloc(sizeof(struct procstat));
531 int res = pipe(pd_in);
538 if ((pid = fork()) < 0)
551 res = execl("/bin/sh", "sh", "-c", program, NULL);
556 pfd[1].fd = pd_in[1];
557 pfd[0].fd = pd_out[0];
558 pfd[1].events = POLLOUT;
559 pfd[0].events = POLLIN;
563 if (poll(pfd, nfds, -1) < 0)
565 if ((pfd[0].revents & POLLERR) || (pfd[1].revents & POLLERR))
568 if (pfd[0].revents & POLLIN) {
569 r = read(pfd[0].fd, buf, BUFSIZE);
574 if (pfd[0].revents & POLLHUP)
580 if (pfd[1].revents & POLLOUT) {
581 if (!chars_written) {
584 if (!chars_written) {
590 if (data_pos < chars_written) {
591 r = write(pfd[1].fd, e + data_pos,
592 chars_written - data_pos);
605 tmp->exit = WEXITSTATUS(status);
614 fix_content_length(struct list* h, int len)
618 char* clheader = "Content-Length: ";
620 LIST_FOREACH(ph, h) {
621 if (!strcasecmp("Content-Length", ph->name)) {
623 free_string(ph->value);
624 ph->value = xmalloc(INT_TO_STRING_LEN);
625 sprintf(ph->value, "%d", len);
631 buf = xmalloc(INT_TO_STRING_LEN + strlen(clheader));
632 tmpbuf = xmalloc(INT_TO_STRING_LEN);
633 sprintf(tmpbuf, "%d", len);
634 strcpy(buf, clheader);
643 do_filter(char* program, struct list* hash)
645 struct email em = prepare_email(hash);
649 f = pipe_to(program, &em);
654 lseek(f->fd, 0, SEEK_SET);
656 destroy_headers(current_headers);
657 destroy_body(current_body);
658 current_headers = make_hlist(f->fd);
659 current_body = get_body(f->fd);
660 fix_content_length(current_headers, current_body->body_len);
661 save_current_headers(hash);
662 set_cur_mail_length_var(curr_email_len, hash);
663 set_exit_code_var(f->exit, hash);
668 //FIXME: what to do with exit code when pipe failed?
673 do_pipe(char* program, struct list* hash)
675 struct email em = prepare_email(hash);
681 f = pipe_to(program, &em);
686 pos = lseek(f->fd, 0, SEEK_END);
687 lseek(f->fd, 0, SEEK_SET);
690 read(f->fd, buf, pos);
691 set_exit_code_var(f->exit, hash);
692 set_last_pipe_var(buf, hash);
702 interp(struct list* ins, struct list* hash)
708 p = (void*) ins->head.next;
709 while ((struct node*) p != &ins->head) {
712 result = get_var(p->u.set.r);
713 set_var(p->u.set.l, result);
717 p = p->u.jump.target;
720 if (eval_cond(p->u.jump_if.cond)) {
721 p = p->u.jump_if.target;
725 case OPC_JUMP_UNLESS:
726 if (!eval_cond(p->u.jump_unless.cond)) {
727 p = p->u.jump_unless.target;
746 do_num_ternary_op(p);
749 result = xmalloc(INT_TO_STRING_LEN);
750 sscanf(get_var(p->u.dpop.par),"%d", &v);
751 sprintf(result, "%d", !v);
752 set_var(p->u.dpop.res, result);
758 do_string_ternary_op(p);
761 do_pipe(get_var(p->u.arrow.what), hash);
764 do_filter(get_var(p->u.arrow.what), hash);
767 send_mail(get_var(p->u.arrow.what),
768 p->u.arrow.copy, hash);
771 deliver(get_var(p->u.arrow.what),
772 p->u.arrow.copy, hash);
778 p = (void*) ((struct node*) p)->next;
780 deliver(default_mailbox, 0, hash);
784 print_vars(struct list* hash)
789 for (i=0; i<HASHSIZE; i++){
790 LIST_FOREACH(p, hash + i)
791 printf("%s=%s\n",p->name, get_var(p->varcode));
796 save_current_headers(struct list* hash)
802 LIST_FOREACH(p, current_headers){
803 pv = get_var_struct(p->name, VAR_HEADER, hash);
806 u = unfold(p->value);
807 set_var(pv->varcode, u);
809 set_modif_flag(pv->varcode, 0);