@author Manjago (kirill@temnenkov.com)
| 33 | * @author Manjago (kirill@temnenkov.com) |
| 34 | */ |
| 35 | public abstract class AbstractRobot implements IRobot { |
| 36 | protected static final String YOU_ARE_NOT_IN_LINKS_OF_ORIGIN = "You are not in links of origin"; |
| 37 | protected static final String ACCESS_DENIED = "Access denied"; |
| 38 | protected static final String YOU_ARE_NOT_WELCOME = "You are not welcome"; |
| 39 | protected static final String WRONG_PASSWORD = "Wrong password"; |
| 40 | protected static final String SORRY_0_IS_OFF_FOR_YOU = "Sorry, {0} is off for you"; |
| 41 | protected static final String WRONG_ASLINK = "%ASLINK command with wrong arg!"; |
| 42 | protected static final String WRONG_PATH = "This message was sent via {0}. {1} works with direct mail only. Also you have to change password :-)"; |
| 43 | protected static final Pattern aslink = Pattern |
| 44 | .compile( |
| 45 | "^%ASLINK ((\\d)?:?(\\d{1,5})/(\\d{1,5})\\.?(\\d{1,5})?@?(\\S+)?)$", |
| 46 | Pattern.CASE_INSENSITIVE | Pattern.MULTILINE); |
| 47 | |
| 48 | protected static final Pattern HELP = Pattern.compile("^%HELP$", |
| 49 | Pattern.CASE_INSENSITIVE); |
| 50 | |
| 51 | protected abstract String getRobotName(); |
| 52 | |
| 53 | protected abstract boolean isEnabled(Link link); |
| 54 | |
| 55 | protected abstract String getPasswordOption(); |
| 56 | |
| 57 | protected Link getAndCheckLink(FtnMessage fmsg) { |
| 58 | if (fmsg.getPkt() != null) { |
| 59 | if (!fmsg.getPkt().getFromAddr().equals(fmsg.getFromAddr())) { |
| 60 | FtnTools.writeReply( |
| 61 | fmsg, |
| 62 | ACCESS_DENIED, |
| 63 | MessageFormat.format(WRONG_PATH, fmsg.getPkt() |
| 64 | .getFromAddr().toString(), getRobotName())); |
| 65 | return null; |
| 66 | } |
| 67 | } |
| 68 | FtnAddress linkAddress = fmsg.getFromAddr(); |
| 69 | Matcher m = aslink.matcher(fmsg.getText()); |
| 70 | if (m.find()) { |
| 71 | try { |
| 72 | linkAddress = new FtnAddress(m.group(1)); |
| 73 | } catch (NumberFormatException e) { |
| 74 | FtnTools.writeReply(fmsg, ACCESS_DENIED, WRONG_ASLINK); |
| 75 | return null; |
| 76 | } |
| 77 | } |
| 78 | Link link = FtnTools.getLinkByFtnAddress(linkAddress); |
| 79 | if (link == null) { |
| 80 | FtnTools.writeReply(fmsg, ACCESS_DENIED, |
| 81 | YOU_ARE_NOT_IN_LINKS_OF_ORIGIN); |
| 82 | return null; |
| 83 | } |
| 84 | if (!isEnabled(link)) { |
| 85 | FtnTools.writeReply(fmsg, YOU_ARE_NOT_WELCOME, MessageFormat |
| 86 | .format(SORRY_0_IS_OFF_FOR_YOU, getRobotName())); |
| 87 | return null; |
| 88 | } |
| 89 | String password = getPassword(link); |
| 90 | if (!password.equals(fmsg.getSubject())) { |
| 91 | FtnTools.writeReply(fmsg, ACCESS_DENIED, WRONG_PASSWORD); |
| 92 | return null; |
nothing calls this directly
no outgoing calls
no test coverage detected