16 Administrating wildduck via command line
Matt Simerson edited this page 2024-03-30 13:39:51 -07:00

REST api

We can administrate wilduck via the REST api. In the examples, we craft http queries and send them via curl.

You can save these commands to ~/.bashrc file (which is executed if you are comming through ssh, the ~/.profile file is for interactive login). Not as aliases (because alias can not have arguments), but you can save them as bash functions, which behave exactly like aliases but can have arguments.

Saving functions to ~/.bashrc file

Here is an example:

wduck-get-user() {
  echo "Geeting info about user with id: $1"
  curl -i http://localhost:8080/users/$1

}

Crash course about bash functions:

You specify the function as functionname() { ... }, there is no need to specify the arguments. You can call functions with 0 or more arguments:

$ functionname
$ functionname myargument1
$ functionname arg1 arg2

Functions saved in ~/.bashrc can be called any time, just as with an alias defined there.

Source from ~/.bashrc

It is better to have a separate file for wildduck related commands, and source it in ~/.bashrc.

Create a file named ~/.wildduck.commands, and source it by adding this to the end of ~/.bashrc:

# include .wildduck.commands if it exists
if [ -f $HOME/.wildduck.commands ]; then
    . $HOME/.wildduck.commands
    echo ".wildduck.commands file has been sourced"
fi

Please note . file is the same as source file, but dot syntax is POSIX compatible, while source is a bash builtin (and some other shells too). Bash makes no distinction between dot and source.

List of commands

The examples below take commands from the wildduck API. Refer to the official api for the latest syntax.

List all users

curl -i http://localhost:8080/users

Function snippet to be saved in :~/.wildduck.commands`:

wduck-users() {
  echo "List all users"
  curl -i http://localhost:8080/users
}

Query user information

  curl -i http://localhost:8080/users/$USERID

Function snippet to be saved in :~/.wildduck.commands`:

wduck-user() {
  USERID=$1
  echo "Querying user $USERID"
  curl -i http://localhost:8080/users/$USERID
}

Searching messages in the whole database (chinese char)

Some mongodb foo:

mongo
> use wildduck
> db.
messages.
find({'headers.value': /[姚轉]/ }).
toArray().
map(doc => '/users/' + doc.user.str + '/mailboxes/' + doc.mailbox.str + '/messages/' + doc.uid )

Example output:
[
  "/users/5b1xxx8dc5/mailboxes/5b1xxxdc6/messages/14343",
  "/users/5b1xxx8dc5/mailboxes/5b1xxxdc6/messages/10837",
]

Where doc.user is the owner of the mailbox, doc.mailbox is the mailbox where the message located (inbox, sent, draft, etc), and doc.uid is a number, what wildduck uses for message id. It is independent from mongodb builtin doc._id.

Function snippet to be saved in :~/.wildduck.commands`:

wduck-message-delete() {
  URL=$1
  echo "Delete(format: /users/USERID/mailboxes/MAILBOXID/messages/UID) message: $URL"
  curl -i -XDELETE http://localhost:8080$URL
}

Use it like:

wduck-message-delete /users/5b1xxx8dc5/mailboxes/5b1xxxdc6/messages/10837

where xxx are real chars.

Or in a shell script:

#! /bin/bash

source ~/.wildduck.commands

declare -a arr=(
"/users/5b1xxxc5/mailboxes/5b1xxxc6/messages/14343"
"/users/5b3xxx54/mailboxes/5b3xxx55/messages/10837"
)
for i in "${arr[@]}"
do
  wduck-message-delete $i
done