This repository has been archived by the owner on Aug 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathORSUpdateDispatcher.m
104 lines (94 loc) · 2.88 KB
/
ORSUpdateDispatcher.m
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
//
// ORSUpdateDispatcher.m
// Twitter Engine
//
// Created by Nicholas Toumpelis on 12/04/2009.
// Copyright 2009 Ocean Road Software. All rights reserved.
//
// Version 0.7
#import "ORSUpdateDispatcher.h"
@implementation ORSUpdateDispatcher
- (id) initWithEngine:(ORSTwitterEngine *)engine {
if (self = [super init]) {
twitterEngine = engine;
queueArray = [NSMutableArray new];
}
return self;
}
- (void) addMessage:(NSString *)message {
@synchronized(self) {
BOOL directMessage = NO;
NSScanner *scanner = [NSScanner scannerWithString:message];
NSString *token = NULL;
[scanner scanUpToString:@" " intoString:&token];
if ([token isEqualToString:@"d"] || [token isEqualToString:@"D"]) {
[scanner scanUpToString:@" " intoString:&token];
directMessage = YES;
message = [[scanner string]
substringFromIndex:[scanner scanLocation]+1];
} else {
directMessage = NO;
}
unsigned int updateLength = [message length];
NSMutableString *messagePart = NULL;
NSMutableString *remainingMessage = [NSMutableString
stringWithString:message];
BOOL firstMessage = YES;
if (updateLength > 140) {
while ([remainingMessage length] > 136) {
NSRange rangeOfLastWhitespace = [remainingMessage
rangeOfString:@" " options:NSBackwardsSearch
range:NSMakeRange(0, 136)];
NSRange rangeOfMessagePart = NSMakeRange(0,
rangeOfLastWhitespace.location+1);
messagePart = [NSMutableString
stringWithString:[remainingMessage
substringWithRange:rangeOfMessagePart]];
[remainingMessage deleteCharactersInRange:rangeOfMessagePart];
if (firstMessage) {
[messagePart appendString:@" »"];
firstMessage = NO;
} else {
[messagePart insertString:@"» " atIndex:0];
[messagePart appendString:@" »"];
}
[queueArray addObject:(NSString *)messagePart];
}
[remainingMessage insertString:@"» " atIndex:0];
[queueArray addObject:(NSString *)remainingMessage];
} else {
[queueArray addObject:(NSString *)message];
}
if (directMessage) {
[self initiateDMDispatch:[NSNotification
notificationWithName:@"" object:token]];
} else {
[self initiateStatusDispatch:nil];
}
}
}
- (void) initiateStatusDispatch:(NSNotification *)note {
@synchronized(self) {
if ([queueArray count] > 0) {
[twitterEngine sendUpdate:[queueArray objectAtIndex:0]
inReplyTo:nil];
[queueArray removeObjectAtIndex:0];
[self performSelector:@selector(initiateStatusDispatch:)
withObject:nil
afterDelay:6.0];
}
}
}
- (void) initiateDMDispatch:(NSNotification *)note {
@synchronized(self) {
if ([queueArray count] > 0) {
[twitterEngine newDM:[queueArray objectAtIndex:0]
toUser:(NSString *)[note object]];
[queueArray removeObjectAtIndex:0];
[self performSelector:@selector(initiateDMDispatch:)
withObject:nil
afterDelay:6.0];
}
}
}
@end