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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
- (void)applicationDidFinishLaunching
{
__auto_type environment = OFApplication.environment;
if (environment[@"OBJMATRIX_USER"] == nil ||
environment[@"OBJMATRIX_PASS"] == nil ||
environment[@"OBJMATRIX_HS"] == nil) {
[of_stderr writeString: @"Please set OBJMATRIX_USER, "
@"OBJMATRIX_PASS and OBJMATRIX_HS in "
@"the environment!\n"];
[OFApplication terminateWithStatus: 1];
}
OFURL *homeserver = [OFURL URLWithString: environment[@"OBJMATRIX_HS"]];
id <MTXStorage> storage =
[MTXSQLite3Storage storageWithPath: @"tests.db"];
[MTXClient logInWithUser: environment[@"OBJMATRIX_USER"]
password: environment[@"OBJMATRIX_PASS"]
homeserver: homeserver
storage: storage
block: ^ (MTXClient *client, id exception) {
if (exception != nil) {
of_log(@"Error logging in: %@", exception);
[OFApplication terminateWithStatus: 1];
}
_client = [client retain];
of_log(@"Logged in client: %@", _client);
[_client startSyncLoop];
[self fetchRoomList];
}];
}
- (void)fetchRoomList
{
[_client fetchRoomListWithBlock: ^ (OFArray<OFString *> *rooms,
id exception) {
if (exception != nil) {
of_log(@"Failed to fetch room list: %@", exception);
[OFApplication terminateWithStatus: 1];
}
of_log(@"Fetched room list: %@", rooms);
[self joinRoom];
}];
}
- (void)joinRoom
{
OFString *room = @"#test:nil.im";
[_client joinRoom: room
block: ^ (OFString *roomID, id exception) {
if (exception != nil) {
of_log(@"Failed to join room %@: %@", room, exception);
[OFApplication terminateWithStatus: 1];
}
_roomID = [roomID copy];
of_log(@"Joined room %@", _roomID);
[self sendMessage];
}];
}
- (void)sendMessage
{
[_client sendMessage: @"ObjMatrix test successful!"
roomID: _roomID
block: ^ (id exception) {
if (exception != nil) {
of_log(@"Failed to send message to room %@: %@",
_roomID, exception);
[OFApplication terminateWithStatus: 1];
}
of_log(@"Message sent to %@", _roomID);
of_log(
@"Waiting 5 seconds before leaving room and logging out");
[self performSelector: @selector(leaveRoom)
afterDelay: 5];
}];
}
- (void)leaveRoom
{
[_client leaveRoom: _roomID
block: ^ (id exception) {
if (exception != nil) {
of_log(@"Failed to leave room %@: %@", exception);
[OFApplication terminateWithStatus: 1];
}
of_log(@"Left room %@", _roomID);
[self logOut];
}];
}
- (void)logOut
{
[_client logOutWithBlock: ^ (id exception) {
if (exception != nil) {
of_log(@"Failed to log out: %@\n", exception);
[OFApplication terminateWithStatus: 1];
}
of_log(@"Logged out client");
[OFApplication terminate];
}];
}
@end
|
|
|
|
|
|
|
|
<
|
|
|
|
|
<
|
|
<
|
<
|
|
|
|
|
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
- (void)applicationDidFinishLaunching
{
__auto_type environment = OFApplication.environment;
if (environment[@"OBJMATRIX_USER"] == nil ||
environment[@"OBJMATRIX_PASS"] == nil ||
environment[@"OBJMATRIX_HS"] == nil) {
[OFStdErr writeString: @"Please set OBJMATRIX_USER, "
@"OBJMATRIX_PASS and OBJMATRIX_HS in "
@"the environment!\n"];
[OFApplication terminateWithStatus: 1];
}
OFURL *homeserver = [OFURL URLWithString: environment[@"OBJMATRIX_HS"]];
id <MTXStorage> storage =
[MTXSQLite3Storage storageWithPath: @"tests.db"];
[MTXClient logInWithUser: environment[@"OBJMATRIX_USER"]
password: environment[@"OBJMATRIX_PASS"]
homeserver: homeserver
storage: storage
block: ^ (MTXClient *client, id exception) {
if (exception != nil) {
OFLog(@"Error logging in: %@", exception);
[OFApplication terminateWithStatus: 1];
}
_client = [client retain];
OFLog(@"Logged in client: %@", _client);
[_client startSyncLoop];
[self fetchRoomList];
}];
}
- (void)fetchRoomList
{
[_client fetchRoomListWithBlock: ^ (OFArray<OFString *> *rooms,
id exception) {
if (exception != nil) {
OFLog(@"Failed to fetch room list: %@", exception);
[OFApplication terminateWithStatus: 1];
}
OFLog(@"Fetched room list: %@", rooms);
[self joinRoom];
}];
}
- (void)joinRoom
{
OFString *room = @"#test:nil.im";
[_client joinRoom: room block: ^ (OFString *roomID, id exception) {
if (exception != nil) {
OFLog(@"Failed to join room %@: %@", room, exception);
[OFApplication terminateWithStatus: 1];
}
_roomID = [roomID copy];
OFLog(@"Joined room %@", _roomID);
[self sendMessage];
}];
}
- (void)sendMessage
{
[_client sendMessage: @"ObjMatrix test successful!"
roomID: _roomID
block: ^ (id exception) {
if (exception != nil) {
OFLog(@"Failed to send message to room %@: %@",
_roomID, exception);
[OFApplication terminateWithStatus: 1];
}
OFLog(@"Message sent to %@", _roomID);
OFLog(@"Waiting 5 seconds before leaving room and logging out");
[self performSelector: @selector(leaveRoom) afterDelay: 5];
}];
}
- (void)leaveRoom
{
[_client leaveRoom: _roomID block: ^ (id exception) {
if (exception != nil) {
OFLog(@"Failed to leave room %@: %@", exception);
[OFApplication terminateWithStatus: 1];
}
OFLog(@"Left room %@", _roomID);
[self logOut];
}];
}
- (void)logOut
{
[_client logOutWithBlock: ^ (id exception) {
if (exception != nil) {
OFLog(@"Failed to log out: %@\n", exception);
[OFApplication terminateWithStatus: 1];
}
OFLog(@"Logged out client");
[OFApplication terminate];
}];
}
@end
|