-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathEventRules.m
executable file
·178 lines (142 loc) · 3.97 KB
/
EventRules.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
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
//
// EventRules.m
// KnockKnock
//
// Created by Patrick Wardle on 03/25/18.
// Copyright (c) 2018 Objective-See. All rights reserved.
//
#import "Command.h"
#import "Utilities.h"
#import "EventRules.h"
//plugin name
#define PLUGIN_NAME @"Event Rules"
//plugin description
#define PLUGIN_DESCRIPTION NSLocalizedString(@"actions executed by emond", @"actions executed by emond")
//plugin icon
#define PLUGIN_ICON @"eventRulesIcon"
//default rule directory
#define DEFAULT_EMOND_RULES @"/etc/emond.d/rules/"
//config file
#define EMOND_CONFIG @"/etc/emond.d/emond.plist"
@implementation EventRules
//init
// ->set name, description, etc
-(id)init
{
//super
self = [super init];
if(self)
{
//set name
self.name = PLUGIN_NAME;
//set description
self.description = PLUGIN_DESCRIPTION;
//set icon
self.icon = PLUGIN_ICON;
}
return self;
}
//scan for emond commands
-(void)scan
{
//rules directories
NSMutableArray* rulesDirectories = nil;
//rule plists
NSArray* ruleFiles = nil;
//config
NSDictionary* config = nil;
//additional rule directories
NSMutableArray* additionalRuleDirs = nil;
//commands
NSMutableArray* commands = nil;
//Command obj
Command* commandObj = nil;
//alloc
rulesDirectories = [NSMutableArray array];
//load config
config = [NSDictionary dictionaryWithContentsOfFile:EMOND_CONFIG][@"config"];
if(nil != config)
{
//grab additional rules
additionalRuleDirs = config[@"additionalRulesPaths"];
}
//add default
[rulesDirectories addObject:DEFAULT_EMOND_RULES];
//add any additional
for(NSString* additionalRuleDir in additionalRuleDirs)
{
//add
[rulesDirectories addObject:additionalRuleDir];
}
//process each rule directory
for(NSString* ruleDirectory in rulesDirectories)
{
//get rule (plist) files
ruleFiles = directoryContents(ruleDirectory, nil);
//get commands for each rule file
for(NSString* ruleFile in ruleFiles)
{
//get commands
commands = [self extractCommands:[ruleDirectory stringByAppendingPathComponent:ruleFile]];
//process all commands
for(NSString* command in commands)
{
//create Command object for job
commandObj = [[Command alloc] initWithParams:@{KEY_RESULT_PLUGIN:self, KEY_RESULT_COMMAND:command, KEY_RESULT_PATH:[ruleDirectory stringByAppendingPathComponent:ruleFile]}];
if(nil == commandObj)
{
//skip
continue;
}
//process item
// save and report to UI
[super processItem:commandObj];
}
}
}
return;
}
//extract commands from a rule file
-(NSMutableArray*)extractCommands:(NSString*)ruleFile
{
//plist contents
NSMutableArray* rules = nil;
//commands
NSMutableArray* commands = nil;
//actions
NSArray* actions = nil;
//alloc
commands = [NSMutableArray array];
//load rule file
rules = [NSMutableArray arrayWithContentsOfFile:ruleFile];
if(nil == rules)
{
//bail
goto bail;
}
//process all rules
for(NSDictionary* rule in rules)
{
actions = rule[@"actions"];
if( (nil == actions) ||
(YES != [actions isKindOfClass:[NSArray class]]) )
{
//skip
continue;
}
//process all actions
for(NSDictionary* action in actions)
{
if(nil == action[@"command"])
{
//skip
continue;
}
//add
[commands addObject:action[@"command"]];
}
}
bail:
return commands;
}
@end