/**
 * @function ferite_init_error_system
 * @declaration void ferite_init_error_system( FeriteScript *script, FeriteNamespace *ns )
 * @brief Setup the special error handling class on a script
 * @param FeriteScript *script The current script
 * @param FeriteNamespace *ns	 The namespace in which the class should be registered
 */
void ferite_init_error_system( FeriteScript *script, FeriteNamespace *ns )
{
	FeriteClass *ferite_error_class = NULL;
	FeriteVariable *var, *errobj = NULL;
	FeriteNamespaceBucket *nsb = NULL;
	
	FE_ENTER_FUNCTION;
	ferite_error_class = ferite_register_class( script, ns, "Error" );
	ferite_register_class_variable( script, ferite_error_class, "num", ferite_create_number_long_variable( script, "num", 0, FE_STATIC ), 0 );
	ferite_register_class_variable( script, ferite_error_class, "str", ferite_create_string_variable( script, "str", NULL, FE_STATIC ), 0 );
	ferite_register_class_variable( script, ferite_error_class, "backtrace", ferite_create_uarray_variable( script, "backtrace", 32, FE_STATIC ), 0 );

	fe_create_cls_fnc( script, ferite_error_class, "constructor", ferite_error_constructor_sn, "sn", FE_FALSE );
	fe_create_cls_fnc( script, ferite_error_class, "constructor", ferite_error_constructor_s, "s", FE_FALSE );
	fe_create_cls_fnc( script, ferite_error_class, "backtrace", ferite_error_backtrace, "", FE_FALSE );
	
	nsb = ferite_find_namespace( script, script->mainns, "err", FENS_VAR );
	if( nsb != NULL ) {
		errobj = ferite_build_object( script, ferite_error_class );
		var = nsb->data;
		VAO(var) = VAO(errobj);
		VAO(errobj) = NULL;
		ferite_variable_destroy( script, errobj );
	}
	
	FE_LEAVE_FUNCTION(NOWT);
}
int create_address(FeriteScript *script, FeriteVariable *object, ADDRESS *addr) {
	FeriteVariable *v;
	if( addr->mailbox ) {
		v = fe_new_str( "mailbox", addr->mailbox, 0, FE_CHARSET_DEFAULT );
		if( v == NULL ) {
			//set_error_string( script, self,"Internal ferite error" );
			return -1;
		}
		ferite_object_set_var(script, VAO(object), "mailbox", v );
	}

	if( addr->host ) {
		v = fe_new_str("host", addr->host, 0, FE_CHARSET_DEFAULT );
		if( v == NULL ) {
			//set_error_string( script, self, "Internal error" );
			return -1;
		}
		ferite_object_set_var(script, VAO(object), "host", v );
	}

	if( addr->personal ) {
		v = fe_new_str("name", addr->personal, 0, FE_CHARSET_DEFAULT );
		if( v == NULL ) {
			//set_error_string( script, self, "Internal error" );
			return -1;
		}
		ferite_object_set_var(script, VAO(object), "name", v );
	}

	return 0;
}
FeriteVariable *create_ferite_header_object( FeriteScript *script, ENVELOPE *env )
{
    FeriteVariable *header = NULL, *v = NULL;
    FeriteNamespaceBucket *nsb = NULL;
    #define BUFSIZE 1025
    char buf[BUFSIZE];
    int i = 0;

    if( env == NULL)
      return NULL;

    ADDRESS *address_source[6] = { env->from, env->reply_to, env->cc, env->bcc, env->sender, env->to };
    char *address_target[6] = { "from", "reply_to" , "cc", "bcc", "sender" , "to" };
    int n_address_source = 6;

    char *source[5] = { env->subject, env->date, env->message_id, env->in_reply_to, env->references };
    char *target[5] = { "subject", "date", "ID", "in_reply_to", "references" };
    int  n_source = 5;

    nsb = ferite_find_namespace( script, script->mainns, "Mail.MessageHeader", FENS_CLS );
    if( nsb == NULL)
	    return NULL;

    header = ferite_build_object( script, (FeriteClass *)nsb->data );
    if( header == NULL )
	    return NULL;


    //Parse each address structure in address_source[] and write the result to the
    //corresonding object member in address_target[]
    for(i=0; i < n_address_source; i++) {
	    v = create_address_list( script, address_source[i] );
	    if( v == NULL ) {
		    //set_error_string( script, self, "Couldn't create address list" );
		    return NULL;
	    }
	    ferite_object_set_var(script, VAO(header), address_target[i], v );
    }

    //Copy  each header-field in source[] to the
    //corresonding object member in address_target[]
    for(i=0; i< n_source; i++) {
	    if( source[i] ) {
		    v = fe_new_str(target[i], source[i] , 0, FE_CHARSET_DEFAULT );
		    if( v == NULL ) {
			    //set_error_string( script, self, "Couldn't create mail header" );
			    return NULL;
		    }
		    ferite_object_set_var(script, VAO(header), target[i], v );
		    if( debug_cmail_module && strcmp( target[i], "subject" ) == 0 ) {
				output_printf(OUTPUT_DEBUG,"module.mail: Processing email with subject: '%s'", source[i] );
		    }
	    }
    }
    return header;
}
Exemple #4
0
FeriteVariable *create_element_node( FeriteScript *script, xmlDocPtr doc, xmlNodePtr node ) 
{
    FeriteVariable *obj = NULL;
    FeriteClass *cls = NULL;
    
    if(( cls = ferite_find_class( script, script->mainns, "XML.Element" ) ) == NULL) {
        ferite_error( script , 0, "Can't locate class XML.Element" ); 
    }
    obj = ferite_new_object( script, cls, NULL );
    
    ((XMLDoc *)VAO(obj)->odata)->doc = doc; 
    ((XMLDoc *)VAO(obj)->odata)->node = node;
    
    return obj;
}
FeriteVariable *ferite_amtarray_get_exceptions( FeriteScript *script, FeriteAMTArray *array, FeriteVariable *index, int want_exceptions )
{
	FeriteVariable *rval = NULL;
	
	FE_ENTER_FUNCTION;
	switch(F_VAR_TYPE(index)) {
		case F_VAR_LONG:
			rval = ferite_amtarray_get_index( script, array, VAI(index) );
			if( want_exceptions && rval == NULL ) {
				ferite_error( script, 0, "Index does not exist '%d' in array.\n", VAI(index) );
				FE_LEAVE_FUNCTION( NULL );
			}
			break;
		case F_VAR_DOUBLE:
			rval = ferite_amtarray_get_index( script, array, (int)floor(VAF(index)) );
			if( want_exceptions && rval == NULL ) {
				ferite_error( script, 0, "Index does not exist '%d' in array.\n", (int)floor(VAF(index)) );
				FE_LEAVE_FUNCTION( NULL );
			}
			break;
		case F_VAR_STR:
			rval = ferite_amtarray_get_hash( script, array, FE_STR2PTR(index));
			if( want_exceptions && rval == NULL ) {
				ferite_error( script, 0, "Invalid index: no such key '%s' in array.\n", FE_STR2PTR(index) );
				FE_LEAVE_FUNCTION( NULL );
			}
			break;
		case F_VAR_OBJ: {
			FeriteVariable *hash_value = NULL, *return_value = NULL;
			FeriteFunction *hash = ferite_object_get_function_for_params( script, VAO(index), "hash", NULL );	    
			hash_value = ferite_call_function( script, VAO(index), NULL, hash, NULL );

			if( F_VAR_TYPE(hash_value) != F_VAR_STR ) {
				if( want_exceptions )
					ferite_error( script, 0, "Invalid index: unable to get string key from object to access array.\n" );
				FE_LEAVE_FUNCTION(NULL);
			}
			return_value = ferite_amtarray_get_hash( script, array, FE_STR2PTR(hash_value) );
			if( want_exceptions && return_value == NULL ) {
				ferite_error( script, 0, "Invalid index: no such key '%s' (otained from object hash) in array.\n", FE_STR2PTR(hash_value) );
			}
			ferite_variable_destroy( script, hash_value );
			FE_LEAVE_FUNCTION( return_value );	    
		}
	}
	FE_LEAVE_FUNCTION( rval );
}
FeriteVariable *create_ferite_mail_object( FeriteScript *script, FeriteVariable *header, FeriteVariable *content )
{
    FeriteVariable *mailobject;
    FeriteNamespaceBucket *nsb;

    nsb = ferite_find_namespace( script, script->mainns, "Mail.Message", FENS_CLS );
    if( nsb == NULL )
	    return NULL;
    mailobject = ferite_build_object( script, (FeriteClass *)nsb->data );
    if( mailobject == NULL )
	    return NULL;
	if( header )
	    ferite_object_set_var(script, VAO(mailobject), "header", header );
	if( content )
	    ferite_object_set_var(script, VAO(mailobject), "content", content );
    return mailobject;
}
BODY *create_imap_content_object(FeriteScript* script, FeriteVariable* fe_parent)
{
	FeriteVariable *parts_list,*fe_child,*v;
	BODY *root,*new_body;
	PART *new_part,*last_part =  NULL;
	int i=0;

	//Get array of child objects
        parts_list = ferite_hash_get( script, VAO(fe_parent)->variables->variables, "parts");
	//This is true if the top level is not a multipart
	if( parts_list == NULL )
		return( create_imap_content_leaf( script, fe_parent ));


	root = mail_newbody();
        root->type=TYPEMULTIPART;
	v = ferite_hash_get( script, VAO(fe_parent)->variables->variables, "subtype");
	RETURN_IF_NULL(v);
	if( VAS(v)->data )
		root->subtype = cpystr( VAS(v)->data );

        //Loop trough child array
        i=0;
	while( i < VAUA(parts_list)->size) {
		new_part = mail_newbody_part();
		fe_child = ferite_uarray_get_index( script, VAUA(parts_list) , i );
		RETURN_IF_NULL(fe_child);

		// Is it a multipart or a leaf?
		if( ferite_hash_get( script, VAO(fe_child)->variables->variables, "parts"))
			new_body = create_imap_content_object( script , fe_child );
		else
			new_body = create_imap_content_leaf( script, fe_child );
		RETURN_IF_NULL(new_body);

		new_part->body = *new_body; //Fixme, this sucks

		if( last_part == NULL )
			root->nested.part = last_part = new_part;
		else
			last_part = last_part->next = new_part;
		i++;
	}
	return root;
}
Exemple #8
0
FeriteVariable *system_call_tm( FeriteScript *script, struct tm *tm )
{
    FeriteVariable *object = NULL, *pointer = NULL, **args = NULL;
    FeriteClass *cls = NULL;
    FeriteFunction *function = NULL;

    if((cls = ferite_find_class( script, script->mainns, "Date" )) != NULL)
    {
        pointer = system_create_pointer_var( script, "struct::tm", tm );
        object = ferite_new_object( script, cls, NULL );
        function = ferite_object_get_function( script, VAO(object), "__RegisterFromPointer__" );
        args = ferite_create_parameter_list_from_data( script, "o", VAO(pointer) );
        ferite_variable_destroy( script, ferite_call_function( script, VAO(object), NULL, function, args ) );
        ferite_variable_destroy( script, pointer );
        ferite_delete_parameter_list( script, args );
        FE_RETURN_VAR( object );
    }
    FE_RETURN_NULL_OBJECT;
}
/**
 * @function ferite_raise_script_error
 * @declaration void ferite_raise_script_error( FeriteScript *script, int err, char *fmt, ... )
 * @brief Raise an exception within the ferite engine.
 * @param FeriteScript *script The running script
 * @param int err	The error code
 * @param char *fmt	The format of the error string
 * @description Use the same formating codes as printf with this function
 */
void ferite_raise_script_error( FeriteScript *script, int err, char *fmt, ... )
{
	FeriteNamespaceBucket *nsb = NULL;
	FeriteVariable *global_error_object = NULL, *new_error_object = NULL, *backtrace = NULL;
	FeriteVariable *error_object_str = NULL, *error_object_num = NULL, *error_object_backtrace = NULL;
	FeriteBuffer *error_buffer = NULL;
	char *msg;
	va_list ap;

	FE_ENTER_FUNCTION;
	va_start( ap, fmt );

	error_buffer = ferite_buffer_new(script, 0);
	ferite_buffer_vprintf( script, error_buffer, fmt, &ap );
	msg = ferite_buffer_get( script, script->error, NULL );
	
	FUD(("ERROR RAISED: %s %d\n", msg, err ));

	nsb = ferite_namespace_element_exists( script, script->mainns, "err" );
	FE_ASSERT( nsb && nsb->type == FENS_VAR );
	global_error_object = nsb->data;
	script->error_state = FE_ERROR_THROWN;

	if( VAO(global_error_object) == NULL )
	{
		nsb = ferite_namespace_element_exists( script, script->mainns, "Error" );
		if( nsb == NULL )
		{
			FE_LEAVE_FUNCTION( NOWT );
			exit(1);
		}
		new_error_object = ferite_new_object( script, nsb->data, NULL );
		VAO(global_error_object) = VAO(new_error_object);
		FINCREF(VAO(global_error_object));
		ferite_variable_destroy( script, new_error_object );
	}

	error_object_str = ferite_object_get_var( script, VAO(global_error_object), "str" );
	ferite_str_set( script, VAS(error_object_str), msg, strlen(msg), FE_CHARSET_DEFAULT );
	ffree( msg );

	error_object_num = ferite_object_get_var( script, VAO(global_error_object), "num" );
	VAI(error_object_num) = err;
	
	backtrace = ferite_generate_backtrace( script, FE_FALSE );
	error_object_backtrace = ferite_object_get_var( script, VAO(global_error_object), "backtrace");
	ferite_variable_fast_assign( script, error_object_backtrace, backtrace );
	
	ferite_buffer_delete( script, error_buffer );
	
	FE_LEAVE_FUNCTION( NOWT );
}
ADDRESS *create_imap_address( FeriteScript* script, FeriteVariable* fe_object){
	ADDRESS *root=NULL, *newaddr;
	FeriteVariable *fe_list,*v,*fe_addr;
	int i;

	RETURN_IF_NULL(fe_object && VAO(fe_object));

	fe_list = ferite_hash_get( script, VAO(fe_object)->variables->variables, "list" );
	RETURN_IF_NULL(fe_list);

	for( i=0; i<  VAUA(fe_list)->size; i++ ){
		fe_addr  = ferite_uarray_get_index( script, VAUA(fe_list) , i );
		RETURN_IF_NULL(fe_addr);
		if(root == NULL)
			root = newaddr = mail_newaddr();
		else
			newaddr = newaddr->next =  mail_newaddr();

		v =  ferite_hash_get( script, VAO(fe_addr)->variables->variables , "mailbox");
		RETURN_IF_NULL(v);
		if(VAS(v)->data)
			newaddr->mailbox = cpystr(VAS(v)->data);


		v =  ferite_hash_get( script, VAO(fe_addr)->variables->variables , "host");
		RETURN_IF_NULL(v);
		if(VAS(v)->data)
			newaddr->host = cpystr(VAS(v)->data);


		v =  ferite_hash_get( script, VAO(fe_addr)->variables->variables , "name");
		RETURN_IF_NULL(v);
		if(VAS(v)->data)
			newaddr->personal = cpystr(VAS(v)->data);
	}
	return root;
}
G_Data QuadFactory::GetData() {
	const std::array<vec3, 4> vertices = {
		vec3(-1,-1,0),
		vec3(1,-1,0),
		vec3(1,1,0),
		vec3(-1,1,0)
	};

	// Indices
	// right now these only work for triangle fan
	const std::array<uint32_t, 4> indices = {
		0,1,2,3
	};

	// For quads just make the VAO once
	GLuint VAO(0);
	if (m_CachedVAOs.size())
		VAO = m_CachedVAOs.begin()->second;

	// If the VAO doesn't exist, create it	
	else {
		// Generate bufferse for vertices and indices
		glGenVertexArrays(1, &VAO);
		glBindVertexArray(VAO);
		std::array<GLuint, 2> buffers;
		glGenBuffers(buffers.size(), buffers.data());

		//vertices
		glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
		glBufferData(GL_ARRAY_BUFFER, sizeof(vec3)*vertices.size(), vertices.data(), GL_STATIC_DRAW);
		glEnableVertexAttribArray(G_Data::GetPosHandle());
		glVertexAttribPointer(G_Data::GetPosHandle(), 3, GL_FLOAT, 0, 0, 0);

		//indices
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
		glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(uint32_t)*indices.size(), indices.data(), GL_STATIC_DRAW);

		glBindVertexArray(0);
		m_CachedVAOs["quad"] = VAO;
	}

	// Create MV given current state
	mat4 MV = glm::translate(m_Trans)*glm::mat4_cast(m_Rot)*glm::scale(m_Scale);

	// return info given current state
	return{ VAO, GLuint(indices.size()), m_Color, MV };
}
FeriteVariable *create_address_list(FeriteScript *script, ADDRESS *root) {
    FeriteNamespaceBucket *nsb;
	FeriteVariable *address_list, *item_list, *item;
	ADDRESS *curr;
	int error;

	nsb = ferite_find_namespace( script, script->mainns, "Mail.AddressList", FENS_CLS );
	if( nsb == NULL )
		return NULL;
	address_list = ferite_build_object( script, (FeriteClass *)nsb->data );
	if( address_list == NULL )
		return NULL;

	//Get the array (member of address_list) and populate it with
	//the entries in the linked list current
	item_list = ferite_hash_get( script, VAO(address_list)->variables->variables, "list" );

	nsb = ferite_find_namespace( script, script->mainns, "Mail.Address", FENS_CLS );
	if( nsb == NULL ) {
		return NULL;
	}

	curr=root;
	while( curr ) {
		item =  ferite_build_object( script, (FeriteClass *)nsb->data );
		if( item == NULL )
			return NULL;
		error = create_address( script, item, curr);
		if(error) {
			return NULL;
		}
		ferite_uarray_add( script, VAUA(item_list), item, NULL , FE_ARRAY_ADD_AT_END);
		curr = curr->next;
	}
	return address_list;
}
/**
 * @function ferite_set_error
 * @declaration void ferite_set_error( FeriteScript *script, int num, char *fmt, ... )
 * @brief Same as ferite_error except this wont raise an exception at runtime
 */
void ferite_set_error( FeriteScript *script, int num, char *fmt, ... )
{
	FeriteNamespaceBucket *nsb = NULL;
	FeriteVariable *global_error_object = NULL, *new_error_object = NULL;
	FeriteVariable *errstr = NULL, *erno = NULL;
	va_list ap;
	char *buf = NULL;

	FE_ENTER_FUNCTION;

	if( !script->is_being_deleted && (script->parent == NULL || !script->parent->is_being_deleted) )
	{
		buf = fmalloc( 4096 );
		va_start( ap, fmt );
		vsprintf( buf, fmt, ap );

		nsb = ferite_namespace_element_exists( script, script->mainns, "err" );
		FE_ASSERT( nsb && nsb->type == FENS_VAR );
		global_error_object = nsb->data;

		if( VAO(global_error_object) == NULL )
		{
			nsb = ferite_namespace_element_exists( script, script->mainns, "Error" );
			new_error_object = ferite_new_object( script, nsb->data, NULL );
			VAO(global_error_object) = VAO(new_error_object);
			FINCREF(VAO(global_error_object));
			ferite_variable_destroy( script, new_error_object );
		}

		errstr = ferite_object_get_var( script, VAO(global_error_object), "str" );
		ferite_str_set( script, VAS(errstr), buf, strlen(buf), FE_CHARSET_DEFAULT );

		erno = ferite_object_get_var( script, VAO(global_error_object), "num" );
		VAI(erno) = num;

		ffree( buf );
		va_end( ap );
	}
	FE_LEAVE_FUNCTION( NOWT );
}
Exemple #14
0
/**
 * @function ferite_new_object
 * @declaration FeriteVariable *ferite_new_object( FeriteScript *script, FeriteClass *nclass, FeriteVariable **plist)
 * @brief Create a new object - the C equivalent to the ferite 'new' keyword.
 * @param FeriteScript *script The script to create the object within.
 * @param FeriteClass *nclass The class to instantiate an object from
 * @param FeriteVariable **plist The parameters to be passed to the objects constructor. This can be NULL for an empty
		  list.
 * @return A variable that references the newly created object. 
 * @warning The return variable is marked as disposable. If you wish to retain a reference and return the object from a
	native function you must 'UNMARK_VARIABLE_AS_DISPOSABLE'
 */
FeriteVariable *ferite_new_object( FeriteScript *script, FeriteClass *nclass, FeriteVariable **plist)
{
	FeriteVariable *ptr = NULL, *rval = NULL;
	FeriteFunction *func = NULL;
	FeriteVariable **params = NULL;
	
	FE_ENTER_FUNCTION;
	if(nclass != NULL)
	{		
		if( nclass->state == FE_ITEM_IS_ABSTRACT )
		{
			ferite_error( script, 0, "You can't create instances of the abstract class %s\n", nclass->name );
			FE_LEAVE_FUNCTION( NULL );
		}
		else if( nclass->state == FE_ITEM_IS_PROTOCOL )
		{
			ferite_error( script, 0, "You can't create instances of the protocol %s\n", nclass->name );
			FE_LEAVE_FUNCTION( NULL );
		}
		
		if( plist != NULL )
			params = plist;
		else
			params = ferite_create_parameter_list( script,3 );
			
		ptr = ferite_build_object(script,nclass);

		FUD(("NEWOBJECT: Marking as disposable\n" ));
		MARK_VARIABLE_AS_DISPOSABLE( ptr );
		
		FUD(("NEWOBJECT: Searching for constructor\n" ));
		func = ferite_find_constructor( script, VAO(ptr), params );
		if( func != NULL ) {
			FUD(("OPS: Calling constructor in class %s\n", nclass->name ));
			
			if( func->type == FNC_IS_EXTRL ) {
				EXTERNAL_ENTER(func);
				rval = (func->fncPtr)( script, VAO(ptr), NULL, func, params );
				EXTERNAL_EXIT();
			}
			else
				rval = ferite_script_function_execute( script, VAO(ptr), NULL, func, params );
			
			if( rval == NULL || (rval != NULL && F_VAR_TYPE(rval) == F_VAR_OBJ && VAO(rval) == NULL) )
			{
				if( rval == NULL ) {
					/* So that I don't forget again, this is if the function returns NULL, not if the
					 * ferite function called returns null */
					ferite_error( script, 0, "Unable to instantiate object from class '%s'\n", nclass->name );
				}
				/* clean up the class */
				ferite_delete_object_variable_list( script, VAO(ptr)->variables );
				VAO(ptr)->variables = NULL;
				VAO(ptr)->functions = NULL;
				VAO(ptr)->klass = NULL;
				FDECREF(VAO(ptr));
				
				/* set our return val to NULL */
				VAO(ptr) = NULL;
			}
			if( rval != NULL )
				ferite_variable_destroy( script, rval );
		}
		else			
			ferite_error( script, 0, "Unable to find constructor of the class '%s' for the given parameters\n", nclass->name );
		
		if( plist == NULL )
			ferite_delete_parameter_list( script, params );	   
	}
	FE_LEAVE_FUNCTION( ptr );
}
ENVELOPE *create_imap_envelope( FeriteScript *script, FeriteVariable *header ){

	ENVELOPE *env = mail_newenvelope();
	FeriteVariable *v;
	ADDRESS *a;
	int i;

	v = ferite_hash_get( script, VAO(header)->variables->variables, "to" );
	ERROR_IF_NULL(v, script, "Unable to create imap envelope - unable to find to address");
	a = create_imap_address( script, v );
	if(a)
		env->to = a;

	v = ferite_hash_get( script, VAO(header)->variables->variables, "from" );
	ERROR_IF_NULL(v, script, "Unable to create imap envelope - unable to find from address");
	a = create_imap_address( script, v );
	if(a)
		env->from = a;

	v = ferite_hash_get( script, VAO(header)->variables->variables, "cc" );
	ERROR_IF_NULL(v, script, "Unable to create imap envelope - unable to find cc address");
	a = create_imap_address( script, v );
	if(a)
		env->cc = a;

	v = ferite_hash_get( script, VAO(header)->variables->variables, "bcc" );
	ERROR_IF_NULL(v, script, "Unable to create imap envelope - unable to find bcc address");
	a = create_imap_address( script, v );
	if(a)
		env->bcc = a;

	v = ferite_hash_get( script, VAO(header)->variables->variables, "return_path" );
	if(v) {
		a = create_imap_address( script, v );
		if(a)
			env->return_path = a;
	}
	if(env->return_path == NULL) {
		v = ferite_hash_get( script, VAO(header)->variables->variables, "from" );
		ERROR_IF_NULL(v, script, "Unable to create imap envelope - unable to find create return path");
		a = create_imap_address( script, v );
		if(a)
			env->return_path = a;
	}

	v = ferite_hash_get( script, VAO(header)->variables->variables, "sender" );
	ERROR_IF_NULL(v, script, "Unable to create imap envelope - unable to find sender");
	a = create_imap_address( script, v );
	if(a)
		env->sender = a;

	v = ferite_hash_get( script, VAO(header)->variables->variables, "subject" );
	ERROR_IF_NULL(v, script, "Unable to create imap envelope - unable to find subject");
	env->subject = cpystr( VAS(v)->data );
	RETURN_IF_NULL( env->subject );

	v = ferite_hash_get( script, VAO(header)->variables->variables, "in_reply_to" );
	ERROR_IF_NULL(v, script, "Unable to create imap envelope - unable to find in reply to header");
	env->in_reply_to = cpystr( VAS(v)->data );
	RETURN_IF_NULL( env->in_reply_to );


	v = ferite_hash_get( script, VAO(header)->variables->variables, "ID" );
	ERROR_IF_NULL(v, script, "Unable to create imap envelope - unable to find proposed message id");
	env->message_id = cpystr( VAS(v)->data );

	return env;
}
BODY*  create_imap_content_leaf( FeriteScript *script, FeriteVariable *leaf ){
	BODY *body = NULL;
	FeriteVariable *v = NULL;

	body =  mail_newbody();

	// fixme NULL/0 ?
	v = ferite_hash_get(script,VAO(leaf)->variables->variables,"encoding");
	RETURN_IF_NULL(v);
	body->encoding=VAI(v);

	v = ferite_hash_get( script, VAO(leaf)->variables->variables, "content");
	RETURN_IF_NULL(v);

	//fixme memcopy? Encode?
	body->contents.text.data =strdup(VAS(v)->data);
	body->contents.text.size =strlen(VAS(v)->data); //fixme

	v = ferite_hash_get( script, VAO(leaf)->variables->variables, "type" );
	RETURN_IF_NULL(v);
	body->type=VAI(v);

	v = ferite_hash_get(script,VAO(leaf)->variables->variables,"subtype");
	RETURN_IF_NULL(v);
	body->subtype=cpystr( VAS(v)->data );

	v = ferite_hash_get(script,VAO(leaf)->variables->variables,"ID");
	RETURN_IF_NULL(v);
	if( VAS(v)->length > 0 ) {
		body->id = cpystr( VAS(v)->data );
	}

	v  = ferite_hash_get(script,VAO(leaf)->variables->variables,"filename");
	RETURN_IF_NULL(v);
	if( strlen(VAS(v)->data)) {
		PARAMETER *param = mail_newbody_parameter();
		param->attribute = cpystr("filename");
		param->value=cpystr( VAS(v)->data );

		body->disposition.parameter = param;
		body->disposition.type = cpystr("attachment");
	}

	v = ferite_hash_get( script, VAO(leaf)->variables->variables, "filepath" );
	RETURN_IF_NULL(v);
	if( strlen(VAS(v)->data) ) {
		off_t size;
		int fd;
		char *buf;
		struct stat statinfo;

		fd = open(VAS(v)->data,O_RDONLY);
		if( fd == -1 ) {
			char buffer[2048];
			sprintf(buffer, "Unable to create email content leaf, unable to open file: %s", VAS(v)->data);
			ERROR_IF_NULL(NULL, script, buffer);
		}
		size=lseek(fd,0,SEEK_END);
		lseek(fd,0,SEEK_SET);
		buf=malloc(size*sizeof(char)+1);
		if(buf){
			read(fd,buf,size);
			body->contents.text.data=buf;
			body->contents.text.size=size;
			close(fd);
		}
		else{
			ferite_error(script, 0, "Out of memory\n");
			close(fd);
			return NULL;
		}
	}

	v = ferite_hash_get(script,VAO(leaf)->variables->variables, "charset");
	RETURN_IF_NULL(v);
	if( strlen(VAS(v)->data) ) {
		PARAMETER *param = mail_newbody_parameter();
		if( body->parameter )
			param->next = body->parameter;
		body->parameter=param;

		body->parameter->attribute=cpystr("CHARSET");
		body->parameter->value=cpystr(VAS(v)->data);
	}

	return body;
}
FeriteVariable *create_ferite_content_object( FeriteScript *script, MAILSTREAM *stream, BODY *body, int msgno, char *sec )
{
	if( body != NULL ) {
		FeriteVariable *object = NULL,*v = NULL;
		FeriteNamespaceBucket *nsb = NULL;
		char *object_name = NULL;

		object_name = ( body->type == TYPEMULTIPART ) ? "Mail.MessageMultiPart" : "Mail.MessagePart" ;

		nsb = ferite_find_namespace( script, script->mainns, object_name , FENS_CLS );
		if( nsb == NULL )
			return NULL;
		object = ferite_build_object( script, (FeriteClass *)nsb->data );
		if( object == NULL )
			return NULL;

		v  = fe_new_lng("type",body->type);
		ferite_object_set_var(script, VAO(object), "type", v);
		output_printf(OUTPUT_DEBUG,"module.mail: setting type %d", body->type);
		if( body->subtype ) {
			v = fe_new_str( "subtype", body->subtype, 0, FE_CHARSET_DEFAULT );
			ferite_object_set_var( script, VAO(object), "subtype", v );
		}

		if( body->type == TYPEMULTIPART ) {
			PART *part = NULL;
			int i = 0;
			char sec2[200];
			FeriteVariable *ret = NULL, *parts = NULL;

			parts = ferite_hash_get(script,VAO(object)->variables->variables,"parts");
			part = body->nested.part;
			while( part ) {
				i++;
				if( sec ) {
					snprintf(sec2,200,"%s.%d",sec,i);
				} else {
					snprintf(sec2,200,"%d",i);
				}
				ret = create_ferite_content_object( script, stream, &part->body, msgno, sec2 );
				ferite_uarray_add( script, VAUA(parts), ret , NULL, FE_ARRAY_ADD_AT_END );
				part = part->next;
			}
			v = fe_new_lng("nparts", i);
			ferite_object_set_var(script, VAO(object), "nparts", v );
		}
		else
		{
			long len = 0,len2 = 0;
			unsigned char *buf = NULL, *buf2 = NULL;
			SIZEDTEXT src, dest;
			FeriteVariable *v = NULL;
			PARAMETER *param = NULL;
			int i = 0;

			if( sec == NULL )
				sec = "1";
			buf = mail_fetchbody( stream, msgno, sec, &len );

			switch(body->encoding){
				case ENCQUOTEDPRINTABLE:
					if( debug_cmail_module )
						output_printf(OUTPUT_DEBUG,"module.mail: Decoding from encoded quotable");
					buf2 = rfc822_qprint(buf,len,&len2);
					break;
				case ENCBASE64: 
					if( debug_cmail_module )
						output_printf(OUTPUT_DEBUG,"module.mail: Decoding from base64");
					buf2=rfc822_base64(buf,len,&len2);
					break;
				default: 
					buf2=buf;
					len2=len;
			}

			if( debug_cmail_module ) {
				output_printf(OUTPUT_DEBUG,"module.mail: id: %s, description: %s", body->id, body->description);
				output_printf(OUTPUT_DEBUG,"module.mail: block type: %d.%s", body->type, body->subtype);
			}
			if( body->parameter ) /* Try and get the content type correctly */ {
				PARAMETER *ptr = body->parameter;
				while( ptr != NULL ) {
					if( caseless_compare( ptr->attribute, "charset" ) ) {
						if( debug_cmail_module )
							output_printf(OUTPUT_DEBUG,"module.mail: Found content type for block: %s", ptr->value);
						v = fe_new_str("charset", ptr->value, 0, FE_CHARSET_DEFAULT);
						ferite_object_set_var(script, VAO(object), "charset", v);
					}
					ptr = ptr->next;
				}
			}
			v = fe_new_bin_str("content", buf2, len2, FE_CHARSET_DEFAULT );
			ferite_object_set_var(script, VAO(object), "content", v );

			v = fe_new_lng("encoding", body->encoding );
			ferite_object_set_var(script, VAO(object), "encoding", v );

			if( body->id ) {
				v = fe_new_str("ID", body->id, strlen(body->id), FE_CHARSET_DEFAULT);
				ferite_object_set_var(script, VAO(object), "ID", v);
			}
			
			if( body->disposition.type && strcasecmp(body->disposition.type, "attachment") == 0) {
				param = body->disposition.parameter;
				while(param){
					if( param->attribute && ((strcasecmp(param->attribute,"filename") == 0) ||
												(strcasecmp(param->attribute,"name") == 0) ||
												(strcasecmp(param->attribute,"name*") == 0) ||
												(strcasecmp(param->attribute,"filename*") == 0) )) {
						v = fe_new_str("filename", param->value, 0, FE_CHARSET_DEFAULT );
						ferite_object_set_var(script, VAO(object), "filename", v );
						break;
					}
					param=param->next;
				}
			}
			/* If filename was not found in Content-Disposition header search for it among the parameters */
			if( strcmp(VAS(ferite_object_get_var(script, VAO(object), "filename"))->data, "") == 0 ) {
				param = body->parameter;
				while(param){
					if( param->attribute && ((strcasecmp(param->attribute,"filename") == 0) || 
												(strcasecmp(param->attribute,"name") == 0))) {
						v = fe_new_str("filename", param->value, 0, FE_CHARSET_DEFAULT );
						ferite_object_set_var(script, VAO(object), "filename", v );
						break;
					}
					param = param->next;
				}
			}
		}
		return object;
	}
	return NULL;
}
Exemple #18
0
/*{{{ FeriteVariable *ferite_build_object( FeriteScript *script, FeriteClass *nclass) */
FeriteVariable *ferite_build_object( FeriteScript *script, FeriteClass *nclass)
{
	FeriteVariable *ptr = NULL;

	FE_ENTER_FUNCTION;

	if( nclass != NULL )
	{
		FUD(("BUILDOBJECT: Creating an instance of %s\n", nclass->name ));
		ptr = ferite_create_object_variable( script, nclass->name, FE_ALLOC );
		if( script )
			VAO(ptr) = ferite_stack_pop( script, script->objects );
		if( VAO(ptr) == NULL ) {
			VAO(ptr) = fmalloc( sizeof( FeriteObject ) );
			FUD(( "Allocating object %p\n", VAO(ptr) ));
		}
		VAO(ptr)->name = fstrdup( nclass->name );
		VAO(ptr)->klass = nclass;
		
		FUD(("BUILDOBJECT: Creating a duplicate varaible hash\n"));
		VAO(ptr)->variables = ferite_duplicate_object_variable_list( script, nclass );
		
		FUD(("BUILDOBJECT: Linking function list up\n"));
		VAO(ptr)->functions = nclass->object_methods;
		
		VAO(ptr)->oid = nclass->id;
		VAO(ptr)->odata = NULL;
		
		VAO(ptr)->refcount = 1;

		ferite_add_to_gc( script, VAO(ptr) );
	}
	FE_LEAVE_FUNCTION( ptr );
}