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