Member of the EVE Tweet Fleet
Aug 03

How-to: Bash Switch/Case Fall-through

By Xeross Posted in Developing, How-To's, Tech, Uncategorized Leave a Comment

To have a fallthrough case with the bash case statement you simply use ;& instead of ;; in the case you want to have fall-through behavior, so as follows:

case $VAR in
    normal)
        echo "This doesn't do fallthrough"
        ;;
    fallthrough)
        echo -n "This does"
        ;&
    fallthrough)
        echo "fall-through"
        ;;
esac
Jul 02

Control VLC Media Player through D-Bus

By Xeross Posted in How-To's, Linux, Tech Leave a Comment

I recently switched back to VLC from MPD/MPC because VLC seems to play more formats and is easier to use/set-up. However I did want to keep my global keybindings, considering I couldn’t find an option in VLC to enable them and knowing that it has a D-Bus interface I decided to figure out how it works.

After some research I found out that VLC adheres to the Media Player Remote Interfacing Specification” (or MPRIS for short) for its D-BUS commands, which can be found here. To use it you can use the command line utility to send D-Bus messages dbus-send for example:

dbus-send --print-reply --session --dest=org.mpris.MediaPlayer2.vlc /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Stop

Which returns.

method return sender=:1.50 -> dest=:1.143 reply_serial=2

And stops the audio output.

The spec lists all the available commands however as I’ll only be using the org.mpris.MediaPlayer2.Player.* commands I made a simple wrapper script:

#!/bin/sh

# Simple proxy script to interface with VLC over DBUS
# Available commands can be found at http://specifications.freedesktop.org/mpris-spec/latest/Player_Node.html

dbus-send --print-reply --session --dest=org.mpris.MediaPlayer2.vlc /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.$1

Now I can simply call vlc-proxy <command> to control VLC.

References:

Dec 16

Displaying the configured 403 error in Symfony 1.4

By Xeross Posted in Developing, How-To's, Tech Comments (1)

Today I had to figure out how to trigger a 403 error and display the appropriate error page. I wanted to use the configured 403 page that the authentication system displays on failure. So after a bit of digging I found that to display this page you call the following (From the controller):

$this->forward(sfConfig::get('sf_secure_module'), sfConfig::get('sf_secure_action'));

To set the HTTP status code to 403 you would use (Again from the controller):

$this->getContext()->getResponse()->setStatusCode(403);

This should give you a 403 response with the configured permission/access denied page.

Note: I haven’t worked with Symfony a lot so there might be a better way, yet I haven’t been able to found one, if there’s a better way, let me know in the comments!

Tagged with:
Jul 27

Specify (Envelope) From address with mailx

By Xeross Posted in How-To's, Linux, SysAdmin, Tech Leave a Comment

As I was setting up my Icinga install today I needed to modify the notification command so it had a specific from address, and it was using mailx.

So off I go onto google, etc. most of the answers said you could do something like:

mailx [args] example@domain.tld -- <sendmail options>

Turns out that that syntax doesn’t exist in the versions nowadays, luckily it turns out to be quite easy:

mailx [args] -S from="Name <example@domain.tld>"

Of course you can also only specify an email for the from parameter.

Anyway, hope this was helpful.

~Xeross

Tagged with:
Mar 12

Can’t change time/date on Xen guests

By Xeross Posted in How-To's, Linux, Tech Comments (2)

So I’ve encountered this problem with my VPS on 2host, for some reason the current time was 15 minutes behind the actual time the NTP servers were reporting.

I had NTPd running and everything but for some reason I couldn’t change the system clock in any way, it would just stay the same.

Luckily someone on the InspIRCd support channel mentioned a sysctl setting that would enable me to change the clock.

All you need to do is add

xen.independent_wallclock=1

To /etc/sysctl.conf and then reload sysctl.conf with “sysctl -p”

Something you also might need to do is append jiffies to the current clock source

echo "jiffies" >> /sys/devices/system/clocksource/clocksource0/current_clocksource

Credits/thanks go to linuxwave for the actual commands/info.

Mar 01

How-To: Fix a Dead Razer Product

By Xeross Posted in How-To's, Tech Comments (1)

So tonight was a bit hectic, I was trying to figure out how to prevent the side buttons of the mouse performing the previous/next button actions in my browser. The first thought was to update my drivers/firmware but the firmware update failed and my Razer DeathAdder died.

After searching a lot on google (I enabled keyboard mouse controls) I found out you are able to restore the functioning of the product through the following steps.

  1. Unplug the Razer product
  2. Shutdown your PC
  3. Plug the product into another USB port
  4. Reboot your PC
  5. Drivers should be automatically reinstalled again
  6. If not manually install them, they can be found in the folder the drivers software is installed in
  7. Perform the firmware update again if you want

~Xeross

Tagged with:
Dec 21

World of Warcraft Tooltips

By Xeross Posted in Gaming, How-To's, Tech, World of Warcraft Leave a Comment

Since I’ve started playing WoW again and also have also been blogging about it I added World of Warcraft Tooltips (Quests, Items, NPCs, Etc.) to my blog, there isn’t any WordPress pluging at the moment that does this (Atleast not on wordpress.org) but it’s actually pretty easy.

All you need to do is add the following html code to the section of your wordpress template.

<script src="http://static.wowhead.com/widgets/power.js"></script>

And after that any links to Items, Quests, Etc. on WoWHead will automatically have a tooltip when you hover over them.

Example: ‘Tis the Season

If you have any questions don’t hesitate to ask them.

~Xeross

Edit: This is currently not enabled, will be re-enabled in the future.

Oct 12

GIT: Change revision author’s email/name

By Xeross Posted in How-To's, Tech, Version Control Comments (8)

So recently I dived a little deeper into GIT, the best version control system in my opinion. however after making a few commits to a repository I created (For some modification to a program’s source code) I noticed it didn’t have my name on it as the author, and after 1 failed attempt of setting the name/email the their values of commits after that were even worse.

So I needed a way to change the author’s name and email on the commits, after searching a little using google I encountered this page on GitHub, however I’ve decided to write a tutorial on it anyways, we’ll use the code from GitHub though.

Ok to do this we are going to use a bash script (which is basically a  list of commands to execute) they’re work on linux and also on windows if you have msysgit. here’s the code.

git filter-branch --env-filter '

an="$GIT_AUTHOR_NAME"
am="$GIT_AUTHOR_EMAIL"
cn="$GIT_COMMITTER_NAME"
cm="$GIT_COMMITTER_EMAIL"

if [ "$GIT_COMMITTER_EMAIL" = "EMAIL_OF_COMMITER_HERE" ]
then
    cn="NEW_NAME_COMMITER"
    cm="NEW_EMAIL_COMMITER"
fi
if [ "$GIT_AUTHOR_EMAIL" = "EMAIL_OF_AUTHOR_HERE" ]
then
    an="NEW_NAME_AUTHOR"
    am="NEW_EMAIL_AUTHOR"
fi

export GIT_AUTHOR_NAME="$an"
export GIT_AUTHOR_EMAIL="$am"
export GIT_COMMITTER_NAME="$cn"
export GIT_COMMITTER_EMAIL="$cm"
'

As you can see this code uses the git filter-branch command, and everything on line 2-22 is basically the filter code. Now to replace a certain commiter or author just replace EMAIL_OF_AUTHOR/COMMITER_HERE with the email of that committer/author, and replace NEW_NAME_COMMITER/AUTHOR and NEW_EMAIL_COMMITER/AUTHOR with the new email and name you want him/her to have. You can replace multiple commiters/authors by copy-pasting the if blocks (See below for that part)

if [ "$GIT_COMMITTER_EMAIL" = "EMAIL_OF_COMMITER_HERE" ]
then
    cn="NEW_NAME_COMMITER"
    cm="NEW_EMAIL_COMMITER"
fi
if [ "$GIT_AUTHOR_EMAIL" = "EMAIL_OF_AUTHOR_HERE" ]
then
    an="NEW_NAME_AUTHOR"
    am="NEW_EMAIL_AUTHOR"
fi

You can duplicate these for as many authors/commiters you want to replace. now after you finished this document save it somewhere you can access it easily (From the command line/console), and after that go inside your git repository’s folder (Ofcourse also on the command line aka console), when in there type the following.

sh /path/to/previous/saved/script.ext

When you’ve done that a progress counter will start to run, this is the git filter-branch scanning through all your commits. After this process finishes your repo will be updated with the new name/email (use “git log” to check), now to push it to a remote repository push like normal but instead of “git push” use “git push -f”, after this is done your remote repo is also updated.

Well that’s it, hope it was useful and if you have any questions or remarks don’t hesitate to post a comment ;)

Regards, Xeross

Tagged with:
Aug 30

Decreasing bandwidth usage of my blog

By Xeross Posted in How-To's, Tech Leave a Comment

So I recently installed a syntax highlighter and by coincidence the author of that plugin also had a post about decreasing wordpress bandwidth usage. So I’ve read it and I thought let’s check how big the frontpage of my blog is when downloaded by a new visitor (The FireFox extension FireBug works great for this). A whopping 1.05MB! as my website is hosted on my server at home and my upload speed is at max 150KB total that would mean to load the pages take about 7 seconds to load.

So I went back to this persons blog post (You can find it here btw) and started reading through it again. First I tried wp-minify but as he stated this doesn’t work properly in Internet Explorer and your images wont load if your css contains relative urls, so this wasn’t going to work.

He also mentioned that his syntax highlighting plugin uses a lot of bandwidth to load all languages it supports so I made it only load the languages it needed. Dunno how did affect the bandwidth usage but it decreased page load times.

After that there was 1 last thing I had to test, enabling GZip compressed output. So after a little searching I found a WordPress plugin to enable it. I tested it, No changes at all. So I checked my apache installation and noticed mod_deflate wasn’t enabled (This is needed to enable the compression). So after enabling this I checked the size again, but it only decreased by 0.03 MB (That’s about 30kb).

So leaving that enabled I decided to check if the compression was indeed enabled(I used this page to test it). Firstly I checked the frontpage, It said compression enabled, and compressed to 23% of the original size, But this wouldn’t explain why the size still was 1.02MB. So I decided to check an image on my blog to see if that was being compressed, filled in the url hit check, and the results, not compressed. This meant that the compression was only enabled on WordPress pages.

So I decided to search on google how I could enable GZip compression for all files through a .htaccess file and found this. Though this person said don’t enable it on images and also not on flash as that might make flash stop working I ignored it and tried anyway by just adding the following code to my .htaccess file.

SetOutputFilter DEFLATE
BrowserMatch ^Mozilla/4.0[678] no-gzip
BrowserMatch bMSI[E] !no-gzip !gzip-only-text/html

After adding this I hit ctrl+f5 in my firefox (Clears cache before loading page) and checked the size again, now it was down to 163KB! that means it would load in around a second instead of 7 seconds. I’m now trying to find out how to fix WP-Minify because that would bring it down even more, and I might also try to decrease the size of the images used in the sites layout/design. I’ll report how that went after I’ve done it.

Hope this was helpful, If you have any questions or feedback don’t hesitate to comment.

Regards, Xeross

Tagged with:
Aug 30

Passing additional arguments to a callback function in Javascript

By Xeross Posted in Developing, How-To's, Tech Comments (9)

jQuery PNG LogoI was working on my CMS (Just took a brake to write this), and I tried to figure out how to pass additional arguments to a javascript callback. After some searching I found what I was looking for, I told the people in the jQuery IRC channel and someone gave me shorter and cleaner code that does the same. So let me run you through the code (Note: This is jquery but the same method would also work with normal Javascript) .

In this example I will use an argument that defines where to load the data got from an AJAX request in.

//Custom function that's used instead of the long jQuery Syntax
function loadUrl(url, destination)
{
    // Use a get request to get the page at URL
    $.get(url, function(html, status) { loadedPage(html, status, destination) });
}

// The callback function
function loadedPage(html, status, destination)
{
    // If the AJAX call failed stop the function
    if(status != "success")
        return;

    // Set the html of destination (Which is in this case a jQuery Selector) to the jquery repsonse
    $(destination).html(html);
}

If you haven’t figured out how to pass the additional arguments from the above example I’ll explain in detail, Let’s take a closer look at line 5(Formatted for readability).

$.get(url,
    function(html, status)
    {
        loadedPage(html, status, destination)
    }
);

As you can see we are using a custom defined function for the callback, What we do is make a function that has the arguments that get passed to the callback.

function(html, status)
{

}

And in that function we call our real callback function but with the additional arguments we passed to loadUrl()

function(html, status)
{
    loadedPage(html, status, destination)
}

And that’s it, this can be done with as many additional parameters as you like just add them to the call of the real callback function.

Leave a comment if you have any questions or feedback.

Regards, Xeross

Tagged with: