How to create a Thunderbird native extension?

275 views Asked by At

I need to write an extension for Thunderbird. The extension will be used to do some text mining and relies on native C++ code. From my understanding, Thunderbird extensions are now mostly written in JavaScript and XPCOM is being slowly deprecated (https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM).

Besides, XPCOM seems a bit heavy and I would like an easier path to access my C++ code. Are there any alternatives besides XPCOM to access C++ code from a thunderbird extension?

Thanks!

1

There are 1 answers

0
guderkar On

Take a look at js-ctypes (https://developer.mozilla.org/en-US/docs/Mozilla/js-ctypes)

Little example:

Components.utils.import("resource://gre/modules/FileUtils.jsm");
Components.utils.import("resource://gre/modules/ctypes.jsm")

// path to C++ lib (/home/username/.thunderbird/PROFILE/extensions/EXTNAME/components/lib.so)
var libPath = FileUtils.getFile("ProfD", ["extensions", "EXTNAME", "components", "lib.so"]);

var lib = ctypes.open(libPath.path);
var libFunction = lib.declare("concatStrings", // function name in C++ code
                              ctypes.default_abi,
                              ctypes.char.ptr, // return value
                              ctypes.char.ptr, // param1
                              ctypes.char.ptr // param2
);
var ret = libFunction("abc", "efg");
lib.close()

Also be aware that C++ compiler does name mangling due to function overloading so your function name might be 'concatStrings' in C++ code, but then in assembly it might be something like '_123concatStrings'. To prevent this declare your function like:

extern "C" const char * concatStrings ( const char * str1, const char * str2 );