Emacs Jabber

Some notes about Emacs Jabber.

Main repository: https://codeberg.org/emacs-jabber/emacs-jabber

My repository: https://gitlab.com/cnngimenez/emacs-jabber

1. A chatbot in Emacs Jabber

This script sends a message and disconnects from the server. A cronjob can execute it at specific times.

1.1. Starting script

I made this script to work with Emacs in Termux.

  #! /usr/bin/env -S emacs -x
;;; script.el --- One line description  -*- lexical-binding: t; -*-

;; Copyright 2025 Christian Gimenez
;;
;; Author: Christian Gimenez
;; Maintainer: Christian Gimenez
;; Version: 0.1.0
;; Keywords: comm
;; URL:
;; Package-Requires: ((emacs "27.1"))

;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.


;;; Commentary:

;; 

;;; Code:

;; Add emacs-jabber paths
(add-to-list 'load-path "/data/data/com.termux/files/home/repos/emacs/emacs-jabber/")
(add-to-list 'load-path "/data/data/com.termux/files/home/repos/emacs/emacs-jabber/lisp")
(add-to-list 'load-path "/data/data/com.termux/files/home/repos/emacs/emacs-jabber/lisp/jabber-fallback-lib")
(require 'jabber)

;; Import the processing data functions.
;; The following data is required:
;; * `message-make' function that makes the string message to send.
;;
;; Variables/constants expected: `my-message-botname', `my-message-muc-to',
;; `my-message-subject', `my-message-test-to'.
;;
;; The variable `my-message-connection-data' is an alist with the following:
;;   '((username . "USERNAME") (server . "SERVER") (resource . "bot")
;;     (password . "ACCOUNT-PASSWORD"))
(add-to-list 'load-path default-directory)
(require 'my-message)

(defun my-send-message (jc)
  "Send a message.
JC is a current and established jabber connection.

Once connected send and message and do chatbot stuff.

Useful as a hook."
  (message "Sending message now.")
  ;; Join to a MUC before sending. Else, the server may answer with error.
  ;; (jabber-muc-join jc my-message-muc-to my-message-botname)

  (let ((message (my-message-make)))
    (sleep-for 2)
    (jabber-send-message jc my-message-test-to my-message-subject message "chat")
    ;; (jabber-send-message jc my-message-muc-to my-message-subject message "groupchat")
    (sleep-for 5))

  (jabber-disconnect-one jc)
  (message "Jabber message sent."))

;; --------------------------------------------------
;; Setup Jabber to only connect and send the message

;; (setq jabber-debug-log-xml "~/xmpp-bot.log") ;; for debugging purpaoses.
(setq jabber-post-connect-hooks nil)  ;; Removing presences, MUC auto-join, etc.
(add-hook 'jabber-post-connect-hooks #'my-send-message)

(message "Starting connection.")
(jabber-connect (alist-get 'username my-message-connection-data)
                (alist-get 'server my-message-connection-data)
                (alist-get 'resource my-message-connection-data)
                nil ;; registerp
                (alist-get 'password my-message-connection-data))
;; Wait for disconnection
(message "Waiting for message and disconnection.")
(while jabber-connections
  ;; Wait until `jabber-connections' is empty. The `jabber-disconnect-one'
  ;; function deletes the jabber connection from the variable.
  (sleep-for 1))

(provide 'script)
;;; script.el ends here

1.2. The script you should edit

  ;;; my-message.el --- Message  -*- lexical-binding: t; -*-

;; Copyright 2025 Christian Gimenez
;;
;; Author: Christian Gimenez
;; Maintainer: Christian Gimenez
;; Version: 0.1.0
;; Keywords: comm
;; URL:
;; Package-Requires: ((emacs "27.1"))

;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.


;;; Commentary:

;; Make the XMPP message.

;;; Code:
(defconst my-message-botname "My bot")
(defconst my-message-muc-to "the-group@muc.server.org")
(defconst my-message-test-to "someone@server.org")
(defconst my-message-subject "Message subject")
(defconst my-message-connection-data '((username . "myusername")
                                (server . "server.org")
                                (resource . "the-bot")
                                (password . "mypassword")))



(defun my-message-make ()
  "Format my message to send."
  "Hello world!")

(provide 'my-message)
;;; my-message.el ends here

2. License

by-sa.png

This work by Christian Gimenez is licensed under Creative Commons Attribution-ShareAlike 4.0 International (CC By-SA 4.0). See more about the license at https://creativecommons.org/licenses/by-sa/4.0/

Reference the original author and original work, even when creating derived works.

Author: Christian Gimenez

Created: 2025-04-12 sáb 19:34

Validate