#line 982 "../../src/builtin/snarf.m4"
/* -*- buffer-read-only: t -*- vi: set ro:
   THIS FILE IS GENERATED AUTOMATICALLY.  PLEASE DO NOT EDIT.
*/
#line 982
#ifdef HAVE_CONFIG_H
#line 982
# include <config.h>
#line 982
#endif
#line 982
#include <sys/types.h>
#line 982

#line 982
#include "mailfromd.h"
#line 982
#include "prog.h"
#line 982
#include "builtin.h"
#line 982

#line 982

#line 1022 "../../src/builtin/snarf.m4"

/* End of snarf.m4 */
#line 1 "email.bi"
/* This file is part of Mailfromd.             -*- c -*-
   Copyright (C) 2010-2022 Sergey Poznyakoff

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */


#include "mflib/email.h"

void
#line 20
bi_domainpart(eval_environ_t env)
#line 20

#line 20

#line 20 "email.bi"
{
#line 20
	
#line 20

#line 20

#line 20
char * MFL_DATASEG str;
#line 20
        
#line 20
get_string_arg(env, 0, &str);
#line 20
        
#line 20

#line 20
        adjust_stack(env, 1);
#line 20

#line 20

#line 20
	if (builtin_module_trace(BUILTIN_IDX_email))
#line 20
		prog_trace(env, "domainpart %s",str);;
#line 20

{
	char *p = strchr(str, '@');
	
#line 23
do {
#line 23
  pushs(env, p ? p+1 : str);
#line 23
  goto endlab;
#line 23
} while (0);
}
endlab:
#line 25
        env_function_cleanup_flush(env, NULL);
#line 25
	return;
#line 25
}

void
#line 27
bi_localpart(eval_environ_t env)
#line 27

#line 27

#line 27 "email.bi"
{
#line 27
	
#line 27

#line 27

#line 27
char * MFL_DATASEG str;
#line 27
        
#line 27
get_string_arg(env, 0, &str);
#line 27
        
#line 27

#line 27
        adjust_stack(env, 1);
#line 27

#line 27

#line 27
	if (builtin_module_trace(BUILTIN_IDX_email))
#line 27
		prog_trace(env, "localpart %s",str);;
#line 27

{
	char *p = strchr(str, '@');

	if (p) {
		size_t off;
		size_t size = p - str;
		char *string_space = (char*) env_data_ref(env, (off = heap_reserve(env, size + 1)));
		memcpy(string_space, str, size);
		string_space[size] = 0;
		
#line 37
do {
#line 37
  push(env, (STKVAL) (mft_size) (off));
#line 37
  goto endlab;
#line 37
} while (0);
	} else
		
#line 39
do {
#line 39
  pushs(env, str);
#line 39
  goto endlab;
#line 39
} while (0);
}
endlab:
#line 41
        env_function_cleanup_flush(env, NULL);
#line 41
	return;
#line 41
}

void
#line 43
bi_dequote(eval_environ_t env)
#line 43

#line 43

#line 43 "email.bi"
{
#line 43
	
#line 43

#line 43

#line 43
char * MFL_DATASEG str;
#line 43
        
#line 43
get_string_arg(env, 0, &str);
#line 43
        
#line 43

#line 43
        adjust_stack(env, 1);
#line 43

#line 43

#line 43
	if (builtin_module_trace(BUILTIN_IDX_email))
#line 43
		prog_trace(env, "dequote %s",str);;
#line 43

{
	size_t len = strlen(str);
	if (len > 1 && str[0] == '<' && str[len-1] == '>') {
		size_t off;
		char *string_space = (char*) env_data_ref(env, (off = heap_reserve(env, len - 1)));
		memcpy(string_space, str + 1, len - 2);
		string_space[len - 2] = 0;
		
#line 51
do {
#line 51
  push(env, (STKVAL) (mft_size) (off));
#line 51
  goto endlab;
#line 51
} while (0);
	} else
		
#line 53
do {
#line 53
  pushs(env, str);
#line 53
  goto endlab;
#line 53
} while (0);
}
endlab:
#line 55
        env_function_cleanup_flush(env, NULL);
#line 55
	return;
#line 55
}

void
#line 57
bi_email_map(eval_environ_t env)
#line 57

#line 57

#line 57 "email.bi"
{
#line 57
	
#line 57

#line 57

#line 57
char *  str;
#line 57
        
#line 57
get_string_arg(env, 0, &str);
#line 57
        
#line 57

#line 57
        adjust_stack(env, 1);
#line 57

#line 57

#line 57
	if (builtin_module_trace(BUILTIN_IDX_email))
#line 57
		prog_trace(env, "email_map %s",str);;
#line 57

{
	mu_address_t addr;
	int f = 0;
	int rc = mu_address_create_hint(&addr, str, NULL, 0);
	if (rc)
		
#line 63
do {
#line 63
  push(env, (STKVAL)(mft_number)(0));
#line 63
  goto endlab;
#line 63
} while (0);
	if (addr->next)
		f |= EMAIL_MULTIPLE;
	if (addr->comments)
		f |= EMAIL_COMMENTS;
	if (addr->personal)
		f |= EMAIL_PERSONAL;
	if (addr->local_part)
		f |= EMAIL_LOCAL;
	if (addr->domain)
		f |= EMAIL_DOMAIN;
	if (addr->route)
		f |= EMAIL_ROUTE;
	mu_address_destroy(&addr);
	
#line 77
do {
#line 77
  push(env, (STKVAL)(mft_number)(f));
#line 77
  goto endlab;
#line 77
} while (0);
}
endlab:
#line 79
        env_function_cleanup_flush(env, NULL);
#line 79
	return;
#line 79
}

#line 982 "../../src/builtin/snarf.m4"

#line 982

#line 982

#line 982
void
#line 982
email_init_builtin(void)
#line 982
{
#line 982
	
#line 982
	#line 20 "email.bi"
va_builtin_install_ex("domainpart", bi_domainpart, 0, dtype_string, 1, 0, 0|0, dtype_string);
#line 27 "email.bi"
va_builtin_install_ex("localpart", bi_localpart, 0, dtype_string, 1, 0, 0|0, dtype_string);
#line 43 "email.bi"
va_builtin_install_ex("dequote", bi_dequote, 0, dtype_string, 1, 0, 0|0, dtype_string);
#line 57 "email.bi"
va_builtin_install_ex("email_map", bi_email_map, 0, dtype_number, 1, 0, 0|0, dtype_string);

#line 982 "../../src/builtin/snarf.m4"
	
#line 982
}
#line 982 "../../src/builtin/snarf.m4"

