1 /********************************************************************\
2 * BitlBee -- An IRC to other IM-networks gateway *
3 * *
4 * Copyright 2002-2010 Wilmer van der Gaast and others *
5 \********************************************************************/
6
7 /* Some stuff that doesn't belong anywhere else. */
8
9 /*
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License with
21 the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
22 if not, write to the Free Software Foundation, Inc., 51 Franklin St.,
23 Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26 #include "bitlbee.h"
27
set_eval_timezone(set_t * set,char * value)28 char *set_eval_timezone(set_t *set, char *value)
29 {
30 char *s;
31
32 if (strcmp(value, "local") == 0 ||
33 strcmp(value, "gmt") == 0 || strcmp(value, "utc") == 0) {
34 return value;
35 }
36
37 /* Otherwise: +/- at the beginning optional, then one or more numbers,
38 possibly followed by a colon and more numbers. Don't bother bound-
39 checking them since users are free to shoot themselves in the foot. */
40 s = value;
41 if (*s == '+' || *s == '-') {
42 s++;
43 }
44
45 /* \d+ */
46 if (!g_ascii_isdigit(*s)) {
47 return SET_INVALID;
48 }
49 while (*s && g_ascii_isdigit(*s)) {
50 s++;
51 }
52
53 /* EOS? */
54 if (*s == '\0') {
55 return value;
56 }
57
58 /* Otherwise, colon */
59 if (*s != ':') {
60 return SET_INVALID;
61 }
62 s++;
63
64 /* \d+ */
65 if (!g_ascii_isdigit(*s)) {
66 return SET_INVALID;
67 }
68 while (*s && g_ascii_isdigit(*s)) {
69 s++;
70 }
71
72 /* EOS */
73 return *s == '\0' ? value : SET_INVALID;
74 }
75
irc_format_timestamp(irc_t * irc,time_t msg_ts)76 char *irc_format_timestamp(irc_t *irc, time_t msg_ts)
77 {
78 time_t now_ts = time(NULL);
79 struct tm now, msg;
80 char *set;
81
82 /* If the timestamp is <= 0 or less than a minute ago, discard it as
83 it doesn't seem to add to much useful info and/or might be noise. */
84 if (msg_ts <= 0 || msg_ts > now_ts - 60) {
85 return NULL;
86 }
87
88 set = set_getstr(&irc->b->set, "timezone");
89 if (strcmp(set, "local") == 0) {
90 localtime_r(&now_ts, &now);
91 localtime_r(&msg_ts, &msg);
92 } else {
93 int hr, min = 0, sign = 60;
94
95 if (set[0] == '-') {
96 sign *= -1;
97 set++;
98 } else if (set[0] == '+') {
99 set++;
100 }
101
102 if (sscanf(set, "%d:%d", &hr, &min) >= 1) {
103 msg_ts += sign * (hr * 60 + min);
104 now_ts += sign * (hr * 60 + min);
105 }
106
107 gmtime_r(&now_ts, &now);
108 gmtime_r(&msg_ts, &msg);
109 }
110
111 if (msg.tm_year == now.tm_year && msg.tm_yday == now.tm_yday) {
112 return g_strdup_printf("\x02[\x02\x02\x02%02d:%02d:%02d\x02]\x02 ",
113 msg.tm_hour, msg.tm_min, msg.tm_sec);
114 } else {
115 return g_strdup_printf("\x02[\x02\x02\x02%04d-%02d-%02d "
116 "%02d:%02d:%02d\x02]\x02 ",
117 msg.tm_year + 1900, msg.tm_mon + 1, msg.tm_mday,
118 msg.tm_hour, msg.tm_min, msg.tm_sec);
119 }
120 }
121
irc_format_servertime(irc_t * irc,time_t msg_ts)122 char *irc_format_servertime(irc_t *irc, time_t msg_ts)
123 {
124 struct tm msg;
125
126 if (msg_ts <= 0) {
127 return NULL;
128 }
129
130 gmtime_r(&msg_ts, &msg);
131
132 return g_strdup_printf("@time=%04d-%02d-%02dT"
133 "%02d:%02d:%02d.000Z",
134 msg.tm_year + 1900, msg.tm_mon + 1, msg.tm_mday,
135 msg.tm_hour, msg.tm_min, msg.tm_sec);
136 }
137
set_eval_self_messages(set_t * set,char * value)138 char *set_eval_self_messages(set_t *set, char *value)
139 {
140 if (is_bool(value) ||
141 g_strcasecmp(value, "prefix") == 0 ||
142 g_strcasecmp(value, "prefix_notice") == 0) {
143 return value;
144 } else {
145 return SET_INVALID;
146 }
147 }
148