ObjXMPP  Check-in [150f2fa932]

Overview
Comment:Add a class for handling JIDs.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 150f2fa9327f6571a1a0e97276ad64d55abfff5f0786555ba1624d5c408d8ff8
User & Date: florob@babelmonkeys.de on 2011-02-13 00:36:25
Other Links: manifest | tags
Context
2011-02-13
01:07
Add support for requesting a certain resource check-in: c322888ad7 user: florob@babelmonkeys.de tags: trunk
00:36
Add a class for handling JIDs. check-in: 150f2fa932 user: florob@babelmonkeys.de tags: trunk
2011-02-12
16:58
Use self for XMPPStanza's init check-in: 86a0735116 user: florob@babelmonkeys.de tags: trunk
Changes

Modified Makefile from [70999db70f] to [7c1f7cb9e3].

1
2
3
4
5
6
7
all: tests/tests

tests/tests: tests/test.m src/XMPPConnection.m src/XMPPStanza.m
	objfw-compile -o $@ $^ -lidn -Wall -Werror -Isrc

clean:
	rm -f src/*.o tests/*.o tests/tests


|




1
2
3
4
5
6
7
all: tests/tests

tests/tests: tests/test.m src/XMPPConnection.m src/XMPPStanza.m src/XMPPJID.m
	objfw-compile -o $@ $^ -lidn -Wall -Werror -Isrc

clean:
	rm -f src/*.o tests/*.o tests/tests

Modified src/XMPPConnection.h from [6a8017ad60] to [b512794cc3].

1

2
3
4
5
6
7
8
#import <ObjFW/ObjFW.h>


@class XMPPConnection;
@class XMPPIQ;
@class XMPPMessage;
@class XMPPPresence;

@protocol XMPPConnectionDelegate

>







1
2
3
4
5
6
7
8
9
#import <ObjFW/ObjFW.h>
#import <XMPPJID.h>

@class XMPPConnection;
@class XMPPIQ;
@class XMPPMessage;
@class XMPPPresence;

@protocol XMPPConnectionDelegate
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46





47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

64
65
66
67
68
69
70
@interface XMPPConnection: OFObject <OFXMLElementBuilderDelegate>
{
	OFTCPSocket *sock;
	OFXMLParser *parser;
	OFXMLElementBuilder *elementBuilder;

	/**
	 * The username (local part of the JID) to connect with
	 */
	OFString *username;

	/**
	 * The password to connect with
	 */
	OFString *password;

	/**
	 * The server to connect to
	 */
	OFString *server;

	/**
	 * The resource to connect with
	 */
	OFString *resource;






	/**
	 * The port to connect to
	 */
	short port;

	/**
	 * Whether to use TLS
	 */
	BOOL useTLS;
	id <XMPPConnectionDelegate> delegate;
	OFMutableArray *mechanisms;
}

@property (copy) OFString *username;
@property (copy) OFString *password;
@property (copy) OFString *server;
@property (copy) OFString *resource;

@property (assign) short port;
@property (assign) BOOL useTLS;
@property (retain) id <XMPPConnectionDelegate> delegate;

/**
 * Connects to the XMPP service
 */







|


















>
>
>
>
>

















>







22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
@interface XMPPConnection: OFObject <OFXMLElementBuilderDelegate>
{
	OFTCPSocket *sock;
	OFXMLParser *parser;
	OFXMLElementBuilder *elementBuilder;

	/**
	 * The username to connect with
	 */
	OFString *username;

	/**
	 * The password to connect with
	 */
	OFString *password;

	/**
	 * The server to connect to
	 */
	OFString *server;

	/**
	 * The resource to connect with
	 */
	OFString *resource;

	/**
	 * The JID bound to this connection (this is determined by the server)
	 */
	XMPPJID *JID;

	/**
	 * The port to connect to
	 */
	short port;

	/**
	 * Whether to use TLS
	 */
	BOOL useTLS;
	id <XMPPConnectionDelegate> delegate;
	OFMutableArray *mechanisms;
}

@property (copy) OFString *username;
@property (copy) OFString *password;
@property (copy) OFString *server;
@property (copy) OFString *resource;
@property (readonly) XMPPJID *JID;
@property (assign) short port;
@property (assign) BOOL useTLS;
@property (retain) id <XMPPConnectionDelegate> delegate;

/**
 * Connects to the XMPP service
 */

Modified src/XMPPConnection.m from [aacb5f2723] to [91413b016c].

9
10
11
12
13
14
15

16
17
18
19
20
21
22
#define NS_STREAM @"http://etherx.jabber.org/streams"

@implementation XMPPConnection
@synthesize username;
@synthesize password;
@synthesize server;
@synthesize resource;

@synthesize port;
@synthesize useTLS;
@synthesize delegate;

- init
{
	self = [super init];







>







9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#define NS_STREAM @"http://etherx.jabber.org/streams"

@implementation XMPPConnection
@synthesize username;
@synthesize password;
@synthesize server;
@synthesize resource;
@synthesize JID;
@synthesize port;
@synthesize useTLS;
@synthesize delegate;

- init
{
	self = [super init];
214
215
216
217
218
219
220


221
222
223
224
225
226
227
228
229

- (void)_handleResourceBind: (XMPPIQ*)iq
{
	OFXMLElement *bindElem = iq.children.firstObject;
	if ([bindElem.name isEqual: @"bind"] &&
	    [bindElem.namespace isEqual: NS_BIND]) {
		OFXMLElement *jidElem = bindElem.children.firstObject;


		of_log(@"Bound to JID: %@", [jidElem.children.firstObject
				stringValue]);
	}
}

- (void)_handleFeatures: (OFXMLElement*)elem
{
	for (OFXMLElement *child in elem.children) {
		if ([[child name] isEqual: @"mechanisms"] &&







>
>
|
<







215
216
217
218
219
220
221
222
223
224

225
226
227
228
229
230
231

- (void)_handleResourceBind: (XMPPIQ*)iq
{
	OFXMLElement *bindElem = iq.children.firstObject;
	if ([bindElem.name isEqual: @"bind"] &&
	    [bindElem.namespace isEqual: NS_BIND]) {
		OFXMLElement *jidElem = bindElem.children.firstObject;
		JID = [[XMPPJID alloc] initWithString:
			[jidElem.children.firstObject stringValue]];
		of_log(@"Bound to JID: %@", [JID fullJID]);

	}
}

- (void)_handleFeatures: (OFXMLElement*)elem
{
	for (OFXMLElement *child in elem.children) {
		if ([[child name] isEqual: @"mechanisms"] &&

Added src/XMPPJID.h version [ba250a7ca4].

























































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#import <ObjFW/ObjFW.h>

/**
 * \brief A class for easy handling of JIDs
 */
@interface XMPPJID: OFObject
{
	/**
	 * The JID's localpart
	 */
	OFString *node;

	/**
	 * The JID's domainpart
	 */
	OFString *domain;

	/**
	 * The JID's resourcepart
	 */
	OFString *resource;
}

@property (copy) OFString *node;
@property (copy) OFString *domain;
@property (copy) OFString *resource;

/**
 * Creates a new XMPPJID
 *
 * \return A new autoreleased XMPPJID
 */
+ JID;

/**
 * Creates a new XMPPJID from a OFString
 *
 * \param str The string to parse into a JID object
 * \return A new autoreleased XMPPJID
 */
+ JIDWithString: (OFString*)str;

/**
 * Initializes an already allocated XMPPJID using a OFString
 *
 * \param str The string to parse into a JID object
 * \return A initialized XMPPJID
 */
- initWithString: (OFString*)str;

/**
 * \return A OFString containing the bare JID
 */
- (OFString*)bareJID;

/**
 * \return A OFString containing the full JID
 */
- (OFString*)fullJID;
@end

Added src/XMPPJID.m version [2fdab65477].































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <assert.h>
#include <stringprep.h>
#import "XMPPJID.h"

@implementation XMPPJID
@synthesize node;
@synthesize domain;
@synthesize resource;

+ JID
{
	return [[[self alloc] init] autorelease];
}

+ JIDWithString: (OFString*)str
{
	return [[[self alloc] initWithString: str] autorelease];
}

- initWithString: (OFString*)str
{
	self = [super init];

	size_t nodesep, resourcesep;
	nodesep = [str indexOfFirstOccurrenceOfString: @"@"];
	resourcesep = [str indexOfFirstOccurrenceOfString: @"/"];

	if (nodesep == -1)
		[self setNode: @""];
	else
		[self setNode: [str substringFromIndex: 0
					       toIndex: nodesep]];

	if (resourcesep == -1) {
		[self setResource: @""];
		resourcesep = [str length];
	} else
		[self setResource: [str substringFromIndex: resourcesep + 1
						 toIndex: [str length]]];

	[self setDomain: [str substringFromIndex: nodesep + 1
					 toIndex: resourcesep]];

	return self;
}

- (void)setNode: (OFString*)node_
{
	OFString *old = node;
	char *nodepart;

	Stringprep_rc rc;
	if ((rc = stringprep_profile([node_ cString], &nodepart, "Nodeprep", 0))
			!= STRINGPREP_OK) {
		of_log(@"Nodeprep failed: %s", stringprep_strerror(rc));
		assert(0);
	}

	@try {
		node = [[OFString alloc] initWithCString: nodepart];
	} @finally {
		free(nodepart);
	}

	[old release];
}

- (void)setDomain: (OFString*)domain_
{
	OFString *old = domain;
	char *srv;

	Stringprep_rc rc;
	if ((rc = stringprep_profile([domain_ cString], &srv, "Nameprep", 0))
			!= STRINGPREP_OK) {
		of_log(@"Nameprep failed: %s", stringprep_strerror(rc));
		assert(0);
	}

	@try {
		domain = [[OFString alloc] initWithCString: srv];
	} @finally {
		free(srv);
	}

	[old release];
}

- (void)setResource: (OFString*)resource_
{
	OFString *old = resource;
	char *res;

	Stringprep_rc rc;
	if ((rc = stringprep_profile([resource_ cString], &res,
				"Resourceprep", 0)) != STRINGPREP_OK) {
		of_log(@"Resourceprep failed: %s", stringprep_strerror(rc));
		assert(0);
	}

	@try {
		resource = [[OFString alloc] initWithCString: res];
	} @finally {
		free(res);
	}

	[old release];
}

- (OFString*)bareJID
{
	if ([node length])
		return [OFString stringWithFormat: @"%@@%@", node, domain];
	else
		return [OFString stringWithFormat: @"%@", domain];
}

- (OFString*)fullJID
{
	if ([node length])
		return [OFString stringWithFormat: @"%@@%@/%@",
		       node, domain, resource];
	else
		return [OFString stringWithFormat: @"%@/%@",
		       domain, resource];
}
@end