diff options
-rw-r--r-- | weechat/tomsg.c | 90 |
1 files changed, 49 insertions, 41 deletions
diff --git a/weechat/tomsg.c b/weechat/tomsg.c index 676586e..6be99ec 100644 --- a/weechat/tomsg.c +++ b/weechat/tomsg.c @@ -7,7 +7,8 @@ #include <errno.h> #include <assert.h> #include "weechat-plugin.h" -#include "net.h" +#include "../ssh/tomsg_clientlib.h" +#include "global.h" #include "debug.h" WEECHAT_PLUGIN_NAME("tomsg") @@ -18,10 +19,10 @@ WEECHAT_PLUGIN_LICENSE("MIT") WEECHAT_PLUGIN_PRIORITY(1000) -static const char *errpfx,*netpfx; +static const char *errpfx, *netpfx; -struct roomdata{ +struct roomdata { char *name; struct t_gui_buffer *buffer; struct t_gui_nick_group *buffer_nickgroup; @@ -29,7 +30,7 @@ struct roomdata{ struct conndata *conn; // do not free }; -struct conndata{ +struct conndata { int fd; struct t_hook *fd_hook; @@ -37,12 +38,12 @@ struct conndata{ bool negotiation_complete; - int nrooms,roomscap; + int nrooms, roomscap; struct roomdata **rooms; - char *username,*pending_username; + char *username, *pending_username; - i64 linebuf_sz,linebuf_len; + i64 linebuf_sz, linebuf_len; char *linebuf; }; @@ -513,37 +514,47 @@ static int connect_cb(const void *_p,void *hostname,int status,int _g,int fd,con } } -static int cmd_tomsg_cb(const void *_p,void *_d,struct t_gui_buffer *buffer,int argc,char **argv,char **_a){ - (void)_p; (void)_d; (void)_a; - if(argc<2){ - weechat_printf(buffer,"%stomsg: Invalid number of arguments to /tomsg",errpfx); +static int cmd_tomsg_cb(const void *_pointer, void *_data, struct t_gui_buffer *buffer, int argc, char **argv, char **_argv_eol) { + (void)_pointer; (void)_data; (void)_argv_eol; + + if (argc < 2) { + weechat_printf(NULL, "%stomsg: Invalid number of arguments to /tomsg", errpfx); return WEECHAT_RC_ERROR; } - if(strcmp(argv[1],"connect")==0){ - if(argc<3||argc>4){ - weechat_printf(buffer,"%stomsg: Invalid number of arguments to /tomsg connect",errpfx); + + if (strcmp(argv[1], "connect") == 0) { + if (argc < 3 || argc > 4) { + weechat_printf(NULL, "%stomsg: Invalid number of arguments to /tomsg connect", errpfx); + return WEECHAT_RC_ERROR; + } + + const char *hostname = argv[2]; + + if (strlen(hostname) == 0) { + weechat_printf(buffer, "%stomsg: Invalid hostname", errpfx); return WEECHAT_RC_ERROR; } - char *hostname=argv[2]; - char *endp; - int port; - if(argc==4){ - port=strtol(argv[3],&endp,10); - if(argv[3][0]=='\0'||*endp!='\0'||port<=0||port>=65536){ - weechat_printf(buffer,"%sInvalid port number",errpfx); + int port = 2222; + if (argc == 4) { + char *endp; + port = strtol(argv[3], &endp, 10); + if (argv[3][0] == '\0' || *endp != '\0' || port <= 0 || port >= 65536) { + weechat_printf(NULL, "%sInvalid port number", errpfx); return WEECHAT_RC_ERROR; } - } else { - port=29536; } - if(strlen(hostname)==0){ - weechat_printf(buffer,"%stomsg: Invalid hostname",errpfx); - return WEECHAT_RC_ERROR; + + debugf("Connecting to %s:%d\n", hostname, port); + + struct tomsg_async_connect *async; + enum tomsg_retval ret = tomsg_async_connect(hostname, port, &async); + if (ret != TOMSG_OK) { + weechat_printf(NULL, "%stomsg: Cannot start connecting to %s:%d: %s\n", + errpfx, hostname, port, tomsg_strerror(ret)); + return WEECHAT_RC_OK; } - debugf("Connecting to %s:%d\n",hostname,port); - char *hostname_copy=strdup(hostname); weechat_hook_connect( NULL, hostname,port,1,0, @@ -551,21 +562,21 @@ static int cmd_tomsg_cb(const void *_p,void *_d,struct t_gui_buffer *buffer,int NULL, connect_cb,NULL,hostname_copy); } else { - weechat_printf(buffer,"%stomsg: Unknown command \"%s\" to /tomsg",errpfx,argv[1]); + weechat_printf(NULL, "%stomsg: Unknown command \"%s\" to /tomsg", errpfx, argv[1]); return WEECHAT_RC_ERROR; } return WEECHAT_RC_OK; } -int weechat_plugin_init(struct t_weechat_plugin *plugin,int argc,char **argv){ +int weechat_plugin_init(struct t_weechat_plugin *plugin, int argc, char **argv){ (void)argc; (void)argv; weechat_plugin = plugin; debug_init(); - errpfx=weechat_prefix("error"); - netpfx=weechat_prefix("network"); + errpfx = weechat_prefix("error"); + netpfx = weechat_prefix("network"); weechat_hook_command( "tomsg", @@ -573,23 +584,20 @@ int weechat_plugin_init(struct t_weechat_plugin *plugin,int argc,char **argv){ "connect <hostname> [port]", " connect: Connect to a tomsg server", NULL, - cmd_tomsg_cb,NULL,NULL); - - weechat_hook_modifier("input_text_display_with_cursor",password_hide_modifier,NULL,NULL); + cmd_tomsg_cb, NULL, NULL); - net_set_push_callback(push_net_callback); - net_set_history_callback(history_push_net_callback); + weechat_hook_modifier("input_text_display_with_cursor", password_hide_modifier, NULL, NULL); - conntable=weechat_hashtable_new( - 16,WEECHAT_HASHTABLE_INTEGER,WEECHAT_HASHTABLE_POINTER,NULL,NULL); + conntable = weechat_hashtable_new( + 16, WEECHAT_HASHTABLE_INTEGER, WEECHAT_HASHTABLE_POINTER, NULL, NULL); return WEECHAT_RC_OK; } -int weechat_plugin_end(struct t_weechat_plugin *plugin){ +int weechat_plugin_end(struct t_weechat_plugin *plugin) { (void)plugin; weechat_hashtable_free(conntable); - conntable=NULL; + conntable = NULL; debug_deinit(); return WEECHAT_RC_OK; } |