renamed package
This commit is contained in:
@@ -0,0 +1,358 @@
|
||||
package de.tilman.callerid;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.Calendar;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.mail.Message;
|
||||
import javax.mail.Session;
|
||||
import javax.mail.Transport;
|
||||
import javax.mail.internet.InternetAddress;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.apache.log4j.BasicConfigurator;
|
||||
import org.apache.log4j.ConsoleAppender;
|
||||
import org.apache.log4j.Level;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.apache.log4j.PatternLayout;
|
||||
|
||||
import com.google.gdata.client.Query;
|
||||
import com.google.gdata.client.contacts.ContactsService;
|
||||
import com.google.gdata.data.contacts.ContactEntry;
|
||||
import com.google.gdata.data.contacts.ContactFeed;
|
||||
import com.google.gdata.data.extensions.PhoneNumber;
|
||||
import com.martiansoftware.jsap.FlaggedOption;
|
||||
import com.martiansoftware.jsap.JSAP;
|
||||
import com.martiansoftware.jsap.JSAPException;
|
||||
import com.martiansoftware.jsap.JSAPResult;
|
||||
import com.martiansoftware.jsap.Switch;
|
||||
import com.martiansoftware.jsap.UnflaggedOption;
|
||||
|
||||
/**
|
||||
* This class retrieves the list of missed calls from a Speedport W 723V (A)
|
||||
* router and sends notifications to a list of e-mail addresses.
|
||||
*
|
||||
* @author Tilman Liero tliero@gmx.net
|
||||
*/
|
||||
public class CallerIdClient {
|
||||
|
||||
private final static Logger log = Logger.getLogger(CallerIdClient.class);
|
||||
|
||||
private static final String TEXT_UNKNOWN = "unbekannt";
|
||||
private static final String TEXT_MAILPREFIX = "Anruf von ";
|
||||
|
||||
String password;
|
||||
int updateInterval;
|
||||
JSAPResult config;
|
||||
LinkedList<String[]> calls = new LinkedList<String[]>();
|
||||
String cookie = null;
|
||||
|
||||
public CallerIdClient(JSAPResult config) {
|
||||
this.config = config;
|
||||
this.password = new String(Base64.encodeBase64(config.getString("router password").getBytes()));
|
||||
this.updateInterval = config.getInt("update interval");
|
||||
|
||||
try {
|
||||
log.info("logging in to Speedport router");
|
||||
postUrl("https://speedport.ip/index/login.cgi", "Username=" + URLEncoder.encode("admin", "UTF-8") + "&" + "Password="
|
||||
+ password);
|
||||
|
||||
log.info("retrieving call list");
|
||||
getUrl("https://speedport.ip/auth/hcti_status_telanrl.php?cookie=SessionID_R3," + cookie);
|
||||
|
||||
log.info("logging out");
|
||||
postUrl("https://speedport.ip/auth/logout.cgi?RequestFile=/pub/top_beenden.php&cookie=SessionID_R3," + cookie, "");
|
||||
|
||||
if (calls.size() > 0) {
|
||||
|
||||
List<ContactEntry> contacts = null;
|
||||
if (config.getString("google username") != null) {
|
||||
log.info("retrieving Google contacts");
|
||||
ContactsService service = new ContactsService("<var>SpeedportCallerIdClient</var>");
|
||||
service.setUserCredentials(config.getString("google username"), config.getString("google password"));
|
||||
URL feedUrl = new URL("https://www.google.com/m8/feeds/contacts/default/full");
|
||||
Query myQuery = new Query(feedUrl);
|
||||
myQuery.setMaxResults(1000);
|
||||
ContactFeed resultFeed = service.query(myQuery, ContactFeed.class);
|
||||
contacts = resultFeed.getEntries();
|
||||
}
|
||||
|
||||
log.info("sending notifications");
|
||||
for (String[] call : calls) {
|
||||
String time = call[0];
|
||||
String caller = call[1];
|
||||
|
||||
if (caller.equals(" ")) {
|
||||
caller = TEXT_UNKNOWN;
|
||||
}
|
||||
else {
|
||||
numberSearch:
|
||||
for (ContactEntry contact : contacts) {
|
||||
for (PhoneNumber phone : contact.getPhoneNumbers()) {
|
||||
if (phone.getPhoneNumber().endsWith(caller.substring(1))) {
|
||||
if (contact.getName().hasFullName()) {
|
||||
caller = contact.getName().getFullName().getValue() + " (" + caller + ")";
|
||||
log.info("found number in contact data for " + contact.getName().getFullName().getValue());
|
||||
}
|
||||
break numberSearch;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sendNotification(time, caller);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
|
||||
log.info("done");
|
||||
}
|
||||
|
||||
private void getUrl(String urlString) throws Exception {
|
||||
log.debug("getUrl(" + urlString + ")");
|
||||
|
||||
URL url = new URL(urlString);
|
||||
URLConnection connection = url.openConnection();
|
||||
|
||||
showHeaders(connection);
|
||||
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
String line;
|
||||
|
||||
while ((line = in.readLine()) != null) {
|
||||
if (line.startsWith("var in_call_list")) {
|
||||
// analyze caller list
|
||||
|
||||
String s = line.substring(39);
|
||||
log.debug(" " + line);
|
||||
|
||||
String[] result = s.split("new Array\\(");
|
||||
for (int x = 0; x < result.length; x++) {
|
||||
// check for unanswered calls
|
||||
if (result[x].substring(result[x].lastIndexOf('"') - 8, result[x].lastIndexOf('"')).equals("00:00:00")) {
|
||||
log.debug("Unanswered call: " + result[x]);
|
||||
|
||||
// determine call time
|
||||
int year = Integer.parseInt(result[x].substring(7, 11));
|
||||
int month = Integer.parseInt(result[x].substring(4, 6));
|
||||
int date = Integer.parseInt(result[x].substring(1, 3));
|
||||
int hour = Integer.parseInt(result[x].substring(12, 14));
|
||||
int min = Integer.parseInt(result[x].substring(15, 17));
|
||||
int sec = Integer.parseInt(result[x].substring(18, 20));
|
||||
|
||||
Calendar callTime = Calendar.getInstance();
|
||||
callTime.set(year, month - 1, date, hour, min, sec);
|
||||
|
||||
Calendar lastCheck = Calendar.getInstance();
|
||||
lastCheck.setTimeInMillis(System.currentTimeMillis() - updateInterval * 1000 * 60);
|
||||
|
||||
if (callTime.after(lastCheck)) {
|
||||
String time = result[x].substring(1, 20);
|
||||
String number = result[x].substring(23, result[x].indexOf('"', 23));
|
||||
calls.add(new String[] { time, number });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
log.debug(" " + line);
|
||||
}
|
||||
in.close();
|
||||
|
||||
}
|
||||
|
||||
private void postUrl(String urlString, String payload) throws Exception {
|
||||
log.debug("postUrl(" + urlString + ", " + payload + ")");
|
||||
|
||||
URL url = new URL(urlString);
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setRequestMethod("POST");
|
||||
connection.setInstanceFollowRedirects(false);
|
||||
connection.setDoInput(true);
|
||||
connection.setDoOutput(true);
|
||||
connection.setUseCaches(false);
|
||||
connection.setRequestProperty("Content-Length", String.valueOf(payload.length()));
|
||||
|
||||
if (cookie != null) {
|
||||
connection.setRequestProperty("Cookie", "SessionID_R3=" + cookie);
|
||||
}
|
||||
// connection.setRequestProperty("Referer",
|
||||
// "https://speedport.ip/auth/hcti_status_telanrl.php?cookie=SessionID_R3,"
|
||||
// + cookie);
|
||||
|
||||
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
|
||||
if (payload.length() > 0) {
|
||||
writer.write(payload);
|
||||
}
|
||||
writer.flush();
|
||||
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
|
||||
for (String line; (line = reader.readLine()) != null;) {
|
||||
if (line.startsWith("SessionID_R3")) {
|
||||
cookie = line.substring(13);
|
||||
} else {
|
||||
if (line.equals("4"))
|
||||
log.error(" could not log in, Speedport adminstration account is currently in use");
|
||||
else
|
||||
log.error(" received unexpected response: " + line);
|
||||
}
|
||||
}
|
||||
|
||||
writer.close();
|
||||
reader.close();
|
||||
|
||||
showHeaders(connection);
|
||||
}
|
||||
|
||||
private void showHeaders(URLConnection connection) {
|
||||
for (int i = 0;; i++) {
|
||||
String headerName = connection.getHeaderFieldKey(i);
|
||||
String headerValue = connection.getHeaderField(i);
|
||||
|
||||
if (headerName == null && headerValue == null) {
|
||||
// No more headers
|
||||
break;
|
||||
}
|
||||
if (headerName == null) {
|
||||
// The header value contains the server's HTTP version
|
||||
log.debug(" Server HTTP version, Response code: " + headerValue);
|
||||
} else {
|
||||
log.debug(" Header: " + headerName + ", Value: " + headerValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void sendNotification(String time, String caller) throws Exception {
|
||||
String host = config.getString("SMTP server address");
|
||||
String from = config.getString("e-mail adress");
|
||||
String pass = config.getString("e-mail password");
|
||||
Properties props = System.getProperties();
|
||||
props.put("mail.smtp.ssl.enable", "true");
|
||||
// props.put("mail.smtp.starttls.enable", "true");
|
||||
props.put("mail.smtp.host", host);
|
||||
props.put("mail.smtp.user", from);
|
||||
props.put("mail.smtp.password", pass);
|
||||
props.put("mail.smtp.port", "465");
|
||||
// props.put("mail.smtp.port", "587");
|
||||
props.put("mail.smtp.auth", "true");
|
||||
|
||||
String[] to = config.getStringArray("recipient");
|
||||
|
||||
Session session = Session.getDefaultInstance(props, null);
|
||||
MimeMessage message = new MimeMessage(session);
|
||||
message.setFrom(new InternetAddress(from));
|
||||
|
||||
InternetAddress[] toAddress = new InternetAddress[to.length];
|
||||
|
||||
for (int i = 0; i < to.length; i++) {
|
||||
toAddress[i] = new InternetAddress(to[i]);
|
||||
}
|
||||
|
||||
for (int i = 0; i < toAddress.length; i++) {
|
||||
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
|
||||
}
|
||||
|
||||
message.setSubject(TEXT_MAILPREFIX + caller);
|
||||
message.setText(TEXT_MAILPREFIX + caller + "\n" + time);
|
||||
|
||||
log.info("sending message for call from " + caller + " at " + time);
|
||||
|
||||
Transport transport = session.getTransport("smtp");
|
||||
transport.connect(host, from, pass);
|
||||
transport.sendMessage(message, message.getAllRecipients());
|
||||
transport.close();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
BasicConfigurator.configure(new ConsoleAppender(new PatternLayout("%d{ISO8601} - %m%n")));
|
||||
log.info("Starting CallerIdClient version 1.0.2");
|
||||
|
||||
JSAP jsap = new JSAP();
|
||||
|
||||
try {
|
||||
UnflaggedOption routerPassword = new UnflaggedOption("router password").setStringParser(JSAP.STRING_PARSER)
|
||||
.setRequired(true);
|
||||
routerPassword.setHelp("the login password for the Speedport W 723V (A) router");
|
||||
jsap.registerParameter(routerPassword);
|
||||
|
||||
UnflaggedOption updateInterval = new UnflaggedOption("update interval").setStringParser(JSAP.INTEGER_PARSER)
|
||||
.setRequired(true);
|
||||
updateInterval.setHelp("the interval beetween runs in minutes");
|
||||
jsap.registerParameter(updateInterval);
|
||||
|
||||
UnflaggedOption smtpServer = new UnflaggedOption("SMTP server address").setStringParser(JSAP.STRING_PARSER)
|
||||
.setRequired(true);
|
||||
smtpServer.setHelp("the SMTP server to send notifications with");
|
||||
jsap.registerParameter(smtpServer);
|
||||
|
||||
UnflaggedOption fromAddress = new UnflaggedOption("e-mail adress").setStringParser(JSAP.STRING_PARSER).setRequired(
|
||||
true);
|
||||
fromAddress.setHelp("e-mail address to send from");
|
||||
jsap.registerParameter(fromAddress);
|
||||
|
||||
UnflaggedOption emailPassword = new UnflaggedOption("e-mail password").setStringParser(JSAP.STRING_PARSER)
|
||||
.setRequired(true);
|
||||
emailPassword.setHelp("the login password for the e-mail account");
|
||||
jsap.registerParameter(emailPassword);
|
||||
|
||||
UnflaggedOption recipients = new UnflaggedOption("recipient").setStringParser(JSAP.STRING_PARSER).setRequired(true)
|
||||
.setGreedy(true);
|
||||
recipients.setHelp("e-mail addresses to send notifications to");
|
||||
jsap.registerParameter(recipients);
|
||||
|
||||
FlaggedOption googleUsername = new FlaggedOption("google username").setStringParser(JSAP.STRING_PARSER).setShortFlag(
|
||||
'u');
|
||||
googleUsername.setHelp("the Google username for contact name resolution");
|
||||
jsap.registerParameter(googleUsername);
|
||||
|
||||
FlaggedOption googlePassword = new FlaggedOption("google password").setStringParser(JSAP.STRING_PARSER).setShortFlag(
|
||||
'p');
|
||||
googleUsername.setHelp("the Google password for contact name resolution");
|
||||
jsap.registerParameter(googlePassword);
|
||||
|
||||
Switch debugSwitch = new Switch("debug").setLongFlag("debug");
|
||||
debugSwitch.setHelp("print debug messages");
|
||||
jsap.registerParameter(debugSwitch);
|
||||
|
||||
Switch helpSwitch = new Switch("help").setLongFlag("help").setShortFlag('?');
|
||||
helpSwitch.setHelp("print help and exit");
|
||||
jsap.registerParameter(helpSwitch);
|
||||
|
||||
} catch (JSAPException e) {
|
||||
log.fatal(e.getMessage(), e);
|
||||
System.exit(-1001);
|
||||
}
|
||||
|
||||
JSAPResult config = jsap.parse(args);
|
||||
|
||||
if (!config.success() || config.getBoolean("help")) {
|
||||
for (java.util.Iterator errs = config.getErrorMessageIterator(); errs.hasNext();) {
|
||||
log.error("Error: " + errs.next());
|
||||
}
|
||||
|
||||
log.error("Usage: java -jar speedportcallerid.jar " + jsap.getUsage() + "\n\n" + jsap.getHelp());
|
||||
System.exit(-1002);
|
||||
}
|
||||
|
||||
if (config.getBoolean("debug")) {
|
||||
log.setLevel(Level.DEBUG);
|
||||
} else {
|
||||
log.setLevel(Level.INFO);
|
||||
}
|
||||
log.info("Log level set to " + log.getLevel());
|
||||
|
||||
new CallerIdClient(config);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package de.tilman.callerid;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
|
||||
import com.google.gdata.client.Query;
|
||||
import com.google.gdata.client.contacts.ContactsService;
|
||||
import com.google.gdata.data.contacts.ContactEntry;
|
||||
import com.google.gdata.data.contacts.ContactFeed;
|
||||
import com.google.gdata.data.extensions.Email;
|
||||
import com.google.gdata.data.extensions.Name;
|
||||
import com.google.gdata.data.extensions.PhoneNumber;
|
||||
import com.google.gdata.util.ServiceException;
|
||||
|
||||
public class GoogleTest {
|
||||
|
||||
private final ContactsService service;
|
||||
|
||||
public GoogleTest() {
|
||||
service = new ContactsService("<var>SpeedportCallerIdClient</var>");
|
||||
try {
|
||||
service.setUserCredentials("tilman.liero@gmail.com", "Gr4m80tt");
|
||||
printAllContacts(service);
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public String getContactName(String telephoneNumber) {
|
||||
return "";
|
||||
}
|
||||
|
||||
public static void printAllContacts(ContactsService myService) throws ServiceException, IOException {
|
||||
// Request the feed
|
||||
URL feedUrl = new URL("https://www.google.com/m8/feeds/contacts/default/full");
|
||||
// ContactFeed resultFeed = myService.getFeed(feedUrl, ContactFeed.class);
|
||||
|
||||
Query myQuery = new Query(feedUrl);
|
||||
myQuery.setMaxResults(500);
|
||||
ContactFeed resultFeed = myService.query(myQuery, ContactFeed.class);
|
||||
|
||||
// Print the results
|
||||
System.out.println(resultFeed.getTitle().getPlainText());
|
||||
int i = 0;
|
||||
for (ContactEntry entry : resultFeed.getEntries()) {
|
||||
System.out.println("Nr. " + ++i);
|
||||
if (entry.hasName()) {
|
||||
Name name = entry.getName();
|
||||
if (name.hasFullName()) {
|
||||
String fullNameToDisplay = name.getFullName().getValue();
|
||||
if (name.getFullName().hasYomi()) {
|
||||
fullNameToDisplay += " (" + name.getFullName().getYomi() + ")";
|
||||
}
|
||||
System.out.println("\\\t\\\t" + fullNameToDisplay);
|
||||
}
|
||||
} else {
|
||||
System.out.println("\t (no name found)");
|
||||
}
|
||||
System.out.println("Email addresses:");
|
||||
for (Email email : entry.getEmailAddresses()) {
|
||||
System.out.print(" " + email.getAddress());
|
||||
if (email.getRel() != null) {
|
||||
System.out.print(" rel:" + email.getRel());
|
||||
}
|
||||
if (email.getLabel() != null) {
|
||||
System.out.print(" label:" + email.getLabel());
|
||||
}
|
||||
if (email.getPrimary()) {
|
||||
System.out.print(" (primary) ");
|
||||
}
|
||||
System.out.print("\n");
|
||||
}
|
||||
System.out.println("Telephone Numbers:");
|
||||
for (PhoneNumber phone : entry.getPhoneNumbers()) {
|
||||
System.out.print(" " + phone.getPhoneNumber());
|
||||
if (phone.getUri() != null) {
|
||||
System.out.print(" uri:" + phone.getUri());
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
new GoogleTest();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user