aboutsummaryrefslogtreecommitdiff
path: root/outboxPost.go
blob: 5772932ecaebac9441af281f57fc717e3ac3b157 (plain)
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
package main

import "fmt"
import "net/http"
import "database/sql"
import _ "github.com/lib/pq"
import "encoding/json"
import "reflect"
import "io/ioutil"
import "os"
import "regexp"
import "strings"
import "os/exec"

func ParseOutboxRequest(w http.ResponseWriter, r *http.Request, db *sql.DB) {
	
	var activity Activity

	actor := GetActorFromPath(db, r.URL.Path, "/")
	contentType := GetContentType(r.Header.Get("content-type"))

	defer r.Body.Close()
	if contentType == "multipart/form-data" || contentType == "application/x-www-form-urlencoded" {
		r.ParseMultipartForm(5 << 20)		
		if(BoardHasAuthType(db, actor.Name, "captcha") && CheckCaptcha(db, r.FormValue("captcha"))) {		
			f, header, _ := r.FormFile("file")
			if(header != nil) {
				if(header.Size > (7 << 20)){
					w.WriteHeader(http.StatusRequestEntityTooLarge)
					w.Write([]byte("7MB max file size"))
					return
				}
				
				contentType, _ := GetFileContentType(f)

				if(!SupportedMIMEType(contentType)) {
					w.WriteHeader(http.StatusNotAcceptable)
					w.Write([]byte("file type not supported"))
					return
				}
			}

			var nObj = CreateObject("Note")
			nObj = ObjectFromForm(r, db, nObj)
			
			nObj.Actor = Domain + "/" + actor.Name

			nObj = WriteObjectToDB(db, nObj)
			activity := CreateActivity("Create", nObj)
			activity = AddFollowersToActivity(db, activity)
			MakeActivityRequest(db, activity)

			var id string
			op := len(nObj.InReplyTo) - 1
			if op >= 0 {		
				if nObj.InReplyTo[op].Id == "" {
					id = nObj.Id  
				} else {
					id = nObj.InReplyTo[0].Id + "|" + nObj.Id
				}
			}

			w.WriteHeader(http.StatusOK)
			w.Write([]byte(id))
			return
		}

		w.WriteHeader(http.StatusForbidden)
		w.Write([]byte("captcha could not auth"))
	} else {
		activity = GetActivityFromJson(r, db)
		if IsActivityLocal(db, activity) {
			if !VerifyHeaderSignature(r, *activity.Actor) {
				w.WriteHeader(http.StatusBadRequest)										
				w.Write([]byte(""))					
				return
			}
			
			switch activity.Type {
			case "Create":
				w.WriteHeader(http.StatusBadRequest)
				w.Write([]byte(""))
				break
				
			case "Follow":
				var validActor bool
				var validLocalActor bool

				validActor = (activity.Object.Actor != "")
				validLocalActor = (activity.Actor.Id == actor.Id)

				var rActivity Activity
				if validActor && validLocalActor {
					rActivity = AcceptFollow(activity)
					SetActorFollowingDB(db, rActivity)
					MakeActivityRequest(db, activity)
				}

				FollowingBoards = GetActorFollowingDB(db, Domain)
				Boards = GetBoardCollection(db)
				break
				
			case "Delete":
				fmt.Println("This is a delete")
				w.WriteHeader(http.StatusBadRequest)
				w.Write([]byte("could not process activity"))										
				break
				
			case "Note":
				w.WriteHeader(http.StatusBadRequest)
				w.Write([]byte("could not process activity"))
				break

			case "New":
				name := activity.Object.Alias
				prefname := activity.Object.Name
				summary := activity.Object.Summary
				restricted := activity.Object.Sensitive

				actor := CreateNewBoardDB(db, *CreateNewActor(name, prefname, summary, authReq, restricted))
				
				if actor.Id != "" {
					var board []ObjectBase
					var item ObjectBase			
					var removed bool = false

					item.Id = actor.Id
					for _, e := range FollowingBoards {
						if e.Id != item.Id {
							board = append(board, e)
						} else {
							removed = true
						}
					}

					if !removed {
						board = append(board, item)
					}

					FollowingBoards = board
					Boards = GetBoardCollection(db)
					return
				}
				
				w.WriteHeader(http.StatusBadRequest)
				w.Write([]byte(""))
				break
				
			default:
				w.WriteHeader(http.StatusBadRequest)
				w.Write([]byte("could not process activity"))			
			}
		} else {
			fmt.Println("is NOT activity")		
			w.WriteHeader(http.StatusBadRequest)
			w.Write([]byte("could not process activity"))			
		}
	}
}

func ObjectFromJson(r *http.Request, obj ObjectBase) ObjectBase {
	body, _ := ioutil.ReadAll(r.Body)

	var respActivity ActivityRaw

	err := json.Unmarshal(body, &respActivity)

	CheckError(err, "error with object from json")

	if HasContextFromJson(respActivity.AtContextRaw.Context) {
		var jObj ObjectBase
		jObj = GetObjectFromJson(respActivity.ObjectRaw)
		jObj.To = GetToFromJson(respActivity.ToRaw)
		jObj.Cc = GetToFromJson(respActivity.CcRaw)
	}
	
	return obj
}

func GetObjectFromJson(obj []byte) ObjectBase {
	var generic interface{}

	err := json.Unmarshal(obj, &generic)

	CheckError(err, "error with getting obj from json")

	t := reflect.TypeOf(generic)

	var nObj ObjectBase
	if t != nil {
		switch t.String() {
		case "[]interface {}":
			var lObj ObjectBase		
			var arrContext ObjectArray
			err = json.Unmarshal(obj, &arrContext.Object)
			CheckError(err, "error with []interface{} oject from json")
			if len(arrContext.Object) > 0 {
				lObj = arrContext.Object[0]
			}
			nObj = lObj
			break

		case "map[string]interface {}":
			var arrContext Object
			err = json.Unmarshal(obj, &arrContext.Object)
			CheckError(err, "error with object from json")
			nObj = *arrContext.Object
			break
			
		case "string":
			var lObj ObjectBase
			var arrContext ObjectString
			err = json.Unmarshal(obj, &arrContext.Object)
			CheckError(err, "error with string object from json")
			lObj.Id = arrContext.Object
			nObj = lObj
			break
		}
	}

	return nObj
}

func GetActorFromJson(actor []byte) Actor{
	var generic interface{}
	var nActor Actor		
	err := json.Unmarshal(actor, &generic)

	if err != nil {
		return nActor
	}

	t := reflect.TypeOf(generic)
	if t != nil {
		switch t.String() {
		case "map[string]interface {}":
			err = json.Unmarshal(actor, &nActor)
			CheckError(err, "error with To []interface{}")
			
		case "string":
			var str string
			err = json.Unmarshal(actor, &str)
			CheckError(err, "error with To string")
			nActor.Id = str
		}
		
		return nActor		
	}
	
	return nActor
}

func GetToFromJson(to []byte) []string {
	var generic interface{}

	err := json.Unmarshal(to, &generic)

	if err != nil {
		return nil
	}

	t := reflect.TypeOf(generic)

	if t != nil {
		var nStr []string		
		switch t.String() {
		case "[]interface {}":
			err = json.Unmarshal(to, &nStr)
			CheckError(err, "error with To []interface{}")
			return nStr
			
		case "string":
			var str string
			err = json.Unmarshal(to, &str)
			CheckError(err, "error with To string")
			nStr = append(nStr, str)
			return nStr			
		}
	}
	
	return nil
}

func HasContextFromJson(context []byte) bool {
	var generic interface{}

	err := json.Unmarshal(context, &generic)

	CheckError(err, "error with getting context")

	t := reflect.TypeOf(generic)

	hasContext := false

	switch t.String() {
	case "[]interface {}":
		var arrContext AtContextArray
		err = json.Unmarshal(context, &arrContext.Context)
		CheckError(err, "error with []interface{}")
		if len(arrContext.Context) > 0 {
			if arrContext.Context[0] == "https://www.w3.org/ns/activitystreams" {
				hasContext = true
			}
		}
	case "string":
		var arrContext AtContextString
		err = json.Unmarshal(context, &arrContext.Context)
		CheckError(err, "error with string")
		if arrContext.Context == "https://www.w3.org/ns/activitystreams" {
			hasContext = true
		}
	}
	
	return hasContext
}

func ObjectFromForm(r *http.Request, db *sql.DB, obj ObjectBase) ObjectBase {
	
	file, header, _ := r.FormFile("file")
	
	if file != nil {
		defer file.Close()

		var tempFile = new(os.File)
		obj.Attachment, tempFile = CreateAttachmentObject(file, header)

		defer tempFile.Close();

		fileBytes, _ := ioutil.ReadAll(file)

		tempFile.Write(fileBytes)

		re := regexp.MustCompile(`image/(jpe?g|png|webp)`)
		if re.MatchString(obj.Attachment[0].MediaType) {
			fileLoc := strings.ReplaceAll(obj.Attachment[0].Href, Domain, "")

			cmd := exec.Command("exiv2", "rm", "." + fileLoc)

			err := cmd.Run()

			CheckError(err, "error with removing exif data from image")

		}

		obj.Preview = CreatePreviewObject(obj.Attachment[0])
	}

	obj.AttributedTo = EscapeString(r.FormValue("name"))
	obj.TripCode = EscapeString(r.FormValue("tripcode"))
	obj.Name = EscapeString(r.FormValue("subject"))
	obj.Content = EscapeString(r.FormValue("comment"))
	obj.Sensitive = (r.FormValue("sensitive") != "")

	obj = ParseOptions(r, obj)

	var originalPost ObjectBase
	originalPost.Id = EscapeString(r.FormValue("inReplyTo"))

	obj.InReplyTo = append(obj.InReplyTo, originalPost)

	var activity Activity

	if !IsInStringArray(activity.To, originalPost.Id) {
		activity.To = append(activity.To, originalPost.Id)
	}	

	if originalPost.Id != "" {
		if !IsActivityLocal(db, activity) {
			id := FingerActor(originalPost.Id).Id
			actor := GetActor(id)
			if !IsInStringArray(obj.To, actor.Id) {
				obj.To = append(obj.To, actor.Id)
			}
		}
	}

	replyingTo := ParseCommentForReplies(r.FormValue("comment"))

	for _, e := range replyingTo {

		has := false
		
		for _, f := range obj.InReplyTo {
			if e.Id == f.Id {
				has = true
				break
			}
		}
		
		if !has {
			obj.InReplyTo = append(obj.InReplyTo, e)

			var activity Activity
			
			activity.To = append(activity.To, e.Id)
			
			if !IsActivityLocal(db, activity) {
				id := FingerActor(e.Id).Id
				actor := GetActor(id)
				if !IsInStringArray(obj.To, actor.Id) {
					obj.To = append(obj.To, actor.Id)
				}				
			}
		}
	}

	return obj
}

func ParseOptions(r *http.Request, obj ObjectBase) ObjectBase {
	options := EscapeString(r.FormValue("options"))
	if options != "" {
		option := strings.Split(options, ";")
		email := regexp.MustCompile(".+@.+\\..+")		
		wallet := regexp.MustCompile("wallet:.+")
		delete := regexp.MustCompile("delete:.+")
		for _, e := range option {
			if e == "noko" {
				obj.Option = append(obj.Option, "noko")				 
			} else if e == "sage" {
				obj.Option = append(obj.Option, "sage")				 				
			} else if e == "nokosage" {
				obj.Option = append(obj.Option, "nokosage")				 								
			} else if email.MatchString(e) {
				obj.Option = append(obj.Option, "email:" + e)				 												
			} else if wallet.MatchString(e) {
				obj.Option = append(obj.Option, "wallet")				 																
				var wallet CryptoCur
				value := strings.Split(e, ":")
				wallet.Type = value[0]
				wallet.Address = value[1]
				obj.Wallet = append(obj.Wallet, wallet)
			} else if delete.MatchString(e) {
				obj.Option = append(obj.Option, e)
			}
		}
	}

	return obj
}

func GetActivityFromJson(r *http.Request, db *sql.DB) Activity {
	body, _ := ioutil.ReadAll(r.Body)

	var respActivity ActivityRaw

	var nActivity Activity

	var nType string

	err := json.Unmarshal(body, &respActivity)

	CheckError(err, "error with activity from json")

	if HasContextFromJson(respActivity.AtContextRaw.Context) {
		var jObj ObjectBase

		if respActivity.Type == "Note" {
			jObj = GetObjectFromJson(body)
			nType = "Create"
		} else {
			jObj = GetObjectFromJson(respActivity.ObjectRaw)
			nType = respActivity.Type
		}

		actor := GetActorFromJson(respActivity.ActorRaw)
		to := GetToFromJson(respActivity.ToRaw)
		cc := GetToFromJson(respActivity.CcRaw)

		nActivity.AtContext.Context = "https://www.w3.org/ns/activitystreams"
		nActivity.Type = nType
		nActivity.Actor = &actor
		nActivity.Published = respActivity.Published
		nActivity.Auth = respActivity.Auth
		
		if len(to) > 0 {
			nActivity.To = to
		}

		if len(cc) > 0 {
			nActivity.Cc = cc
		}

		nActivity.Name = respActivity.Name
		nActivity.Object = &jObj
	}

	return nActivity	
}

func CheckCaptcha(db *sql.DB, captcha string) bool {
	parts := strings.Split(captcha, ":")

	if strings.Trim(parts[0], " ") == "" || strings.Trim(parts[1], " ") == ""{
		return false
	}
	
	path  := "public/" + parts[0] + ".png"
	code  := GetCaptchaCodeDB(db, path)

	if code != "" {
		DeleteCaptchaCodeDB(db, path)
		CreateNewCaptcha(db)
	}

	if (code == strings.ToUpper(parts[1])) {
		return true
	}

	return false
}

func ParseInboxRequest(w http.ResponseWriter, r *http.Request, db *sql.DB) {
	activity := GetActivityFromJson(r, db)

	if activity.Actor.PublicKey.Id == "" {
		nActor := FingerActor(activity.Actor.Id)
		activity.Actor = &nActor
	}
	
	if !VerifyHeaderSignature(r, *activity.Actor) {
		response := RejectActivity(activity)
		MakeActivityRequest(db, response)				
		return
	}

	switch(activity.Type) {
	case "Create":
		for _, e := range activity.To {
			if IsActorLocal(db, e) {
				if !IsActorLocal(db, activity.Actor.Id) {
					WriteObjectToCache(db, *activity.Object)
				}
			}
		}
		break

	case "Delete":
		for _, e := range activity.To {
			actor := GetActorFromDB(db, e)
			if actor.Id != "" {
				if activity.Object.Replies != nil {
					for _, k := range activity.Object.Replies.OrderedItems {
						TombstoneObject(db, k.Id)					
					}
				}
				TombstoneObject(db, activity.Object.Id)
				break
			}
		}
		break

		
	case "Follow":
		for _, e := range activity.To {
			if GetActorFromDB(db, e).Id != "" {
				response := AcceptFollow(activity)
				response = SetActorFollowerDB(db, response)
				MakeActivityRequest(db, response)
			} else {
				fmt.Println("follow request for rejected")				
				response := RejectActivity(activity)
				MakeActivityRequest(db, response)
				return
			}
		}
		break

	case "Reject":
		if activity.Object.Object.Type == "Follow" {
			fmt.Println("follow rejected")									
			SetActorFollowingDB(db, activity)
		}
		break		
	}

}

func MakeActivityFollowingReq(w http.ResponseWriter, r *http.Request, activity Activity) bool {
	actor := GetActor(activity.Object.Id)
	
	resp, err := http.NewRequest("POST", actor.Inbox, nil)

	CheckError(err, "Cannot make new get request to actor inbox for following req")

	defer resp.Body.Close()

	body, _ := ioutil.ReadAll(resp.Body)

	var respActivity Activity

	err = json.Unmarshal(body, &respActivity)

	if respActivity.Type == "Accept" {
		return true
	}

	return false
}

func RemoteActorHasAuth(actor string, code string) bool {

	if actor == "" || code == "" {
		return false
	}
	
	req, err := http.NewRequest("GET", actor + "/verification&code=" + code, nil)

	CheckError(err, "could not make remote actor auth req")

	resp, err := http.DefaultClient.Do(req)

	CheckError(err, "could not make remote actor auth resp")

	defer resp.Body.Close()

	if resp.StatusCode == 200 {
		return true
	}

	return false
}