summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordefanor <defanor@uberspace.net>2021-09-04 13:13:09 +0300
committerdefanor <defanor@uberspace.net>2021-09-04 13:13:09 +0300
commitf5942754fc0658d364ce4c38f0ecb91c91c2912a (patch)
tree91f25e06be66ebfeaae39dbbeb225fce81ec275d
parent0d5408caf8f1586b37b273ed4b1f38b24c411846 (diff)
Print names and shorter timestamps in xmpp.el
The conversation buffers now follow the <https://docs.modernxmpp.org/client/design/#names> recommendations for names, and the rexmpp_get_name function is adjusted to match those more closely.
-rw-r--r--emacs/xml_interface.c13
-rw-r--r--emacs/xmpp.el46
-rw-r--r--src/rexmpp.c4
3 files changed, 44 insertions, 19 deletions
diff --git a/emacs/xml_interface.c b/emacs/xml_interface.c
index 2541281..92e8c5f 100644
--- a/emacs/xml_interface.c
+++ b/emacs/xml_interface.c
@@ -140,6 +140,17 @@ void req_process (rexmpp_t *s,
xmlNodeAddContent(rep, payload_str);
free(payload_str);
}
+ if (rexmpp_xml_match(child, NULL, "get-name")) {
+ char *jid = xmlNodeGetContent(child);
+ if (jid != NULL) {
+ char *name = rexmpp_get_name(s, jid);
+ if (name != NULL) {
+ xmlNodeAddContent(rep, name);
+ free(name);
+ }
+ free(jid);
+ }
+ }
print_xml(rep);
xmlFreeNode(rep);
return;
@@ -234,7 +245,7 @@ int my_xml_out_cb (rexmpp_t *s, xmlNodePtr node) {
}
-main (int argc, char **argv) {
+int main (int argc, char **argv) {
/* The minimal initialisation: provide an allocated rexmpp_t
structure and an initial jid. */
diff --git a/emacs/xmpp.el b/emacs/xmpp.el
index e04cfe5..2eaeb38 100644
--- a/emacs/xmpp.el
+++ b/emacs/xmpp.el
@@ -41,6 +41,9 @@
(defvar xmpp-command "rexmpp_xml_interface"
"A command to run an XMPP client subprocess.")
+(defvar xmpp-timestamp-format "%H:%M"
+ "Time string format to use in query buffers.")
+
(defvar xmpp-proc nil
"XMPP process buffer. This should be defined for all the
XMPP-related buffers.")
@@ -142,22 +145,26 @@
proc xml
(lambda (message-body)
(let ((message-from (xml-get-attribute-or-nil xml 'from)))
- (pcase (xml-get-attribute-or-nil xml 'type)
- ("chat"
- (when message-body
- (with-current-buffer (xmpp-query message-from proc)
- (xmpp-insert
- (concat (format-time-string "%FT%T%z")
- " < " (car (xml-node-children message-body)) "\n"))
- (xmpp-message-notify))))
- ("groupchat"
- (when message-body
- (with-current-buffer (xmpp-muc-buffer message-from proc)
- (xmpp-insert
- (concat (format-time-string "%FT%T%z") ", "
- (xmpp-jid-resource message-from) ": "
- (car (xml-node-children message-body)) "\n"))
- (xmpp-message-notify))))))))))
+ (xmpp-with-name
+ message-from
+ (lambda (message-from-name)
+ (pcase (xml-get-attribute-or-nil xml 'type)
+ ("chat"
+ (when message-body
+ (with-current-buffer (xmpp-query message-from proc)
+ (xmpp-insert
+ (concat (format-time-string xmpp-timestamp-format) ", "
+ message-from-name ": "
+ (car (xml-node-children message-body)) "\n"))
+ (xmpp-message-notify))))
+ ("groupchat"
+ (when message-body
+ (with-current-buffer (xmpp-muc-buffer message-from proc)
+ (xmpp-insert
+ (concat (format-time-string xmpp-timestamp-format) ", "
+ (xmpp-jid-resource message-from) ": "
+ (car (xml-node-children message-body)) "\n"))
+ (xmpp-message-notify))))))))))))
(defun xmpp-set-from (proc xml)
(let* ((name (xml-node-name xml))
@@ -192,8 +199,8 @@
(with-current-buffer buf
(xmpp-insert
(concat
- (format-time-string "%FT%T%z")
- " > " (car (xml-node-children message-body)) "\n")))))))
+ (format-time-string xmpp-timestamp-format)
+ ", me: " (car (xml-node-children message-body)) "\n")))))))
("groupchat" nil)))))))
(defun xmpp-process (proc xml)
@@ -242,6 +249,9 @@
(xmpp-proc-write `((request nil ,req)) cur-proc))
(push (cons req cb) xmpp-request-queue))))
+(defun xmpp-with-name (jid cb &optional proc)
+ (xmpp-request `(get-name nil ,jid) cb proc))
+
(defun xmpp-stop (&optional proc)
(interactive)
(xmpp-request '(stop) nil proc))
diff --git a/src/rexmpp.c b/src/rexmpp.c
index d8a67c8..70ed886 100644
--- a/src/rexmpp.c
+++ b/src/rexmpp.c
@@ -250,6 +250,7 @@ xmlNodePtr rexmpp_find_event (rexmpp_t *s,
return NULL;
}
+/* https://docs.modernxmpp.org/client/design/#names */
char *rexmpp_get_name (rexmpp_t *s, const char *jid_str) {
struct rexmpp_jid jid;
if (rexmpp_jid_parse(jid_str, &jid) != 0) {
@@ -291,6 +292,9 @@ char *rexmpp_get_name (rexmpp_t *s, const char *jid_str) {
}
}
}
+ if (jid.local[0] != '\0') {
+ return strdup(jid.local);
+ }
return strdup(jid.bare);
}