]> jfr.im git - irc/quakenet/newserv.git/blob - miscreply/time.c
LUA: port luadb to dbapi2 to drop postgres dependency
[irc/quakenet/newserv.git] / miscreply / time.c
1 /* time.c */
2
3 #include "miscreply.h"
4 #include "../irc/irc.h"
5 #include "../core/error.h"
6
7 #include <stdio.h>
8
9
10
11 /*
12 * Return the current date in the following format:
13 * "Day Month DD YYYY -- HH:MM +/-HH:MM"
14 * e.g. "Saturday March 27 2010 -- 19:03 +01:00"
15 * where the last part is the timezone offset
16 *
17 */
18 static char *timelongdate(void) {
19 static char buf[64];
20 unsigned int off;
21 struct tm *tmp;
22 time_t gt, lt;
23 size_t n;
24 char c;
25
26 lt = time(NULL);
27
28 tmp = localtime(&lt);
29
30 if (tmp == NULL)
31 return "";
32
33 n = strftime(buf, sizeof(buf) - 8, "%A %B %e %Y -- %H:%M", tmp);
34
35 if (!n)
36 return "";
37
38 tmp = gmtime(&lt);
39
40 if (tmp == NULL)
41 return "";
42
43 gt = mktime(tmp);
44
45 if (gt > lt) {
46 c = '-';
47 off = (unsigned int) (gt - lt);
48 } else {
49 c = '+';
50 off = (unsigned int) (lt - gt);
51 }
52
53 off = (off / 60) % 6000;
54 sprintf(&buf[n], " %c%02u:%02u", c, off / 60, off % 60);
55
56 return buf;
57 }
58
59
60
61 /* handle remote time request
62 *
63 * <source numeric> TIME/TI <target server numeric>
64 *
65 * cargv[0] = target server numeric
66 * can be a * in which case the request is for all servers (snircd extension)
67 *
68 */
69 int handletimemsg(void *source, int cargc, char **cargv) {
70
71 nick *snick; /* struct nick for source nick */
72 long unsigned int timestamp = getnettime(); /* current nettime */
73 long unsigned int offset = timestamp - time(NULL); /* offset nettime to system clock */
74 char *sourcenum = (char *)source; /* source user numeric */
75
76 /* check parameters */
77 if (cargc < 1) {
78 miscreply_needmoreparams(sourcenum, "TIME");
79 return CMD_OK;
80 }
81
82 /* find source user */
83 if (!(snick = miscreply_finduser(sourcenum, "TIME")))
84 return CMD_OK;
85
86 /*
87 * 391 RPL_TIME "source 391 target servername timestamp offset :longdate"
88 * "irc.netsplit.net 391 foobar irc.netsplit.net 1269713493 0 :Saturday March 27 2010 -- 19:11 +01:00"
89 */
90 irc_send("%s 391 %s %s %lu %ld :%s", getmynumeric(), sourcenum, myserver->content, timestamp, offset, timelongdate());
91
92 return CMD_OK;
93 }