aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--weechat/tomsg.c71
1 files changed, 42 insertions, 29 deletions
diff --git a/weechat/tomsg.c b/weechat/tomsg.c
index 0895428..03f0f5e 100644
--- a/weechat/tomsg.c
+++ b/weechat/tomsg.c
@@ -272,14 +272,37 @@ static void close_room(struct roomdata *room){
free(room);
}
-struct pending_ids {
+struct room_and_msgid {
+ struct roomdata *room;
+ int64_t msgid;
+};
+
+static void reply_get_message_net_callback(int fd, struct net_response res, void *payload) {
+ (void)fd;
+ struct room_and_msgid *data = (struct room_and_msgid*)payload;
+
+ debugf("Got reply from get_message for msgid=%" PRIi64 "\n", res.msgid);
+
+ const char *green = weechat_color("green");
+
+ size_t prefixlen = strlen(green) + 3 + strlen(res.username) + 2;
+ char *buffer = malloc(prefixlen + strlen(res.message) + 1);
+ sprintf(buffer, "%s> <%s> %s", weechat_color("green"), res.username, res.message);
+ edit_reply_message(data->room, data->msgid, buffer);
+ free(buffer);
+
+ free(data);
+}
+
+struct sent_message_data {
struct roomdata *room;
int num; // must be <=2
i64 ids[2];
+ i64 replyid; // -1 if not a reply
};
static void message_net_callback(int fd,struct net_response res,void *payload){
- struct pending_ids *pending_ids = (struct pending_ids*)payload;
+ struct sent_message_data *sent_message_data = (struct sent_message_data*)payload;
debugf("message_net_callback(fd=%d,res={.type=%d})\n", fd, res.type);
if (res.type == NET_ERROR) {
@@ -287,12 +310,22 @@ static void message_net_callback(int fd,struct net_response res,void *payload){
assert(conn);
weechat_printf(conn->buffer, "tomsg: send threw error: %s", res.error);
} else if (res.type == NET_NUMBER) {
- for (int i = 0; i < pending_ids->num; i++) {
- edit_message_msgid(pending_ids->room, -pending_ids->ids[i], res.number);
+ for (int i = 0; i < sent_message_data->num; i++) {
+ edit_message_msgid(sent_message_data->room, -sent_message_data->ids[i], res.number);
+ }
+
+ if (sent_message_data->replyid != -1) {
+ struct room_and_msgid *get_payload = malloc(sizeof(struct room_and_msgid));
+ assert(get_payload);
+ get_payload->room = sent_message_data->room;
+ get_payload->msgid = res.number;
+ net_sendf(fd, reply_get_message_net_callback, get_payload, "get_message %" PRIi64, sent_message_data->replyid);
}
} else {
- debugf("message_net_callback: res.type=%d\n",res.type);
+ debugf("message_net_callback: res.type=%d\n", res.type);
}
+
+ free(sent_message_data);
}
static int room_input_cb(const void *room_vp,void *_d,struct t_gui_buffer *buffer,const char *input){
@@ -312,11 +345,12 @@ static int room_input_cb(const void *room_vp,void *_d,struct t_gui_buffer *buffe
tosend=input+skipfirst;
}
- struct pending_ids *payload=malloc(sizeof(struct pending_ids));
+ struct sent_message_data *payload=malloc(sizeof(struct sent_message_data));
assert(payload);
payload->room=room;
payload->num=1;
payload->ids[0]=room->next_pending_id++;
+ payload->replyid=-1;
net_sendf(conn->fd,message_net_callback,payload,"send %s -1 %s",room->name,tosend);
display_message(room,gettimestamp(),conn->username,tosend,-payload->ids[0],false,false);
@@ -410,28 +444,6 @@ static void members_net_callback(int fd,struct net_response res,void *payload){
room_update_attributes(room);
}
-struct room_and_msgid {
- struct roomdata *room;
- int64_t msgid;
-};
-
-static void reply_get_message_net_callback(int fd, struct net_response res, void *payload) {
- (void)fd;
- struct room_and_msgid *data = (struct room_and_msgid*)payload;
-
- debugf("Got reply from get_message for msgid=%" PRIi64 "\n", res.msgid);
-
- const char *green = weechat_color("green");
-
- size_t prefixlen = strlen(green) + 3 + strlen(res.username) + 2;
- char *buffer = malloc(prefixlen + strlen(res.message) + 1);
- sprintf(buffer, "%s> <%s> %s", weechat_color("green"), res.username, res.message);
- edit_reply_message(data->room, data->msgid, buffer);
- free(buffer);
-
- free(data);
-}
-
static void push_net_callback(int fd,struct net_response res,void *payload){
(void)payload;
debugf("push_net_callback(fd=%d,res={.type=%d})\n",fd,res.type);
@@ -841,12 +853,13 @@ static int cmd_tomsg_cb(
const char *message_body=argv_eol[3];
- struct pending_ids *msg_payload=malloc(sizeof(struct pending_ids));
+ struct sent_message_data *msg_payload=malloc(sizeof(struct sent_message_data));
assert(msg_payload);
msg_payload->room=room;
msg_payload->num=2;
msg_payload->ids[0]=room->next_pending_id++;
msg_payload->ids[1]=room->next_pending_id++;
+ msg_payload->replyid=msgid;
int fd=room->conn->fd;
net_sendf(fd,message_net_callback,msg_payload,"send %s %" PRIi64 " %s",room->name,msgid,message_body);