]> jfr.im git - irc/quakenet/newserv.git/blame - trusts/formats.c
Use getnettime() where appropriate.
[irc/quakenet/newserv.git] / trusts / formats.c
CommitLineData
8a95d3e4
CP
1#include <stdio.h>
2#include <stdint.h>
3#include <time.h>
82a316e7
CP
4#include <string.h>
5#include <stdlib.h>
6#include "../lib/strlfunc.h"
acd5f58f 7#include "../irc/irc.h"
82a316e7 8#include "trusts.h"
8a95d3e4 9
6e6e98da 10char *trusts_cidr2str(struct irc_in_addr *ip, unsigned char bits) {
8a95d3e4 11 static char buf[100];
8a95d3e4 12
6e6e98da 13 snprintf(buf, sizeof(buf), "%s/%u", IPtostr(*ip), (irc_in_addr_is_ipv4(ip))?bits-96:bits);
8a95d3e4
CP
14
15 return buf;
16}
17
18char *trusts_timetostr(time_t t) {
19 static char buf[100];
20
21 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", localtime(&t));
22
23 return buf;
24}
25
82a316e7
CP
26char *dumpth(trusthost *th, int oformat) {
27 static char buf[512];
28
29 if(oformat) {
34110ca7 30 snprintf(buf, sizeof(buf), "#%u,%s,%u,%u,%jd", th->group->id, trusts_cidr2str(&th->ip, th->bits), th->count, th->maxusage, (intmax_t)th->lastseen);
82a316e7 31 } else {
34110ca7 32 snprintf(buf, sizeof(buf), "%u,%s,%u,%u,%jd,%jd,%u,%u", th->group->id, trusts_cidr2str(&th->ip, th->bits), th->id, th->maxusage, (intmax_t)th->lastseen, (intmax_t)th->created, th->maxpernode, th->nodebits);
82a316e7
CP
33 }
34
35 return buf;
36}
37
38char *dumptg(trustgroup *tg, int oformat) {
39 static char buf[512];
40
41 if(oformat) {
1f685425 42 snprintf(buf, sizeof(buf), "#%u,%s,%u,%u,%d,%u,%u,%jd,%jd,%jd,%s,%s,%s", tg->id, tg->name->content, tg->count, tg->trustedfor, tg->mode, tg->maxperident, tg->maxusage, (intmax_t)tg->expires, (intmax_t)tg->lastseen, (intmax_t)tg->lastmaxusereset, tg->createdby->content, tg->contact->content, tg->comment->content);
82a316e7 43 } else {
1f685425 44 snprintf(buf, sizeof(buf), "%u,%s,%u,%d,%u,%u,%jd,%jd,%jd,%s,%s,%s", tg->id, tg->name->content, tg->trustedfor, tg->mode, tg->maxperident, tg->maxusage, (intmax_t)tg->expires, (intmax_t)tg->lastseen, (intmax_t)tg->lastmaxusereset, tg->createdby->content, tg->contact->content, tg->comment->content);
82a316e7
CP
45 }
46
47 return buf;
48}
49
50int parsetg(char *buf, trustgroup *tg, int oformat) {
51 char *line, *createdby, *contact, *comment, *name, *id;
52 unsigned long expires, lastseen, lastmaxusereset;
53 char xbuf[1024];
54 int pos;
55
56/* #id,ticket35153,14,20,1,1,17,1879854575,1222639249,0,nterfacer,Qwhois&2120764,Non-Commercial Bouncer (Created by: doomie)
57 ,name ,current
58 ,trustedfor
59 ,mode
60 ,maxperident
61 ,maxusage
62 ,expires ,lastseen ,lastmaxusereset
63 ,createdby,contact ,comment
64*/
65 int r;
66
67 strlcpy(xbuf, buf, sizeof(xbuf));
68 line = xbuf;
69
70 if(!*line)
71 return 0;
72
73 if(oformat) {
74 if(line[0] != '#')
75 return 0;
76 line++;
77 }
78
79 id = line;
80 line = strchr(xbuf, ',');
81 if(!line)
82 return 0;
83 *line++ = '\0';
84
ce7bce4c
CP
85 if(oformat && (id[0] == '#'))
86 id++;
87
82a316e7
CP
88 tg->id = strtoul(id, NULL, 10);
89 if(!tg->id)
90 return 0;
91
92 name = line;
93 line = strchr(line, ',');
94 if(!line)
95 return 0;
96 *line++ = '\0';
97
98 if(oformat) {
99 r = sscanf(line, "%*u,%u,%u,%u,%u,%lu,%lu,%lu,%n",
100 /*current, */ &tg->trustedfor, &tg->mode, &tg->maxperident,
101 &tg->maxusage, &expires, &lastseen, &lastmaxusereset, &pos);
102 } else {
103 r = sscanf(line, "%u,%u,%u,%u,%lu,%lu,%lu,%n",
104 &tg->trustedfor, &tg->mode, &tg->maxperident,
105 &tg->maxusage, &expires, &lastseen, &lastmaxusereset, &pos);
106 }
107 if(r != 7)
108 return 0;
109
110 tg->expires = (time_t)expires;
111 tg->lastseen = (time_t)lastseen;
1f685425 112 tg->lastmaxusereset = (time_t)lastmaxusereset;
82a316e7
CP
113
114 createdby = &line[pos];
115 contact = strchr(createdby, ',');
116 if(!contact)
117 return 0;
118 *contact++ = '\0';
119
120 comment = strchr(contact, ',');
121 if(!comment)
122 return 0;
123 *comment++ = '\0';
124
125 tg->name = getsstring(name, TRUSTNAMELEN);
1f685425 126 tg->createdby = getsstring(createdby, CREATEDBYLEN);
82a316e7
CP
127 tg->comment = getsstring(comment, COMMENTLEN);
128 tg->contact = getsstring(contact, CONTACTLEN);
129 if(!tg->name || !tg->createdby || !tg->comment || !tg->contact) {
130 freesstring(tg->name);
131 freesstring(tg->createdby);
132 freesstring(tg->comment);
133 freesstring(tg->contact);
134 return 0;
135 }
136
137 return 1;
138}
139
140int parseth(char *line, trusthost *th, unsigned int *tgid, int oformat) {
caf2d02a 141 unsigned long lastseen, created;
cebc4cab 142 int maxpernode, nodebits;
82a316e7
CP
143 char *ip, xbuf[1024], *id;
144
145/* #id,213.230.192.128/26,20,23,1222732944
146 ip ,cur,max,lastseen */
147
148 strlcpy(xbuf, line, sizeof(xbuf));
149 id = line = xbuf;
150
151 line = strchr(line, ',');
152 if(!line)
153 return 0;
154 *line++ = '\0';
155
ce7bce4c
CP
156 if(oformat && (id[0] == '#'))
157 id++;
158
82a316e7
CP
159 *tgid = strtoul(id, NULL, 10);
160 if(!*tgid)
161 return 0;
162
163 ip = line;
164 line = strchr(line, ',');
165 if(!line)
166 return 0;
167 *line++ = '\0';
168
6e6e98da 169 if(!ipmask_parse(ip, &th->ip, &th->bits))
82a316e7
CP
170 return 0;
171
172 if(oformat) {
173 if(sscanf(line, "%*u,%u,%lu", /*current, */&th->maxusage, &lastseen) != 2)
174 return 0;
acd5f58f 175 created = getnettime();
82a316e7 176 } else {
cebc4cab 177 if(sscanf(line, "%u,%u,%lu,%lu,%d,%d", &th->id, &th->maxusage, &lastseen, &created, &maxpernode, &nodebits) != 6)
82a316e7
CP
178 return 0;
179 }
180
181 th->lastseen = (time_t)lastseen;
caf2d02a 182 th->created = (time_t)created;
cebc4cab
GB
183 th->maxpernode = maxpernode;
184 th->nodebits = nodebits;
82a316e7
CP
185
186 return 1;
187}
188
189char *rtrim(char *buf) {
190 static char obuf[1024];
191 size_t len = strlcpy(obuf, buf, sizeof(obuf));
192
193 if((len < sizeof(obuf)) && (len > 0)) {
0555113a 194 int i;
82a316e7
CP
195 for(i=len-1;i>=0;i--) {
196 if(obuf[i] != ' ')
197 break;
198
199 obuf[i] = '\0';
200 }
201 }
202
203 return obuf;
204}