-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMSHTTPConnection.xm
More file actions
141 lines (100 loc) · 5.57 KB
/
MSHTTPConnection.xm
File metadata and controls
141 lines (100 loc) · 5.57 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#import <sqlite3.h>
#import <ChatKit.framework/CKConversationList.h>
#import <ChatKit.framework/CKComposition.h>
#import <ChatKit.framework/CKEntity.h>
#import <Chatkit.framework/CKConversation.h>
#import "CocoaHTTPServerHeaders/HTTPDataResponse.h"
#import "CocoaHTTPServerHeaders/HTTPLogging.h"
#import "MSHTTPConnection.h"
@implementation MSHTTPConnection
- (NSObject<HTTPResponse> *)httpResponseForMethod:(NSString *)method URI:(NSString *)path
{
NSString *relativePath = [path substringFromIndex:1];
NSArray* components = [relativePath componentsSeparatedByString:@"/"];
if ([components[0] isEqualToString:@"getMessages"])
{
NSLog(@"Getting messages");
if ([components count] != 2)
{
return nil;
}
NSString* sender = components[1];
sqlite3 * database;
NSString *sqLiteDb = @"/var/mobile/Library/SMS/sms.db";
sqlite3_open([sqLiteDb UTF8String], &database);
NSString *query = @"select message.text, message.date, handle.id from message, handle where message.handle_id = handle.ROWID and is_from_me = 0 and handle.id = ? and text != '' order by date";
sqlite3_stmt *statement;
NSMutableArray* responses = [NSMutableArray array];
if (sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, nil) == SQLITE_OK) {
sqlite3_bind_text(statement, 1, [sender UTF8String], -1, NULL);
while (sqlite3_step(statement) == SQLITE_ROW)
{
char *textChars = (char *) sqlite3_column_text(statement, 0);
int date = sqlite3_column_int(statement, 1);
char *senderChars = (char *) sqlite3_column_text(statement, 2);
if (textChars[0] != '\0') {
NSString *text = [[NSString alloc] initWithUTF8String:textChars];
NSNumber *dateNum = [NSNumber numberWithInt:date];
NSString *sender = [[NSString alloc] initWithUTF8String:senderChars];
[responses addObject:@{@"message":text, @"date":dateNum, @"sender":sender}];
}
}
sqlite3_finalize(statement);
}
NSData* responseData = [NSJSONSerialization dataWithJSONObject:responses options:0 error:nil];
return [[HTTPDataResponse alloc] initWithData:responseData];
}
else if ([components[0] isEqualToString:@"sendMessage"])
{
if ([components count] != 3) {
return nil;
}
NSString* receipient = components[1];
NSString* message = [components[2] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
CKConversationList* conversationList = [CKConversationList sharedConversationList];
CKEntity* entity = [CKEntity copyEntityForAddressString:receipient];
CKConversation * conversation = [conversationList conversationForHandles:@[[entity handle]] displayName:@"DisplayName" joinedChatsOnly:NO create:YES];
NSAttributedString* text = [[NSAttributedString alloc] initWithString:message];
CKComposition* composition = [[CKComposition alloc] initWithText:text subject:nil];
id smsMessage = [conversation performSelector:@selector(messageWithComposition:) withObject:composition];
[conversation sendMessage:smsMessage newComposition:YES];
NSString* response = @"{\"code\":200}";
return [[HTTPDataResponse alloc] initWithData:[response dataUsingEncoding:NSUTF8StringEncoding]];
}
else if ([components[0] isEqualToString:@"getNewMessages"])
{
NSDate * date = [NSDate date];
NSNumber * previousDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"previousDate"];
NSTimeInterval previousTime;
if (previousDate == nil) {
previousTime = 0;
} else {
previousTime = [previousDate doubleValue];
}
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithDouble:[date timeIntervalSinceReferenceDate]] forKey:@"previousDate"];
sqlite3 * database;
NSString *sqLiteDb = @"/var/mobile/Library/SMS/sms.db";
sqlite3_open([sqLiteDb UTF8String], &database);
NSString *query = [NSString stringWithFormat:@"select message.text, message.date, handle.id from message, handle where message.handle_id = handle.ROWID and date > %f and is_from_me = 0 and text != '' order by date", previousTime];
sqlite3_stmt *statement;
NSMutableArray* responses = [NSMutableArray array];
if (sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, nil) == SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
char *textChars = (char *) sqlite3_column_text(statement, 0);
int date = sqlite3_column_int(statement, 1);
char *senderChars = (char *) sqlite3_column_text(statement, 2);
if (textChars[0] != '\0') {
NSString *text = [[NSString alloc] initWithUTF8String:textChars];
NSNumber *dateNum = [NSNumber numberWithInt:date];
NSString *sender = [[NSString alloc] initWithUTF8String:senderChars];
[responses addObject:@{@"message":text, @"date":dateNum, @"sender":sender}];
}
}
sqlite3_finalize(statement);
}
NSData* responseData = [NSJSONSerialization dataWithJSONObject:responses options:0 error:nil];
return [[HTTPDataResponse alloc] initWithData:responseData];
}
return [super httpResponseForMethod:method URI:path];
}
@end