summaryrefslogtreecommitdiff
path: root/aplus.go
blob: dfc73e18993c33f317f04ccdfce4b709a6271642 (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
package main

import (
	"encoding/json"
	"fmt"
	"html"
	"io"
	"net/http"
	"net/url"
	"os"
	"strings"
)

func submit_code(course_code int, attendance_code string) {
	cur_body := launch_aplus(course_code)

	if verbose {
		fmt.Printf("%s", cur_body)
	}

	current_url := fmt.Sprintf("%s/student/", base_aplus_link)
	// the links to submit the code for a class is under the dayPanel div
	daypanel_start := strings.Index(cur_body, "<div class=\"dayPanel\"")

	// link_start - link_end is to extract the URL
	// TODO: make this more elegant to allow multiple of these links
	//       (maybe use an html parser)
	// 	 or at least use extract_attribute()
	link_start := daypanel_start
	link_start += strings.Index(cur_body[link_start:], "<a href=\"./?module")
	link_start += len("<a href=\"")
	link_end := link_start
	link_end += strings.Index(cur_body[link_end:], "\"")

	link_url := current_url + cur_body[link_start:link_end]
	link_url = html.UnescapeString(link_url)

	if verbose {
		fmt.Println("!!!")
		fmt.Printf("%i %i %i\n", daypanel_start, link_start, link_end)
		fmt.Println(link_url)
		fmt.Println("!!!")
	}

	resp, _ := client.Get(link_url)
	body, _ := io.ReadAll(resp.Body)

	form_str := get_form_from_request_body(body)
	form_values := parse_form(form_str)
	form_values["ctl01$sessionCode"][0] = attendance_code

	resp, _ = client.PostForm(link_url, form_values)
	body, _ = io.ReadAll(resp.Body)

	if strings.Index(string(body), "ctl01_errorMessage") != -1 {
		errormsg_start := strings.Index(string(body), "ctl01_errorMessage")
		errormsg_start += strings.Index(string(body)[errormsg_start:], "Text\">")
		errormsg_start += len("Text\">")
		errormsg_end := errormsg_start
		errormsg_end += strings.Index(string(body)[errormsg_start:], "<")
		fmt.Println(string(body)[errormsg_start:errormsg_end])
	} else if strings.Index(string(body), "ctl02_codeSuccessMessage") != -1 {
		fmt.Println("Code successfully recorded")
	}
}

func launch_aplus(course_code int) string {
	err := init_aplus_toolid(course_code)
	if err != nil {
		fmt.Println("Failed to get A+ base URL and tool ID. Try again with a different course code.")
		fmt.Printf("Error: %s\n", err)
		os.Exit(1)
	}

	aplus_link := fmt.Sprintf("%s/courses/%d/external_tools/sessionless_launch?id=%d&access_token=%s",
		base_link, course_code, external_tools_code, token)
	aplus := get_aplus(token, aplus_link, client)

	resp, _ := client.Get(aplus)
	body, _ := io.ReadAll(resp.Body)

	form_str := get_form_from_request_body(body)
	form_values := parse_form(form_str)

	resp, _ = client.PostForm(base_aplus_link, form_values)
	body, _ = io.ReadAll(resp.Body)
	return string(body)
}

func get_aplus(token string, link string, client http.Client) string {
	resp, _ := client.Get(link)

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

	var aplus Aplus
	json.Unmarshal(body, &aplus)

	return aplus.URL
}

func get_form_from_request_body(req_body []byte) string {
	body_str := string(req_body)
	form_start := strings.Index(body_str, "<form")
	form_end := strings.Index(body_str, "</form>") + 7
	form_html := req_body[form_start:form_end]

	return string(form_html)
}

// parse_form extracts form fields and values from the given HTML form string.
func parse_form(form_html string) url.Values {
	form_values := make(url.Values)
	inputs := strings.Split(form_html, "<input")

	for _, input := range inputs {
		// Extract field name and value
		name := extract_attribute(input, "name")
		value := html.UnescapeString(extract_attribute(input, "value"))

		if name != "" {
			form_values.Add(name, value)
		}
	}

	return form_values
}

func extract_attribute(input string, attribute string) string {
	start := strings.Index(input, attribute+"=\"")
	if start == -1 {
		start = strings.Index(input, attribute+"='")
	}

	if start == -1 {
		return ""
	}

	start += len(attribute) + 2

	end := strings.Index(input[start:], "\"")
	if end == -1 {
		end = strings.Index(input[start:], "'")
	}

	if end == -1 {
		return ""
	}

	return input[start : start+end]
}