Skip to content

Conversation

@ZarTek-Creole
Copy link
Contributor

Title

WIP: Add `timerexists` and `utimerexists` Tcl commands to check timer existence

📝 Description

## Summary

This pull request introduces two new experimental Tcl commands for Eggdrop:

- `timerexists <name>`
- `utimerexists <name>`

These commands allow script authors to check whether a timer or utimer with a given name currently exists.  
This is meant to complement `killtimer` and `killutimer` by providing a safe way to check existence before attempting to remove a timer.

---

## 🔧 Status

🚧 **Draft / WIP** — Not tested yet.  
Needs review and validation from Eggdrop core devs.

---

## 📚 Proposed Commands

### `timerexists <timerName>`
Returns `1` if a timer with this name exists, `0` otherwise.

### `utimerexists <timerName>`
Same as above, but for secondly-based timers.

---

## 💡 Motivation

Currently, scripts must use complex or error-prone workarounds to detect timers.  
This patch introduces a clean, reliable way to check if a timer/utimer exists by name.

### Example: current workaround

```tcl
set t [utimer 1 myTimer] 0 myTimer
if {[info exists t] && [catch {killutimer myTimer} error]} {
  putlog "Warning: Unable to kill utimer ($error)."
}

With this feature:

utimer 1 myTimer 0 myTimer
if {[utimerexists myTimer]} {
  killutimer myTimer
}

🧩 Integration details

  • Implemented in src/tclmisc.c
  • Reuses the logic from killtimer / killutimer
  • No known side-effects
  • Syntax is consistent with existing Eggdrop command naming (explicit and clear)

⚠️ Notes

  • ❌ Not yet tested
  • ❗ Documentation still needs to be updated (doc/tcl-commands.doc)
  • 🧪 Needs review by maintainers to confirm viability, naming, behavior, etc.
  • 🤝 Suggestions and improvements welcome

Add new Tcl commands `timerexists` and `utimerexists` to check if a timer or utimer with a given name exists. This enhances scripting capabilities and debugging of timers.

Patch by: ZarTek-Creole
{
BADARGS(2, 2, " timerName");

if (find_timer(timer, argv[1])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tcl_AppendResult() is deprecated. Use Tcl_SetResult() if you can. You could do a one-liner, something like:

Tcl_SetResult(interp, find_timer(timer, argv[1]) ? "1" : "0", TCL_STATIC);

@michaelortmann
Copy link
Member

i guess you want to add the documentation to doc/sphinx_source/using/tcl-commands.rst

@michaelortmann
Copy link
Member

is there any relation to #507?

@ZarTek-Creole ZarTek-Creole marked this pull request as ready for review April 11, 2025 14:56
Copy link
Contributor Author

@ZarTek-Creole ZarTek-Creole left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tcl_AppendResult() is deprecated. Use Tcl_SetResult() if you can. You could do a one-liner, something like:

Tcl_SetResult(interp, find_timer(timer, argv[1]) ? "1" : "0", TCL_STATIC);

Good point 👍
You're right — Tcl_AppendResult() has been deprecated since Tcl 8.4, and your Tcl_SetResult() one-liner is perfectly clean for this static return value.

Updated implementation:

Tcl_SetResult(interp, find_timer(timer, argv[1]) ? "1" : "0", TCL_STATIC);

For even better Tcl 8.5+ compatibility (or native boolean handling), this would be more future-proof:

Tcl_SetObjResult(interp, Tcl_NewBooleanObj(find_timer(timer, argv[1]) != NULL));

This makes [timerexists foo] return a native Tcl boolean, which works seamlessly with if and other logic. That said, the current "1"/"0" strings are still fine and match the codebase's conventions.

References:


i guess you want to add the documentation to doc/sphinx_source/using/tcl-commands.rst

Good catch — I'll add this to the docs in tcl-commands.rst.


is there any relation to #507?

Not directly, though #507 might benefit from similar cleanup. I'll check the ticket and update if needed.

@crazycatdevs
Copy link
Contributor

Good idea but with another name, as (u)timerexists is already existing in alltools.tcl and used by several scripts and doesn't search on the same argument (command vs name).

@ZarTek-Creole ZarTek-Creole changed the title WIP: Add timerexists and utimerexists Tcl commands to check timer existence WIP: Add timerexistsname and utimerexistsname Tcl commands to check timer existence Apr 22, 2025
@ZarTek-Creole ZarTek-Creole marked this pull request as draft May 4, 2025 13:50
@ZarTek-Creole ZarTek-Creole marked this pull request as ready for review May 4, 2025 13:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants