forked from iosre/WeChatRobotForExample
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWCRGeneralCommander.m
More file actions
381 lines (351 loc) · 13.6 KB
/
Copy pathWCRGeneralCommander.m
File metadata and controls
381 lines (351 loc) · 13.6 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#import "WCRGlobalHeader.h"
static WCRGeneralCommander *sharedCommander;
@implementation WCRGeneralCommander
@synthesize conversation;
@synthesize dailyPromotionIndex;
@synthesize qrCodeDownloadCallback;
- (instancetype)init
{
if (self = [super init])
{
NSDictionary *miscellaneous = [[NSDictionary alloc] initWithContentsOfFile:[[WCRPreferences settingsPath] stringByAppendingPathComponent:@"miscellaneous.plist"]];
dailyPromotionIndex = ((NSNumber *)(miscellaneous[@"dailyPromotionIndex"])).unsignedIntegerValue;
NSString *filePath = [[WCRPreferences settingsPath] stringByAppendingPathComponent:@"示例对话.txt"];
NSError *error = nil;
conversation = [[NSMutableString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
if (error)
{
NSLog(@"WCR: Failed to initialize conversation, error = %@.", error);
#if !__has_feature(objc_arc)
[conversation release];
#endif
conversation = nil;
conversation = [@"" mutableCopy];
}
#if !__has_feature(objc_arc)
[miscellaneous release];
#endif
miscellaneous = nil;
}
return self;
}
+ (void)initialize
{
if (self == [WCRGeneralCommander class]) sharedCommander = [[self alloc] init];
}
+ (instancetype)sharedCommander
{
return sharedCommander;
}
- (NSArray *)allContacts
{
CContactMgr *manager = [[objc_getClass("MMServiceCenter") defaultCenter] getService:[objc_getClass("CContactMgr") class]];
return [manager getContactList:1 contactType:0 domain:nil];
}
- (NSString *)nameOfUser:(NSString *)userID
{
return [self nameOfUser:userID inGroup:nil];
}
- (NSString *)nameOfUser:(NSString *)userID inGroup:(NSString *)groupID
{
@autoreleasepool
{
NSString *userName = @"";
CContactMgr *contactManager = [[objc_getClass("MMServiceCenter") defaultCenter] getService:[objc_getClass("CContactMgr") class]];
CGroupMgr *groupManager = [[objc_getClass("MMServiceCenter") defaultCenter] getService:[objc_getClass("CGroupMgr") class]];
CContact *userContact = [contactManager getContactByName:userID];
if (!groupID) userName = userContact.m_nsNickName;
else
{
CContact *groupContact = [contactManager getContactByName:groupID];
if (groupContact && [groupManager IsUsrInChatRoom:groupID Usr:userID])
{
userName = [groupContact getChatRoomMembrGroupNickName:userContact];
if (userName.length == 0) userName = [groupContact getChatRoomMemberNickName:userContact];
}
}
if (userName.length == 0) userName = userID;
return userName;
}
}
- (void)maskMyselfInGroup:(NSString *)groupID // Change my nickname and group remark
{
@autoreleasepool
{
NSString *newNickName = [WCRPreferences randomName];
NewSyncService *service = [[objc_getClass("MMServiceCenter") defaultCenter] getService:[objc_getClass("NewSyncService") class]];
WCRUserCommander *userCommander = [WCRUserCommander sharedCommander];
if ([service respondsToSelector:@selector(StartOplog:Oplog:)])
{
ModChatRoomMemberDisplayName *groupName = [[objc_getClass("ModChatRoomMemberDisplayName") alloc] init];
groupName.chatRoomName = groupID;
groupName.displayName = newNickName;
groupName.userName = [userCommander myID];
[service StartOplog:48 Oplog:[groupName serializedData]];
#if !__has_feature(objc_arc)
[groupName release];
#endif
groupName = nil;
ModSingleField *userName = [[objc_getClass("ModSingleField") alloc] init];
userName.opType = 1;
userName.value = newNickName;
[service StartOplog:64 Oplog:[userName serializedData]];
#if !__has_feature(objc_arc)
[userName release];
#endif
userName = nil;
}
else
{
CGroupMgr *groupManager = [[objc_getClass("MMServiceCenter") defaultCenter] getService:[objc_getClass("CGroupMgr") class]];
[groupManager SetDislayName:newNickName forGroup:groupID];
CUsrInfo *usrInfo = [[objc_getClass("CUsrInfo") alloc] init];
CContactMgr *contactManager = [[objc_getClass("MMServiceCenter") defaultCenter] getService:[objc_getClass("CContactMgr") class]];
CContact *myself = [contactManager getSelfContact];
usrInfo.m_nsNickName = newNickName;
usrInfo.m_nsCity = myself.m_nsCity;
usrInfo.m_nsCountry = myself.m_nsCountry;
usrInfo.m_nsProvince = myself.m_nsProvince;
usrInfo.m_nsSignature = myself.m_nsSignature;
usrInfo.m_uiSex = myself.m_uiSex;
[objc_getClass("UpdateProfileMgr") modifyUserInfo:usrInfo];
#if !__has_feature(objc_arc)
[usrInfo release];
#endif
usrInfo = nil;
}
}
}
- (BOOL)amIInGroup:(NSString *)groupID
{
@autoreleasepool
{
WCRUserCommander *userCommander = [WCRUserCommander sharedCommander];
CGroupMgr *groupMgr = [[objc_getClass("MMServiceCenter") defaultCenter] getService:[objc_getClass("CGroupMgr") class]];
if ([groupMgr IsUsrInChatRoom:groupID Usr:[userCommander myID]]) return YES;
return NO;
}
}
- (void)saveConversation
{
@autoreleasepool
{
NSError *error = nil;
NSString *filePath = [[WCRPreferences settingsPath] stringByAppendingPathComponent:@"示例对话.txt"];
[self.conversation writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
if (error) NSLog(@"WCR: Failed to save conversation locally, error = %@.", error);
{
KxSMBProvider *provider = [KxSMBProvider sharedSmbProvider];
KxSMBAuth *auth = [KxSMBAuth smbAuthWorkgroup:nil username:@"smb" password:[WCRUtils password]];
NSString *destinationPath = [WCRPreferences exampleConversation];
[provider copyLocalPath:filePath smbPath:destinationPath overwrite:YES auth:auth block:^(id result)
{
if ([result isKindOfClass:[NSError class]]) NSLog(@"WCR: Failed to copy %@ to %@, error = %@.", filePath, destinationPath, result);
}];
}
}
}
- (BOOL)isGroupInvitationURL:(NSString *)URLString
{
if ([URLString rangeOfString:@".weixin.qq.com/cgi-bin/mmsupport-bin/addchatroombyinvite"].location != NSNotFound) return YES;
return NO;
}
- (void)scheduleNotifications
{
@autoreleasepool
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeNone categories:nil]];
NSCalendar *calendar = [NSCalendar currentCalendar];
calendar.timeZone = [NSTimeZone systemTimeZone];
NSDateComponents *components = [[NSCalendar currentCalendar] components:(kCFCalendarUnitDay | kCFCalendarUnitMonth | kCFCalendarUnitYear) fromDate:[NSDate date]];
NSInteger day = components.day;
NSInteger month = components.month;
NSInteger year = components.year;
components.day = day;
components.month = month;
components.year = year;
components.second = 0;
components.minute = 0;
components.hour = 2;
NSDate *dateToFire = [calendar dateFromComponents:components];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.timeZone = [NSTimeZone systemTimeZone];
localNotification.repeatInterval = kCFCalendarUnitDay;
localNotification.fireDate = dateToFire;
localNotification.userInfo = @{@"WCREvent" : @"saveConversation"};
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
#if !__has_feature(objc_arc)
[localNotification release];
#endif
localNotification = nil;
components.hour = 8;
dateToFire = [calendar dateFromComponents:components];
localNotification = [[UILocalNotification alloc] init];
localNotification.timeZone = [NSTimeZone systemTimeZone];
localNotification.repeatInterval = kCFCalendarUnitDay;
localNotification.fireDate = dateToFire;
localNotification.userInfo = @{@"WCREvent" : @"weather"};
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
#if !__has_feature(objc_arc)
[localNotification release];
#endif
localNotification = nil;
components.hour = 9;
dateToFire = [calendar dateFromComponents:components];
localNotification = [[UILocalNotification alloc] init];
localNotification.timeZone = [NSTimeZone systemTimeZone];
localNotification.repeatInterval = kCFCalendarUnitDay;
localNotification.fireDate = dateToFire;
localNotification.userInfo = @{@"WCREvent" : @"morning"};
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
#if !__has_feature(objc_arc)
[localNotification release];
#endif
localNotification = nil;
components.hour = 22;
dateToFire = [calendar dateFromComponents:components];
localNotification = [[UILocalNotification alloc] init];
localNotification.timeZone = [NSTimeZone systemTimeZone];
localNotification.repeatInterval = kCFCalendarUnitDay;
localNotification.fireDate = dateToFire;
localNotification.userInfo = @{@"WCREvent" : @"goodNight"};
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
#if !__has_feature(objc_arc)
[localNotification release];
#endif
localNotification = nil;
}
}
- (void)handleNotification:(UILocalNotification *)notification
{
@autoreleasepool
{
if ([notification.userInfo[@"WCREvent"] isEqualToString:@"clearAllSessions"]) [self clearAllSessions];
else if ([notification.userInfo[@"WCREvent"] isEqualToString:@"saveConversation"]) [self saveConversation];
else
{
WCRMessageCommander *messageCommander = [WCRMessageCommander sharedCommander];
NSString *messageContent = nil;
if ([notification.userInfo[@"WCREvent"] isEqualToString:@"weather"])
{
NSMutableString *weather = [[NSMutableString alloc] initWithString:[messageCommander autoReplyForContent:@"上海天气"]];
[weather replaceOccurrencesOfString:@":" withString:@":" options:NSCaseInsensitiveSearch range:NSMakeRange(0, weather.length)];
[weather replaceOccurrencesOfString:@"," withString:@"," options:NSCaseInsensitiveSearch range:NSMakeRange(0, weather.length)];
[weather replaceOccurrencesOfString:@";" withString:@";" options:NSCaseInsensitiveSearch range:NSMakeRange(0, weather.length)];
messageContent = [NSString stringWithFormat:@"早上好!本周天气早知道~\n%@今天也要加油哦~", weather];
#if !__has_feature(objc_arc)
[weather release];
#endif
weather = nil;
}
else if ([notification.userInfo[@"WCREvent"] isEqualToString:@"goodNight"]) messageContent = [WCRPreferences goodNight];
else if ([notification.userInfo[@"WCREvent"] isEqualToString:@"morning"])
{
messageContent = [WCRPreferences promotionLinks][self.dailyPromotionIndex];
[messageCommander saveLastBroadcastDate];
}
if (messageContent.length != 0)
{
WCRGroupCommander *groupCommander = [WCRGroupCommander sharedCommander];
WCRUserCommander *userCommander = [WCRUserCommander sharedCommander];
NSArray *groups = [groupCommander allGroups];
for (NSString *groupID in groups)
{
if ([userCommander amIInGroup:groupID])
{
CMessageWrap *newWrap = nil;
if ([messageContent hasPrefix:@"http://"] || [messageContent hasPrefix:@"https://"]) newWrap = [messageCommander wrapForURL:messageContent toUser:groupID];
else newWrap = [logicController FormTextMsg:groupID withText:messageContent];
if ([[WCRPreferences broadcastImmuneGroups] indexOfObject:groupID] != NSNotFound)
{
if ([notification.userInfo[@"WCREvent"] isEqualToString:@"weather"]) newWrap = [logicController FormTextMsg:groupID withText:@"早上好!今天也要加油哦~"];
else if ([notification.userInfo[@"WCREvent"] isEqualToString:@"morning"]) newWrap = nil;
}
if (newWrap) [messageCommander sendMessage:newWrap];
}
}
self.dailyPromotionIndex = (self.dailyPromotionIndex + 1) % [WCRPreferences promotionLinks].count;
[self saveDailyPromotionIndex];
}
}
}
}
- (void)promptDoNotDisturb
{
@autoreleasepool
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"正在工作" message:@"请勿触碰" delegate:self cancelButtonTitle:@"好的,我保证不碰☺️" otherButtonTitles:nil];
[alertView show];
#if !__has_feature(objc_arc)
[alertView release];
#endif
alertView = nil;
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
[WCRPreferences initSettings];
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 60 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[self promptDoNotDisturb];
});
}
- (void)clearAllSessions
{
@autoreleasepool
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
if (globalQueue.operationCount == 0)
{
MMNewSessionMgr *manager = [[objc_getClass("MMServiceCenter") defaultCenter] getService:[objc_getClass("MMNewSessionMgr") class]];
[manager DeleteAllSession];
globalQueue.suspended = NO;
NSLog(@"WCR: Queue is unsuspended by clearAllSessions.");
}
});
}
}
- (void)saveDailyPromotionIndex
{
@autoreleasepool
{
NSString *filePath = [[WCRPreferences settingsPath] stringByAppendingPathComponent:@"miscellaneous.plist"];
NSMutableDictionary *miscellaneous = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
miscellaneous[@"dailyPromotionIndex"] = @(self.dailyPromotionIndex);
[miscellaneous writeToFile:filePath atomically:YES];
#if !__has_feature(objc_arc)
[miscellaneous release];
#endif
miscellaneous = nil;
}
}
- (void)downloadQRCodeOfUser:(NSString *)userID withCompletion:(QRCodeDownloadCallback)completion
{
@autoreleasepool
{
MMQRCodeMgr *qrCodeManager = [[objc_getClass("MMServiceCenter") defaultCenter] getService:[objc_getClass("MMQRCodeMgr") class]];
[qrCodeManager getQRCodeFromServer:userID withStyle:6];
self.qrCodeDownloadCallback = nil;
self.qrCodeDownloadCallback = completion;
}
}
- (void)saveQRCodePathOfUser:(NSString *)userID
{
@autoreleasepool
{
[self downloadQRCodeOfUser:userID withCompletion:^(NSString *path)
{
}];
}
}
#if !__has_feature(objc_arc)
- (void)dealloc
{
[conversation release];
conversation = nil;
[super dealloc];
}
#endif
@end