Skip to content

Commit

Permalink
echoArea && htdpointrequest
Browse files Browse the repository at this point in the history
  • Loading branch information
kreon committed Jun 20, 2015
1 parent 031def4 commit 8f9a4e1
Show file tree
Hide file tree
Showing 4 changed files with 246 additions and 1 deletion.
10 changes: 9 additions & 1 deletion jnode-httpd-module/src/org/jnode/httpd/HttpdModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@ public class HttpdModule extends JnodeModule {
private static final String CONFIG_PORT = "port";
private static final String CONFIG_LINK_REG = "linkreg";
private static final String CONFIG_POINT_REG = "pointreg";
private static final String CONFIG_HDG_REG = "hdgpointreg";
private static final String CONFIG_EXTERNAL = "external";

private static final Logger logger = Logger.getLogger(HttpdModule.class);
public static final Logger logger = Logger.getLogger(HttpdModule.class);
private short port;
private boolean linkreg;
private boolean pointreg;
private boolean hdgpointreg;
private String external;

public HttpdModule(String configFile) throws JnodeModuleException {
Expand All @@ -66,6 +68,8 @@ public HttpdModule(String configFile) throws JnodeModuleException {
pointreg = Boolean.valueOf(properties.getProperty(CONFIG_POINT_REG,
"false"));
external = properties.getProperty(CONFIG_EXTERNAL);
hdgpointreg = Boolean.valueOf(properties.getProperty(CONFIG_HDG_REG,
"false"));
HTML.setExternalPath(external);
}

Expand All @@ -87,6 +91,10 @@ public void start() {
Spark.get(new SelfRoute());
Spark.get(new SelfRoute("/"));
Spark.get(new SelfRoute(""));
Spark.get(new EchoareaCSVRoute());

Spark.post(new HDGPointRequestRoute(hdgpointreg));

if (pointreg) {
Spark.get(new BecomePointRoute(true));
Spark.get(new PointRequestConfirmRoute());
Expand Down
49 changes: 49 additions & 0 deletions jnode-httpd-module/src/org/jnode/httpd/dto/EchoareaCSV.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.jnode.httpd.dto;

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

@DatabaseTable(tableName = "httpd_echoarea_csv")
public class EchoareaCSV {
@DatabaseField(columnName = "name", id = true)
private String name;
@DatabaseField(columnName = "description")
private String description;
@DatabaseField(columnName = "num")
private Long num;
@DatabaseField(columnName = "latest")
private Long latest;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public Long getNum() {
return num;
}

public void setNum(Long num) {
this.num = num;
}

public Long getLatest() {
return latest;
}

public void setLatest(Long latest) {
this.latest = latest;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.jnode.httpd.routes.get;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.jnode.httpd.dto.EchoareaCSV;

import com.j256.ormlite.dao.GenericRawResults;

import jnode.dto.Echoarea;
import jnode.logger.Logger;
import jnode.orm.ORMManager;
import spark.Request;
import spark.Response;
import spark.Route;

public class EchoareaCSVRoute extends Route {
private static final long MAX_CACHE_TIME = 3600000;
private long latest = 0;

public EchoareaCSVRoute() {
super("/echoarea.csv");
}

@Override
public Object handle(Request req, Response resp) {
resp.type("text/plain; charset=utf-8");
long now = new Date().getTime();
StringBuilder sb = new StringBuilder();
if (now - latest > MAX_CACHE_TIME) {
try {
ORMManager.get(EchoareaCSV.class).executeRaw(
"DELETE FROM httpd_echoarea_csv;");
GenericRawResults<String[]> results = ORMManager
.get(Echoarea.class)
.getRaw("SELECT e.name,e.description,(SELECT count(id) FROM echomail "
+ "WHERE echoarea_id=e.id) AS num,(SELECT max(date) FROM echomail "
+ "WHERE echoarea_id=e.id) AS latest FROM echoarea e ORDER BY e.name;");
latest = now;
for (String[] row : results.getResults()) {
EchoareaCSV csv = new EchoareaCSV();
csv.setName(row[0]);
csv.setDescription(row[1]);
csv.setNum(new Long(row[2]));
csv.setLatest(new Long(row[3]));
ORMManager.get(EchoareaCSV.class).save(csv);
sb.append(csv.getName() + "," + csv.getLatest()/1000L + ","
+ csv.getNum() + "," + csv.getDescription()
+ "\r\n");
}
} catch (SQLException e) {
Logger.getLogger(EchoareaCSVRoute.class)
.l1("Echoarea Error", e);
return "error,0,0,SQLError\r\n";
}
} else {
List<EchoareaCSV> list = ORMManager.get(EchoareaCSV.class)
.getOrderAnd("name", true);
for (EchoareaCSV csv : list) {
sb.append(csv.getName() + "," + csv.getLatest() + ","
+ csv.getNum() + "," + csv.getDescription() + "\r\n");
}
}
return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package org.jnode.httpd.routes.post;

import org.jnode.httpd.HttpdModule;
import org.jnode.httpd.dto.PointRequest;

import jnode.dto.Echoarea;
import jnode.dto.Link;
import jnode.ftn.FtnTools;
import jnode.ftn.types.FtnAddress;
import jnode.main.MainHandler;
import jnode.orm.ORMManager;
import spark.Request;
import spark.Response;
import spark.Route;

public class HDGPointRequestRoute extends Route {

private boolean enabled = false;

public HDGPointRequestRoute(boolean enabled) {
super("/hdgpntrequest");
this.enabled = enabled;
}

@Override
public Object handle(Request req, Response resp) {
if (!enabled) {
return "ERROR\r\nAUTOPOINT DISABLED\r\n";
}
String name = req.queryParams("_name");
String email = req.queryParams("_email");
String password = req.queryParams("_password");
String about = req.queryParams("_about");
String error = "";
// check this shit
{
if (!name.matches("^[A-Z][a-z]+ [A-Z][a-z]+$")) {
error += "NAME_CHECK_FAILED ";
}
if (!email
.matches("^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$")) {
error += "MAIL_CHECK_FAILED ";
}
if (password.length() < 8) {
error += "MIN_PASSWD_LEN=8 ";
}
if (!(password.matches(".*[0-9].*")
&& password.matches(".*[A-Z].*") && password
.matches(".*[a-z].*"))) {
error += "WEAK_PASSWD ";
}
if (error.length() > 0) {
error(error);
return "ERROR\r\n" + error;
}
}
// seems ok
String guessedAddress = guessNewPointAddress();
if (guessedAddress == null) {
error("NO_PNT_ADDRESS_SPACE");
return "ERROR\r\nNO_PNT_ADDRESS_SPACE";
}
// do point request
PointRequest pReq = new PointRequest();
pReq.setAddr(guessedAddress);
pReq.setEmail(email);
pReq.setName(name);
pReq.setPassword(password);
// save
ORMManager.get(PointRequest.class).save(pReq);
// create link
Link link = new Link();
link.setLinkAddress(guessedAddress);
link.setLinkName(name);
link.setProtocolAddress("-");
link.setProtocolPassword(password);
link.setPaketPassword(password);
ORMManager.get(Link.class).save(link);
// write echomail
{
String techArea = MainHandler.getCurrentInstance().getProperty(
"stat.area", null);
String text = guessedAddress + "," + name + "," + about;
if (techArea != null) {
Echoarea area = FtnTools.getAreaByName(techArea, null);
FtnTools.writeEchomail(area, "New HTDGPoint", text);
}
FtnTools.writeNetmail(FtnTools.getPrimaryFtnAddress(),
FtnTools.getPrimaryFtnAddress(), MainHandler
.getCurrentInstance().getInfo().getStationName(),
MainHandler.getCurrentInstance().getInfo().getSysop(),
"New HTDG point", text);
ok(text);
}
return "OK\r\n" + guessedAddress;
}

private void error(String error) {
HttpdModule.logger.l2("HDGPRError: " + error);
}

private void ok(String ok) {
HttpdModule.logger.l3("HDGPR: " + ok);
}

private String guessNewPointAddress() {
FtnAddress baseNodeAddr = FtnTools.getPrimaryFtnAddress().clone();
if (baseNodeAddr.getPoint() != 0) {
return null;
}
// from 100 to 10000
for (int i = 100; i < 10000; i++) {
baseNodeAddr.setPoint(i);
Link l = FtnTools.getLinkByFtnAddress(baseNodeAddr);
if (l == null) {
return baseNodeAddr.toString();
}
}
return null;
}
}

0 comments on commit 8f9a4e1

Please sign in to comment.