diff options
| author | 2024-05-15 13:29:07 +0200 | |
|---|---|---|
| committer | 2024-05-15 13:29:07 +0200 | |
| commit | 0a6cfba399cbd7510b6ffa9ddfde5a8ca31c4828 (patch) | |
| tree | 5214b932909a3ff0f877bb081fa453cc5fe4ec05 /data.c | |
| parent | Use xs_regex_match() where applicable. (diff) | |
| download | penes-snac2-0a6cfba399cbd7510b6ffa9ddfde5a8ca31c4828.tar.gz penes-snac2-0a6cfba399cbd7510b6ffa9ddfde5a8ca31c4828.tar.xz penes-snac2-0a6cfba399cbd7510b6ffa9ddfde5a8ca31c4828.zip | |
Moved functions around.
Diffstat (limited to 'data.c')
| -rw-r--r-- | data.c | 220 |
1 files changed, 109 insertions, 111 deletions
| @@ -2270,7 +2270,7 @@ int instance_unblock(const char *instance) | |||
| 2270 | } | 2270 | } |
| 2271 | 2271 | ||
| 2272 | 2272 | ||
| 2273 | /** content filtering **/ | 2273 | /** operations by content **/ |
| 2274 | 2274 | ||
| 2275 | int content_check(const char *file, const xs_dict *msg) | 2275 | int content_check(const char *file, const xs_dict *msg) |
| 2276 | /* checks if a message's content matches any of the regexes in file */ | 2276 | /* checks if a message's content matches any of the regexes in file */ |
| @@ -2307,6 +2307,114 @@ int content_check(const char *file, const xs_dict *msg) | |||
| 2307 | } | 2307 | } |
| 2308 | 2308 | ||
| 2309 | 2309 | ||
| 2310 | xs_list *content_search(snac *user, const char *regex, | ||
| 2311 | int priv, int skip, int show, int max_secs, int *timeout) | ||
| 2312 | /* returns a list of posts which content matches the regex */ | ||
| 2313 | { | ||
| 2314 | if (regex == NULL || *regex == '\0') | ||
| 2315 | return xs_list_new(); | ||
| 2316 | |||
| 2317 | xs *i_regex = xs_tolower_i(xs_dup(regex)); | ||
| 2318 | |||
| 2319 | xs_set seen; | ||
| 2320 | |||
| 2321 | xs_set_init(&seen); | ||
| 2322 | |||
| 2323 | if (max_secs == 0) | ||
| 2324 | max_secs = 3; | ||
| 2325 | |||
| 2326 | time_t t = time(NULL) + max_secs; | ||
| 2327 | *timeout = 0; | ||
| 2328 | |||
| 2329 | /* iterate all timelines simultaneously */ | ||
| 2330 | xs_list *tls[3] = {0}; | ||
| 2331 | char *md5s[3] = {0}; | ||
| 2332 | int c[3] = {0}; | ||
| 2333 | |||
| 2334 | tls[0] = timeline_simple_list(user, "public", 0, XS_ALL); /* public */ | ||
| 2335 | tls[1] = timeline_instance_list(0, XS_ALL); /* instance */ | ||
| 2336 | tls[2] = priv ? timeline_simple_list(user, "private", 0, XS_ALL) : xs_list_new(); /* private or none */ | ||
| 2337 | |||
| 2338 | /* first positioning */ | ||
| 2339 | for (int n = 0; n < 3; n++) | ||
| 2340 | xs_list_next(tls[n], &md5s[n], &c[n]); | ||
| 2341 | |||
| 2342 | show += skip; | ||
| 2343 | |||
| 2344 | while (show > 0) { | ||
| 2345 | /* timeout? */ | ||
| 2346 | if (time(NULL) > t) { | ||
| 2347 | *timeout = 1; | ||
| 2348 | break; | ||
| 2349 | } | ||
| 2350 | |||
| 2351 | /* find the newest post */ | ||
| 2352 | int newest = -1; | ||
| 2353 | double mtime = 0.0; | ||
| 2354 | |||
| 2355 | for (int n = 0; n < 3; n++) { | ||
| 2356 | if (md5s[n] != NULL) { | ||
| 2357 | xs *fn = _object_fn_by_md5(md5s[n], "content_search"); | ||
| 2358 | double mt = mtime(fn); | ||
| 2359 | |||
| 2360 | if (mt > mtime) { | ||
| 2361 | newest = n; | ||
| 2362 | mtime = mt; | ||
| 2363 | } | ||
| 2364 | } | ||
| 2365 | } | ||
| 2366 | |||
| 2367 | if (newest == -1) | ||
| 2368 | break; | ||
| 2369 | |||
| 2370 | char *md5 = md5s[newest]; | ||
| 2371 | |||
| 2372 | /* advance the chosen timeline */ | ||
| 2373 | if (!xs_list_next(tls[newest], &md5s[newest], &c[newest])) | ||
| 2374 | md5s[newest] = NULL; | ||
| 2375 | |||
| 2376 | xs *post = NULL; | ||
| 2377 | |||
| 2378 | if (!valid_status(object_get_by_md5(md5, &post))) | ||
| 2379 | continue; | ||
| 2380 | |||
| 2381 | if (!xs_match(xs_dict_get_def(post, "type", "-"), POSTLIKE_OBJECT_TYPE)) | ||
| 2382 | continue; | ||
| 2383 | |||
| 2384 | char *content = xs_dict_get(post, "content"); | ||
| 2385 | |||
| 2386 | if (xs_is_null(content)) | ||
| 2387 | continue; | ||
| 2388 | |||
| 2389 | /* strip HTML */ | ||
| 2390 | xs *c = xs_regex_replace(content, "<[^>]+>", " "); | ||
| 2391 | c = xs_regex_replace_i(c, " {2,}", " "); | ||
| 2392 | c = xs_tolower_i(c); | ||
| 2393 | |||
| 2394 | /* apply regex */ | ||
| 2395 | if (xs_regex_match(c, i_regex)) { | ||
| 2396 | if (xs_set_add(&seen, md5) == 1) | ||
| 2397 | show--; | ||
| 2398 | } | ||
| 2399 | } | ||
| 2400 | |||
| 2401 | xs_list *r = xs_set_result(&seen); | ||
| 2402 | |||
| 2403 | if (skip) { | ||
| 2404 | /* BAD */ | ||
| 2405 | while (skip--) { | ||
| 2406 | r = xs_list_del(r, 0); | ||
| 2407 | } | ||
| 2408 | } | ||
| 2409 | |||
| 2410 | xs_free(tls[0]); | ||
| 2411 | xs_free(tls[1]); | ||
| 2412 | xs_free(tls[2]); | ||
| 2413 | |||
| 2414 | return r; | ||
| 2415 | } | ||
| 2416 | |||
| 2417 | |||
| 2310 | /** notifications **/ | 2418 | /** notifications **/ |
| 2311 | 2419 | ||
| 2312 | xs_str *notify_check_time(snac *snac, int reset) | 2420 | xs_str *notify_check_time(snac *snac, int reset) |
| @@ -2485,116 +2593,6 @@ void notify_clear(snac *snac) | |||
| 2485 | } | 2593 | } |
| 2486 | 2594 | ||
| 2487 | 2595 | ||
| 2488 | /** searches **/ | ||
| 2489 | |||
| 2490 | xs_list *content_search(snac *user, const char *regex, | ||
| 2491 | int priv, int skip, int show, int max_secs, int *timeout) | ||
| 2492 | /* returns a list of posts which content matches the regex */ | ||
| 2493 | { | ||
| 2494 | if (regex == NULL || *regex == '\0') | ||
| 2495 | return xs_list_new(); | ||
| 2496 | |||
| 2497 | xs *i_regex = xs_tolower_i(xs_dup(regex)); | ||
| 2498 | |||
| 2499 | xs_set seen; | ||
| 2500 | |||
| 2501 | xs_set_init(&seen); | ||
| 2502 | |||
| 2503 | if (max_secs == 0) | ||
| 2504 | max_secs = 3; | ||
| 2505 | |||
| 2506 | time_t t = time(NULL) + max_secs; | ||
| 2507 | *timeout = 0; | ||
| 2508 | |||
| 2509 | /* iterate all timelines simultaneously */ | ||
| 2510 | xs_list *tls[3] = {0}; | ||
| 2511 | char *md5s[3] = {0}; | ||
| 2512 | int c[3] = {0}; | ||
| 2513 | |||
| 2514 | tls[0] = timeline_simple_list(user, "public", 0, XS_ALL); /* public */ | ||
| 2515 | tls[1] = timeline_instance_list(0, XS_ALL); /* instance */ | ||
| 2516 | tls[2] = priv ? timeline_simple_list(user, "private", 0, XS_ALL) : xs_list_new(); /* private or none */ | ||
| 2517 | |||
| 2518 | /* first positioning */ | ||
| 2519 | for (int n = 0; n < 3; n++) | ||
| 2520 | xs_list_next(tls[n], &md5s[n], &c[n]); | ||
| 2521 | |||
| 2522 | show += skip; | ||
| 2523 | |||
| 2524 | while (show > 0) { | ||
| 2525 | /* timeout? */ | ||
| 2526 | if (time(NULL) > t) { | ||
| 2527 | *timeout = 1; | ||
| 2528 | break; | ||
| 2529 | } | ||
| 2530 | |||
| 2531 | /* find the newest post */ | ||
| 2532 | int newest = -1; | ||
| 2533 | double mtime = 0.0; | ||
| 2534 | |||
| 2535 | for (int n = 0; n < 3; n++) { | ||
| 2536 | if (md5s[n] != NULL) { | ||
| 2537 | xs *fn = _object_fn_by_md5(md5s[n], "content_search"); | ||
| 2538 | double mt = mtime(fn); | ||
| 2539 | |||
| 2540 | if (mt > mtime) { | ||
| 2541 | newest = n; | ||
| 2542 | mtime = mt; | ||
| 2543 | } | ||
| 2544 | } | ||
| 2545 | } | ||
| 2546 | |||
| 2547 | if (newest == -1) | ||
| 2548 | break; | ||
| 2549 | |||
| 2550 | char *md5 = md5s[newest]; | ||
| 2551 | |||
| 2552 | /* advance the chosen timeline */ | ||
| 2553 | if (!xs_list_next(tls[newest], &md5s[newest], &c[newest])) | ||
| 2554 | md5s[newest] = NULL; | ||
| 2555 | |||
| 2556 | xs *post = NULL; | ||
| 2557 | |||
| 2558 | if (!valid_status(object_get_by_md5(md5, &post))) | ||
| 2559 | continue; | ||
| 2560 | |||
| 2561 | if (!xs_match(xs_dict_get_def(post, "type", "-"), POSTLIKE_OBJECT_TYPE)) | ||
| 2562 | continue; | ||
| 2563 | |||
| 2564 | char *content = xs_dict_get(post, "content"); | ||
| 2565 | |||
| 2566 | if (xs_is_null(content)) | ||
| 2567 | continue; | ||
| 2568 | |||
| 2569 | /* strip HTML */ | ||
| 2570 | xs *c = xs_regex_replace(content, "<[^>]+>", " "); | ||
| 2571 | c = xs_regex_replace_i(c, " {2,}", " "); | ||
| 2572 | c = xs_tolower_i(c); | ||
| 2573 | |||
| 2574 | /* apply regex */ | ||
| 2575 | if (xs_regex_match(c, i_regex)) { | ||
| 2576 | if (xs_set_add(&seen, md5) == 1) | ||
| 2577 | show--; | ||
| 2578 | } | ||
| 2579 | } | ||
| 2580 | |||
| 2581 | xs_list *r = xs_set_result(&seen); | ||
| 2582 | |||
| 2583 | if (skip) { | ||
| 2584 | /* BAD */ | ||
| 2585 | while (skip--) { | ||
| 2586 | r = xs_list_del(r, 0); | ||
| 2587 | } | ||
| 2588 | } | ||
| 2589 | |||
| 2590 | xs_free(tls[0]); | ||
| 2591 | xs_free(tls[1]); | ||
| 2592 | xs_free(tls[2]); | ||
| 2593 | |||
| 2594 | return r; | ||
| 2595 | } | ||
| 2596 | |||
| 2597 | |||
| 2598 | /** the queue **/ | 2596 | /** the queue **/ |
| 2599 | 2597 | ||
| 2600 | static xs_dict *_enqueue_put(const char *fn, xs_dict *msg) | 2598 | static xs_dict *_enqueue_put(const char *fn, xs_dict *msg) |