UNPKG

jj-cli

Version:
1 lines 4.16 kB
"use strict";const e=require("../../untils/logs"),t=require("../../untils/fs"),{post2:o,post:s,post3:a}=require("../../untils/http"),i=require("../../untils/common"),{saveDataToJson:r,readDataFromJson:n}=require("../../untils/files"),l=require("crypto");let c,h,d,u,f,g,E,C,p="ZohoCliq.Channels.CREATE,ZohoCliq.Channels.READ,ZohoCliq.Channels.UPDATE,ZohoCliq.Channels.DELETE,ZohoCliq.Bots.CREATE,ZohoCliq.Bots.READ,ZohoCliq.Bots.UPDATE,ZohoCliq.Bots.DELETE,ZohoCliq.Chats.CREATE,ZohoCliq.Chats.READ,ZohoCliq.Chats.UPDATE,ZohoCliq.Chats.DELETE,ZohoCliq.Buddies.CREATE,ZohoCliq.Buddies.READ,ZohoCliq.Buddies.UPDATE,ZohoCliq.Buddies.DELETE,ZohoCliq.Messages.READ,ZohoCliq.Attachments.READ,ZohoCliq.Webhooks.CREATE,ZohoCliq.StorageData.ALL",k="https://test.order.place/",q="CT_2230748186735020432_653935780-B1";const _=async({currentWorkingDir:t,cliBinDir:o,email:s,token:a})=>{if(h=t,c=o,!a)return void e.logError("Please provide a valid token");d=a,u=s,w("1000",$(a));const i=await n();C=i,!C&&E&&await v(E),C&&(C.refresh_token=C.refresh_token||E),C&&C.access_token||(e.logInfo("missing access token"),await m()),await y()},y=async()=>{const o=await t.findFiles(h,"",!0,!0);if(!o||0===o.length)return void e.logError("No file found");const s=o.map((e=>e.shortPath+` [${e.size}]-(${e.time})`));let a;if(s.length>1){const t=await i.askSelectList(s,"File");if(!t)return void e.logError("Your Choose Wrong!");a=o.find((e=>t.startsWith(e.shortPath)))}else a=o[0];if(a){e.logFriendly(`Your Choose:${a.shortPath}`);try{await i.runTask("sending...",(async()=>{await Z(a)}))}catch(t){e.logWarn("Failed to send file, retrying...",t)}}else e.logError("No file found")},Z=async t=>{let o=`https://cliq.zoho.com/api/v2/chats/${q}/files`;u&&(o=`https://cliq.zoho.com/api/v2/buddies/${u}/files`);try{var s;const i=await a(o,{file:{value:t.fullPath,options:{filename:t.shortPath}}},{Authorization:`Zoho-oauthtoken ${C.access_token}`});if(String(i.code).startsWith("2"))return e.logSuccess("Successfully sent file");e.logWarn("Failed to send file",(null==i||null===(s=i.message)||void 0===s?void 0:s.message)||(null==i?void 0:i.message)||"unknown error"),e.logInfo("trying refresh token..."),C.refresh_token?(await v(C.refresh_token),await Z(t)):(await m(),await Z(t))}catch(t){e.logWarn("Failed to send file, retrying...",t)}},m=async()=>{const t=`https://accounts.zoho.com/oauth/v2/auth?scope=${p}&client_id=${f}&state=${Math.floor(9e9*Math.random())+1e9}&response_type=code&redirect_uri=${k}&access_type=offline`;e.logFriendly(t);let o=await i.askGenAuthUrl("Please visit the following URL to authorize the CLIQ CLI, then enter the redirected URL:");if(!o)return void e.logError("Failed to generate auth URL");const s=o.split("code=")[1].split("&")[0];s?await A(s):e.logError("Failed to get the code from the URL")},A=async t=>{let s=`https://accounts.zoho.com/oauth/v2/token?code=${t}&grant_type=authorization_code&scope=${p}&client_id=${f}&client_secret=${g}&redirect_uri=${k}`;try{const t=await o(s);if(String(t.code).startsWith("2")){const o=JSON.parse(t&&t.data);return o.refresh_token=o.refresh_token||E,C=o,r(o),void e.logSuccess("Successfully generated access token")}e.logError("Failed to get access token",t.data)}catch(t){e.logError("Failed to get access token",t)}},v=async t=>{let s=`https://accounts.zoho.com/oauth/v2/token?refresh_token=${t}&grant_type=refresh_token&scope=${p}&client_id=${f}&client_secret=${g}&redirect_uri=${k}`;try{const a=await o(s);if(String(a.code).startsWith("2")){const o=JSON.parse(a&&a.data);return o.refresh_token=o.refresh_token||t||E,C=o,r(o),void e.logSuccess("Successfully refreshToken")}await m()}catch(t){e.logError("Failed to get access token",t)}},w=(e,t)=>{const o=t.split("");let s="",a="";s+=e;let i="ba79";s+=".",i+=o[10],s+="a5a030",a+=e,i+="fe5b1eaa83",a+=".",s+=o[29],i+=o[6],a+=o[0],i+="cb5dcf6",s+="2774cbd",i+=o[0],s+="6667e23",a+="BWFR4",s+=o[29],a+=o[10],a+="K",s+="02cfbd4329.",a+=o[6],i+="3999f",s+="7a82a245844fc",i+=o[29],s+=o[29],a+="UXWJR1VPARSCJE",a+=o[29],s+="931b2458",a+="UXZAUV",s+="31a4b",i+="bda89902ea99",f=a,g=i,s+="dae2b",E=s},$=e=>l.createHash("md5").update(e).digest("hex");module.exports={cliq:_};