]>
jfr.im git - irc/quakenet/newserv.git/blob - trusts/formats.c
6 #include "../lib/strlfunc.h"
7 #include "../irc/irc.h"
10 char *trusts_cidr2str(struct irc_in_addr
*ip
, unsigned char bits
) {
12 struct irc_in_addr iptemp
;
16 int curbits
= bits
- i
* 16;
23 uint16_t mask
= 0xffff & ~((1 << (16 - curbits
)) - 1);
24 iptemp
.in6_16
[i
] = htons(ntohs(ip
->in6_16
[i
]) & mask
);
27 snprintf(buf
, sizeof(buf
), "%s/%u", IPtostr(iptemp
), (irc_in_addr_is_ipv4(&iptemp
))?bits
-96:bits
);
32 char *trusts_timetostr(time_t t
) {
35 strftime(buf
, sizeof(buf
), "%Y-%m-%d %H:%M:%S", localtime(&t
));
40 char *dumpth(trusthost
*th
, int oformat
) {
44 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
);
46 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
);
52 char *dumptg(trustgroup
*tg
, int oformat
) {
56 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
->flags
& TRUST_ENFORCE_IDENT
, 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
);
58 snprintf(buf
, sizeof(buf
), "%u,%s,%u,%d,%u,%u,%jd,%jd,%jd,%s,%s,%s", tg
->id
, tg
->name
->content
, tg
->trustedfor
, tg
->flags
, 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
);
64 int parsetg(char *buf
, trustgroup
*tg
, int oformat
) {
65 char *line
, *createdby
, *contact
, *comment
, *name
, *id
;
66 unsigned long expires
, lastseen
, lastmaxusereset
;
70 /* #id,ticket35153,14,20,1,1,17,1879854575,1222639249,0,nterfacer,Qwhois&2120764,Non-Commercial Bouncer (Created by: doomie)
76 ,expires ,lastseen ,lastmaxusereset
77 ,createdby,contact ,comment
81 strlcpy(xbuf
, buf
, sizeof(xbuf
));
94 line
= strchr(xbuf
, ',');
99 if(oformat
&& (id
[0] == '#'))
102 tg
->id
= strtoul(id
, NULL
, 10);
107 line
= strchr(line
, ',');
113 r
= sscanf(line
, "%*u,%u,%u,%u,%u,%lu,%lu,%lu,%n",
114 /*current, */ &tg
->trustedfor
, &tg
->flags
, &tg
->maxperident
,
115 &tg
->maxusage
, &expires
, &lastseen
, &lastmaxusereset
, &pos
);
117 if(tg
->maxperident
> 0)
118 tg
->flags
|= TRUST_RELIABLE_USERNAME
;
120 r
= sscanf(line
, "%u,%u,%u,%u,%lu,%lu,%lu,%n",
121 &tg
->trustedfor
, &tg
->flags
, &tg
->maxperident
,
122 &tg
->maxusage
, &expires
, &lastseen
, &lastmaxusereset
, &pos
);
127 tg
->expires
= (time_t)expires
;
128 tg
->lastseen
= (time_t)lastseen
;
129 tg
->lastmaxusereset
= (time_t)lastmaxusereset
;
131 createdby
= &line
[pos
];
132 contact
= strchr(createdby
, ',');
137 comment
= strchr(contact
, ',');
142 tg
->name
= getsstring(name
, TRUSTNAMELEN
);
143 tg
->createdby
= getsstring(createdby
, CREATEDBYLEN
);
144 tg
->comment
= getsstring(comment
, COMMENTLEN
);
145 tg
->contact
= getsstring(contact
, CONTACTLEN
);
146 if(!tg
->name
|| !tg
->createdby
|| !tg
->comment
|| !tg
->contact
) {
147 freesstring(tg
->name
);
148 freesstring(tg
->createdby
);
149 freesstring(tg
->comment
);
150 freesstring(tg
->contact
);
157 int parseth(char *line
, trusthost
*th
, unsigned int *tgid
, int oformat
) {
158 unsigned long lastseen
, created
;
159 int maxpernode
, nodebits
;
160 char *ip
, xbuf
[1024], *id
;
162 /* #id,213.230.192.128/26,20,23,1222732944
163 ip ,cur,max,lastseen */
165 strlcpy(xbuf
, line
, sizeof(xbuf
));
168 line
= strchr(line
, ',');
173 if(oformat
&& (id
[0] == '#'))
176 *tgid
= strtoul(id
, NULL
, 10);
181 line
= strchr(line
, ',');
186 if(!ipmask_parse(ip
, &th
->ip
, &th
->bits
))
190 if(sscanf(line
, "%*u,%u,%lu", /*current, */&th
->maxusage
, &lastseen
) != 2)
192 created
= getnettime();
196 if(sscanf(line
, "%u,%u,%lu,%lu,%d,%d", &th
->id
, &th
->maxusage
, &lastseen
, &created
, &maxpernode
, &nodebits
) != 6)
200 th
->lastseen
= (time_t)lastseen
;
201 th
->created
= (time_t)created
;
202 th
->maxpernode
= maxpernode
;
203 th
->nodebits
= nodebits
;
208 char *rtrim(char *buf
) {
209 static char obuf
[1024];
210 size_t len
= strlcpy(obuf
, buf
, sizeof(obuf
));
212 if((len
< sizeof(obuf
)) && (len
> 0)) {
214 for(i
=len
-1;i
>=0;i
--) {