mirror of
https://github.com/ZoiteChat/zoitechat.git
synced 2026-11-08 17:05:38 +00:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
420adde173 |
No files matched your search
@@ -0,0 +1,24 @@
|
|||||||
|
# Connection and context navigation
|
||||||
|
|
||||||
|
`get_connections()` returns one Connection snapshot per live connection:
|
||||||
|
`id`, `network`, `server`, `context`. Connection IDs distinguish simultaneous
|
||||||
|
connections with identical names. Prefer its server-tab context where present.
|
||||||
|
`get_connection(context=None)` finds the owner of a live context.
|
||||||
|
|
||||||
|
`get_contexts(connection_id=None, types=None)` lists live contexts, optionally
|
||||||
|
filtered by connection ID (or a Connection object) and session types: server=1,
|
||||||
|
channel=2, query=3. `connection.contexts(types=None)` is a convenience method.
|
||||||
|
`find_context_by_id(connection_id, channel=None)` / `connection.find(channel)`
|
||||||
|
resolve using that connection's IRC comparator; no channel selects the server tab.
|
||||||
|
Missing/closed contexts return None. Existing find_context() remains unchanged.
|
||||||
|
|
||||||
|
These helpers use the existing channels list's ID/context fields, so no new C
|
||||||
|
context ABI is needed. Snapshots are not persistent IDs across client restarts;
|
||||||
|
refresh them after closing/recreating connections. Always check Context.set()
|
||||||
|
when explicitly selecting a context that might have closed.
|
||||||
|
|
||||||
|
```python
|
||||||
|
connection = zoitechat.get_connection()
|
||||||
|
for context in connection.contexts(types=(2,)):
|
||||||
|
context.prnt('A channel on this exact connection')
|
||||||
|
```
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
# Post-display print observers
|
|
||||||
|
|
||||||
Python: `hook_print_after(name, callback, userdata=None, priority=PRI_NORM)`.
|
|
||||||
The callback is `(word, word_eol, userdata, attrs)`, matching hook_print_attrs().
|
|
||||||
It runs after display_event() has handled the text, so prnt() follows the original
|
|
||||||
text. Return values are ignored. Events eaten by a pre-hook or hidden by client
|
|
||||||
preferences do not invoke these observers. This is post-display notification,
|
|
||||||
not a notification that the entire incoming IRC command has finished updating state.
|
|
||||||
|
|
||||||
C: `zoitechat_hook_print_after(ph, name, priority, flags, callback, userdata)`;
|
|
||||||
flags must be zero. Check `get_info("api_hook_print_after")` for "1" before
|
|
||||||
accessing the appended Windows function-table slot. Existing pre-hook timing is
|
|
||||||
unchanged. Unhook/unload use existing lifecycle functions. Recursive emission
|
|
||||||
skips an already-running observer to prevent it immediately invoking itself.
|
|
||||||
|
|
||||||
```python
|
|
||||||
def after(word, word_eol, userdata, attrs):
|
|
||||||
zoitechat.prnt('This follows the original channel message')
|
|
||||||
|
|
||||||
zoitechat.hook_print_after('Channel Message', after)
|
|
||||||
```
|
|
||||||
@@ -0,0 +1,75 @@
|
|||||||
|
"""Navigate live connections by ID instead of ambiguous server/network names."""
|
||||||
|
from collections import namedtuple
|
||||||
|
import operator
|
||||||
|
import _zoitechat as api
|
||||||
|
|
||||||
|
__all__ = ['Connection', 'get_connections', 'get_connection', 'get_contexts',
|
||||||
|
'find_context_by_id']
|
||||||
|
|
||||||
|
|
||||||
|
class Connection(namedtuple('_Connection', 'id network server context')):
|
||||||
|
__slots__ = ()
|
||||||
|
|
||||||
|
def contexts(self, types=None):
|
||||||
|
return get_contexts(self.id, types)
|
||||||
|
|
||||||
|
def find(self, channel=None):
|
||||||
|
return find_context_by_id(self.id, channel)
|
||||||
|
|
||||||
|
|
||||||
|
def _id(connection):
|
||||||
|
return operator.index(connection.id if isinstance(connection, Connection) else connection)
|
||||||
|
|
||||||
|
|
||||||
|
def get_connections():
|
||||||
|
"""Return one snapshot per live connection, including disconnected ones."""
|
||||||
|
entries = api.get_list('channels')
|
||||||
|
by_id = {}
|
||||||
|
order = []
|
||||||
|
for entry in entries:
|
||||||
|
if entry.id not in by_id:
|
||||||
|
order.append(entry.id)
|
||||||
|
if entry.id not in by_id or entry.type == 1:
|
||||||
|
by_id[entry.id] = Connection(entry.id, entry.network, entry.server,
|
||||||
|
entry.context)
|
||||||
|
return [by_id[connection_id] for connection_id in order]
|
||||||
|
|
||||||
|
|
||||||
|
def get_connection(context=None):
|
||||||
|
"""Find the owning connection of a Context; return None when it has closed."""
|
||||||
|
context = context or api.get_context()
|
||||||
|
for entry in api.get_list('channels'):
|
||||||
|
if entry.context == context:
|
||||||
|
for connection in get_connections():
|
||||||
|
if connection.id == entry.id:
|
||||||
|
return connection
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def get_contexts(connection_id=None, types=None):
|
||||||
|
"""Return contexts, optionally restricted to a connection and session types."""
|
||||||
|
if connection_id is not None:
|
||||||
|
connection_id = _id(connection_id)
|
||||||
|
if types is not None:
|
||||||
|
types = frozenset(types)
|
||||||
|
return [entry.context for entry in api.get_list('channels')
|
||||||
|
if (connection_id is None or entry.id == connection_id)
|
||||||
|
and (types is None or entry.type in types)]
|
||||||
|
|
||||||
|
|
||||||
|
def find_context_by_id(connection_id, channel=None):
|
||||||
|
"""Resolve a live context on exactly one connection; None selects its server tab."""
|
||||||
|
connection_id = _id(connection_id)
|
||||||
|
previous = api.get_context()
|
||||||
|
try:
|
||||||
|
for entry in api.get_list('channels'):
|
||||||
|
if entry.id != connection_id:
|
||||||
|
continue
|
||||||
|
if channel is None:
|
||||||
|
if entry.type == 1:
|
||||||
|
return entry.context
|
||||||
|
elif entry.context.set() and api.nickcmp(entry.channel, channel) == 0:
|
||||||
|
return entry.context
|
||||||
|
finally:
|
||||||
|
previous.set()
|
||||||
|
return None
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
"""Opt-in post-display print observers."""
|
|
||||||
import _zoitechat as api
|
|
||||||
from _zoitechat_embedded import ffi, lib
|
|
||||||
|
|
||||||
__all__ = ['hook_print_after']
|
|
||||||
|
|
||||||
|
|
||||||
def hook_print_after(name, callback, userdata=None, priority=api.PRI_NORM):
|
|
||||||
"""callback(word, word_eol, userdata, attrs); its return value is ignored."""
|
|
||||||
if not callable(callback):
|
|
||||||
raise TypeError('callback must be callable')
|
|
||||||
plugin = api.__get_current_plugin()
|
|
||||||
hook = plugin.add_hook(callback, userdata)
|
|
||||||
handle = lib.zoitechat_hook_print_after(lib.ph, name.encode(), priority, 0,
|
|
||||||
lib._on_print_attrs_hook, hook.handle)
|
|
||||||
if handle == ffi.NULL:
|
|
||||||
hook.is_unload = True
|
|
||||||
plugin.remove_hook(id(hook))
|
|
||||||
raise RuntimeError('unable to install post-display hook')
|
|
||||||
hook.zoitechat_hook = handle
|
|
||||||
return id(hook)
|
|
||||||
+1
-41
@@ -143,8 +143,7 @@ enum
|
|||||||
HOOK_PRINT_ATTRS = 1 << 4, /* same as above, with attributes */
|
HOOK_PRINT_ATTRS = 1 << 4, /* same as above, with attributes */
|
||||||
HOOK_TIMER = 1 << 5, /* timeouts */
|
HOOK_TIMER = 1 << 5, /* timeouts */
|
||||||
HOOK_FD = 1 << 6, /* sockets & fds */
|
HOOK_FD = 1 << 6, /* sockets & fds */
|
||||||
HOOK_DELETED = 1 << 7, /* marked for deletion */
|
HOOK_DELETED = 1 << 7 /* marked for deletion */
|
||||||
HOOK_PRINT_AFTER = 1 << 8 /* post-display observers */
|
|
||||||
};
|
};
|
||||||
|
|
||||||
enum
|
enum
|
||||||
@@ -352,7 +351,6 @@ plugin_add (session *sess, char *filename, void *handle, void *init_func,
|
|||||||
pl->zoitechat_emit_print_attrs = zoitechat_emit_print_attrs;
|
pl->zoitechat_emit_print_attrs = zoitechat_emit_print_attrs;
|
||||||
pl->zoitechat_event_attrs_create = zoitechat_event_attrs_create;
|
pl->zoitechat_event_attrs_create = zoitechat_event_attrs_create;
|
||||||
pl->zoitechat_event_attrs_free = zoitechat_event_attrs_free;
|
pl->zoitechat_event_attrs_free = zoitechat_event_attrs_free;
|
||||||
pl->zoitechat_hook_print_after = zoitechat_hook_print_after;
|
|
||||||
|
|
||||||
/* run zoitechat_plugin_init, if it returns 0, close the plugin */
|
/* run zoitechat_plugin_init, if it returns 0, close the plugin */
|
||||||
if (((zoitechat_init_func *)init_func) (pl, &pl->name, &pl->desc, &pl->version, arg) == 0)
|
if (((zoitechat_init_func *)init_func) (pl, &pl->name, &pl->desc, &pl->version, arg) == 0)
|
||||||
@@ -623,13 +621,6 @@ plugin_hook_run (session *sess, char *name, char *word[], char *word_eol[],
|
|||||||
|
|
||||||
hook = list->data;
|
hook = list->data;
|
||||||
next = list->next;
|
next = list->next;
|
||||||
if (type == HOOK_PRINT_AFTER && hook->tag)
|
|
||||||
{
|
|
||||||
list = next;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (type == HOOK_PRINT_AFTER)
|
|
||||||
hook->tag = 1;
|
|
||||||
hook->pl->context = sess;
|
hook->pl->context = sess;
|
||||||
|
|
||||||
/* run the plugin's callback function */
|
/* run the plugin's callback function */
|
||||||
@@ -638,7 +629,6 @@ plugin_hook_run (session *sess, char *name, char *word[], char *word_eol[],
|
|||||||
case HOOK_COMMAND:
|
case HOOK_COMMAND:
|
||||||
ret = ((zoitechat_cmd_cb *)hook->callback) (word, word_eol, hook->userdata);
|
ret = ((zoitechat_cmd_cb *)hook->callback) (word, word_eol, hook->userdata);
|
||||||
break;
|
break;
|
||||||
case HOOK_PRINT_AFTER:
|
|
||||||
case HOOK_PRINT_ATTRS:
|
case HOOK_PRINT_ATTRS:
|
||||||
ret = ((zoitechat_print_attrs_cb *)hook->callback) (word, attrs, hook->userdata);
|
ret = ((zoitechat_print_attrs_cb *)hook->callback) (word, attrs, hook->userdata);
|
||||||
break;
|
break;
|
||||||
@@ -653,15 +643,6 @@ plugin_hook_run (session *sess, char *name, char *word[], char *word_eol[],
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type == HOOK_PRINT_AFTER)
|
|
||||||
{
|
|
||||||
hook->tag = 0;
|
|
||||||
if (!is_session (sess))
|
|
||||||
goto xit;
|
|
||||||
list = next;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((ret & ZOITECHAT_EAT_ZOITECHAT) && (ret & ZOITECHAT_EAT_PLUGIN))
|
if ((ret & ZOITECHAT_EAT_ZOITECHAT) && (ret & ZOITECHAT_EAT_PLUGIN))
|
||||||
{
|
{
|
||||||
eat = 1;
|
eat = 1;
|
||||||
@@ -744,15 +725,6 @@ plugin_emit_print (session *sess, char *word[], time_t server_time)
|
|||||||
HOOK_PRINT | HOOK_PRINT_ATTRS);
|
HOOK_PRINT | HOOK_PRINT_ATTRS);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
|
||||||
plugin_emit_print_after (session *sess, char *word[], time_t server_time)
|
|
||||||
{
|
|
||||||
zoitechat_event_attrs attrs;
|
|
||||||
attrs.server_time_utc = server_time;
|
|
||||||
if (is_session (sess))
|
|
||||||
plugin_hook_run (sess, word[0], word, NULL, &attrs, HOOK_PRINT_AFTER);
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
int
|
||||||
plugin_emit_dummy_print (session *sess, char *name)
|
plugin_emit_dummy_print (session *sess, char *name)
|
||||||
{
|
{
|
||||||
@@ -1056,15 +1028,6 @@ zoitechat_unhook (zoitechat_plugin *ph, zoitechat_hook *hook)
|
|||||||
return hook->userdata;
|
return hook->userdata;
|
||||||
}
|
}
|
||||||
|
|
||||||
zoitechat_hook *
|
|
||||||
zoitechat_hook_print_after (zoitechat_plugin *ph, const char *name, int pri,
|
|
||||||
int flags, zoitechat_print_attrs_cb *callback, void *userdata)
|
|
||||||
{
|
|
||||||
if (flags != 0 || !name || !callback)
|
|
||||||
return NULL;
|
|
||||||
return plugin_add_hook (ph, HOOK_PRINT_AFTER, pri, name, NULL, callback, 0, userdata);
|
|
||||||
}
|
|
||||||
|
|
||||||
zoitechat_hook *
|
zoitechat_hook *
|
||||||
zoitechat_hook_command (zoitechat_plugin *ph, const char *name, int pri,
|
zoitechat_hook_command (zoitechat_plugin *ph, const char *name, int pri,
|
||||||
zoitechat_cmd_cb *callb, const char *help_text, void *userdata)
|
zoitechat_cmd_cb *callb, const char *help_text, void *userdata)
|
||||||
@@ -1224,9 +1187,6 @@ zoitechat_get_info (zoitechat_plugin *ph, const char *id)
|
|||||||
return text_find_format_string (e);
|
return text_find_format_string (e);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strcmp (id, "api_hook_print_after") == 0)
|
|
||||||
return "1";
|
|
||||||
|
|
||||||
hash = str_hash (id);
|
hash = str_hash (id);
|
||||||
/* do the session independant ones first */
|
/* do the session independant ones first */
|
||||||
switch (hash)
|
switch (hash)
|
||||||
|
|||||||
@@ -183,7 +183,6 @@ int plugin_emit_command (session *sess, char *name, char *word[], char *word_eol
|
|||||||
int plugin_emit_server (session *sess, char *name, char *word[], char *word_eol[],
|
int plugin_emit_server (session *sess, char *name, char *word[], char *word_eol[],
|
||||||
time_t server_time);
|
time_t server_time);
|
||||||
int plugin_emit_print (session *sess, char *word[], time_t server_time);
|
int plugin_emit_print (session *sess, char *word[], time_t server_time);
|
||||||
void plugin_emit_print_after (session *sess, char *word[], time_t server_time);
|
|
||||||
int plugin_emit_dummy_print (session *sess, char *name);
|
int plugin_emit_dummy_print (session *sess, char *name);
|
||||||
int plugin_emit_keypress (session *sess, unsigned int state, unsigned int keyval, gunichar key);
|
int plugin_emit_keypress (session *sess, unsigned int state, unsigned int keyval, gunichar key);
|
||||||
GList* plugin_command_list(GList *tmp_list);
|
GList* plugin_command_list(GList *tmp_list);
|
||||||
|
|||||||
@@ -2320,7 +2320,6 @@ text_emit_with_message (int index, session *sess, char *a, char *b, char *c, cha
|
|||||||
if (!prefs.hex_away_omit_alerts || !sess->server->is_away)
|
if (!prefs.hex_away_omit_alerts || !sess->server->is_away)
|
||||||
sound_play_event (index);
|
sound_play_event (index);
|
||||||
display_event (sess, index, word, stripcolor_args, timestamp, message);
|
display_event (sess, index, word, stripcolor_args, timestamp, message);
|
||||||
plugin_emit_print_after (sess, word, timestamp);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char *
|
char *
|
||||||
|
|||||||
@@ -206,12 +206,6 @@ struct _zoitechat_plugin
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/* api_hook_print_after=1: observer returns are ignored; flags must be zero. */
|
|
||||||
zoitechat_hook *zoitechat_hook_print_after (zoitechat_plugin *ph,
|
|
||||||
const char *name, int pri, int flags,
|
|
||||||
int (*callback) (char *word[], zoitechat_event_attrs *attrs, void *userdata),
|
|
||||||
void *userdata);
|
|
||||||
|
|
||||||
zoitechat_hook *
|
zoitechat_hook *
|
||||||
zoitechat_hook_command (zoitechat_plugin *ph,
|
zoitechat_hook_command (zoitechat_plugin *ph,
|
||||||
const char *name,
|
const char *name,
|
||||||
@@ -431,7 +425,6 @@ zoitechat_pluginpref_list (zoitechat_plugin *ph,
|
|||||||
#define zoitechat_hook_server ((ZOITECHAT_PLUGIN_HANDLE)->zoitechat_hook_server)
|
#define zoitechat_hook_server ((ZOITECHAT_PLUGIN_HANDLE)->zoitechat_hook_server)
|
||||||
#define zoitechat_hook_server_attrs ((ZOITECHAT_PLUGIN_HANDLE)->zoitechat_hook_server_attrs)
|
#define zoitechat_hook_server_attrs ((ZOITECHAT_PLUGIN_HANDLE)->zoitechat_hook_server_attrs)
|
||||||
#define zoitechat_hook_print ((ZOITECHAT_PLUGIN_HANDLE)->zoitechat_hook_print)
|
#define zoitechat_hook_print ((ZOITECHAT_PLUGIN_HANDLE)->zoitechat_hook_print)
|
||||||
#define zoitechat_hook_print_after ((ZOITECHAT_PLUGIN_HANDLE)->zoitechat_hook_print_after)
|
|
||||||
#define zoitechat_hook_print_attrs ((ZOITECHAT_PLUGIN_HANDLE)->zoitechat_hook_print_attrs)
|
#define zoitechat_hook_print_attrs ((ZOITECHAT_PLUGIN_HANDLE)->zoitechat_hook_print_attrs)
|
||||||
#define zoitechat_hook_timer ((ZOITECHAT_PLUGIN_HANDLE)->zoitechat_hook_timer)
|
#define zoitechat_hook_timer ((ZOITECHAT_PLUGIN_HANDLE)->zoitechat_hook_timer)
|
||||||
#define zoitechat_hook_fd ((ZOITECHAT_PLUGIN_HANDLE)->zoitechat_hook_fd)
|
#define zoitechat_hook_fd ((ZOITECHAT_PLUGIN_HANDLE)->zoitechat_hook_fd)
|
||||||
|
|||||||
Reference in new issue
Block a user