Added CTCP Ping command (and display correctly when reply is received).

This commit is contained in:
Sebastien Helleu 2003-10-12 17:03:38 +00:00
parent c67cfeaa87
commit 836d7a139a
8 changed files with 242 additions and 62 deletions

View File

@ -11,7 +11,7 @@ Version 0.0.3 (under dev!):
* french translation
* new IRC command: /stats, /service, /squit, /motd, /lusers, /links, /time,
/trace, /admin, /info, /servlist, /squery, /who, /whowas, /die, /summon,
/users, /wallops, /userhost, /ison
/users, /wallops, /userhost, /ison, /ctcp ping
* code cleanup
Version 0.0.2 (2003-10-05):

View File

@ -23,6 +23,18 @@ OUTPUT=irc.a
OBJS=irc-commands.o irc-send.o irc-recv.o irc-display.o irc-server.o irc-channel.o irc-nick.o
DEFINES=WEE_CURSES
ifeq ($(GUI), curses)
DEFINES=WEE_CURSES
endif
ifeq ($(GUI), gtk)
INCLUDES=`pkg-config --cflags gtk+-2.0`
DEFINES=WEE_GTK
endif
ifeq ($(GUI), qt)
DEFINES=WEE_QT
endif
all: $(OBJS)
ar r $(OUTPUT) $(OBJS)

View File

@ -28,6 +28,7 @@
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include <sys/utsname.h>
@ -522,7 +523,10 @@ irc_cmd_recv_nick (t_irc_server *server, char *host, char *arguments)
int
irc_cmd_recv_notice (t_irc_server *server, char *host, char *arguments)
{
char *pos, *pos2;
char *pos, *pos2, *pos_usec;
struct timeval tv;
struct timezone tz;
long sec1, usec1, sec2, usec2, difftime;
if (host)
{
@ -548,13 +552,13 @@ irc_cmd_recv_notice (t_irc_server *server, char *host, char *arguments)
WEECHAT_ERROR, "notice");
return -1;
}
irc_display_prefix (server->window, PREFIX_SERVER);
if (strncmp (pos, "\01VERSION", 8) == 0)
{
pos += 9;
pos2 = strchr (pos, '\01');
if (pos2)
pos2[0] = '\0';
irc_display_prefix (server->window, PREFIX_SERVER);
gui_printf_color (server->window, COLOR_WIN_CHAT, "CTCP ");
gui_printf_color (server->window, COLOR_WIN_CHAT_CHANNEL, "VERSION ");
gui_printf_color (server->window, COLOR_WIN_CHAT, _("reply from"));
@ -562,7 +566,48 @@ irc_cmd_recv_notice (t_irc_server *server, char *host, char *arguments)
gui_printf_color (server->window, COLOR_WIN_CHAT, ": %s\n", pos);
}
else
gui_printf_color (server->window, COLOR_WIN_CHAT, "%s\n", pos);
{
if (strncmp (pos, "\01PING", 5) == 0)
{
pos += 5;
while (pos[0] == ' ')
pos++;
pos_usec = strchr (pos, ' ');
if (pos_usec)
{
pos_usec[0] = '\0';
pos_usec++;
pos2 = strchr (pos_usec, '\01');
if (pos2)
{
pos2[0] = '\0';
gettimeofday (&tv, &tz);
sec1 = atol (pos);
usec1 = atol (pos_usec);
sec2 = tv.tv_sec;
usec2 = tv.tv_usec;
difftime = ((sec2 * 1000000) + usec2) - ((sec1 * 1000000) + usec1);
irc_display_prefix (server->window, PREFIX_SERVER);
gui_printf_color (server->window, COLOR_WIN_CHAT, "CTCP ");
gui_printf_color (server->window, COLOR_WIN_CHAT_CHANNEL, "PING ");
gui_printf_color (server->window, COLOR_WIN_CHAT, _("reply from"));
gui_printf_color (server->window, COLOR_WIN_CHAT_NICK, " %s", host);
gui_printf_color (server->window, COLOR_WIN_CHAT,
_(": %ld.%ld seconds\n"),
difftime / 1000000,
(difftime % 1000000) / 1000);
}
}
}
else
{
irc_display_prefix (server->window, PREFIX_SERVER);
gui_printf_color (server->window, COLOR_WIN_CHAT, "%s\n", pos);
}
}
return 0;
}
@ -784,6 +829,7 @@ irc_cmd_recv_privmsg (t_irc_server *server, char *host, char *arguments)
if (pos[0] == ':')
pos++;
/* version asked by another user => answer with WeeChat version */
if (strcmp (pos, "\01VERSION\01") == 0)
{
buf = (struct utsname *) malloc (sizeof (struct utsname));
@ -808,38 +854,61 @@ irc_cmd_recv_privmsg (t_irc_server *server, char *host, char *arguments)
}
else
{
/* private message received */
ptr_channel = channel_search (server, host);
if (!ptr_channel)
/* ping request from another user => answer */
if (strncmp (pos, "\01PING", 5) == 0)
{
ptr_channel = channel_new (server, CHAT_PRIVATE, host);
pos += 5;
while (pos[0] == ' ')
pos++;
pos2 = strchr (pos, '\01');
if (pos2)
pos2[0] = '\0';
else
pos = NULL;
if (pos && !pos[0])
pos = NULL;
if (pos)
server_sendf (server, "NOTICE %s :\01PING %s\01\r\n",
host, pos);
else
server_sendf (server, "NOTICE %s :\01PING\01\r\n",
host);
}
else
{
/* private message received => display it */
ptr_channel = channel_search (server, host);
if (!ptr_channel)
{
gui_printf (server->window,
_("%s cannot create new private window \"%s\"\n"),
WEECHAT_ERROR, host);
return -1;
ptr_channel = channel_new (server, CHAT_PRIVATE, host);
if (!ptr_channel)
{
gui_printf (server->window,
_("%s cannot create new private window \"%s\"\n"),
WEECHAT_ERROR, host);
return -1;
}
}
if (!ptr_channel->topic)
{
ptr_channel->topic = strdup (host2);
gui_redraw_window_title (ptr_channel->window);
}
gui_printf_color_type (ptr_channel->window,
MSG_TYPE_NICK,
COLOR_WIN_CHAT_DARK, "<");
gui_printf_color_type (ptr_channel->window,
MSG_TYPE_NICK,
COLOR_WIN_NICK_PRIVATE,
"%s", host);
gui_printf_color_type (ptr_channel->window,
MSG_TYPE_NICK,
COLOR_WIN_CHAT_DARK, "> ");
gui_printf_color_type (ptr_channel->window,
MSG_TYPE_MSG,
COLOR_WIN_CHAT, "%s\n", pos);
}
if (!ptr_channel->topic)
{
ptr_channel->topic = strdup (host2);
gui_redraw_window_title (ptr_channel->window);
}
gui_printf_color_type (ptr_channel->window,
MSG_TYPE_NICK,
COLOR_WIN_CHAT_DARK, "<");
gui_printf_color_type (ptr_channel->window,
MSG_TYPE_NICK,
COLOR_WIN_NICK_PRIVATE,
"%s", host);
gui_printf_color_type (ptr_channel->window,
MSG_TYPE_NICK,
COLOR_WIN_CHAT_DARK, "> ");
gui_printf_color_type (ptr_channel->window,
MSG_TYPE_MSG,
COLOR_WIN_CHAT, "%s\n", pos);
}
}
else

View File

@ -28,6 +28,7 @@
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include <sys/utsname.h>
@ -126,6 +127,8 @@ int
irc_cmd_send_ctcp (t_irc_server *server, char *arguments)
{
char *pos, *pos2;
struct timeval tv;
struct timezone tz;
pos = strchr (arguments, ' ');
if (pos)
@ -163,6 +166,12 @@ irc_cmd_send_ctcp (t_irc_server *server, char *arguments)
server_sendf (server, "PRIVMSG %s :\01ACTION\01\r\n",
arguments);
}
if (strcasecmp (pos, "ping") == 0)
{
gettimeofday (&tv, &tz);
server_sendf (server, "PRIVMSG %s :\01PING %d %d\01\r\n",
arguments, tv.tv_sec, tv.tv_usec);
}
}
return 0;

View File

@ -11,7 +11,7 @@ Version 0.0.3 (under dev!):
* french translation
* new IRC command: /stats, /service, /squit, /motd, /lusers, /links, /time,
/trace, /admin, /info, /servlist, /squery, /who, /whowas, /die, /summon,
/users, /wallops, /userhost, /ison
/users, /wallops, /userhost, /ison, /ctcp ping
* code cleanup
Version 0.0.2 (2003-10-05):

View File

@ -23,6 +23,18 @@ OUTPUT=irc.a
OBJS=irc-commands.o irc-send.o irc-recv.o irc-display.o irc-server.o irc-channel.o irc-nick.o
DEFINES=WEE_CURSES
ifeq ($(GUI), curses)
DEFINES=WEE_CURSES
endif
ifeq ($(GUI), gtk)
INCLUDES=`pkg-config --cflags gtk+-2.0`
DEFINES=WEE_GTK
endif
ifeq ($(GUI), qt)
DEFINES=WEE_QT
endif
all: $(OBJS)
ar r $(OUTPUT) $(OBJS)

View File

@ -28,6 +28,7 @@
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include <sys/utsname.h>
@ -522,7 +523,10 @@ irc_cmd_recv_nick (t_irc_server *server, char *host, char *arguments)
int
irc_cmd_recv_notice (t_irc_server *server, char *host, char *arguments)
{
char *pos, *pos2;
char *pos, *pos2, *pos_usec;
struct timeval tv;
struct timezone tz;
long sec1, usec1, sec2, usec2, difftime;
if (host)
{
@ -548,13 +552,13 @@ irc_cmd_recv_notice (t_irc_server *server, char *host, char *arguments)
WEECHAT_ERROR, "notice");
return -1;
}
irc_display_prefix (server->window, PREFIX_SERVER);
if (strncmp (pos, "\01VERSION", 8) == 0)
{
pos += 9;
pos2 = strchr (pos, '\01');
if (pos2)
pos2[0] = '\0';
irc_display_prefix (server->window, PREFIX_SERVER);
gui_printf_color (server->window, COLOR_WIN_CHAT, "CTCP ");
gui_printf_color (server->window, COLOR_WIN_CHAT_CHANNEL, "VERSION ");
gui_printf_color (server->window, COLOR_WIN_CHAT, _("reply from"));
@ -562,7 +566,48 @@ irc_cmd_recv_notice (t_irc_server *server, char *host, char *arguments)
gui_printf_color (server->window, COLOR_WIN_CHAT, ": %s\n", pos);
}
else
gui_printf_color (server->window, COLOR_WIN_CHAT, "%s\n", pos);
{
if (strncmp (pos, "\01PING", 5) == 0)
{
pos += 5;
while (pos[0] == ' ')
pos++;
pos_usec = strchr (pos, ' ');
if (pos_usec)
{
pos_usec[0] = '\0';
pos_usec++;
pos2 = strchr (pos_usec, '\01');
if (pos2)
{
pos2[0] = '\0';
gettimeofday (&tv, &tz);
sec1 = atol (pos);
usec1 = atol (pos_usec);
sec2 = tv.tv_sec;
usec2 = tv.tv_usec;
difftime = ((sec2 * 1000000) + usec2) - ((sec1 * 1000000) + usec1);
irc_display_prefix (server->window, PREFIX_SERVER);
gui_printf_color (server->window, COLOR_WIN_CHAT, "CTCP ");
gui_printf_color (server->window, COLOR_WIN_CHAT_CHANNEL, "PING ");
gui_printf_color (server->window, COLOR_WIN_CHAT, _("reply from"));
gui_printf_color (server->window, COLOR_WIN_CHAT_NICK, " %s", host);
gui_printf_color (server->window, COLOR_WIN_CHAT,
_(": %ld.%ld seconds\n"),
difftime / 1000000,
(difftime % 1000000) / 1000);
}
}
}
else
{
irc_display_prefix (server->window, PREFIX_SERVER);
gui_printf_color (server->window, COLOR_WIN_CHAT, "%s\n", pos);
}
}
return 0;
}
@ -784,6 +829,7 @@ irc_cmd_recv_privmsg (t_irc_server *server, char *host, char *arguments)
if (pos[0] == ':')
pos++;
/* version asked by another user => answer with WeeChat version */
if (strcmp (pos, "\01VERSION\01") == 0)
{
buf = (struct utsname *) malloc (sizeof (struct utsname));
@ -808,38 +854,61 @@ irc_cmd_recv_privmsg (t_irc_server *server, char *host, char *arguments)
}
else
{
/* private message received */
ptr_channel = channel_search (server, host);
if (!ptr_channel)
/* ping request from another user => answer */
if (strncmp (pos, "\01PING", 5) == 0)
{
ptr_channel = channel_new (server, CHAT_PRIVATE, host);
pos += 5;
while (pos[0] == ' ')
pos++;
pos2 = strchr (pos, '\01');
if (pos2)
pos2[0] = '\0';
else
pos = NULL;
if (pos && !pos[0])
pos = NULL;
if (pos)
server_sendf (server, "NOTICE %s :\01PING %s\01\r\n",
host, pos);
else
server_sendf (server, "NOTICE %s :\01PING\01\r\n",
host);
}
else
{
/* private message received => display it */
ptr_channel = channel_search (server, host);
if (!ptr_channel)
{
gui_printf (server->window,
_("%s cannot create new private window \"%s\"\n"),
WEECHAT_ERROR, host);
return -1;
ptr_channel = channel_new (server, CHAT_PRIVATE, host);
if (!ptr_channel)
{
gui_printf (server->window,
_("%s cannot create new private window \"%s\"\n"),
WEECHAT_ERROR, host);
return -1;
}
}
if (!ptr_channel->topic)
{
ptr_channel->topic = strdup (host2);
gui_redraw_window_title (ptr_channel->window);
}
gui_printf_color_type (ptr_channel->window,
MSG_TYPE_NICK,
COLOR_WIN_CHAT_DARK, "<");
gui_printf_color_type (ptr_channel->window,
MSG_TYPE_NICK,
COLOR_WIN_NICK_PRIVATE,
"%s", host);
gui_printf_color_type (ptr_channel->window,
MSG_TYPE_NICK,
COLOR_WIN_CHAT_DARK, "> ");
gui_printf_color_type (ptr_channel->window,
MSG_TYPE_MSG,
COLOR_WIN_CHAT, "%s\n", pos);
}
if (!ptr_channel->topic)
{
ptr_channel->topic = strdup (host2);
gui_redraw_window_title (ptr_channel->window);
}
gui_printf_color_type (ptr_channel->window,
MSG_TYPE_NICK,
COLOR_WIN_CHAT_DARK, "<");
gui_printf_color_type (ptr_channel->window,
MSG_TYPE_NICK,
COLOR_WIN_NICK_PRIVATE,
"%s", host);
gui_printf_color_type (ptr_channel->window,
MSG_TYPE_NICK,
COLOR_WIN_CHAT_DARK, "> ");
gui_printf_color_type (ptr_channel->window,
MSG_TYPE_MSG,
COLOR_WIN_CHAT, "%s\n", pos);
}
}
else

View File

@ -28,6 +28,7 @@
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include <sys/utsname.h>
@ -126,6 +127,8 @@ int
irc_cmd_send_ctcp (t_irc_server *server, char *arguments)
{
char *pos, *pos2;
struct timeval tv;
struct timezone tz;
pos = strchr (arguments, ' ');
if (pos)
@ -163,6 +166,12 @@ irc_cmd_send_ctcp (t_irc_server *server, char *arguments)
server_sendf (server, "PRIVMSG %s :\01ACTION\01\r\n",
arguments);
}
if (strcasecmp (pos, "ping") == 0)
{
gettimeofday (&tv, &tz);
server_sendf (server, "PRIVMSG %s :\01PING %d %d\01\r\n",
arguments, tv.tv_sec, tv.tv_usec);
}
}
return 0;