1 /*
2  * ctcp.c: client to client protocol
3  *
4  * Copyright(c) 1997-2000 - All Rights Reserved
5  *
6  * See the COPYRIGHT file.
7  */
8 
9 #ifndef lint
10 static char rcsid[] = "@(#)$Id: ctcp.c,v 1.14 2000/07/31 22:33:55 kalt Exp $";
11 #endif
12 
13 #include "os.h"
14 
15 #include "struct.h"
16 #include "server.h"
17 #include "utils.h"
18 #include "patchlevel.h"
19 
20 extern	int	cmd_msgnotice(char *, char *);
21 
22 /*
23  * user commands
24  */
25 
26 /* cmd_ctcp: send out a CTCP request */
27 int
cmd_ctcp(p)28 cmd_ctcp(p)
29   char *p;
30 {
31   char *wp, *wp2, wbuf[512];
32 
33   wp = index(p, ' ');
34   if (!wp)
35       return -1;
36   *wp++ = '\0';
37   wp2 = wp;
38   while (*wp2 && *wp2 != ' ')
39     {
40       *wp2 = toupper(*wp2);
41       wp2++;
42     }
43   sprintf(wbuf, "%s \001%s\001", p, wp);
44   return cmd_msgnotice("PRIVMSG", wbuf);
45 }
46 
47 /* cmd_ping: ping a user */
48 int
cmd_ping(p)49 cmd_ping(p)
50   char *p;
51 {
52   char buf[512];
53 
54   if (!*p)
55       return -1;
56   if (index(p, ' '))
57       return -4;
58   sprintf(buf, "%s PING %lu", p, time(NULL));
59   return cmd_ctcp(buf);
60 }
61 
62 /*
63  * basic flood protection stuff.
64  * I've always said it's easy, and it is, but this is a simple method
65  * which I don't like much.  I'll improve it when I get a chance.
66  */
67 static	time_t	last_t = 0;
68 static	char	last_cnt;
69 
70 void
sic_ctcp(nick,toself,txt)71 sic_ctcp(nick, toself, txt)
72   char *nick, *txt;
73   char toself;
74 {
75   char *b, *e, *s;
76   int cnt = 0;
77   char reply[512] = "", temp[512];
78 
79   if ((time(NULL) - last_t) < 10)
80     {
81       if (++last_cnt >= 3)
82 	  return;
83     }
84   else
85     {
86       last_t = time(NULL);
87       last_cnt = 1;
88     }
89 
90   sprintf(reply, "%s ", nick);
91   while (b = index(txt, '\001'))
92     {
93       if ((e = index(++b, '\001')) == NULL)
94 	  break;
95       if ((s = index(b, ' ')) == NULL)
96 	  s = e;
97 
98       *s = '\0';
99       temp[0] = '\0';
100 
101       if (!strcmp("CLIENTINFO", b))
102 	  strcpy(temp, "\001CLIENTINFO CLIENTINFO PING TIME VERSION\001");
103       else if (!strcmp("PING", b))
104 	{
105 	  char *sp = index(s+1, ' ');
106 
107 	  if (sp && sp < e)
108 	    {
109 	      *sp++ = '\001';
110 	      *sp = '\000';
111 	    }
112           sprintf(temp, "\001PING %s", (s != e) ? s+1 : "");
113 	}
114       else if (!strcmp("TIME", b))
115 	{
116 	  char btime[80];
117 	  time_t t;
118 	  int gmtoff;
119 	  struct tm lt, gmt;
120 	  const char *wday[7]={"Sun", "Mon", "Tue", "Wed", "Thu", "Fri","Sat"};
121 	  const char *mth[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
122 				    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
123 
124 	  t = time(NULL);
125  	  lt = *localtime(&t);
126  	  gmt = *gmtime(&t);
127 	  gmtoff = (lt.tm_hour - gmt.tm_hour) * 60 + lt.tm_min - gmt.tm_min;
128 	  if (lt.tm_year < gmt.tm_year)
129 	    gmtoff -= 24 * 60;
130 	  else if (lt.tm_year > gmt.tm_year)
131 	    gmtoff += 24 * 60;
132 	  else if (lt.tm_yday < gmt.tm_yday)
133 	    gmtoff -= 24 * 60;
134 	  else if (lt.tm_yday > gmt.tm_yday)
135 	    gmtoff += 24 * 60;
136 
137 	  sprintf(temp, "\001TIME %s, %d %s %s %2.2d%2.2d\001",
138 		  wday[lt.tm_wday], lt.tm_mday, mth[lt.tm_mon],
139 		  my_cftime(btime, 80, "%Y %R", t),
140 		  (int) (gmtoff/60), (int) (gmtoff%60));
141 	}
142       else if (!strcmp("VERSION", b))
143 	  sprintf(temp, "\001VERSION sic %s\001", version);
144 
145       if (s)
146 	  if (s == e)
147 	      *s = '\001';
148 	  else
149 	      *s = ' ';
150 
151       if (strlen(temp) + strlen(reply) > 256)
152 	  break;
153       strcat(reply, temp);
154 
155       if (cnt++ > 4)
156 	  break;
157       txt = e+1;
158     }
159   if (cnt > 0 && cnt <= 5)
160       cmd_msgnotice("NOTICE", reply);
161   else
162     {
163     }
164 }
165