quickfilter addon looks good for the job and the need to implement it yourself would maybe be obsolete:
https://services.addons.thunderbird.net/EN-us/thunderbird/addon/quickfilters/