-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.cpp
More file actions
703 lines (546 loc) · 21.4 KB
/
Copy pathscript.cpp
File metadata and controls
703 lines (546 loc) · 21.4 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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
//
// Created by Micael Cossa on 01/02/2026.
//
#define WIN32_LEAN_AND_MEAN
#include "globals.h"
#include "utils.h"
#include <iostream>
#include <thread>
#include <vector>
#include <regex>
#include <sstream>
#include <cwctype>
bool stop_playing = false;
double pricedealing = 0;
HHOOK hooka;
// Stop mechanism
LRESULT CALLBACK StopPlayingProc(int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode >= 0) {
if(wParam == WM_RBUTTONDOWN) {
stop_playing = true;
std::cout << "Play interrupted! \n";
PostThreadMessage(GetCurrentThreadId(), WM_QUIT, 0, 0);
}
}
return CallNextHookEx(hooka, nCode, wParam, lParam);
}
void ActionsScript::addAction(WINDOWS_ACTION action) {
auto node = std::make_unique<ScriptNode>(ScriptNode{std::move(action), nullptr});
auto* raw = node.get();
size++;
if (!header) {
header = std::move(node);
footer = raw;
} else {
footer->next = std::move(node);
footer = raw;
}
}
void ActionsScript::playAllActions(bool repeater) const {
auto* temp = header.get();
DWORD local_threadID =0;
if (!repeater){
std::jthread hookworker([&](HOOKPROC func) {
registerHookThread(hooka, local_threadID, func);
}, StopPlayingProc);
hookworker.detach();
}
while(temp && !stop_playing) {
// Should we stop running the script??! (for now, only special functions have this functionality)
bool action_failed = temp->val.playAction(repeater);
if(action_failed) {
// Is there a protocol for the action if it failed?!
if(!temp->val.failed_action) {
stop_playing = true; // indicates the REPEAT_SCRIPT that it should not run again!
break;
}
//PostThreadMessage(local_threadID, WM_QUIT, 0, 0);
Sleep(300); // give time for the thread exit to finish;
scripts.find(temp->val.failed_action.value())->second.playAllActions(true);
}
temp = temp->next.get();
Sleep(300);
}
PostThreadMessage(local_threadID, WM_QUIT, 0, 0);
// Reset the stop_playing to false because REPEAT_SCRIPT action does it by itself
if(!repeater)
stop_playing = false;
}
void ActionsScript::printAllActions() const {
auto* temp = header.get();
int pos = 1;
while(temp) {
WINDOWS_ACTION action = temp->val;
std::cout << pos << ". -> ";
action.printAction();
temp = temp->next.get();
pos++;
}
}
[[nodiscard]] std::string_view ActionsScript::getName() const noexcept{
return name;
}
[[nodiscard]] int ActionsScript::getNumberOfActions() const noexcept{
return size;
}
void setClipboardText(const std::wstring& text) {
if(!OpenClipboard(nullptr)) {
std::cout << "Unable to open clipboard \n";
return;
}
size_t size = (text.size()+1)*sizeof(wchar_t);
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, size);
if (!hMem) {
std::cout << "Unable to GlobalAlloc \n";
CloseClipboard();
return;
}
void* ptr = GlobalLock(hMem);
memcpy(ptr, text.c_str(), size);
GlobalUnlock(hMem);
HANDLE handle = SetClipboardData(CF_UNICODETEXT, hMem);
if(!handle)
std::cout << "Unable to set clipboard " << GetLastError() << " \n";
CloseClipboard();
}
void ActionsScript::removeAction(int pos) {
auto* temp = header.get();
if(pos == 0) {
if(!header)
return;
header = std::move(header->next);
} else {
for(int i = 1; i < pos; ++i) {
if(!temp) {
std::cout << "Remove action denied, out of bounds? \n";
return;
}
temp = temp->next.get();
}
temp->next = std::move(temp->next->next);
if(!temp->next)
footer = temp;
}
size--;
}
// Completely unnecessary to do it like this, but I wanted to act smart and overcomplicate things because it looks cool, and I am stupid!
void ActionsScript::swapAction(const int pos1, const int pos2) {
if(pos1 > size || pos2 > size) {
std::cout << "Swap action denied, out of bounds. \n";
return;
}
auto* min_pos_temp = header.get();
const int max_pos = max(pos1, pos2), min_pos = min(pos1, pos2);
for(int i = 1; i < min_pos; ++i) {
min_pos_temp = min_pos_temp->next.get();
}
auto* max_pos_temp = min_pos_temp;
for(int i = min(pos1, pos2); i < max_pos; ++i) {
max_pos_temp = max_pos_temp->next.get();
}
std::swap(min_pos_temp->val, max_pos_temp->val);
}
void ActionsScript::moveAction(int oldpos, int newpos) {
if (oldpos == newpos || oldpos < 1 || oldpos > size || newpos < 1 || newpos > size) {
std::cout << "Move action denied, invalid positions.\n";
return;
}
// Get the action to move
auto* temp = header.get();
for (int i = 1; i < oldpos; ++i) {
temp = temp->next.get();
}
WINDOWS_ACTION action_to_move = temp->val;
// Remove it from old position
removeAction(oldpos - 1);
// Insert at new position
insertActionAt(action_to_move, newpos - 1);
}
void ActionsScript::insertActionAt(WINDOWS_ACTION action, int pos) {
auto new_node = std::make_unique<ScriptNode>(ScriptNode{std::move(action), nullptr});
if (pos == 0) {
// Insert at beginning
new_node->next = std::move(header);
header = std::move(new_node);
if (!header->next)
footer = header.get();
} else {
// Find node before insertion point
auto* temp = header.get();
for (int i = 1; i < pos; ++i) {
if (!temp) return;
temp = temp->next.get();
}
// Insert after temp
new_node->next = std::move(temp->next);
temp->next = std::move(new_node);
if (!temp->next->next)
footer = temp->next.get();
}
size++;
}
void WINDOWS_ACTION::printAction() const noexcept {
std::cout << getActionName();
if (auto* location = std::get_if<LOCATION>(&data)) {
std::cout << " Pos ("
<< location->X << " , "
<< location->Y << ")\n";
} else if (auto* text = std::get_if<std::string>(&data)) {
std::cout << " Text: " << *text << "\n";
} else if (auto* number = std::get_if<WORD>(&data)) {
std::cout << " Number: " << *number << "\n";
} else if (auto* intn = std::get_if<int>(&data)) {
std::cout << " Number: " << *intn << "\n";
} else if (auto* locPair = std::get_if<std::pair<LOCATION, LOCATION>>(&data)) {
std::cout << " From ("
<< locPair->first.X << " , " << locPair->first.Y << ")"
<< " To ("
<< locPair->second.X << " , " << locPair->second.Y << ")\n";
} else if(auto* repeatinfo = std::get_if<std::pair<std::string, int>>(&data)) {
std::cout << " Times: " << repeatinfo->second << " \n";
} else {
// std::monostate or unknown
std::cout << "\n";
}
}
std::wstring getClipboardText() {
std::wstring result;
if (!OpenClipboard(nullptr)) {
std::cout << "Unable to access clipboard data \n";
return L"none";
}
HANDLE textData = GetClipboardData(CF_UNICODETEXT);
if (textData) {
auto *text = static_cast<wchar_t *>(GlobalLock(textData));
if (text)
result = text;
else
result = L"none";
GlobalUnlock(textData);
}
CloseClipboard();
return result;
}
std::string_view WINDOWS_ACTION::getActionName() const noexcept {
switch(action) {
case ACTION_TYPE::LEFT_MOUSE_CLICK:
return "LEFT MOUSE CLICK";
case ACTION_TYPE::RIGHT_MOUSE_CLICK:
return "RIGHT MOUSE CLICK";
case ACTION_TYPE::KEYBOARD_CONTROL_COMBO:
return "KEYBOARD CTRL + LETTER";
case ACTION_TYPE::KEYBOARD_TEXT:
return "KEYBOARD TEXT";
case ACTION_TYPE::PASTE_CLIPBOARD:
return "PASTE CLIPBOARD";
case ACTION_TYPE::SPECIAL_FUNCTION:
return "SPECIAL FUNCTION";
case ACTION_TYPE::INSERT_VKKEY:
return "INSERT_VKKEY";
case ACTION_TYPE::WAIT_MILLISECONDS:
return "WAIT_MILLISECONDS";
case ACTION_TYPE::DRAG_MOUSE:
return "DRAG_MOUSE";
case ACTION_TYPE::SPECIAL_FUNCTION2:
return "SPECIAL_FUNCTION2";
case ACTION_TYPE::SPECIAL_FUNCTION3:
return "SPECIAL_FUNCTION3";
case ACTION_TYPE::REPEAT_SCRIPT:
return "REPEAT_SCRIPT";
case ACTION_TYPE::SPECIAL_FUNCTION4:
return "SPECIAL_FUNCTION4";
case ACTION_TYPE::SPECIAL_FUNCTION5:
return "SPECIAL_FUNCTION5";
}
return "unknown action";
}
bool WINDOWS_ACTION::playAction(bool repeaterCall) const noexcept{
std::vector<INPUT> inputs;
switch(action) {
case ACTION_TYPE::LEFT_MOUSE_CLICK: {
auto location = std::get<LOCATION>(data);
SetCursorPos(location.X, location.Y);
Sleep(50); // timing issues
inputs.resize(2);
inputs[0].type = INPUT_MOUSE;
inputs[0].mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
inputs[1].type = INPUT_MOUSE;
inputs[1].mi.dwFlags = MOUSEEVENTF_LEFTUP;
break;
}
case ACTION_TYPE::RIGHT_MOUSE_CLICK: {
auto location = std::get<LOCATION>(data);
SetCursorPos(location.X, location.Y);
inputs.resize(2);
inputs[0].type = INPUT_MOUSE;
inputs[0].mi.dwFlags = MOUSEEVENTF_RIGHTDOWN;
inputs[1].type = INPUT_MOUSE;
inputs[1].mi.dwFlags = MOUSEEVENTF_RIGHTUP;
break;
}
case ACTION_TYPE::KEYBOARD_CONTROL_COMBO: {
WORD vk = VkKeyScan(std::get<std::string>(data)[0]) & 0xFF;
sendKey(VK_CONTROL, false);
sendKey(vk, false);
sendKey(vk, true);
sendKey(VK_CONTROL, true);
break;
}
case ACTION_TYPE::PASTE_CLIPBOARD: {
sendKey(VK_CONTROL, false);
sendKey('V', false);
sendKey('V', true);
sendKey(VK_CONTROL, true);
break;
}
case ACTION_TYPE::KEYBOARD_TEXT: {
auto text = std::get<std::string>(data);
// Convert std::string → UTF-16
int len = MultiByteToWideChar(
CP_UTF8, 0,
text.c_str(), -1,
nullptr, 0
);
std::wstring wtext(len, L'\0');
MultiByteToWideChar(
CP_UTF8, 0,
text.c_str(), -1,
wtext.data(), len
);
for (wchar_t ch : wtext) {
if (ch == L'\0') break;
sendChar(ch);
}
break;
}
case ACTION_TYPE::SPECIAL_FUNCTION: {
std::wstring text = getClipboardText();
if(text == L"none" || text.empty())
return true;
auto pos = text.find(L"NLPG Ref.No:");
if (pos == std::wstring::npos) {
std::cout << "Unable to find NLPG Number \n";
return true;
} else {
pos += wcslen(L"NLPG Ref.No:");
while (pos < text.size() && iswspace(text[pos])) {
++pos;
}
size_t start = pos;
while (pos < text.size() && iswdigit(text[pos])) ++pos;
auto number = text.substr(start, pos - start);
std::wcout << "Analysing number... ";
if(number.empty() || number[0] == ' ' || number[0] == '\t') {
std::wcout << L" Empty number detected \n";
return true;
}
setClipboardText(number);
std::wcout << L"Number detected " << number << "\n";
break;
}
break;
}
case ACTION_TYPE::INSERT_VKKEY: {
auto word_key = std::get<WORD>(data);
sendKey(word_key, false);
sendKey(word_key, true);
break;
}
case ACTION_TYPE::WAIT_MILLISECONDS: {
auto milliseconds = std::get<int>(data);
Sleep(milliseconds);
break;
}
case ACTION_TYPE::DRAG_MOUSE: {
std::pair<LOCATION, LOCATION> locations = std::get<std::pair<LOCATION, LOCATION>>(data);
// Move to start position
SetCursorPos(locations.first.X, locations.first.Y);
Sleep(100);
// Press mouse button
INPUT input{};
input.type = INPUT_MOUSE;
input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
SendInput(1, &input, sizeof(INPUT));
// Wait for the drag to register
Sleep(300);
// Smooth drag with intermediate steps
int steps = 20;
for (int i = 1; i <= steps; ++i) {
int x = locations.first.X + (locations.second.X - locations.first.X) * i / steps;
int y = locations.first.Y + (locations.second.Y - locations.first.Y) * i / steps;
SetCursorPos(x, y);
Sleep(10); // Small delay between each step
}
// Hold at final position
Sleep(500);
// Release mouse button
input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
SendInput(1, &input, sizeof(INPUT));
break;
}
case ACTION_TYPE::SPECIAL_FUNCTION4: {
// Convert to std::string
std::wstring clipboardText = getClipboardText();
if(clipboardText == L"none" || clipboardText.empty())
return true;
// Regex pattern to match the task and number
std::wregex pattern(LR"(SM-Task-(\d{5})\s+(\d+(\.\d{1,2})?))");
std::wsmatch matches;
if (std::regex_search(clipboardText, matches, pattern)) {
std::wstring task = L"SM-Task-" + matches[1].str(); // Task ID
pricedealing = std::stod(matches[2].str()); // Number
std::wcout << L"proccessing "<< task << " " << pricedealing << std::endl;
setClipboardText(task);
} else {
std::cerr << "No matching format found!" << std::endl;
return true;
}
break;
}
case ACTION_TYPE::SPECIAL_FUNCTION5: {
std::wstring clipboardText = getClipboardText();
if(clipboardText == L"none" || clipboardText.empty())
return true;
// Regex pattern to find the first £ followed by a number, with optional commas and decimals
std::wregex pattern(LR"(\u00A3\s?(\d{1,3}(?:,\d{3})*(?:\.\d{1,2})?))");
std::wsmatch matches;
if (std::regex_search(clipboardText, matches, pattern)) {
std::wstring amountStr = matches[1].str();
// Remove commas (if present)
amountStr.erase(std::remove(amountStr.begin(), amountStr.end(), L','), amountStr.end());
// Convert to double
try {
double amount = std::stod(amountStr);
if(pricedealing == amount) {
std::cout << "price match! " << amount << "\n ";
} else {
std::cout << "price did not match " << amount << "\n";
}
setClipboardText(amountStr);
break;
}
catch (const std::exception& e) {
std::cout << "Failed to convert amount: " << e.what() << std::endl;
return true;
}
} else {
std::cout << "No valid pound amount found!" << std::endl;
return true;
}
break;
}
case ACTION_TYPE::SPECIAL_FUNCTION2: { // file name
std::wstring input = getClipboardText();
if(input == L"none" || input.empty())
return true;
std::wstring output;
std::wregex range_pattern(LR"(^\d+-(\d+\s+))", std::regex_constants::icase);
input = std::regex_replace(input, range_pattern, L"$1");
std::wregex hyphen_code_pattern(LR"(-([a-zA-Z]))", std::regex_constants::icase);
input = std::regex_replace(input, hyphen_code_pattern, L" $1");
std::vector<std::wstring> code_keywords = {
L"HD", L"SD", L"MW", L"CO", L"FIREDOOR", L"LAS", L"cr", L"-" , L"ELE", L"EIR", L"cr", L"eicr", L"bcn", L"ct", L"emer", L"trada", L"bmtrada", L"BM", L"FIRE", L"ct", L"Full", L"Full H", L"Htg", L"Hse", L"AMENDED", L"RADS", L"Rads", L"FAN", L"DEICR", L"SWI", L"KIT", L"KIT Survey", L"LAS", L"BATTERY", L"BAT", L"HO", L"KIT Asbes", L"Asbest", L"WAIVER", L"Waiver Mains", L"DA LAS", L"CP12", L"Gas Safe",
L"Handover", L"Boiler", L"Form", L"IMS", L"DFHN", L"CB", L"CB5", L"CB4", L"CB3", L"HWT", L"EIC", L"Asbestos", L"DA KIT", L"LAS HO", L"TEST", L"TEST ONLY"
};
std::wregex date_pattern(LR"((\d{2})\s*([a-zA-Z]{3,4})\s*(\d{2}))", std::regex_constants::icase);
std::wsmatch date_match;
std::wstring remaining = input;
std::wstring formatted_date;
// Extract and format date
if (std::regex_search(input, date_match, date_pattern)) {
std::wstring day = date_match[1].str();
std::wstring month = date_match[2].str();
std::wstring year = date_match[3].str();
formatted_date = day + L" " + month + L" " + year;
// Remove date from input
remaining = input.substr(0, date_match.position());
} else {
// Default date if no date found
formatted_date = L"01 jun 2019";
}
// Trim trailing whitespace
while (!remaining.empty() && std::iswspace(remaining.back())) {
remaining.pop_back();
}
// Split remaining into words
std::vector<std::wstring> words;
std::wstringstream ss(remaining);
std::wstring word;
while (ss >> word) {
words.push_back(word);
}
// Find where the code section starts (first keyword match)
size_t code_start_index = words.size();
for (size_t i = 0; i < words.size(); ++i) {
for (const auto& keyword : code_keywords) {
std::wstring word_lower = words[i];
std::wstring keyword_lower = keyword;
std::transform(word_lower.begin(), word_lower.end(), word_lower.begin(), ::towlower);
std::transform(keyword_lower.begin(), keyword_lower.end(), keyword_lower.begin(), ::towlower);
if (word_lower == keyword_lower) {
code_start_index = i;
break;
}
}
if (code_start_index < words.size()) {
break; // Found the start of code section
}
}
// LIMIT: Address can be maximum 3 words
// If no keyword found or keyword is after word 3, split at word 3
if (code_start_index > 3) {
code_start_index = 3;
}
// Build street address (words before code section)
std::wstring street_address;
for (size_t i = 0; i < code_start_index; ++i) {
if (i > 0) street_address += L" ";
street_address += words[i];
}
// Build code section (words from code start onward)
std::wstring code_section;
for (size_t i = code_start_index; i < words.size(); ++i) {
if (i > code_start_index) code_section += L" ";
code_section += words[i];
}
// Safety: If no address found (empty input), use placeholder
if (street_address.empty()) {
street_address = L"NO ADDRESS";
}
// If no code section found, use default
if (code_section.empty()) {
code_section = L"cert";
}
// Build final output - ALWAYS output something
output = street_address;
output += L"\n" + formatted_date;
output += L"\n" + code_section;
// Set to clipboard
setClipboardText(output);
std::wcout << L"Processed:\n" << output << L"\n";
break;
}
case ACTION_TYPE::SPECIAL_FUNCTION3: // paste the remaining
break;
case ACTION_TYPE::REPEAT_SCRIPT:
// make sure we dont repeat infinitely
if(!repeaterCall) {
auto repeatinfo = std::get<std::pair<std::string, int>>(data);
for(int i = 0; i < repeatinfo.second; ++i) {
if(stop_playing)
break;
scripts.find(repeatinfo.first)->second.playAllActions(true);
}
stop_playing = false;
}
break;
}
if (!inputs.empty()) {
SendInput(
static_cast<UINT>(inputs.size()),
inputs.data(),
sizeof(INPUT)
);
}
return false;
}