Skip to content

Commit

Permalink
Add support for queries, add subcommand history
Browse files Browse the repository at this point in the history
  • Loading branch information
jajm committed Dec 6, 2021
1 parent 1077ede commit a874e5c
Show file tree
Hide file tree
Showing 15 changed files with 642 additions and 241 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Added support for queries for direct message between two users
- Added command `/rocketchat history`

### Changed

- History is no longer loaded automatically (use command `/rocketchat history`)

## [0.2.0] - 2020-10-29

### Added
Expand Down
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ target_include_directories(rocketchat_core PRIVATE
${LIBWEBSOCKETS_INCLUDE_DIRS}
${JANSSON_INCLUDE_DIRS})
target_compile_definitions(rocketchat_core PRIVATE -DUOFF_T_LONG)
target_compile_options(rocketchat_core PRIVATE -Wall -Werror)
target_compile_options(rocketchat_core PRIVATE -Wall -Werror -Wno-error=deprecated-declarations)
target_link_directories(rocketchat_core PRIVATE
${GLIB_LIBRARY_DIRS}
${LIBWEBSOCKETS_LIBRARY_DIRS}
Expand All @@ -43,7 +43,7 @@ target_include_directories(fe_rocketchat PRIVATE
${LIBWEBSOCKETS_INCLUDE_DIRS}
${JANSSON_INCLUDE_DIRS})
target_compile_definitions(fe_rocketchat PRIVATE -DUOFF_T_LONG)
target_compile_options(fe_rocketchat PRIVATE -Wall -Werror)
target_compile_options(fe_rocketchat PRIVATE -Wall -Werror -Wno-error=deprecated-declarations)
target_link_directories(fe_rocketchat PRIVATE
${GLIB_LIBRARY_DIRS}
${LIBWEBSOCKETS_LIBRARY_DIRS}
Expand Down
39 changes: 30 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,12 @@ waiting 2 seconds to autocomplete a nick
* list users
* join an existing room (public/private channel, discussion, direct message).
leaving a room is not implemented
* create direct message using `/query <nick>`
* receive/send messages from/to rooms
* automatic join to rooms when a message is received
* room history: the last 10 messages are loaded when joining a room
* room history: the last 10 messages can be loaded using `/rocketchat history`
* nicklist

## TODO

* Use queries when possible. For now every room is a channel, even direct message between two users.
* Allow to use the name of the room instead of its ID when joining

## Installation

### Requirements
Expand Down Expand Up @@ -115,17 +111,42 @@ general (ID: GENERAL)
test (ID: 497BsckNzoaT2PeMF)
```

and join one or more using their ID
and join one or more using their ID or their name

```
/join aQhQEXsMuoS439dGS,GENERAL,497BsckNzoaT2PeMF
/join aQhQEXsMuoS439dGS,GENERAL,test
```

To automatically join a room when connected to the server you can add it to
your channels list

```
/channel add -auto 497BsckNzoaT2PeMF MyRocketChat
/channel add -auto test MyRocketChat
```

To start a private discussion with someone, use the `/query` command

```
/query john
```

## Commands

### `/rocketchat channels`

List public channels

### `/rocketchat users`

List users

### `/rocketchat history`

Print the last 10 lines of history for the current channel/query

## Known bugs

* `/query <nick> <message>` only works when the query already exists. If the
query does not exist yet the message will not be sent.

[Rocket.Chat]: https://rocket.chat/
3 changes: 3 additions & 0 deletions src/core/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
#include "common.h"
#include "rocketchat.h"

typedef struct {
char *rid;
} MODULE_QUERY_REC;
12 changes: 12 additions & 0 deletions src/core/rocketchat-channels.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef __ROCKETCHAT_CHANNELS_H
#define __ROCKETCHAT_CHANNELS_H

#include "channels.h"

#define ROCKETCHAT_CHANNEL(channel) \
PROTO_CHECK_CAST(CHANNEL(channel), CHANNEL_REC, chat_type, "rocketchat")

#define IS_ROCKETCHAT_CHANNEL(channel) \
(ROCKETCHAT_CHANNEL(channel) ? TRUE : FALSE)

#endif
13 changes: 13 additions & 0 deletions src/core/rocketchat-queries.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "module.h"
#include "modules.h"
#include "rocketchat-queries.h"

const char *
rocketchat_query_get_rid(ROCKETCHAT_QUERY_REC *query)
{
MODULE_QUERY_REC * module_query;

module_query = MODULE_DATA(query);

return module_query->rid;
}
9 changes: 9 additions & 0 deletions src/core/rocketchat-queries.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
#ifndef __ROCKETCHAT_QUERIES_H
#define __ROCKETCHAT_QUERIES_H

#include "queries.h"
#include "rocketchat-servers.h"

#define ROCKETCHAT_QUERY(query) \
PROTO_CHECK_CAST(QUERY(query), ROCKETCHAT_QUERY_REC, chat_type, "rocketchat")

#define IS_ROCKETCHAT_QUERY(query) \
(ROCKETCHAT_QUERY(query) ? TRUE : FALSE)

#define STRUCT_SERVER_REC ROCKETCHAT_SERVER_REC
struct _ROCKETCHAT_QUERY_REC {
#include "query-rec.h"
};

const char * rocketchat_query_get_rid(ROCKETCHAT_QUERY_REC *query);

#endif
48 changes: 48 additions & 0 deletions src/core/rocketchat-room.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (C) 2020 Julian Maurice
*
* 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 2 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "module.h"
#include "rocketchat.h"
#include "rocketchat-room.h"

ROCKETCHAT_ROOM_REC *rocketchat_room_new(const char *id, char type, const char *name, const char *fname)
{
ROCKETCHAT_ROOM_REC *room;

room = g_new0(ROCKETCHAT_ROOM_REC, 1);
room->id = g_strdup(id);
room->type = type;
if (name) {
room->name = g_strdup(name);
}
if (fname) {
room->fname = g_strdup(fname);
}

return room;
}

void rocketchat_room_free(ROCKETCHAT_ROOM_REC *room)
{
if (room) {
g_free(room->id);
g_free(room->name);
g_free(room->fname);
g_free(room);
}
}
16 changes: 16 additions & 0 deletions src/core/rocketchat-room.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef __ROCKETCHAT_ROOM_H
#define __ROCKETCHAT_ROOM_H

#include "rocketchat.h"

struct _ROCKETCHAT_ROOM_REC {
char *id;
char type;
char *name;
char *fname;
};

ROCKETCHAT_ROOM_REC *rocketchat_room_new(const char *id, char type, const char *name, const char *fname);
void rocketchat_room_free(ROCKETCHAT_ROOM_REC *room);

#endif
5 changes: 5 additions & 0 deletions src/core/rocketchat-servers.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "levels.h"
#include "rocketchat-servers.h"
#include "rocketchat-result-callbacks.h"
#include "rocketchat-room.h"
#include "jansson.h"
#include "libwebsockets.h"

Expand All @@ -37,6 +38,7 @@ static void sig_server_connected(ROCKETCHAT_SERVER_REC *server)
server->buffer = g_string_new(NULL);
server->result_callbacks = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, (GDestroyNotify)rocketchat_result_callback_free);
server->sent_messages = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
server->rooms = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, (GDestroyNotify)rocketchat_room_free);

support = json_array();
json_array_append_new(support, json_string("1"));
Expand Down Expand Up @@ -78,6 +80,9 @@ static void sig_server_destroyed(ROCKETCHAT_SERVER_REC *server)
g_hash_table_destroy(server->sent_messages);
server->sent_messages = NULL;

g_hash_table_destroy(server->rooms);
server->rooms = NULL;

lws_set_opaque_user_data(server->wsi, NULL);

g_free(server->userId);
Expand Down
1 change: 1 addition & 0 deletions src/core/rocketchat-servers.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ struct _ROCKETCHAT_SERVER_REC {
GString *buffer;
GHashTable *result_callbacks;
GHashTable *sent_messages;
GHashTable *rooms;
char *userId;
};

Expand Down
1 change: 1 addition & 0 deletions src/core/rocketchat.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
typedef struct _ROCKETCHAT_SERVER_REC ROCKETCHAT_SERVER_REC;
typedef struct _ROCKETCHAT_QUERY_REC ROCKETCHAT_QUERY_REC;
typedef struct _ROCKETCHAT_RESULT_CALLBACK_REC ROCKETCHAT_RESULT_CALLBACK_REC;
typedef struct _ROCKETCHAT_ROOM_REC ROCKETCHAT_ROOM_REC;

#define ROCKETCHAT_PROTOCOL_NAME "rocketchat"
#define ROCKETCHAT_PROTOCOL (chat_protocol_lookup(ROCKETCHAT_PROTOCOL_NAME))
Expand Down
Loading

0 comments on commit a874e5c

Please sign in to comment.