Wednesday, May 28, 2014

Add new user in Linux/Ubuntu

To add a new user using command line on an Ubuntu machine there are at least two commands that may be used: useradd and adduser

Use adduser instead of useradd if you are not totally certain useradd is the tool you want to use because:
1. useradd is a low level tool.
2. useradd will not add home directory for new user.
3. useradd will not add many other defaults for new user.
4. adduser is more user friendly (but uses useradd in backend).
5. adduser will create home directory.
6. manpages of useradd recommends use of adduser.

Use it like this:
sudo adduser <username>

and to add a system user which has no shell (cannot log in, but just run programs):
sudo adduser --system <username>
 instead of
sudo useradd <username> -s /bin/false
Using adduser, the system user a home directory will be created.

See http://askubuntu.com/questions/374870/home-directory-not-being-created and http://askubuntu.com/questions/345974/what-is-the-difference-between-adduser-and-useradd for good reasons with links to manpages.

Java Listeners and Adapters, almost anonymous functions

Java allows developers to easily add listeners to different events.
e.g. button.addMouseListener(this);
However, often you don't want to implement a listener interface for your class or add an inner class
MyClass implements MouseListener
MyClass {
    private class ListenerClass implements MouseListener {
            public void mouseClicked(MouseEvent e) {}
            public void mouseEntered(MouseEvent e) {}
            .
    }
}

With all the functions required by the interface cluttering up your code. Often you don't even need more than one or two of the functions.

Enter Adapters.
Adapters are classes which are made to match a listeners interface and only that. They do nothing when called, but the developer will override the function he needs:
button.addMouseListener( new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent e) {
    }
}


This adds flexibility to Java, almost like the anonymous functions of JavaScript.

Source: https://blogs.oracle.com/CoreJavaTechTips/entry/listeners_vs_adapters
http://docs.oracle.com/javase/tutorial/java/javaOO/innerclasses.html

Wednesday, May 7, 2014

Play2 PersistenceException

You might come across the error:
[PersistenceException: Error loading on models.Period.startYear]
in Play 2 Framework.
This is most likely caused by your database having a NULLable field which doesn't map to a nullable member in the model.
E.g.:

You database has the following fields:
ADD COLUMN `start_year` INT(11) NULL DEFAULT NULL;
ADD COLUMN `end_year` INT(11) NULL DEFAULT NULL;

 which means start_year may be (null) or an int.

Your java model has the following members:
@Entity
public class Period extends Model {
    @Id
    public Long periodId;

    @Required
    public int startYear;
   
    @Required
    public int endYear;
}

Which means that startYear may be an int, but not null.

To fix, make change the database fields to not be nullable.
Or the java model members nullable also, by using the Integer object wrapper instead of int:
@Entity
public class Period extends Model {
    @Id
    public Long periodId;

    @Required
    public Integer startYear;
   
    @Required
    public Integer endYear;
}


See: http://stackoverflow.com/questions/18405141/persistenceexception-when-i-was-using-ebean-in-play-framework

Friday, January 10, 2014

Installing Imapfilter

I installed Imapfilter to sort mails on the imap server (imap.domeneshop.no) I connect to into folder (The server does not support Sieve (and managesieve or sieverules plugins for Roundcube), which is an alternative that does not require a running program).

Here is how:

* Download imapfilter from github: https://github.com/lefcha/imapfilter/archive/master.zip
unzip -d imapfilter.master.zip
cd imapfilter-master/src
sudo apt-get install libssl-dev
sudo apt-get install lua5.1-dev
* Edit imapfilter.h, core.c, lua.c, pcre.c, and system.c and add lua5.1/ before lua.h, lualib.h, and lauxlib.h
sudo apt-get install libpcre3-dev
* Update Makefile to say -llua5.1 instead of -llua
make all
sudo make install



Errors I got:
auth.c:4:11: fatal error: openssl/hmac.h: No such file or directory

lua.c:5:17: fatal error: lua.h: No such file or directory
compilation terminated.
make: *** [lua.o] Error 1


pcre.c:7:18: fatal error: pcre.h: No such file or directory

cc -o imapfilter   auth.o buffer.o cert.o core.o file.o imapfilter.o list.o log.o lua.o memory.o misc.o namespace.o pcre.o regexp.o request.o response.o session.o signal.o socket.o system.o -lm -llua -lpcre -lssl -lcrypto
/usr/bin/ld: cannot find -llua
collect2: ld returned 1 exit status
make: *** [imapfilter] Error 1


All fixed by the above procedure.

Sunday, January 5, 2014

Roundcube Webmail with Domeneshop

I just set up Roundcube to fetch emails from my Domeneshop mail server and had some trouble getting it to work.
Under step 3 of the Roundcube Webmail Installer I was checking that send and receive email worked correctly and got errors. Here is the settings I used and the corresponding error I got:

Server: smtp.domeneshop.no
Port: 587


Error: SMTP send:  NOT OK(Connection failed: HELO was not accepted: )

Server: ssl://smtp.domeneshop.no
Port: 587


SMTP send:  NOT OK(Connection failed: Failed to connect socket: fsockopen(): unable to connect to ssl://smtp.domeneshop.no:587 (Unknown error))

Server: tls://smtp.domeneshop.no
Port: 587


SMTP send:  NOT OK(Connection failed: HELO was not accepted: )

I discovered, after reading through the options of main.inc.php, that you could set host for HELO. This is usually the mail servers domain (e.g. domeneshop.no) if you have an email address using your email servers domain (e.g. asmund@domeneshop.no). But, I have my email using my own domain so setting it to:

$rcmail_config['smtp_helo_host'] = 'herikstad.net';

Solved my problem. :)

Sources: http://www.domeneshop.no/faq.cgi?id=48
http://schoudhury.com/blog/articles/send-email-using-gmail-from-php-with-fsockopen/
http://stackoverflow.com/questions/13717225/email-sending-with-ci-using-hotmail
http://www.imc.org/ietf-smtp/mail-archive/msg05499.html

http://trac.roundcube.net/wiki/Howto_Config/Webservers
https://www.howtoforge.com/running-roundcube-0.7.1-on-nginx-lemp-on-debian-squeeze-ubuntu-11.10
http://mylinuxlife.com/setting-up-roundcube-webmail/

Tuesday, October 29, 2013

Strip HTML entities with Postable

Postable is a great little page for translating HTML code in your text into non-interpretable characters so that you can easily blog about code without parts of it disappearing because it gets interpreted as HTML tags.
Just enter the code snippet into the field. Press Make it friendly and copy/paste the result into your blog.
Great! :)

http://www.elliotswan.com/postable/

Note: It is run through PHP, so no guarantees that your text isn't stored somewhere. But then again, you're about to blog it right?

Sources: http://www.elliotswan.com/2006/03/24/allow-me-to-introduce-you-to-my-new-little-app-postable/

Linux command to take screenshot

On newer versions of KDE when you press the PrintScreen button, a dialog pops up asking you if you want to copy or save a screenshot.

You can also do screenshots from Gimp: Open Gimp, select File -> Create -> Screen Shot...

To be cool or on older versions of Linux you might want to be able to take screen shot using a command line terminal (virtual terminal, you still need to be in X Windows).

Enter the following command:
import -window root <location to put image>
e.g.
import -window root ~/screenshot.jpg

So then you can easily press Alt + F2, enter the command above. Then you won't get the terminal window in your screenshot.
On my system, once you have done so once, the next time you start entering the text it autocompletes.

Note: You need ImageMagick installed as import is included in that package.

Source: http://www.linuxforums.org/forum/newbie/10213-print-screen-available-linux.html