Archive

Archive for April, 2009

Email to SMS project

April 30, 2009 Leave a comment
https://www.getafreelancer.com/projects/NET/Email-SMS.html

Overview

Create a windows service application written in
.NET, that will check a Pop3 email account every 5 minutes
(configurable in app.config).
On receipt of an email, in the format [number]@freebiesms.mobi, then the service will call a webservice,
(http://www.freebiesms.co.uk/sendsms.asmx?op=SendSms)

Using these parameters:

FromName: The From name in the email
FromNumber: Hard coded, as 00447834567890
ToNumber: The [number] in the email address
Message: The Body of the email, or subject line if no body. (Plain text)
locale: Derived from the prefix of the [number], 0044 is en-GB.
Full list of valid locales here: http://spreadsheets.google.com/pub?key=p_x5hMjj9OXDTjsYYhluJKQ
This list must be configurable in the app.config

Any response from the web service, either error or success must be sent back to the sender via email.

Points to note:

* Should support unicode, if email body in unicode (i.e. Russian) then the message should be in unicode.
* The service must run without any user interaction, any errors should
be logged, and ignored. The service should not stop in the event
of any failure, just try again in 5 minutes.
* I would prefer .NET 1.1 if possible.
* Help with setting up the catch-all email address, and help with installation on the server is necessary.

Categories: Uncategorized

Sending SMS via Soap from Cocoa (Mac)

April 21, 2009 2 comments
Here is some code that was used as part of my IPhone App for sending SMS messages via SOAP;

Full download can be found here;http://sites.google.com/site/freesmsuk/free-sms-iphone-app

#import "sendSms.h"
#import <libxml/xmlstring.h>
#import <CFNetwork/CFHTTPMessage.h>
#import <CFNetwork/CFHTTPStream.h>

@implementation sendSms_SendSms

@synthesize fromName;
@synthesize fromNumber;
@synthesize toNumber;
@synthesize message;
@synthesize locale;

– (id)init
{
    if((self = [super init])) {
    }
   
    return self;
}

– (void)dealloc
{
   
    [super dealloc];
}

– (NSString *)nsPrefix
{
    return @"sendSms";
}

– (NSString *)serializedFormUsingElementName:(NSString *)elName
{
    NSMutableString *serializedForm = [NSMutableString string];
   
    [serializedForm appendFormat:@"<%@ xsi:type="sendSms:SendSms"", elName];
    [serializedForm appendString:[self serializedAttributeString]];
    [serializedForm appendFormat:@">"];
   
    [serializedForm appendString:[self serializedElementString]];
   
    [serializedForm appendFormat:@"n</%@>", elName];
   
    return serializedForm;
}

– (NSString *)serializedAttributeString
{
    NSMutableString *serializedForm = [NSMutableString string];
   
   
   
    return serializedForm;
}

– (NSString *)serializedElementString
{
    NSMutableString *serializedForm = [NSMutableString string];
   

    [serializedForm appendFormat:@"n<sendSms:FromName>"];
    [serializedForm appendFormat:fromName];
    [serializedForm appendFormat:@"</sendSms:FromName>n"];
   
    [serializedForm appendFormat:@"<sendSms:FromNumber>"];
    [serializedForm appendFormat:fromNumber];
    [serializedForm appendFormat:@"</sendSms:FromNumber>n"];
   
    [serializedForm appendFormat:@"<sendSms:ToNumber>"];
    [serializedForm appendFormat:toNumber];
    [serializedForm appendFormat:@"</sendSms:ToNumber>n"];
   
    [serializedForm appendFormat:@"<sendSms:Message>"];
    [serializedForm appendFormat:message];
    [serializedForm appendFormat:@"</sendSms:Message>n"];
   
    [serializedForm appendFormat:@"<sendSms:locale>"];
    [serializedForm appendFormat:locale];
    [serializedForm appendFormat:@"</sendSms:locale>"];

   
    return serializedForm;
}
/* elements */
/* attributes */
– (NSDictionary *)attributes
{
    NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
   
    return attributes;
}
+ (sendSms_SendSms *)deserializeNode:(xmlNodePtr)cur
{
    sendSms_SendSms *newObject = [[sendSms_SendSms new] autorelease];
   
    [newObject deserializeAttributesFromNode:cur];
    [newObject deserializeElementsFromNode:cur];
   
    return newObject;
}
– (void)deserializeAttributesFromNode:(xmlNodePtr)cur
{
}
– (void)deserializeElementsFromNode:(xmlNodePtr)cur
{
   
   
}
@end

@implementation sendSms_SendSmsResponse

@synthesize statusString;

– (id)init
{
    if((self = [super init])) {
    }
   
    return self;
}
– (void)dealloc
{
   
    [super dealloc];
}
– (NSString *)nsPrefix
{
    return @"sendSms";
}
– (NSString *)serializedFormUsingElementName:(NSString *)elName
{
    NSMutableString *serializedForm = [NSMutableString string];
   
    [serializedForm appendFormat:@"<%@ xsi:type="sendSms:SendSmsResponse"", elName];
    [serializedForm appendString:[self serializedAttributeString]];
    [serializedForm appendFormat:@">"];
   
    [serializedForm appendString:[self serializedElementString]];
   
    [serializedForm appendFormat:@"n</%@>", elName];
   
    return serializedForm;
}
– (NSString *)serializedAttributeString
{
    NSMutableString *serializedForm = [NSMutableString string];
   
   
   
    return serializedForm;
}
– (NSString *)serializedElementString
{
    NSMutableString *serializedForm = [NSMutableString string];
   
   
   
    return serializedForm;
}
/* elements */
/* attributes */
– (NSDictionary *)attributes
{
    NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
   
    return attributes;
}
+ (sendSms_SendSmsResponse *)deserializeNode:(xmlNodePtr)cur
{
    sendSms_SendSmsResponse *newObject = [[sendSms_SendSmsResponse new] autorelease];
   
    [newObject deserializeAttributesFromNode:cur];
    [newObject deserializeElementsFromNode:cur];
   
    return newObject;
}
– (void)deserializeAttributesFromNode:(xmlNodePtr)cur
{
}
– (void)deserializeElementsFromNode:(xmlNodePtr)cur
{
   
   
    if(xmlStrEqual(cur->name, (const xmlChar *) "SendSmsResponse")){
       
        // In this case we have got the ok answer
        statusString = @"SENT";
       
    } else if (xmlStrEqual(cur->name, (const xmlChar *) "Fault")){
       
        // Something is wrong       
        NSLog(@" bodyNode last prev last content in deserializeElementsFromNode %s", cur->last->prev->last->content);
       
        NSString *tmp = [NSString stringWithFormat:@"%s", cur->last->prev->last->content ];
       
        // Strip out the garbage
        //NSString *tmp1  = [tmp substringToIndex:174];   
        //NSString *tmp2 = [tmp1 substringFromIndex:106];
       
       
        // Strip out the garbage
        NSString *tmp1  = [tmp substringToIndex:174];
       
        NSLog(@" tmp1 %@", tmp1);
       
        NSRange nsr = [tmp rangeOfString:@" at"];
       
        // Remove trailer message after at
        if (nsr.location < 174 ){
       
        tmp1  = [tmp1 substringToIndex:nsr.location];
           
        };
       
        NSLog(@" tmp1 %@", tmp1);

        NSString *tmp2 = [tmp1 substringFromIndex:106];
       
        NSLog(@" tmp2 %@Ò", tmp2);
       
        statusString = [[NSString alloc] initWithString:tmp2 ];
       
    }
   
    if (statusString == NULL){
        statusString = @"UNDEFINED";
    };
   
}
@end
@implementation sendSms
+ (void)initialize
{
    [[USGlobals sharedInstance].wsdlStandardNamespaces setObject:@"xsd" forKey:@"http://www.w3.org/2001/XMLSchema"%5D;
    [[USGlobals sharedInstance].wsdlStandardNamespaces setObject:@"sendSms" forKey:@"http://FreebieSMS.co.uk"%5D;
}
+ (sendSmsSoap *)sendSmsSoap
{
    return [[[sendSmsSoap alloc] initWithAddress:@"http://www.freebiesms.co.uk/sendsms.asmx"%5D autorelease];
}
+ (sendSmsSoap12 *)sendSmsSoap12
{
    return [[[sendSmsSoap12 alloc] initWithAddress:@"http://www.freebiesms.co.uk/sendsms.asmx"%5D autorelease];
}
@end
@implementation sendSmsSoap
@synthesize address;
@synthesize logXMLInOut;
– (id)init
{
    if((self = [super init])) {
        address = nil;
        cookies = nil;
        logXMLInOut = NO;
    }
   
    return self;
}
– (id)initWithAddress:(NSString *)anAddress
{
    if((self = [self init])) {
        self.address = [NSURL URLWithString:anAddress];
    }
   
    return self;
}
– (void)addCookie:(NSHTTPCookie *)toAdd
{
    if(toAdd != nil) {
        if(cookies == nil) cookies = [[NSMutableArray alloc] init];
        [cookies addObject:toAdd];
    }
}
– (sendSmsSoapResponse *)SendSmsUsingParameters:(sendSms_SendSms *)aParameters
{
    sendSmsSoap_SendSms *operation = [(sendSmsSoap_SendSms*)[sendSmsSoap_SendSms alloc] initWithBinding:self
        parameters:aParameters
    ];
   
    NSOperationQueue *queue = [[NSOperationQueue new] autorelease];
   
    [queue addOperation:operation];
   
    [queue waitUntilAllOperationsAreFinished];
   
    return operation.response;
}
– (NSString *)sendHTTPCallUsingBody:(NSString *)outputBody soapAction:(NSString *)soapAction
{
    NSMutableDictionary *HTTPHeaders = [NSMutableDictionary dictionary];
    if(cookies != nil) {
        NSDictionary *fCookies = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
        [HTTPHeaders addEntriesFromDictionary:fCookies];
    }
    [HTTPHeaders setObject:@"http://www.freebiesms.co.uk/sendsms.asmx HTTP/1.1" forKey:@"POST"];
    [HTTPHeaders setObject:@"text/xml;charset=UTF-8" forKey:@"Content-Type"];
    [HTTPHeaders setObject:soapAction forKey:@"SOAPAction"];
    [HTTPHeaders setObject:@"wsdl2objc" forKey:@"User-Agent"];
    [HTTPHeaders setObject:@"www.freebiesms.co.uk" forKey:@"Host"];
    [HTTPHeaders setObject:@"641" forKey:@"Content-Length"];

   
    CFHTTPMessageRef request = CFHTTPMessageCreateRequest(kCFAllocatorDefault, (CFStringRef)@"POST", (CFURLRef)self.address, kCFHTTPVersion1_1);
   
    CFDataRef bodyData = (CFDataRef)[outputBody dataUsingEncoding:NSUTF8StringEncoding];
    CFHTTPMessageSetBody(request, bodyData);
   
    for(NSString *key in [HTTPHeaders allKeys]) {
        CFHTTPMessageSetHeaderFieldValue(request, (CFStringRef)key, (CFStringRef)[HTTPHeaders objectForKey:key]);
    }
   
    NSDictionary *outputHeaders = (NSDictionary*)CFHTTPMessageCopyAllHeaderFields(request);
   
    if(self.logXMLInOut) {
        NSLog(@"OutputHeaders:n%@", outputHeaders);
        NSLog(@"OutputBody:n%@", outputBody);
    }
   
    CFReadStreamRef readStream = CFReadStreamCreateForHTTPRequest(kCFAllocatorDefault, request);
   
    if(!CFReadStreamOpen(readStream)) {
        CFStreamError myErr = CFReadStreamGetError(readStream);
        // An error has occurred.
        if (myErr.domain == kCFStreamErrorDomainPOSIX) {
        // Interpret myErr.error as a UNIX errno.
        } else if (myErr.domain == kCFStreamErrorDomainMacOSStatus) {
        // Interpret myErr.error as a MacOS error code.
            //OSStatus macError = (OSStatus)myErr.error;
        // Check other error domains.
        }
    }
   
    CFReadStreamSetProperty(readStream, kCFStreamPropertyHTTPShouldAutoredirect, kCFBooleanTrue);
   
    //CFHTTPMessageRef resp = (CFHTTPMessageRef)CFReadStreamCopyProperty(readStream, kCFStreamPropertyHTTPResponseHeader);
    //NSString *statusLine = (NSString*)CFHTTPMessageCopyResponseStatusLine(resp);
   
    //NSLog(@"status line %@",statusLine );
   
    NSMutableString *responseBody = [NSMutableString string];
   
    static unsigned int kReadBufSize = 1024;
    CFIndex numBytesRead;
    do {
        UInt8 buf[kReadBufSize];
            numBytesRead = CFReadStreamRead(readStream, buf, sizeof(buf));
       
        if( numBytesRead > 0 ) {
            [responseBody appendString:[[[NSString alloc] initWithBytes:buf length:numBytesRead encoding:NSUTF8StringEncoding] autorelease]];
        } else if( numBytesRead < 0 ) {
            CFStreamError error = CFReadStreamGetError(readStream);
            //[[NSApplication sharedApplication] presentError:(NSError*)error];
            NSLog(@"error %@", error);
        }
    //} while( numBytesRead > 0 );
        sleep(1);
        NSLog(@" rebelotte");
        } while( numBytesRead < 1 );
   
    CFHTTPMessageRef response = (CFHTTPMessageRef)CFReadStreamCopyProperty(readStream, kCFStreamPropertyHTTPResponseHeader);
    NSDictionary *responseHeaders = (NSDictionary*)CFHTTPMessageCopyAllHeaderFields(response);
    [responseHeaders autorelease];
   
    if(cookies) [cookies release];
    cookies = [[NSHTTPCookie cookiesWithResponseHeaderFields:responseHeaders forURL:self.address] mutableCopy];
   
    if(self.logXMLInOut) {
        NSLog(@"ResponseHeaders:n%@", responseHeaders);
        NSLog(@"ResponseBody:n%@", responseBody);
    }
   
    return responseBody;
}
@end
@implementation sendSmsSoap_SendSms
@synthesize binding;
@synthesize response;
@synthesize parameters;
– (id)initWithBinding:(sendSmsSoap *)aBinding
  parameters:(sendSms_SendSms *)aParameters
{
    if((self = [super init])) {
        response = nil;
       
        self.binding = aBinding;
       
        self.parameters = aParameters;
    }
   
    return self;
}
– (void)dealloc
{
    if(binding != nil) [binding release];
    if(parameters != nil) [parameters release];
   
    [super dealloc];
}
– (void)main
{
    [response autorelease];
    response = [sendSmsSoapResponse new];
   
    sendSmsSoap_envelope *envelope = [sendSmsSoap_envelope sharedInstance];
   
    NSMutableDictionary *headerElements = nil;
    headerElements = [NSMutableDictionary dictionary];
   
    NSMutableDictionary *bodyElements = nil;
    bodyElements = [NSMutableDictionary dictionary];
        if(parameters != nil) [bodyElements setObject:parameters forKey:@"SendSms"];
   
    NSString *operationXMLString = [envelope serializedFormUsingHeaderElements:headerElements bodyElements:bodyElements];
   
    NSLog(@" operationXMLString %@", operationXMLString);
   
    NSString *responseBody = [binding sendHTTPCallUsingBody:operationXMLString soapAction:@"http://FreebieSMS.co.uk/SendSms"%5D;
   
    NSLog(@" sendSmsSoap_SendSms main responseBody %@", responseBody);
   
    xmlDocPtr doc;
    xmlNodePtr cur;
   
    const char *buffer = [responseBody cStringUsingEncoding:NSUTF8StringEncoding];
   
    doc = xmlParseMemory(buffer, [responseBody length]);
   
    NSAssert(doc != NULL, @"Errors while parsing returned XML");
   
    cur = xmlDocGetRootElement(doc);
    cur = cur->children;
   
    for( ; cur != NULL ; cur = cur->next) {
        if(cur->type == XML_ELEMENT_NODE) {
           
            if(xmlStrEqual(cur->name, (const xmlChar *) "Body")) {
                NSMutableArray *responseBodyParts = [NSMutableArray array];
               
                xmlNodePtr bodyNode;
                for(bodyNode=cur->children ; bodyNode != NULL ; bodyNode = bodyNode->next) {
                    if(cur->type == XML_ELEMENT_NODE) {
                        if(xmlStrEqual(bodyNode->name, (const xmlChar *) "SendSmsResponse") || xmlStrEqual(bodyNode->name, (const xmlChar *) "Fault") ) {
                            sendSms_SendSmsResponse *bodyObject = [sendSms_SendSmsResponse deserializeNode:bodyNode];
                            NSAssert1(bodyObject != nil, @"Errors while parsing body %s", bodyNode->name);
                            [responseBodyParts addObject:bodyObject];
                           
                        } else {
                           
                            NSLog(@" xml str no SendSmsResponse nor Fault %s", bodyNode->name );
                        }
                    }
                }
               
                response.bodyParts = responseBodyParts;
            }
        }
    }
   
    xmlFreeDoc(doc);
   
    xmlCleanupParser();
}
@end
static sendSmsSoap_envelope *sendSmsSoapSharedEnvelopeInstance = nil;
@implementation sendSmsSoap_envelope
+ (sendSmsSoap_envelope *)sharedInstance
{
    if(sendSmsSoapSharedEnvelopeInstance == nil) {
        sendSmsSoapSharedEnvelopeInstance = [sendSmsSoap_envelope new];
    }
   
    return sendSmsSoapSharedEnvelopeInstance;
}
– (NSString *)serializedFormUsingHeaderElements:(NSDictionary *)headerElements bodyElements:(NSDictionary *)bodyElements
{
    NSMutableString *serializedForm = [NSMutableString string];
   
    [serializedForm appendFormat:@"<?xml version="1.0" encoding="utf-8"?>n"];
    [serializedForm appendFormat:@"<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"n"%5D;
    [serializedForm appendFormat:@"xmlns:xsl="http://www.w3.org/1999/XSL/Transform&quot; xsl:version="1.0"n"];
    [serializedForm appendFormat:@"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"n"%5D;
    [serializedForm appendFormat:@"xmlns:xsd="http://www.w3.org/2001/XMLSchema"n"%5D;
    [serializedForm appendFormat:@"xmlns:sendSms="http://FreebieSMS.co.uk""%5D;
    [serializedForm appendFormat:@">n"];
   
    if(headerElements != nil) {
        [serializedForm appendFormat:@"<soap:Header>n"];
       
        for(NSString *key in [headerElements allKeys]) {
            id header = [headerElements objectForKey:key];
            NSString *elementName = [NSString stringWithFormat:@"%@:%@", [header nsPrefix], key];
           
            [serializedForm appendFormat:@"%@n", [header serializedFormUsingElementName:elementName]];
        }
       
        [serializedForm appendFormat:@"</soap:Header>"];
    }
   
    if(bodyElements != nil) {
        [serializedForm appendFormat:@"<soap:Body>n"];
       
        for(NSString *key in [bodyElements allKeys]) {
            id body = [bodyElements objectForKey:key];
            NSString *elementName = [NSString stringWithFormat:@"%@:%@", [body nsPrefix], key];
           
            [serializedForm appendFormat:@"%@n", [body serializedFormUsingElementName:elementName]];
        }
       
        [serializedForm appendFormat:@"</soap:Body>n"];
    }
   
    [serializedForm appendFormat:@"</soap:Envelope>"];
   
    return serializedForm;
}
@end
@implementation sendSmsSoapResponse
@synthesize headers;
@synthesize bodyParts;
– (id)init
{
    if((self = [super init])) {
        headers = nil;
        bodyParts = nil;
    }
   
    return self;
}
@end
@implementation sendSmsSoap12
@synthesize address;
@synthesize logXMLInOut;
– (id)init
{
    if((self = [super init])) {
        address = nil;
        cookies = nil;
        logXMLInOut = NO;
    }
   
    return self;
}
– (id)initWithAddress:(NSString *)anAddress
{
    if((self = [self init])) {
        self.address = [NSURL URLWithString:anAddress];
    }
   
    return self;
}
– (void)addCookie:(NSHTTPCookie *)toAdd
{
    if(toAdd != nil) {
        if(cookies == nil) cookies = [[NSMutableArray alloc] init];
        [cookies addObject:toAdd];
    }
}
– (sendSmsSoap12Response *)SendSmsUsingParameters:(sendSms_SendSms *)aParameters
{
    sendSmsSoap12_SendSms *operation = [(sendSmsSoap12_SendSms*)[sendSmsSoap12_SendSms alloc] initWithBinding:self
        parameters:aParameters
    ];
   
    NSOperationQueue *queue = [[NSOperationQueue new] autorelease];
   
    [queue addOperation:operation];
   
    [queue waitUntilAllOperationsAreFinished];
   
    return operation.response;
}
– (NSString *)sendHTTPCallUsingBody:(NSString *)outputBody soapAction:(NSString *)soapAction
{
    NSMutableDictionary *HTTPHeaders = [NSMutableDictionary dictionary];
    if(cookies != nil) {
        NSDictionary *fCookies = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
        [HTTPHeaders addEntriesFromDictionary:fCookies];
    }
    [HTTPHeaders setObject:@"wsdl2objc" forKey:@"UserAgent"];
    [HTTPHeaders setObject:soapAction forKey:@"SOAPAction"];
   
    CFHTTPMessageRef request = CFHTTPMessageCreateRequest(kCFAllocatorDefault, (CFStringRef)@"POST", (CFURLRef)self.address, kCFHTTPVersion1_1);
   
    CFDataRef bodyData = (CFDataRef)[outputBody dataUsingEncoding:NSUTF8StringEncoding];
    CFHTTPMessageSetBody(request, bodyData);
   
    for(NSString *key in [HTTPHeaders allKeys]) {
        CFHTTPMessageSetHeaderFieldValue(request, (CFStringRef)key, (CFStringRef)[HTTPHeaders objectForKey:key]);
    }
   
    NSDictionary *outputHeaders = (NSDictionary*)CFHTTPMessageCopyAllHeaderFields(request);
   
    if(self.logXMLInOut) {
        NSLog(@"OutputHeaders:n%@", outputHeaders);
        NSLog(@"OutputBody:n%@", outputBody);
    }
   
    CFReadStreamRef readStream = CFReadStreamCreateForHTTPRequest(kCFAllocatorDefault, request);
   
    if(!CFReadStreamOpen(readStream)) {
        CFStreamError myErr = CFReadStreamGetError(readStream);
        // An error has occurred.
        if (myErr.domain == kCFStreamErrorDomainPOSIX) {
        // Interpret myErr.error as a UNIX errno.
        } else if (myErr.domain == kCFStreamErrorDomainMacOSStatus) {
        // Interpret myErr.error as a MacOS error code.
            //OSStatus macError = (OSStatus)myErr.error;
        // Check other error domains.
        }
    }
   
    CFReadStreamSetProperty(readStream, kCFStreamPropertyHTTPShouldAutoredirect, kCFBooleanTrue);
   
    //CFHTTPMessageRef response = (CFHTTPMessageRef)CFReadStreamCopyProperty(readStream, kCFStreamPropertyHTTPResponseHeader);
    //NSString *statusLine = (NSString*)CFHTTPMessageCopyResponseStatusLine(response);
   
    NSMutableString *responseBody = [NSMutableString string];
   
    static unsigned int kReadBufSize = 1024;
    CFIndex numBytesRead;
    do {
        UInt8 buf[kReadBufSize];
        numBytesRead = CFReadStreamRead(readStream, buf, sizeof(buf));
        if( numBytesRead > 0 ) {
            [responseBody appendString:[[[NSString alloc] initWithBytes:buf length:numBytesRead encoding:NSUTF8StringEncoding] autorelease]];
        } else if( numBytesRead < 0 ) {
            //CFStreamError error = CFReadStreamGetError(readStream);
            //[[NSApplication sharedApplication] presentError:(NSError*)error];
        }
    } while( numBytesRead > 0 );
   
    CFHTTPMessageRef response = (CFHTTPMessageRef)CFReadStreamCopyProperty(readStream, kCFStreamPropertyHTTPResponseHeader);
    NSDictionary *responseHeaders = (NSDictionary*)CFHTTPMessageCopyAllHeaderFields(response);
    [responseHeaders autorelease];
   
    if(cookies) [cookies release];
    cookies = [[NSHTTPCookie cookiesWithResponseHeaderFields:responseHeaders forURL:self.address] mutableCopy];
   
    if(self.logXMLInOut) {
        NSLog(@"ResponseHeaders:n%@", responseHeaders);
        NSLog(@"ResponseBody:n%@", responseBody);
    }
   
    return responseBody;
}
@end
@implementation sendSmsSoap12_SendSms
@synthesize binding;
@synthesize response;
@synthesize parameters;
– (id)initWithBinding:(sendSmsSoap12 *)aBinding
  parameters:(sendSms_SendSms *)aParameters
{
    if((self = [super init])) {
        response = nil;
       
        self.binding = aBinding;
       
        self.parameters = aParameters;
    }
   
    return self;
}
– (void)dealloc
{
    if(binding != nil) [binding release];
    if(parameters != nil) [parameters release];
   
    [super dealloc];
}
– (void)main
{
    [response autorelease];
    response = [sendSmsSoap12Response new];
   
    sendSmsSoap12_envelope *envelope = [sendSmsSoap12_envelope sharedInstance];
   
    NSMutableDictionary *headerElements = nil;
    headerElements = [NSMutableDictionary dictionary];
   
    NSMutableDictionary *bodyElements = nil;
    bodyElements = [NSMutableDictionary dictionary];
        if(parameters != nil) [bodyElements setObject:parameters forKey:@"SendSms"];
   
    NSString *operationXMLString = [envelope serializedFormUsingHeaderElements:headerElements bodyElements:bodyElements];
   
    NSString *responseBody = [binding sendHTTPCallUsingBody:operationXMLString soapAction:@"http://FreebieSMS.co.uk/SendSms"%5D;
   
    xmlDocPtr doc;
    xmlNodePtr cur;
   
    const char *buffer = [responseBody cStringUsingEncoding:NSUTF8StringEncoding];
   
    doc = xmlParseMemory(buffer, [responseBody length]);
   
    NSAssert(doc != NULL, @"Errors while parsing returned XML");
   
    cur = xmlDocGetRootElement(doc);
    cur = cur->children;
   
    for( ; cur != NULL ; cur = cur->next) {
        if(cur->type == XML_ELEMENT_NODE) {
           
            if(xmlStrEqual(cur->name, (const xmlChar *) "Body")) {
                NSMutableArray *responseBodyParts = [NSMutableArray array];
               
                xmlNodePtr bodyNode;
                for(bodyNode=cur->children ; bodyNode != NULL ; bodyNode = bodyNode->next) {
                    if(cur->type == XML_ELEMENT_NODE) {
                        if(xmlStrEqual(bodyNode->name, (const xmlChar *) "SendSmsResponse")) {
                            sendSms_SendSmsResponse *bodyObject = [sendSms_SendSmsResponse deserializeNode:bodyNode];
                            NSAssert1(bodyObject != nil, @"Errors while parsing body %s", bodyNode->name);
                            [responseBodyParts addObject:bodyObject];
                        }
                    }
                }
               
                response.bodyParts = responseBodyParts;
            }
        }
    }
   
    xmlFreeDoc(doc);
   
    xmlCleanupParser();
}
@end
static sendSmsSoap12_envelope *sendSmsSoap12SharedEnvelopeInstance = nil;
@implementation sendSmsSoap12_envelope
+ (sendSmsSoap12_envelope *)sharedInstance
{
    if(sendSmsSoap12SharedEnvelopeInstance == nil) {
        sendSmsSoap12SharedEnvelopeInstance = [sendSmsSoap12_envelope new];
    }
   
    return sendSmsSoap12SharedEnvelopeInstance;
}
– (NSString *)serializedFormUsingHeaderElements:(NSDictionary *)headerElements bodyElements:(NSDictionary *)bodyElements
{
    NSMutableString *serializedForm = [NSMutableString string];
   
    [serializedForm appendFormat:@"<?xml version="1.0" encoding="utf-8"?>n"];
    [serializedForm appendFormat:@"<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"n"%5D;
    [serializedForm appendFormat:@"xmlns:xsl="http://www.w3.org/1999/XSL/Transform&quot; xsl:version="1.0"n"];
    [serializedForm appendFormat:@"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"n"%5D;
    [serializedForm appendFormat:@"xmlns:xsd="http://www.w3.org/2001/XMLSchema"n"%5D;
    [serializedForm appendFormat:@"xmlns:sendSms="http://FreebieSMS.co.uk"n"%5D;
    [serializedForm appendFormat:@">n"];
   
    if(headerElements != nil) {
        [serializedForm appendFormat:@"<soap:Header>n"];
       
        for(NSString *key in [headerElements allKeys]) {
            id header = [headerElements objectForKey:key];
            NSString *elementName = [NSString stringWithFormat:@"%@:%@", [header nsPrefix], key];
           
            [serializedForm appendFormat:@"%@n", [header serializedFormUsingElementName:elementName]];
        }
       
        [serializedForm appendFormat:@"</soap:Header>"];
    }
   
    if(bodyElements != nil) {
        [serializedForm appendFormat:@"<soap:Body>n"];
       
        for(NSString *key in [bodyElements allKeys]) {
            id body = [bodyElements objectForKey:key];
            NSString *elementName = [NSString stringWithFormat:@"%@:%@", [body nsPrefix], key];
           
            [serializedForm appendFormat:@"%@n", [body serializedFormUsingElementName:elementName]];
        }
       
        [serializedForm appendFormat:@"</soap:Body>n"];
    }
   
    [serializedForm appendFormat:@"</soap:Envelope>"];
   
    return serializedForm;
}
@end
@implementation sendSmsSoap12Response
@synthesize headers;
@synthesize bodyParts;
– (id)init
{
    if((self = [super init])) {
        headers = nil;
        bodyParts = nil;
    }
   
    return self;
}
@end

Categories: Uncategorized