aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Smeding <tom.smeding@gmail.com>2020-07-07 21:21:46 +0200
committerTom Smeding <tom.smeding@gmail.com>2020-07-07 21:21:46 +0200
commit34d7ffdf32a59019fc750bb3f365cb7b5f229fc6 (patch)
treeb25b9d9e27902b4bfd2a1a15e5835c2560767b0c
parentab3cb2e9c40c1c8623fb419743f32cb388610f15 (diff)
weechat: Update to latest protocol
-rw-r--r--weechat/net.c23
-rw-r--r--weechat/net.h22
-rw-r--r--weechat/tomsg.c26
3 files changed, 53 insertions, 18 deletions
diff --git a/weechat/net.c b/weechat/net.c
index bab7f3a..80d417b 100644
--- a/weechat/net.c
+++ b/weechat/net.c
@@ -222,8 +222,9 @@ void net_handle_recv(int fd,const char *msg){
}
const char *roomp=p+1;
p=strchr(roomp,' ');
- const char *q,*r;
- if(p==NULL||(q=strchr(p+1,' '))==NULL||(r=strchr(q+1,' '))==NULL){
+ const char *q,*r,*s;
+ if(p==NULL||(q=strchr(p+1,' '))==NULL||(r=strchr(q+1,' '))==NULL
+ ||(s=strchr(r+1,' '))==NULL){
fprintf(debugf,"net_handle_recv: not enough arguments to 'message' <%s>\n",msg);
return;
}
@@ -232,9 +233,13 @@ void net_handle_recv(int fd,const char *msg){
i64 usernamelen=q-usernamep;
const char *stampp=q+1;
i64 stamplen=r-stampp;
- const char *textp=r+1;
+ const char *msgidp=r+1;
+ i64 msgidlen=s-msgidp;
+ const char *textp=s+1;
i64 textlen=msglen-(textp-msg);
+ (void)msgidp; (void)msgidlen;
+
struct net_response res;
res.type=NET_MESSAGE;
const char *endp;
@@ -337,19 +342,25 @@ void net_handle_recv(int fd,const char *msg){
free(res.room);
free(res.username);
} else if(cmdlen==6&&memcmp(cmd,"invite",6)==0){
- if(*p=='\0'){
+ const char *q;
+ if(*p=='\0'||(q=strchr(p+1,' '))==NULL){
fprintf(debugf,"net_handle_recv: no arguments to 'invite' <%s>\n",msg);
return;
}
const char *roomp=p+1;
- i64 roomlen=msglen-(roomp-msg);
+ i64 roomlen=q-roomp;
+ const char *userp=q+1;
+ i64 userlen=msglen-(userp-msg);
struct net_response res;
res.type=NET_INVITE;
res.room=malloc(roomlen+1);
- assert(res.room);
+ res.username=malloc(userlen+1);
+ assert(res.room&&res.username);
memcpy(res.room,roomp,roomlen);
res.room[roomlen]='\0';
+ memcpy(res.username,userp,userlen);
+ res.username[userlen]='\0';
cb(fd,res,payload);
free(res.room);
} else if(cmdlen==6&&memcmp(cmd,"online",6)==0){
diff --git a/weechat/net.h b/weechat/net.h
index 6348a89..7f4e9da 100644
--- a/weechat/net.h
+++ b/weechat/net.h
@@ -5,17 +5,17 @@
enum net_response_type{
- NET_OK,
- NET_NUMBER,
- NET_ERROR,
- NET_NAME,
- NET_LIST,
- NET_PONG,
- NET_MESSAGE,
- NET_HISTORY,
- NET_JOIN,
- NET_INVITE,
- NET_ONLINE,
+ NET_OK, // -
+ NET_NUMBER, // number
+ NET_ERROR, // error
+ NET_NAME, // name
+ NET_LIST, // nitems, items
+ NET_PONG, // -
+ NET_MESSAGE, // room, username, timestamp, message
+ NET_HISTORY, // room, username, timestamp, message
+ NET_JOIN, // room, username
+ NET_INVITE, // room, username
+ NET_ONLINE, // online.username, online.num
};
struct net_response{
diff --git a/weechat/tomsg.c b/weechat/tomsg.c
index 90e43b0..5a71afa 100644
--- a/weechat/tomsg.c
+++ b/weechat/tomsg.c
@@ -34,6 +34,8 @@ struct conndata{
struct t_gui_buffer *buffer;
+ bool negotiation_complete;
+
int nrooms,roomscap;
struct roomdata **rooms;
@@ -77,7 +79,7 @@ static void message_net_callback(int fd,struct net_response res,void *payload){
assert(conn);
if(res.type==NET_ERROR){
weechat_printf(conn->buffer,"tomsg: send threw error: %s",res.error);
- } else if(res.type!=NET_OK){
+ } else if(res.type!=NET_NUMBER){
fprintf(debugf,"message_net_callback: res.type=%d\n",res.type);
}
}
@@ -379,6 +381,11 @@ static int conn_input_cb(const void *conn_vp,void *_d,struct t_gui_buffer *buffe
(void)_d;
struct conndata *conn=(struct conndata*)conn_vp;
fprintf(debugf,"conn_input_cb(conn=%p,buffer=%p,input=\"%s\")\n",conn,buffer,input);
+
+ if(!conn->negotiation_complete){
+ weechat_printf(conn->buffer,"Server protocol version not yet negotiated, please wait...");
+ return WEECHAT_RC_OK;
+ }
char *input2=strdup(input);
assert(input2);
@@ -457,6 +464,20 @@ static int conn_close_cb(const void *conn_vp,void *_d,struct t_gui_buffer *buffe
return WEECHAT_RC_OK;
}
+static void version_net_callback(int fd,struct net_response res,void *payload){
+ (void)payload;
+ fprintf(debugf,"version_net_callback(fd=%d,res={.type=%d})\n",fd,res.type);
+ struct conndata *conn=weechat_hashtable_get(conntable,&fd);
+ assert(conn);
+ if(res.type==NET_OK){
+ conn->negotiation_complete=true;
+ weechat_printf(conn->buffer,"Version negotiation complete.");
+ } else {
+ conn_destroy(conn);
+ weechat_printf(NULL,"tomsg: Server has incompatible protocol version (we want 1)!");
+ }
+}
+
static int connect_cb(const void *_p,void *hostname,int status,int _g,int fd,const char *err,const char *_i){
(void)_p; (void)_g; (void)_i;
switch(status){
@@ -466,6 +487,7 @@ static int connect_cb(const void *_p,void *hostname,int status,int _g,int fd,con
conn->fd=fd;
conn->fd_hook=weechat_hook_fd(fd,1,0,0,fd_hook_callback,conn,NULL);
conn->buffer=weechat_buffer_new((char*)hostname,conn_input_cb,conn,NULL,conn_close_cb,conn,NULL);
+ conn->negotiation_complete=false;
conn->nrooms=0;
conn->roomscap=2;
conn->rooms=malloc(conn->roomscap*sizeof(struct roomdata));
@@ -481,6 +503,8 @@ static int connect_cb(const void *_p,void *hostname,int status,int _g,int fd,con
weechat_hashtable_set(conntable,&fd,conn);
+ net_sendf(fd,version_net_callback,NULL,"version 1");
+
return WEECHAT_RC_OK;
}