1
1
package drivers
2
2
3
3
import (
4
+ "encoding/hex"
4
5
"encoding/json"
5
6
"fmt"
6
7
"io"
@@ -14,6 +15,7 @@ import (
14
15
cfg "github.com/brokenCursor/usb-modem-cli/config"
15
16
"github.com/spf13/viper"
16
17
"github.com/warthog618/sms/encoding/gsm7"
18
+ "github.com/warthog618/sms/encoding/ucs2"
17
19
)
18
20
19
21
// DO NOT USE DIRECTLY
@@ -31,6 +33,17 @@ type (
31
33
pppConnected struct {
32
34
Connected string `json:"ppp_status"`
33
35
}
36
+
37
+ rawIncomingSMS struct {
38
+ Messages []struct {
39
+ ID string `json:"id"`
40
+ Source string `json:"number"`
41
+ Content string `json:"content"`
42
+ Tag string `json:"tag"`
43
+ Date string `json:"date"`
44
+ DraftGroupID string `json:"draft_group_id"`
45
+ } `json:"messages"`
46
+ }
34
47
)
35
48
36
49
func init () {
@@ -227,14 +240,8 @@ func (m *zte8810ft) GetCellConnStatus() (*LinkStatus, error) {
227
240
}
228
241
229
242
func (m * zte8810ft ) SendSMS (phone string , message string ) error {
230
- // GET /goform/goform_set_cmd_process?goformId=SEND_SMS
231
- // Prepare everything to make a request
232
-
233
243
// Encode message into GSM-7 as byte array
234
244
encodedMsg , err := gsm7 .Encode ([]byte (message ))
235
- switch err {
236
-
237
- }
238
245
if err != nil {
239
246
return ActionError {Action : "sms send" , Err : err }
240
247
}
@@ -304,3 +311,79 @@ func (m *zte8810ft) SendSMS(phone string, message string) error {
304
311
305
312
return nil
306
313
}
314
+
315
+ func (m * zte8810ft ) ReadAllSMS () ([]SMS , error ) {
316
+ // http://10.96.170.1/goform/goform_get_cmd_process?cmd=sms_data_total&page=0&data_per_page=100&mem_store=1&tags=12&order_by=order+by+id+desc&_=1724578532798
317
+ // Build query
318
+ u := m .getBaseURL ("/goform/goform_get_cmd_process" )
319
+ query := u .Query ()
320
+ query .Add ("cmd" , "sms_data_total" )
321
+ query .Add ("page" , "0" )
322
+ query .Add ("data_per_page" , "100" )
323
+ query .Add ("mem_store" , "1" )
324
+ query .Add ("tags" , "12" ) // 1 - Received, unread | 2 - Sent | 12 - Received, read + unread | 11 - Drafts (??)
325
+ query .Add ("order_by" , "order by id desc" )
326
+ query .Add ("_" , strconv .FormatInt ((time .Now ().UnixMilli )(), 10 ))
327
+ u .RawQuery = query .Encode ()
328
+
329
+ request , err := m .getNewRequest ("GET" , u , http.Header {}, nil )
330
+ if err != nil {
331
+ return nil , ActionError {Action : "sms read" , Err : err }
332
+ }
333
+ m .logger .With ("request" , request .URL .String ()).Debug ("request" )
334
+
335
+ resp , err := m .httpClient .Do (request )
336
+
337
+ // Process errors
338
+ switch {
339
+ case err != nil :
340
+ return nil , ActionError {Action : "status" , Err : err }
341
+ case resp .StatusCode != 200 :
342
+ return nil , ActionError {Action : "status" , Err : fmt .Errorf ("response status %d" , resp .StatusCode )}
343
+ }
344
+
345
+ // Read the response
346
+ defer resp .Body .Close ()
347
+ body , err := io .ReadAll (resp .Body )
348
+ if err != nil {
349
+ return nil , ErrUnknown
350
+ }
351
+
352
+ rawSMS := new (rawIncomingSMS )
353
+
354
+ err = json .Unmarshal (body , & rawSMS )
355
+ if err != nil {
356
+ m .logger .With ("body" , body ).Debug ("failed to unmarshal body to struct" )
357
+ return nil , ActionError {Action : "sms read" , Err : err }
358
+ }
359
+
360
+ processedSMS := make ([]SMS , len (rawSMS .Messages ))
361
+ for i := range rawSMS .Messages {
362
+ processedSMS [i ].Sender = rawSMS .Messages [i ].Source
363
+
364
+ // Extract datetime
365
+ date , err := time .Parse ("06,01,02,15,04,05,-07" , rawSMS .Messages [i ].Date )
366
+ if err != nil {
367
+ m .logger .With ("id" , rawSMS .Messages [i ].ID , "raw_date" , rawSMS .Messages [i ].Date , "err" , err ).Debug ("failed to parse datetime" )
368
+ return nil , ActionError {Action : "sms read" , Err : fmt .Errorf ("failed to decode message datetime" )}
369
+ }
370
+ processedSMS [i ].Time = date
371
+
372
+ // Extract contents
373
+ rawBytes , err := hex .DecodeString (rawSMS .Messages [i ].Content )
374
+ if err != nil {
375
+ m .logger .With ("id" , rawSMS .Messages [i ].ID , "raw_content" , rawSMS .Messages [i ].Content ).Debug ("failed to parse content" )
376
+ return nil , ActionError {Action : "sms read" , Err : fmt .Errorf ("failed to parse message content" )}
377
+ }
378
+
379
+ runes , err := ucs2 .Decode (rawBytes )
380
+ if err != nil {
381
+ m .logger .With ("id" , rawSMS .Messages [i ].ID , "raw_content" , rawSMS .Messages [i ].Content ).Debug ("failed to decode content" )
382
+ return nil , ActionError {Action : "sms read" , Err : fmt .Errorf ("failed to decode message content" )}
383
+ }
384
+
385
+ processedSMS [i ].Message = string (runes )
386
+ }
387
+
388
+ return processedSMS , nil
389
+ }
0 commit comments