machinatrix
A very silly Matrix bot written in C
Loading...
Searching...
No Matches
matrix.c File Reference

Program that interacts with the Matrix server. More...

#include <assert.h>
#include <errno.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <getopt.h>
#include <sys/types.h>
#include <unistd.h>
#include <cjson/cJSON.h>
#include "config.h"
#include "utils.h"
Include dependency graph for matrix.c:

Data Structures

struct  config
 machinatrix-specific configuration. More...
 

Macros

#define API_URL   "/_matrix/client/r0"
 Root URL for the Matrix API.
 
#define SYNC_URL   API_URL "/sync"
 URL for the synchronization endpoint.
 
#define ROOM_FILTER   "filter={\"room\":{\"timeline\":{\"limit\":1}}}"
 Limits the number of synchronization events to one.
 
#define ROOMS_URL   API_URL "/rooms"
 URL for the rooms endpoint.
 
#define SEND_URL   "/send/m.room.message"
 URL fragment for the send-message enpoint for a room.
 
#define SYNC_INTERVAL_MS_STR   "30000"
 Polling interval for the synchronization request.
 
#define TIMEOUT_PARAM   "timeout=" SYNC_INTERVAL_MS_STR
 Sets the polling interval to SYNC_INTERVAL_MS_STR.
 
#define URL_PARTS(c, ...)    (c)->server, __VA_ARGS__, "access_token=", (c)->token
 Prepends the server and appends the token from the configuration.
 
#define BUILD_MATRIX_URL(c, url, ...)    BUILD_URL(url, URL_PARTS((c), __VA_ARGS__))
 Builds a URL using URL_PARTS.
 

Enumerations

enum  { FILTER_FLAG = 1 << 0 }
 

Functions

int main (int argc, const char *const *argv)
 Program entry point.
 
static bool parse_args (int *argc, const char *const **argv, struct config *config)
 Parses command-line arguments and fills config.
 
static void usage (FILE *f)
 Prints a usage message.
 
static bool short_username (const char *user, char *out)
 Extracts the short user name from a @user:server string.
 
static bool read_token (struct mtrix_buffer b)
 Reads the token from a file.
 
static void config_destroy (struct config *config)
 Destructs config.
 
static void config_set_args (struct config *config, const char *const *argv)
 Constructs the machinatrix command line with references to argv.
 
static void config_verbose (const struct config *config, const char *fmt,...)
 Logs a message if verbose output was requested.
 
static cJSON * get_item (const cJSON *j, const char *k)
 Shortcut for `cJSON_GetObjectItemCaseSensitive.
 
static bool init_batch (const struct config *config, char *batch)
 Fetches the inital batch for synchronization.
 
static bool get_next_batch (cJSON *j, char *batch)
 Extracts the next batch from a server response.
 
static bool filter (const struct config *config, char *batch)
 Main request/response loop in filter mode.
 
static bool loop (const struct config *config, char *batch)
 Main request/response loop.
 
static cJSON * parse_json (const char *s)
 Parses a string as JSON.
 
static bool handle_request (const struct config *config, cJSON *root, size_t user_len, bool(*send_msg)(const struct config *, const char *, const char *))
 Handles a single request.
 
static bool check_event_type (const cJSON *event, const char *value)
 Checks that an event has the expected type.
 
static const char * event_body (const cJSON *event)
 Finds the event body, if it contains one.
 
static const char * event_sender (const cJSON *event)
 Finds the event sender, if it contains one.
 
static bool check_mention (const char *text, const char *user)
 Checks if the a user was mentioned.
 
static bool process_input (const struct config *config, const char *input, struct mtrix_buffer *output)
 Invokes the main program.
 
static bool print_msg (const struct config *config, const char *room, const char *msg)
 Prints a message to stdout.
 
static bool send_msg (const struct config *config, const char *room, const char *msg)
 Sends a message to the room.
 
static bool read_output (FILE *f, struct mtrix_buffer *buf)
 Consumes all output from f, appending it to buf.
 

Variables

const char * PROG_NAME = NULL
 To be filled by argv[0] later, for logging.
 
const char * CMD_NAME = NULL
 Always null.
 

Detailed Description

Program that interacts with the Matrix server.

Executes the main program to handle the commands.

Macro Definition Documentation

◆ API_URL

#define API_URL   "/_matrix/client/r0"

Root URL for the Matrix API.

◆ BUILD_MATRIX_URL

#define BUILD_MATRIX_URL ( c,
url,
... )    BUILD_URL(url, URL_PARTS((c), __VA_ARGS__))

Builds a URL using URL_PARTS.

◆ ROOM_FILTER

#define ROOM_FILTER   "filter={\"room\":{\"timeline\":{\"limit\":1}}}"

Limits the number of synchronization events to one.

◆ ROOMS_URL

#define ROOMS_URL   API_URL "/rooms"

URL for the rooms endpoint.

◆ SEND_URL

#define SEND_URL   "/send/m.room.message"

URL fragment for the send-message enpoint for a room.

◆ SYNC_INTERVAL_MS_STR

#define SYNC_INTERVAL_MS_STR   "30000"

Polling interval for the synchronization request.

◆ SYNC_URL

#define SYNC_URL   API_URL "/sync"

URL for the synchronization endpoint.

◆ TIMEOUT_PARAM

#define TIMEOUT_PARAM   "timeout=" SYNC_INTERVAL_MS_STR

Sets the polling interval to SYNC_INTERVAL_MS_STR.

◆ URL_PARTS

#define URL_PARTS ( c,
... )    (c)->server, __VA_ARGS__, "access_token=", (c)->token

Prepends the server and appends the token from the configuration.

Parameters
cPointer to a config object.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
FILTER_FLAG 

Filter mode was chosen.

Function Documentation

◆ check_event_type()

bool check_event_type ( const cJSON * event,
const char * value )
static

Checks that an event has the expected type.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_mention()

bool check_mention ( const char * text,
const char * user )
static

Checks if the a user was mentioned.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ config_destroy()

void config_destroy ( struct config * config)
static

Destructs config.

Here is the caller graph for this function:

◆ config_set_args()

void config_set_args ( struct config * config,
const char *const * argv )
static

Constructs the machinatrix command line with references to argv.

Here is the caller graph for this function:

◆ config_verbose()

void config_verbose ( const struct config * config,
const char * fmt,
... )
static

Logs a message if verbose output was requested.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ event_body()

const char * event_body ( const cJSON * event)
static

Finds the event body, if it contains one.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ event_sender()

const char * event_sender ( const cJSON * event)
static

Finds the event sender, if it contains one.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ filter()

bool filter ( const struct config * config,
char * batch )
static

Main request/response loop in filter mode.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_item()

cJSON * get_item ( const cJSON * j,
const char * k )
inlinestatic

Shortcut for `cJSON_GetObjectItemCaseSensitive.

Here is the caller graph for this function:

◆ get_next_batch()

bool get_next_batch ( cJSON * j,
char * batch )
static

Extracts the next batch from a server response.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ handle_request()

bool handle_request ( const struct config * config,
cJSON * root,
size_t user_len,
bool(* send_msg )(const struct config *, const char *, const char *) )
static

Handles a single request.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ init_batch()

bool init_batch ( const struct config * config,
char * batch )
static

Fetches the inital batch for synchronization.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ loop()

bool loop ( const struct config * config,
char * batch )
static

Main request/response loop.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( int argc,
const char *const * argv )

Program entry point.

Here is the call graph for this function:

◆ parse_args()

bool parse_args ( int * argc,
const char *const ** argv,
struct config * config )
static

Parses command-line arguments and fills config.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_json()

cJSON * parse_json ( const char * s)
static

Parses a string as JSON.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ print_msg()

bool print_msg ( const struct config * config,
const char * room,
const char * msg )
static

Prints a message to stdout.

Here is the caller graph for this function:

◆ process_input()

bool process_input ( const struct config * config,
const char * input,
struct mtrix_buffer * output )
static

Invokes the main program.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_output()

bool read_output ( FILE * f,
struct mtrix_buffer * buf )
static

Consumes all output from f, appending it to buf.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_token()

bool read_token ( struct mtrix_buffer b)
static

Reads the token from a file.

Parameters
bBuffer with the name of the file and maximum token length (including the null terminator). Replaced with the contents of the token.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ send_msg()

bool send_msg ( const struct config * config,
const char * room,
const char * msg )
static

Sends a message to the room.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ short_username()

bool short_username ( const char * user,
char * out )
static

Extracts the short user name from a @user:server string.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ usage()

void usage ( FILE * f)
static

Prints a usage message.

Here is the caller graph for this function:

Variable Documentation

◆ CMD_NAME

const char* CMD_NAME = NULL

Always null.

Command name, used as a second prefix for log messages if non-NULL.

◆ PROG_NAME

const char* PROG_NAME = NULL

To be filled by argv[0] later, for logging.

Program name, used as a prefix for log messages if non-NULL.