This repository has been archived by the owner on Oct 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcMail.js
229 lines (203 loc) · 5.99 KB
/
cMail.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
var status = {
status:"NOT running"
}
module.exports.getStatus=function(){
return {...status,...optionD_G}
}
var optionD_G = {}
module.exports.startD=function(gmail,optionD = {
cost:1000,
message:"please play this message to deliver the message",
lnbitsKey = "",
lnbitsUrl = ""
}){
var optionD_G = optionD
status.status = "starting"
const {google} = require('googleapis');
const fs = require('fs');
var projectInfoRAW = fs.readFileSync("private/projectInfo.json");
var projectInfo = JSON.parse(projectInfoRAW)
console.log(projectInfo)
var localLND = require("./lnd.js")
var lightning = localLND.init(projectInfo.lndAddress, lnbitsKey)
var localDB = require("./db.js")
this.watchGmail(gmail,projectInfo.topicName)
localDB.initDB(db =>{
startGmailD(db)
})
function startGmailD(db){
status.status="running"
gmail.users.getProfile({userId:"me"},(err,res) =>{
if(err){
console.log(err)
} else {
if(res.data.historyId){
console.log(res.data.historyId)
listLabels(gmail,db,res.data.historyId,optionD)
}
}
})
}
/**
* Lists the labels in the user's account.
*
* @param {google.auth.OAuth2} auth An authorized OAuth2 client.
*/
function listLabels(gmail,db,latestHistory,optionD) {
gmail.users.labels.list({
userId: 'me',
}, (err, res) => {
if (err) return console.log('The API returned an error: ' + err);
const labels = res.data.labels;
if (labels.length) {
console.log('Labels:');
labels.forEach((label) => {
console.log(`- ${label.name}`);
console.log(`- ${label.id}`);
if(label.name == "Unpaid"){
optionD.unpaidID = label.id
}
if(label.name == "Paid"){
optionD.paidID = label.id
}
});
} else {
console.log('No labels found.');
}
listen(gmail,db,latestHistory,optionD)
});
//watchGmail(gmail);
}
function listen(gmail,db,latestHistory,optionD){
// Imports the Google Cloud client library
const {PubSub} = require('@google-cloud/pubsub');
// Creates a client
const pubsub = new PubSub({
projectId: projectInfo.projectId,
keyFilename: 'private/credsPubSub.json'
});
/**
* TODO(developer): Uncomment the following lines to run the sample.
*/
const subscriptionName = projectInfo.pullSubName;
const timeout = 60;
// References an existing subscription
const subscription = pubsub.subscription(subscriptionName);
// Create an event handler to handle messages
let messageCount = 0;
var lastHystoryId = latestHistory;
const messageHandler = message => {
//console.log(message);
console.log(`Received message ${message.id}:`);
console.log(`\tData: ${message.data}`);
console.log(`\tAttributes: ${message.attributes}`);
var dataOBJ = JSON.parse(message.data)
var localEmail = dataOBJ.emailAddress
var historyId = dataOBJ.historyId
console.log(`\hID: ${historyId}`);
gmail.users.history.list({
userId:"me",
startHistoryId:lastHystoryId == 0 ? historyId : lastHystoryId,
}).then(res =>{
console.log(res.data)
//TODO:ERROR res.data.historyId
lastHystoryId = res.data.historyId;
if(res.data.history){
res.data.history.forEach(element => {
console.log("entering ")
if(element.messages){
console.log("mexs: ")
element.messages.forEach(el =>{
//console.log(el)
if(el.id){
var messageId = el.id
var threadId = el.threadId
//if in the history list there is a message id, get the message
//It is not needed to read the message, we just need the id to reply to it
gmail.users.messages.get({
userId:"me",
id:el.id
}).then(resMex => {
//console.log(resMex.data)
if(resMex.data.labelIds.includes(optionD.unpaidID)){
console.log("Found Unpaid")
if(resMex.data.payload){
if(resMex.data.payload.headers){
var from
var to
var subject
resMex.data.payload.headers.forEach(head => {
//console.log(head)
if(head.name=="From"){
console.log("From: "+head.value)
from=head.value
}
if(head.name=="To"){
console.log("To: "+head.value)
to=head.value
}
if(head.name=="Subject"){
console.log("Subject: "+head.value)
subject=head.value
}
})
console.log(localEmail)
if(to.includes(localEmail)){
localDB.addMail(gmail,lightning,db,to,from,subject,messageId,threadId,optionD)
} else {
console.log("invalid receiver")
}
}
if(resMex.data.payload.parts){
resMex.data.payload.parts.forEach(part=>{
//console.log(part)
})
}
}
}
})
}
})
}
if(element.messagesAdded){
console.log("mexs added: ")
element.messagesAdded.forEach(el =>{
//console.log(el)
})
}
});
}
})
messageCount += 1;
// "Ack" (acknowledge receipt of) the message
message.ack();
};
// Listen for new messages until timeout is hit
subscription.on(`message`, messageHandler);
localLND.listenInvoice(gmail,lightning,db,optionD)
localLND.listenTransaction(gmail,lightning,db,optionD)
console.log("mhhh")
/*setTimeout(() => {
subscription.removeListener('message', messageHandler);
console.log(`${messageCount} message(s) received.`);
}, timeout * 3000);*/
}
}
module.exports.watchGmail = function watchGmail(gmail,topicName){
gmail.users.watch({
userId: 'me',
requestBody: {
// Replace with `projects/${PROJECT_ID}/topics/${TOPIC_NAME}`
topicName: topicName
},
labelIds: [
"SENT"
],
labelFilterAction:"include"
}).then(res=>{console.log(res.data);})
}
module.exports.stopGamil = function(gmail){
gmail.users.stop({
userId:'me'
})
}