Skip to content

Instantly share code, notes, and snippets.

@gsilano
Created May 10, 2020 08:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gsilano/a3db651cf8fdc0a6046486dec6a11fd5 to your computer and use it in GitHub Desktop.
Save gsilano/a3db651cf8fdc0a6046486dec6a11fd5 to your computer and use it in GitHub Desktop.
X-notifier script to get the number of unread messages in mailboxes of a Roundcube account. For more details, see https://github.com/shemminga/small-hacks
/**********************************************************
Roundcube
**********************************************************/
var name="Roundcube (all mailboxes)";
var ver="2018-06-06";
var needServer=true;
function init(){
this.initStage=ST_PRE;
if(this.server){
if(this.server.indexOf("http")!=0)this.server="http://"+this.server;
if(this.server.charAt(this.server.length-1)!="/")this.server+="/";
}
this.loginData=[this.server,"_user", "_pass","_task=mail&_action=login"];
this.dataURL=this.server+"?_task=mail&_action=getunread&&_remote=1&_unlock=0&_=";
this.updateURL=this.server+"?_task=mail&_action=check-recent&_mbox=INBOX&_refresh=1&_remote=1&_unlock=0&_=";
this.viewURL=this.server;
}
function getCount(aData) {
var fnd=aData.match(/"action":"getunread"/);
if(fnd) {
var cnts = {};
var mailboxes = aData.match(/this\.set_unread_count\(\\"[^\\"]+\\",\d+/g);
if (mailboxes != null) {
for(var i = 0; i < mailboxes.length; i++) {
var mboxCnt = mailboxes[i].match(/this\.set_unread_count\(\\"([^\\"]+)\\",(\d+)/);
if(mboxCnt != null) {
cnts[mboxCnt[1]] = parseInt(mboxCnt[2], 10);
}
}
}
return Object.values(cnts).reduce((acc, val) => acc + val);
}
return -1;
}
function process(aData,aHttp){
switch(this.stage){
case ST_PRE:
this.getHtml(this.viewURL);
return false;
case ST_PRE_RES:
var fnd=aData.match(/"_token"\s+value="(\S+?)"/);
if(fnd){
this.stage=ST_LOGIN;
this.getHtml(this.loginData[LOGIN_URL],this.loginData[LOGIN_POST]+"&_token="+fnd[1]);
return false;
}
break;
case ST_LOGIN_RES:
var fnd=aData.match(/"request_token":"(\S+?)"/);
if(fnd){
this.stage=ST_DATA;
this.token=fnd[1];
}else break;
case ST_DATA:
this.getHtml(this.updateURL+(new Date().getTime()),null,{"X-Requested-With":"XMLHttpRequest","X-Roundcube-Request":this.token});
this.stage=ST_DATA+2;
return true;
case (ST_DATA+2):
var fnd=aData.match(/_task=login&_err=session/);
if(fnd){
this.reset(false);
return this.process("",aHttp);
}
this.getHtml(this.dataURL+(new Date().getTime()),null,{"X-Requested-With":"XMLHttpRequest","X-Roundcube-Request":this.token});
this.stage=ST_DATA;
return false;
}
return this.baseProcess(aData,aHttp);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment