├── requirements.txt ├── .style.yapf ├── .gitignore ├── .travis.yml ├── logger.py ├── db.sql ├── README.md ├── driver.py ├── mention_manager.py ├── server.py ├── newsparser.py ├── newsreader.py ├── database.py ├── markdownrenderer.py ├── bot.py └── LICENSE /requirements.txt: -------------------------------------------------------------------------------- 1 | mistune==2.0.0a2 2 | -------------------------------------------------------------------------------- /.style.yapf: -------------------------------------------------------------------------------- 1 | [style] 2 | based_on_style = yapf 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.pem 2 | *.pyc 3 | __pycache__/ 4 | *.csr 5 | *.ini 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | python: 3 | - "3.6" 4 | - "3.7" 5 | - "3.8" 6 | 7 | install: 8 | - pip install yapf 9 | 10 | script: 11 | - yapf --style=yapf --diff --recursive . 12 | 13 | -------------------------------------------------------------------------------- /logger.py: -------------------------------------------------------------------------------- 1 | import logging 2 | 3 | 4 | class Message: 5 | 6 | def __init__(self, fmt, args): 7 | self.fmt = fmt 8 | self.args = args 9 | 10 | def __str__(self): 11 | return self.fmt.format(*self.args) 12 | 13 | 14 | class StyleAdapter(logging.LoggerAdapter): 15 | 16 | def __init__(self, logger, extra=None): 17 | super(StyleAdapter, self).__init__(logger, extra or {}) 18 | 19 | def log(self, level, msg, *args, **kwargs): 20 | if self.isEnabledFor(level): 21 | msg, kwargs = self.process(msg, kwargs) 22 | self.logger._log(level, Message(msg, args), (), **kwargs) 23 | 24 | 25 | def getLogger(name): 26 | return StyleAdapter(logging.getLogger(name)) 27 | -------------------------------------------------------------------------------- /db.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE IF NOT EXISTS `users` ( 2 | `id` int not null auto_increment, 3 | `uid` int not null unique, 4 | `cid` int not null unique, 5 | `uname` varchar(255) not null, 6 | `no_plus_one` BIT not null default 0, 7 | `is_active` BIT not null default 1, 8 | primary key(`id`) 9 | ); 10 | 11 | CREATE TABLE IF NOT EXISTS `topics` ( 12 | `id` int not null auto_increment, 13 | `cid` int not null, 14 | `topic` varchar(255) not null, 15 | primary key(id), 16 | foreign key(`cid`) references users(`cid`), 17 | unique `ident` (`cid`, `topic`) 18 | ); 19 | 20 | CREATE TABLE IF NOT EXISTS `aliases` ( 21 | `id` int not null auto_increment, 22 | `cid` int not null, 23 | `alias` varchar(255) not null, 24 | foreign key(`cid`) references users(`cid`), 25 | primary key(`id`), 26 | unique `ident` (`cid`, `alias`) 27 | ); 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # COWNotifier 2 | Get notifications via a Telegram bot for your favorite news groups. 3 | Generic server allows multiple users to create and manage followlist 4 | within a news server and get notifications whenever a new post has 5 | been made to one of those groups. 6 | 7 | ## Usage 8 | ### Server side 9 | Clone the repo then edit driver.py's `getConfig` function for the first 10 | setup, by giving the Telegram Bot's token, your server's URL etc. Function 11 | is self-explanatory. Then run `driver.py`. 12 | 13 | ### Bot Side 14 | /start - Create a record within the server for the followlist
15 | /add TOPICNAME - Adds topic to your followlist, in addition to exact 16 | topic name, it also tries to match the name by prepending 17 | metu.ceng. or metu.ceng.course. to the given parameter, 18 | a spesific help for METU CENG users. Can be tweaked within 19 | newsreader.py's closest fuction.
20 | /delete TOPICNAME - Deletes given topic from your followlist
21 | /list - Shows your followlist entries.
22 | /help - Prints out command's and basic info about usage
23 | 24 | --- 25 | 26 | There's a working bot telegram.me/cownotifbot but it's only available to 27 | metu ceng newsgroup, you can simply create a new bot and give token to 28 | server's config and create a bot for your own server. 29 | -------------------------------------------------------------------------------- /driver.py: -------------------------------------------------------------------------------- 1 | import os 2 | import json 3 | import datetime 4 | import traceback 5 | import queue 6 | import logging 7 | 8 | from bot import cowBot 9 | from server import webHook 10 | from logger import getLogger 11 | 12 | logger = getLogger(__name__) 13 | 14 | 15 | def getConf(storage): 16 | if os.path.exists(storage): 17 | return json.loads(open(storage, 'r').read()) 18 | conf = {} 19 | conf['bot'] = {'token': 'BOTTOKEN', 'url': 'https://example.com:8443/'} 20 | conf['web'] = {'cert': 'CERTFILE', 'pubkey': 'PUBKEYFILE'} 21 | conf['news'] = { 22 | 'host': 'HOST', 23 | 'auth': 'HOST', 24 | 'port': 443, 25 | 'user': 'UNAME', 26 | 'pass': 'PASS', 27 | 'last': 'lpost_file', 28 | 'timezone': 3 # Timezone difference to UTC-Z in the form of: x or -x 29 | } 30 | conf['db'] = { 31 | 'host': 'HOST', 32 | 'user': 'USER', 33 | 'pass': 'PASS', 34 | 'name': 'DBNAME' 35 | } 36 | with open(storage, 'w') as f: 37 | json.dump(conf, f) 38 | return conf 39 | 40 | 41 | def main(): 42 | try: 43 | conf = getConf('conf.ini') 44 | q = queue.Queue() 45 | bot = cowBot(conf, q) 46 | wh = webHook(conf, q) 47 | bot.start() 48 | wh.start() 49 | bot.join() 50 | except Exception as e: 51 | logger.critical('{} {}', e, datetime.datetime.now()) 52 | traceback.print_exc() 53 | 54 | 55 | if __name__ == '__main__': 56 | logging.basicConfig(level=logging.INFO) 57 | main() 58 | -------------------------------------------------------------------------------- /mention_manager.py: -------------------------------------------------------------------------------- 1 | import re 2 | 3 | from markdownrenderer import escape 4 | 5 | 6 | class mentionManager: 7 | 8 | def isStudentNumber(self, msg): 9 | search = self.student_no_matcher.search(msg) 10 | return search if search is None else search.group() 11 | 12 | def __init__(self, db, cow_bot): 13 | self.student_no_matcher = re.compile("e?\d{6,7}") 14 | self.db = db 15 | self.cow_bot = cow_bot 16 | self.mention_text = "Your alias *{}* has been mentioned in " + \ 17 | "newsgroup: *{}* with header: *{}* at line: {}\." 18 | 19 | def sendMention(self, cid, alias, newsgroup, header, line_no): 20 | return self.cow_bot.sendMsg( 21 | cid, 22 | self.mention_text.format( 23 | escape(alias), escape(newsgroup), escape(header), line_no), 24 | escaped=True) 25 | 26 | def getMinimalStudentNo(self, student_no): 27 | base = student_no 28 | if base[0] == "e": 29 | base = base[1:] 30 | base = base[:6] 31 | return base 32 | 33 | def parseMentions(self, content, newsgroup): 34 | current_header = "" 35 | line_no = 0 36 | for raw_line in content.split("\n"): 37 | line = raw_line.strip() 38 | if line.startswith("> "): 39 | continue 40 | student_no = self.isStudentNumber(line) 41 | if student_no is not None: 42 | cids = self.db.checkForAlias(self.getMinimalStudentNo(student_no)) 43 | if cids is None: 44 | continue 45 | header = current_header 46 | if len(line) > len(student_no): 47 | header = line 48 | for cid in cids: 49 | self.sendMention(cid, student_no, newsgroup, header, line_no) 50 | else: 51 | current_header = line 52 | line_no = 0 53 | line_no += 1 54 | -------------------------------------------------------------------------------- /server.py: -------------------------------------------------------------------------------- 1 | from socketserver import ThreadingMixIn 2 | from http.server import HTTPServer, BaseHTTPRequestHandler 3 | import datetime 4 | import ssl 5 | import traceback 6 | import threading 7 | import json 8 | import socket 9 | from logger import getLogger 10 | 11 | logger = getLogger(__name__) 12 | 13 | 14 | class webHook(threading.Thread): 15 | 16 | class ReqHandler(BaseHTTPRequestHandler): 17 | 18 | def do_GET(self): 19 | try: 20 | self.send_response(200) 21 | self.end_headers() 22 | if self.path == '/' + self.token: 23 | data = self.rfile.read(int( 24 | self.headers['Content-Length'])).decode('utf-8') 25 | data = json.loads(data) 26 | logger.info('Received {}', data) 27 | self.q.put(data) 28 | except Exception as e: 29 | logger.error('{} {}', e, datetime.datetime.now()) 30 | traceback.print_exc() 31 | 32 | def do_POST(self): 33 | self.do_GET() 34 | 35 | class ThreadedHTTPServer(ThreadingMixIn, HTTPServer): 36 | 37 | def get_request(self): 38 | newsock, addr = self.socket.accept() 39 | newsock = ssl.wrap_socket( 40 | newsock, 41 | certfile=self.certfile, 42 | do_handshake_on_connect=False, 43 | server_side=True) 44 | timeout = newsock.gettimeout() 45 | newsock.settimeout(2.) 46 | newsock.do_handshake() 47 | newsock.settimeout(timeout) 48 | return newsock, addr 49 | 50 | def __init__(self, conf, q): 51 | threading.Thread.__init__(self) 52 | self.ReqHandler.token = conf['bot']['token'] 53 | self.ReqHandler.q = q 54 | httpd = self.ThreadedHTTPServer(('0.0.0.0', 8443), self.ReqHandler) 55 | httpd.certfile = conf['web']['cert'] 56 | self.httpd = httpd 57 | 58 | def run(self): 59 | self.httpd.serve_forever() 60 | -------------------------------------------------------------------------------- /newsparser.py: -------------------------------------------------------------------------------- 1 | import datetime 2 | import traceback 3 | from logger import getLogger 4 | from markdownrenderer import convertDiscourseToTelegram, escape 5 | 6 | logger = getLogger(__name__) 7 | 8 | 9 | def isPlusOne(msg): 10 | return msg.startswith('+1') and len(msg) < 10 11 | 12 | 13 | def getHumanReadableDate(date): 14 | # Date is expected to be a tuple of two elements, from newsreader. 15 | # date[0] -> message's date in raw form: %Y:%m:%dT%H:%M:%S.xxZ 16 | # where 'xx's and Z represent nanosecs(?) and the UTC +0 respectively. 17 | # date[1] -> list of timezone difference to localzone, Turkey's is UTC 3.00, 18 | # so it is [3, 0] representing hours and minutes by default. 19 | rawDate, localTimezone = date 20 | # Parse the raw format and add localTimezone's hour difference. 21 | 22 | machineDate = datetime.datetime.strptime( 23 | rawDate.split('.')[0], 24 | '%Y-%m-%dT%H:%M:%S') + datetime.timedelta(hours=localTimezone) 25 | # return the human readable from, refer to datetime.strftime for format 26 | # options. 27 | # an example of current format: 03 Feb 2020, 18:30:00 28 | return machineDate.strftime('%d %b %Y, %H:%M:%S') 29 | 30 | 31 | class newsArticle: 32 | 33 | def __init__(self, author, topic, subject, date, url, dc_markdown, 34 | mention_manager): 35 | self.author_username = author[0] 36 | self.author_displayname = author[1] 37 | self.topic = topic 38 | self.subject = subject 39 | self.date = getHumanReadableDate(date) 40 | self.url = url 41 | self.dc_markdown = dc_markdown 42 | self.mention_manager = mention_manager 43 | self.broken = False 44 | self.tg_markdown = None 45 | self.is_plus_one = None 46 | self.attachments = None 47 | 48 | def isPlusOne(self): 49 | if self.is_plus_one is None: 50 | self.is_plus_one = isPlusOne(self.dc_markdown) 51 | return self.is_plus_one 52 | 53 | def parse(self): 54 | # TODO(kadircet): Improve handling of 4K text limit. 55 | if self.tg_markdown is None: 56 | self.parseMessage() 57 | return self.tg_markdown 58 | 59 | def getAttachments(self): 60 | if self.attachments is None: 61 | self.parseMessage() 62 | return self.attachments 63 | 64 | def makeHeader(self): 65 | hdr = f"From: {self.author_username}({self.author_displayname})\n"\ 66 | f"Newsgroup: {self.topic}\n"\ 67 | f"Subject: {self.subject}\n"\ 68 | f"Date: {self.date}\n"\ 69 | f"is_plus_one: {self.isPlusOne()}" 70 | link_to_post = f'[Open post]({self.url})\n' 71 | return link_to_post + '```\n' + escape(hdr, True) + '\n```\n' 72 | 73 | def parseMessage(self): 74 | if self.broken: 75 | return 76 | 77 | try: 78 | self.mention_manager.parseMentions(self.dc_markdown, self.topic) 79 | paragraphs, attachments = convertDiscourseToTelegram(self.dc_markdown) 80 | self.tg_markdown = self.makeHeader() + '\n\n'.join(paragraphs) 81 | self.attachments = attachments 82 | except Exception as e: 83 | logger.error('{} {}', e, datetime.datetime.now()) 84 | traceback.print_exc() 85 | # Record failure so we don't retry. 86 | self.broken = True 87 | -------------------------------------------------------------------------------- /newsreader.py: -------------------------------------------------------------------------------- 1 | import traceback 2 | import threading 3 | import time 4 | import datetime 5 | import html 6 | import json 7 | import requests 8 | import sys 9 | from newsparser import newsArticle 10 | from logger import getLogger 11 | 12 | logger = getLogger(__name__) 13 | 14 | 15 | class newsReader: 16 | 17 | def __init__(self, host, port, uname, pw, lfile, auth, timezone): 18 | self.conparams = [host, port, uname, pw, auth] 19 | self.lfile = lfile 20 | self.timezone = timezone 21 | self.initialized = False 22 | self.initConnection() 23 | 24 | # TODO: Make this function higher level 25 | def makeAPICall(self, endpoint, params={}, **kwargs): 26 | post = kwargs.get('post', False) 27 | timeout = kwargs.get('timeout', 30) 28 | retry = kwargs.get('retry', False) 29 | redirect = kwargs.get('redirect', True) 30 | backoff = 1 31 | if not endpoint.endswith('/'): 32 | endpoint += '/' 33 | req_url = self.conparams[0] + endpoint 34 | while True: 35 | try: 36 | # do POST request 37 | if post: 38 | resp = requests.post( 39 | req_url, 40 | data=params, 41 | cookies=self.token, 42 | timeout=timeout, 43 | allow_redirects=redirect) 44 | else: # do GET request 45 | resp = requests.get( 46 | req_url, 47 | params=params, 48 | cookies=self.token, 49 | timeout=timeout, 50 | allow_redirects=redirect) 51 | resp.raise_for_status() 52 | return resp, resp.status_code 53 | except requests.exceptions.RequestException as e: 54 | logger.error('Request Failed: {} -- Endpoint: {}, Data: {}', e, 55 | endpoint, params) 56 | resp = None 57 | if not retry: 58 | return resp, e.response.status_code 59 | logger.error('Retrying in {} seconds', backoff) 60 | time.sleep(backoff) 61 | backoff = max(60, backoff * 2) 62 | except Exception as e: 63 | # Something outside "requests" library failed 64 | logger.error('{} {}', e, datetime.datetime.now()) 65 | traceback.print_exc() 66 | sys.exit(1) 67 | 68 | def populateCategories(self): 69 | # Grab category ids and names 70 | categories = {} 71 | resp, _ = self.makeAPICall('site.json', retry=True) 72 | resp_categories = resp.json()['categories'] 73 | parent = {} 74 | for item in resp_categories: 75 | categories[item['id']] = item['name'] 76 | parent[item['id']] = item.get('parent_category_id', None) 77 | 78 | self.categories = {} 79 | for id, name in categories.items(): 80 | parents = [name] 81 | cur_id = id 82 | while parent[id] is not None: 83 | parents.append(categories[parent[id]]) 84 | id = parent[id] 85 | cat_name = '.'.join(parents[::-1]) 86 | logger.debug('Got category: {} - {}', cur_id, cat_name) 87 | self.categories[cur_id] = cat_name 88 | 89 | def initConnection(self): 90 | self.updateAuthToken() 91 | self.time = time.time() 92 | self.populateCategories() 93 | 94 | # Read last stored post id 95 | try: 96 | with open(self.lfile) as f: 97 | self.last_post = int(f.read().strip()) 98 | except Exception as e: 99 | self.last_post = None 100 | logger.error('{} {}', e, datetime.datetime.now()) 101 | traceback.print_exc() 102 | 103 | # Update the last stored post id 104 | if self.last_post == None: 105 | resp, _ = self.makeAPICall('posts.json', params={'before': 0}, retry=True) 106 | posts = resp.json()['latest_posts'] 107 | remote_post_id = max([p['id'] for p in posts]) 108 | with open(self.lfile, 'w') as f: 109 | f.write(str(remote_post_id)) 110 | self.last_post = remote_post_id 111 | self.initialized = True 112 | 113 | def updateAuthToken(self): 114 | self.token = None 115 | data = {'username': self.conparams[2], 'password': self.conparams[3]} 116 | resp, _ = self.makeAPICall( 117 | self.conparams[4], params=data, post=True, retry=True, redirect=False) 118 | for cookie in resp.headers['Set-Cookie'].split(';'): 119 | cookie = cookie.strip() 120 | if cookie.startswith('_t='): 121 | self.token = {'_t': cookie[3:]} 122 | self.time = time.time() 123 | logger.info('New Auth Token: {} acquired at {}', self.token, self.time) 124 | break 125 | 126 | def getIdForTopic(self, topic): 127 | for cat_id, name in self.categories.items(): 128 | if name == topic: 129 | return cat_id 130 | return -1 131 | 132 | def validTopic(self, topic): 133 | return topic in self.categories.values() 134 | 135 | def closest(self, topic, topics=None): 136 | if topics == None: 137 | topics = self.categories.values() 138 | pos = [] 139 | topic = topic.lower() 140 | for t in topics: 141 | t_lower = t.lower() 142 | if t_lower == topic.lower(): 143 | return [t] 144 | if topic in t_lower: 145 | pos.append(t) 146 | return pos 147 | 148 | def updatePosts(self, mention_manager): 149 | if not self.initialized: 150 | return {} 151 | # TODO: Find token expiration. Currently a day. 152 | if time.time() - self.time > 60. * 60 * 24: 153 | self.updateAuthToken() 154 | 155 | # Get latest posts 156 | resp, _ = self.makeAPICall('posts.json', params={'before': 0}) 157 | if resp == None: 158 | return {} 159 | posts = json.loads(resp.text)['latest_posts'] 160 | last = max([p['id'] for p in posts]) 161 | start = self.last_post 162 | res = {} 163 | while start < last: 164 | start += 1 165 | # Get post 166 | post, code = self.makeAPICall(f'posts/{start}.json') 167 | if post == None: 168 | if code == 403 or code == 404: 169 | continue 170 | start -= 1 171 | break 172 | post = post.json() 173 | # post information we get through 'posts/{id}.json' 174 | # doesn't contain category_id and topic_title so 175 | # we need to make another request for that info. 176 | # TODO: Cache topic_id -> category_id and 177 | # topic_id -> topic_title mappings 178 | 179 | # Get post's topic for category and title 180 | topic, code = self.makeAPICall(f"t/{post['topic_id']}.json") 181 | if topic == None: 182 | if code == 403 or code == 404: 183 | continue 184 | start -= 1 185 | break 186 | topic = topic.json() 187 | if topic['category_id'] not in self.categories: 188 | logger.error('Unknown category_id: {}', topic['category_id']) 189 | continue 190 | if topic['category_id'] not in res: 191 | res[topic['category_id']] = [] 192 | # TODO(kadircet): Also include link to the post via topic_id and post_count. 193 | res[topic['category_id']].append( 194 | newsArticle( 195 | (post['username'], post['name']), 196 | self.categories[topic['category_id']], 197 | topic['title'], 198 | (post['created_at'], self.timezone), 199 | f'{self.conparams[0]}p/{post["id"]}', 200 | post['raw'], # raw msg (markdown) 201 | mention_manager)) 202 | self.last_post = start 203 | with open(self.lfile, 'w') as f: 204 | f.write(str(self.last_post)) 205 | return res 206 | -------------------------------------------------------------------------------- /database.py: -------------------------------------------------------------------------------- 1 | import traceback 2 | import threading 3 | import MySQLdb 4 | import datetime 5 | 6 | 7 | class dataBase: 8 | 9 | def __init__(self, host, uname, pw, dbname, rdr): 10 | self.conn = MySQLdb.connect(host, uname, pw, dbname, charset="utf8mb4") 11 | self.params = [host, uname, pw, dbname] 12 | self.rdr = rdr 13 | self.lock = threading.Lock() 14 | 15 | def close(self): 16 | self.conn.close() 17 | 18 | def ping(self): 19 | try: 20 | self.conn.ping() 21 | return 22 | except Exception as e: 23 | print(e, datetime.datetime.now()) 24 | traceback.print_exc() 25 | backoff = 1 26 | while True: 27 | try: 28 | self.conn = MySQLdb.connect( 29 | self.params[0], 30 | self.params[1], 31 | self.params[2], 32 | self.params[3], 33 | charset="utf8") 34 | except Exception as e: 35 | print(e, datetime.datetime.now()) 36 | traceback.print_exc() 37 | time.sleep(backoff) 38 | backoff = min(60, backoff * 2) 39 | 40 | def registerUser(self, uid, cid, uname): 41 | sql = "INSERT INTO `users` (uid, cid, uname) VALUES (%s,%s,%s)" 42 | self.lock.acquire() 43 | cur = self.conn.cursor() 44 | res = 0 45 | try: 46 | cur.execute(sql, ( 47 | uid, 48 | cid, 49 | uname, 50 | )) 51 | except MySQLdb.IntegrityError: 52 | res = 1 53 | except Exception as e: 54 | print(e, datetime.datetime.now()) 55 | traceback.print_exc() 56 | res = 2 57 | self.conn.commit() 58 | cur.close() 59 | self.lock.release() 60 | return res 61 | 62 | def setUserStatus(self, uid, status): 63 | sql = "UPDATE `users` SET `is_active`=%s WHERE `uid`=%s" 64 | self.lock.acquire() 65 | cur = self.conn.cursor() 66 | res = 0 67 | try: 68 | cur.execute(sql, ( 69 | status, 70 | uid, 71 | )) 72 | except Exception as e: 73 | print(e, datetime.datetime.now()) 74 | traceback.print_exc() 75 | res = 1 76 | self.conn.commit() 77 | cur.close() 78 | self.lock.release() 79 | return res 80 | 81 | def updateUser(self, uid, no_plus_one): 82 | sql = "UPDATE `users` SET `no_plus_one`=%s WHERE `uid`=%s" 83 | self.lock.acquire() 84 | cur = self.conn.cursor() 85 | res = 0 86 | try: 87 | cur.execute(sql, ( 88 | no_plus_one, 89 | uid, 90 | )) 91 | except Exception as e: 92 | print(e, datetime.datetime.now()) 93 | traceback.print_exc() 94 | res = 1 95 | self.conn.commit() 96 | cur.close() 97 | self.lock.release() 98 | return res 99 | 100 | def addTopic(self, cid, topic): 101 | if not self.rdr.validTopic(topic): 102 | topic = self.rdr.closest(topic) 103 | if len(topic) == 0: 104 | return (2, topic) 105 | if len(topic) != 1: 106 | return (4, topic) 107 | topic = topic[0] 108 | cat_id = self.rdr.getIdForTopic(topic) 109 | if cat_id == -1: 110 | return (2, topic) 111 | sql = "INSERT INTO `topics` (cid, topic) VALUES (%s, %s)" 112 | self.lock.acquire() 113 | cur = self.conn.cursor() 114 | res = 0 115 | try: 116 | cur.execute(sql, ( 117 | cid, 118 | cat_id, 119 | )) 120 | except MySQLdb.IntegrityError: 121 | res = 1 122 | except Exception as e: 123 | print(e, datetime.datetime.now()) 124 | traceback.print_exc() 125 | res = 3 126 | self.conn.commit() 127 | cur.close() 128 | self.lock.release() 129 | return (res, topic) 130 | 131 | def deleteTopic(self, cid, topic): 132 | if not self.rdr.validTopic(topic): 133 | topic = self.rdr.closest(topic, self.getTopicsByCid(cid)) 134 | if len(topic) == 0: 135 | return (2, topic) 136 | if len(topic) != 1: 137 | return (4, topic) 138 | topic = topic[0] 139 | cat_id = self.rdr.getIdForTopic(topic) 140 | if cat_id == -1: 141 | return (2, topic) 142 | sql = "DELETE FROM `topics` WHERE cid=%s AND topic=%s" 143 | self.lock.acquire() 144 | cur = self.conn.cursor() 145 | res = 0 146 | try: 147 | cnt = cur.execute(sql, ( 148 | cid, 149 | cat_id, 150 | )) 151 | if cnt == 0: 152 | res = 1 153 | except Exception as e: 154 | print(e, datetime.datetime.now()) 155 | traceback.print_exc() 156 | res = 3 157 | self.conn.commit() 158 | cur.close() 159 | self.lock.release() 160 | return (res, topic) 161 | 162 | def getCids(self): 163 | sql = "SELECT `cid` FROM `users`" 164 | self.lock.acquire() 165 | cur = self.conn.cursor() 166 | res = [] 167 | try: 168 | cur.execute(sql) 169 | for row in cur: 170 | res.append(row[0]) 171 | except Exception as e: 172 | print(e, datetime.datetime.now()) 173 | traceback.print_exc() 174 | res = None 175 | self.conn.commit() 176 | cur.close() 177 | self.lock.release() 178 | return res 179 | 180 | def getTopicsByCid(self, cid): 181 | sql = "SELECT topic FROM `topics` WHERE cid=%s" 182 | self.lock.acquire() 183 | cur = self.conn.cursor() 184 | res = [] 185 | try: 186 | cur.execute(sql, (cid,)) 187 | for row in cur: 188 | try: 189 | res.append(self.rdr.categories.get(int(row[0]), "")) 190 | except: 191 | continue 192 | except Exception as e: 193 | print(e, datetime.datetime.now()) 194 | traceback.print_exc() 195 | res = None 196 | self.conn.commit() 197 | cur.close() 198 | self.lock.release() 199 | return res 200 | 201 | def getTopics(self): 202 | sql = "SELECT topic FROM `topics` GROUP BY topic" 203 | sql2 = ("SELECT `users`.`cid`, `users`.`no_plus_one` FROM `topics`, `users`" 204 | " WHERE `topics`.`topic`=%s and `topics`.`cid`=`users`.`cid` and " 205 | "`users`.`is_active`=1") 206 | self.lock.acquire() 207 | cur = self.conn.cursor() 208 | cur2 = self.conn.cursor() 209 | res = [] 210 | try: 211 | cur.execute(sql) 212 | for row in cur: 213 | cur2.execute(sql2, (row[0],)) 214 | users = [] 215 | for user in cur2: 216 | _user = (user[0], bool(user[1][0])) 217 | users.append(_user) 218 | topic = row[0] 219 | try: 220 | topic = int(topic) 221 | except e: 222 | pass 223 | res.append([topic, users]) 224 | except Exception as e: 225 | print(e, datetime.datetime.now()) 226 | traceback.print_exc() 227 | res = None 228 | self.conn.commit() 229 | cur.close() 230 | self.lock.release() 231 | return res 232 | 233 | def checkForAlias(self, alias): 234 | sql = "SELECT `cid` FROM `aliases` WHERE `alias` = %s" 235 | self.lock.acquire() 236 | cur = self.conn.cursor() 237 | res = [] 238 | try: 239 | cur.execute(sql, (alias,)) 240 | for row in cur: 241 | res.append(row[0]) 242 | except Exception as e: 243 | print(e, datetime.datetime.now()) 244 | traceback.print_exc() 245 | res = None 246 | cur.close() 247 | self.lock.release() 248 | return res 249 | 250 | def addAlias(self, cid, alias): 251 | sql = "INSERT INTO `aliases` (`cid`, `alias`) VALUES (%s, %s)" 252 | self.lock.acquire() 253 | cur = self.conn.cursor() 254 | res = 0 255 | try: 256 | cur.execute(sql, (cid, alias)) 257 | except MySQLdb.IntegrityError: 258 | res = 1 259 | except Exception as e: 260 | print(e, datetime.datetime.now()) 261 | traceback.print_exc() 262 | res = 2 263 | self.conn.commit() 264 | cur.close() 265 | self.lock.release() 266 | return res 267 | 268 | def getAliases(self, cid): 269 | sql = "SELECT `alias` FROM `aliases` WHERE `cid` = %s" 270 | self.lock.acquire() 271 | cur = self.conn.cursor() 272 | res = [] 273 | try: 274 | cur.execute(sql, (cid,)) 275 | for row in cur: 276 | res.append(row[0]) 277 | except Exception as e: 278 | print(e, datetime.datetime.now()) 279 | traceback.print_exc() 280 | cur.close() 281 | self.lock.release() 282 | return res 283 | -------------------------------------------------------------------------------- /markdownrenderer.py: -------------------------------------------------------------------------------- 1 | import datetime 2 | import mistune 3 | import re 4 | import requests 5 | import string 6 | 7 | from logger import getLogger 8 | """Converts discourse markdown into a telegram compatible version.""" 9 | 10 | logger = getLogger(__name__) 11 | 12 | 13 | def pluginEmoji(md): 14 | """Parses markdown emojis in the form of :smiley: 15 | 16 | Generates an `emoji` token with text inside colons. 17 | """ 18 | 19 | def parseEmoji(self, m, state): 20 | return 'emoji', self.render(m.group(1), state) 21 | 22 | EMOJI_REGEX = r""":(\w+):""" 23 | md.inline.register_rule('emoji', EMOJI_REGEX, parseEmoji) 24 | md.inline.rules.append('emoji') 25 | 26 | 27 | def pluginQuote(md): 28 | """Parser for discourse quote style. 29 | 30 | These are in the form of: 31 | [quote="$USER_NAME$, post:$POST_ID$, topic:$TOPIC_ID$(, full:true)"] 32 | QUOTED_TEXT 33 | [/quote] 34 | 35 | Generates a `quote` token with children set to $QUOTED_TEXT$ and params set to 36 | ($USER_NAME$, $POST_ID$, $TOPIC_ID$). 37 | """ 38 | 39 | def parseQuote(self, m, state): 40 | params = m.group(1).split(',') 41 | username = params[0].strip() 42 | post_id = int(params[1].split(':')[1]) 43 | topic_id = int(params[2].split(':')[1]) 44 | return { 45 | 'type': 'quote', 46 | 'children': self.parse(m.group(2), state), 47 | 'params': (username, post_id, topic_id, params[3:]) 48 | } 49 | 50 | QUOTE_REGEX = re.compile(r'\[quote="(.*?)"\]\s*((.*?\n?)*?)\s*\[/quote\]\s*') 51 | md.block.register_rule('quote', QUOTE_REGEX, parseQuote) 52 | md.block.rules.append('quote') 53 | 54 | 55 | def unescape(text): 56 | """Unescapes punctuation and drops markdown markers in text.""" 57 | toEscape = { 58 | False: [ 59 | '_', '*', '[', ']', '(', ')', '~', '`', '>', '#', '+', '-', '=', '|', 60 | '{', '}', '.', '!' 61 | ], 62 | True: ['`', '\\'] 63 | } 64 | 65 | res = '' 66 | i = 0 67 | insideCodeBlock = False 68 | while i < len(text): 69 | # strip markdown markers. 70 | if text[i] in toEscape[insideCodeBlock]: 71 | if text[i] == '`': 72 | insideCodeBlock = not insideCodeBlock 73 | i += 1 74 | continue 75 | # if this is a backslash and the next char is punctuation, print that one 76 | # instead. 77 | if text[i] == '\\': 78 | if i + 1 < len(text) and text[i + 1] in toEscape[insideCodeBlock]: 79 | i += 1 80 | res += text[i] 81 | i += 1 82 | return res 83 | 84 | 85 | def escape(text, inCodeBlock=False): 86 | """Escapes characters specified in toEscape by prepending bachslashes. 87 | 88 | By default toEscape is the list mentioned in telegram bot api. 89 | """ 90 | if not inCodeBlock: 91 | toEscape = [ 92 | '_', '*', '[', ']', '(', ')', '~', '`', '>', '#', '+', '-', '=', '|', 93 | '{', '}', '.', '!' 94 | ] 95 | else: 96 | toEscape = ['`', '\\'] 97 | 98 | res = '' 99 | for t in text: 100 | if t in toEscape: 101 | res += '\\' 102 | res += t 103 | return res 104 | 105 | 106 | def emojiShortCodeToUnicode(shortcode): 107 | """Converts an emoji short code (+1) to its unicode equivalent. 108 | 109 | Uses github api to build a shortcode to unicode mapping. 110 | """ 111 | # Build and cache the mapping at first request, if something fails keep using 112 | # shortcodes as a fallback. 113 | if not hasattr(emojiShortCodeToUnicode, 'mapping'): 114 | try: 115 | mapping = requests.get('https://api.github.com/emojis').json() 116 | for key, value in mapping.items(): 117 | # value can be in one of two forms: 118 | # https://github.githubassets.com/images/icons/emoji/unicode/1f4a4.png?v8 119 | # https://github.githubassets.com/images/icons/emoji/atom.png?v8 120 | # We drop the non-unicode ones. 121 | if '/unicode/' not in value: 122 | mapping[key] = key 123 | continue 124 | # For unicode ones, sometimes there are multiple code points, e.g: 125 | # https://github.githubassets.com/images/icons/emoji/unicode/1f1e6-1f1fa.png?v8 126 | value = value.split('/')[-1].split('.png')[0].split('-') 127 | value = ''.join(map(lambda codepoint: chr(int(codepoint, 16)), value)) 128 | mapping[key] = value 129 | emojiShortCodeToUnicode.mapping = mapping 130 | except Exception as e: 131 | logger.error('{} {}', e, datetime.datetime.now()) 132 | traceback.print_exc() 133 | emojiShortCodeToUnicode.mapping = {} 134 | 135 | unic = emojiShortCodeToUnicode.mapping.get(unescape(shortcode), None) 136 | if not unic: 137 | unic = f':{shortcode}:' 138 | logger.debug('unknown shortcode: {0}', shortcode) 139 | return unic 140 | 141 | 142 | class telegramAttachment: 143 | 144 | def __init__(self, url): 145 | self.url = url 146 | 147 | 148 | class telegramRenderer(mistune.renderers.BaseRenderer): 149 | """Renderer to convert discourse markdown into telegram supported dialect.""" 150 | NAME = 'telegram' 151 | IS_TREE = True 152 | UPLOAD_SCHEME = 'upload://' 153 | 154 | def __init__(self): 155 | super(telegramRenderer, self).__init__() 156 | self.attachments = [] 157 | 158 | def takeAttachments(self): 159 | return self.attachments 160 | 161 | def createDefaultHandler(self, name): 162 | logger.error('Creating default method for unhandled rule: {}', name) 163 | 164 | def __unknown(*args): 165 | logger.error('Unhandled rule {} with data: {}', name, args) 166 | return '' 167 | 168 | return __unknown 169 | 170 | def _get_method(self, name): 171 | try: 172 | return super(telegramRenderer, self).__getattribute__(name) 173 | except: 174 | return self.createDefaultHandler(name) 175 | 176 | def paragraph(self, children): 177 | logger.debug('paragraph: {}', children) 178 | return ''.join(children) 179 | 180 | def text(self, text): 181 | logger.debug('text: {}', text) 182 | return escape(text) 183 | 184 | def emoji(self, children): 185 | logger.debug('emoji: {}', children) 186 | return emojiShortCodeToUnicode(''.join(children)) 187 | 188 | def list(self, children_and_levels, ordered, depth, start=None): 189 | logger.debug('list: {} {} {} {}', children_and_levels, ordered, depth, 190 | start) 191 | if start is None: 192 | start = 1 193 | res = [] 194 | base_padding = ' ' * (depth - 1) 195 | for children, level in children_and_levels: 196 | padding = ' ' * level + base_padding 197 | cur = [] 198 | for child in children: 199 | bullet = (str(start) + '\\.') if ordered else '\\-' 200 | start += 1 201 | cur.append(f'{padding}{bullet} {child}') 202 | res.append('\n'.join(cur)) 203 | return '\n'.join(res) 204 | 205 | def list_item(self, children, level): 206 | logger.debug('list_item: {}', children, level) 207 | return children, level - 1 208 | 209 | def block_text(self, children): 210 | logger.debug('block_text: {}', children) 211 | return ''.join(children) 212 | 213 | def heading(self, children, level): 214 | logger.debug('heading: {}', children) 215 | return ''.join(children) 216 | 217 | def strong(self, children): 218 | logger.debug('strong: {}', children) 219 | return f'*{"".join(children)}*' 220 | 221 | def emphasis(self, children): 222 | logger.debug('emphasis: {}', children) 223 | return f'_{"".join(children)}_' 224 | 225 | def block_code(self, children, info=None): 226 | logger.debug('code: {} {}', children, info) 227 | text = '```' 228 | if info is not None: 229 | text += escape(info.split()[0], True) 230 | text += '\n' 231 | text += escape(''.join(children), True) 232 | text += '\n```' 233 | return text 234 | 235 | def link(self, link, text=None, title=None): 236 | logger.debug('link: {} {} {}', link, text, title) 237 | if link.startswith(self.UPLOAD_SCHEME): 238 | link = 'https://cow.ceng.metu.edu.tr/uploads/short-url/' + link[ 239 | len(self.UPLOAD_SCHEME):] 240 | self.attachments.append(telegramAttachment(link)) 241 | if text is None: 242 | if title is None: 243 | text = link 244 | else: 245 | text = title 246 | text = escape(text) 247 | return f'[{text}]({link})' 248 | 249 | def image(self, src, alt='', title=None): 250 | logger.debug('image: {} {} {}', src, alt, title) 251 | if title is None: 252 | title = alt 253 | if not title: 254 | title = 'Image' 255 | if src.startswith(self.UPLOAD_SCHEME): 256 | src = 'https://cow.ceng.metu.edu.tr/uploads/short-url/' + src[ 257 | len(self.UPLOAD_SCHEME):] 258 | self.attachments.append(telegramAttachment(src)) 259 | return f'[{escape(title)}]({src})' 260 | 261 | def quote(self, children, user, post_id, topic_id, *args): 262 | logger.debug('quote: {} {} {} {}', user, post_id, topic_id, children, 263 | ''.join(*args)) 264 | text = '\n\n'.join(children).replace('\n', '\n\\> ') 265 | return (f'\\> [@{user}](https://cow.ceng.metu.edu.tr/u/{user}) in ' 266 | f'[post](https://cow.ceng.metu.edu.tr/t/{topic_id}/{post_id}):\n' 267 | f'\\> _{text}_') 268 | 269 | def codespan(self, text): 270 | logger.debug('codespan: {}', text) 271 | return f'`{text}`' 272 | 273 | def thematic_break(self): 274 | logger.debug('thematic_break') 275 | return escape('---') 276 | 277 | def block_quote(self, children): 278 | logger.debug('block_quote: {}', children) 279 | text = '\n\n'.join(children).replace('\n', '\n\\> ') 280 | return f'\\> _{text}_' 281 | 282 | def linebreak(self): 283 | logger.debug('linebreak') 284 | return '\n' 285 | 286 | def newline(self): 287 | logger.debug('newline') 288 | return '\n' 289 | 290 | def inline_html(self, text): 291 | logger.debug('inline_html {}', text) 292 | return escape(text) 293 | 294 | 295 | def convertDiscourseToTelegram(content): 296 | logger.debug(f'Got discourse markdown: {content}') 297 | renderer = telegramRenderer() 298 | paragraphs = mistune.markdown( 299 | content, renderer=renderer, plugins=[pluginEmoji, pluginQuote]) 300 | return paragraphs, renderer.takeAttachments() 301 | -------------------------------------------------------------------------------- /bot.py: -------------------------------------------------------------------------------- 1 | import traceback 2 | import datetime 3 | import requests 4 | import sys 5 | import time 6 | import threading 7 | from database import dataBase 8 | from newsreader import newsReader 9 | from mention_manager import mentionManager 10 | from markdownrenderer import escape, unescape 11 | from logger import getLogger 12 | 13 | logger = getLogger(__name__) 14 | 15 | 16 | class cowBot(threading.Thread): 17 | MAX_RETRIES = 3 18 | 19 | def __init__(self, conf, q): 20 | threading.Thread.__init__(self) 21 | self.token = conf['bot']['token'] 22 | self.conf = conf 23 | self.q = q 24 | self.rdr = newsReader(conf['news']['host'], conf['news']['port'], 25 | conf['news']['user'], conf['news']['pass'], 26 | conf['news']['last'], conf['news']['auth'], 27 | conf['news']['timezone']) 28 | self.db = dataBase(conf['db']['host'], conf['db']['user'], 29 | conf['db']['pass'], conf['db']['name'], self.rdr) 30 | self.mention_manager = mentionManager(self.db, self) 31 | 32 | self.url = 'https://api.telegram.org/bot%s/' % self.token 33 | self.setWebhook(conf['bot']['url'] + '%s' % self.token, 34 | conf['web']['pubkey']) 35 | self.registerHandlers() 36 | self.registerTexts() 37 | 38 | def updateTopics(self): 39 | while True: 40 | try: 41 | self.db.ping() 42 | entries = self.db.getTopics() 43 | posts = self.rdr.updatePosts(self.mention_manager) 44 | for entry in entries: 45 | cat_id = entry[0] 46 | relevant_posts = posts.get(cat_id, []) 47 | for user in entry[1]: 48 | for msg in relevant_posts: 49 | if not user[1] or not msg.isPlusOne(): 50 | self.sendArticle(user[0], msg) 51 | except Exception as e: 52 | logger.error('{} {}', e, datetime.datetime.now()) 53 | traceback.print_exc() 54 | time.sleep(10) 55 | 56 | def startHandler(self, data, reply=True): 57 | self.db.ping() 58 | res = self.db.registerUser(data['uid'], data['cid'], data['uname']) 59 | msg = self.texts['error'].format(data['uname']) 60 | if res == 0: 61 | msg = self.texts['welcome'].format(data['uname']) 62 | elif res == 1: 63 | msg = self.texts['registered'].format(data['uname']) 64 | self.db.setUserStatus(data['cid'], 1) 65 | 66 | if reply: 67 | self.sendMsg(data['cid'], msg) 68 | 69 | def handlePlusOne(self, data, no_plus_one, reply=True): 70 | self.db.ping() 71 | res = self.db.updateUser(data['uid'], no_plus_one) 72 | msg = self.texts['error'].format(data['uname']) 73 | if res == 0: 74 | msg = self.texts['updated'].format(data['uname']) 75 | if reply: 76 | self.sendMsg(data['cid'], msg) 77 | 78 | def noPlusOne(self, data, reply=True): 79 | self.handlePlusOne(data, True, reply) 80 | 81 | def yesPlusOne(self, data, reply=True): 82 | self.handlePlusOne(data, False, reply) 83 | 84 | def helpHandler(self, data): 85 | self.sendMsg(data['cid'], self.texts['help']) 86 | 87 | def addHandler(self, data): 88 | text = data['txt'].split(' ') 89 | if len(text) < 2: 90 | self.sendMsg(data['cid'], self.texts['invalid']) 91 | return 92 | 93 | topic = ' '.join(text[1:]) 94 | self.db.ping() 95 | res, added_topic = self.db.addTopic(data['cid'], topic) 96 | 97 | msg = self.texts['error'].format(data['uname']) 98 | if res == 0: 99 | msg = self.texts['added'].format(added_topic) 100 | elif res == 1: 101 | msg = self.texts['exists'].format(added_topic) 102 | elif res == 2: 103 | msg = self.texts['notfound'].format(topic) 104 | elif res == 4: 105 | msg = self.texts['ambiguous'].format(added_topic) 106 | 107 | self.sendMsg(data['cid'], msg) 108 | 109 | def announcementHandler(self, data): 110 | if data['uid'] != 147926496: 111 | return 112 | 113 | text = data['txt'].split(' ') 114 | if len(text) < 2: 115 | self.sendMsg(data['cid'], self.texts['invalid']) 116 | return 117 | text = text[1] 118 | 119 | for cid in self.db.getCids(): 120 | self.sendMsg(cid, text) 121 | 122 | def deleteHandler(self, data): 123 | text = data['txt'].split(' ') 124 | if len(text) < 2: 125 | self.sendMsg(data['cid'], self.texts['invalid']) 126 | return 127 | 128 | topic = ' '.join(text[1:]) 129 | self.db.ping() 130 | res, deleted_topic = self.db.deleteTopic(data['cid'], topic) 131 | 132 | msg = self.texts['error'].format(data['uname']) 133 | if res == 0: 134 | msg = self.texts['deleted'].format(deleted_topic) 135 | elif res == 1: 136 | msg = self.texts['notexists'].format(deleted_topic) 137 | elif res == 2: 138 | msg = self.texts['notfound'].format(topic) 139 | elif res == 4: 140 | msg = self.texts['ambiguous'].format(deleted_topic) 141 | 142 | self.sendMsg(data['cid'], msg) 143 | 144 | def listHandler(self, data): 145 | self.db.ping() 146 | res = self.db.getTopicsByCid(data['cid']) 147 | if res is None: 148 | msg = self.texts['error'].format(data['uname']) 149 | elif len(res) > 0: 150 | msg = '\r\n'.join(res) 151 | else: 152 | msg = "You haven't added any topics yet" 153 | 154 | self.sendMsg(data['cid'], msg) 155 | 156 | def listAll(self, data): 157 | msg = '\n'.join(self.rdr.categories.values()) 158 | self.sendMsg(data['cid'], msg) 159 | 160 | def addAliasHandler(self, data): 161 | cid = data['cid'] 162 | text = data['txt'].split(' ') 163 | if len(text) < 2: 164 | self.sendMsg(cid, self.texts['noalias']) 165 | return 166 | 167 | alias = text[1] 168 | if self.mention_manager.isStudentNumber(alias) is None: 169 | msg = self.texts['aliasnotvalid'].format(alias) 170 | else: 171 | self.db.ping() 172 | alias = self.mention_manager.getMinimalStudentNo(alias) 173 | res = self.db.addAlias(cid, alias) 174 | msg = self.texts['error'].format(data['uname']) 175 | if res == 0: 176 | msg = self.texts['aliasadded'].format(alias) 177 | elif res == 1: 178 | msg = self.texts['aliasexists'].format(alias) 179 | 180 | self.sendMsg(cid, msg) 181 | 182 | def showAliasesHandler(self, data): 183 | cid = data['cid'] 184 | 185 | self.db.ping() 186 | aliases = self.db.getAliases(cid) 187 | msg = '\r\n'.join(aliases) 188 | if len(aliases) == 0: 189 | msg = "You haven't added any aliases yet." 190 | self.sendMsg(cid, msg) 191 | 192 | def makeRequest(self, data, depth=1): 193 | try: 194 | r = requests.post(self.url, json=data) 195 | logger.debug('Request: {}', data) 196 | res = r.json() 197 | logger.debug('Response: {}', res) 198 | if res['ok']: 199 | return True, res 200 | if res['error_code'] == 403 and res['description'] in ( 201 | 'Forbidden: bot was blocked by the user', 202 | 'Forbidden: user is deactivated'): 203 | logger.info('Disabling user: {}', data['chat_id']) 204 | self.db.ping() 205 | self.db.setUserStatus(data['chat_id'], 0) 206 | return False, res 207 | if res['error_code'] == 429 and depth < self.MAX_RETRIES: 208 | params = res.get('parameters', {}) 209 | wait = params.get('retry_after') 210 | if wait: 211 | logger.info('Hit flood control, retrying in {} secs', wait) 212 | time.sleep(wait) 213 | return self.makeRequest(data, depth + 1) 214 | logger.error('Req {} failed with {}', data, res) 215 | return False, res 216 | except Exception as e: 217 | logger.error('{} {}', e, datetime.datetime.now()) 218 | traceback.print_exc() 219 | return False, res 220 | 221 | def setWebhook(self, url, pubkey): 222 | data = {} 223 | 224 | data['method'] = 'deleteWebhook' 225 | self.makeRequest(data) 226 | 227 | data['method'] = 'setWebhook' 228 | data['url'] = url 229 | files = {'certificate': open(pubkey, 'rb')} 230 | resp = requests.post(self.url, data=data, files=files).json() 231 | if resp['ok'] == False: 232 | logger.error('Failed to set webhook {} - {}', data, resp) 233 | sys.exit(1) 234 | 235 | def registerTexts(self): 236 | self.texts = {} 237 | self.texts[ 238 | 'welcome'] = """Hello {}, you can use /help to get a look at commands""" 239 | self.texts['invalid'] = """You forgot to mention the topic name""" 240 | self.texts[ 241 | 'registered'] = """Welcome back {}, type /help to take a look at commands""" 242 | self.texts['exists'] = """{} is already in your list""" 243 | self.texts['notexists'] = """{} does not seem to be in your list""" 244 | self.texts['notfound'] = """{} does not seem to be a valid topic""" 245 | self.texts['added'] = """{} has been successfully added to your list""" 246 | self.texts[ 247 | 'deleted'] = """{} has been successfully deleted from your list""" 248 | self.texts['error'] = """Sorry {}, something went wrong, please try again""" 249 | self.texts[ 250 | 'updated'] = """Well played! Your profile successfully updated, {}.""" 251 | self.texts[ 252 | 'help'] = """/add NEWSGROUP_SUFFIX - Adds first group matching the suffix to watchlist. 253 | 254 | /list - Lists groups in the watchlist. 255 | 256 | /delete NEWSGROUP - Deletes given group from watchlist. 257 | 258 | /help - Shows that list. 259 | 260 | /listall - Lists all possible groups to watch. 261 | 262 | /noplus1 - Enables +1 filtering.*Experimental*. 263 | 264 | /yesplus1 - Disables +1 filtering. 265 | 266 | /addalias STUDENT\_NO - Adds given student number to aliases to receive mention notifications. STUDENT\_NO must be in the form e?\d{6,7}.*Experimental*. 267 | 268 | /showaliases - Lists all registered aliases. 269 | 270 | For any bugs and hugs reach out to @kadircet or @hbostann 271 | Source is available at https://github.com/kadircet/COWNotifier 272 | """ 273 | self.texts[ 274 | 'aliasadded'] = """Alias {}, has been succesfully added to your mention list.""" 275 | self.texts[ 276 | 'aliasnotvalid'] = """Alias must be METU student number, complying with regex e?\d{{6,7}}, {} doesn't comply with it.""" 277 | self.texts[ 278 | 'aliasexists'] = """You already have {} as one of your aliases.""" 279 | self.texts['noalias'] = """You forgot to provide an alias.""" 280 | self.texts['ambiguous'] = """Ambiguous selection: {}.""" 281 | 282 | def registerHandlers(self): 283 | self.handlers = { 284 | 'add': self.addHandler, 285 | 'list': self.listHandler, 286 | 'start': self.startHandler, 287 | 'delete': self.deleteHandler, 288 | 'help': self.helpHandler, 289 | 'listall': self.listAll, 290 | 'no+1': self.noPlusOne, 291 | 'yes+1': self.yesPlusOne, 292 | 'noplus1': self.noPlusOne, 293 | 'yesplus1': self.yesPlusOne, 294 | 'announcement': self.announcementHandler, 295 | 'addalias': self.addAliasHandler, 296 | 'showaliases': self.showAliasesHandler 297 | } 298 | 299 | def sendArticle(self, cid, article): 300 | self.sendMsg(cid, article.parse(), escaped=True) 301 | for attachment in article.getAttachments(): 302 | self.sendAttachment(cid, attachment) 303 | 304 | def sendMsg(self, cid, text, escaped=False): 305 | if text is None: 306 | return 307 | if not escaped: 308 | text = escape(text) 309 | data = {} 310 | data['method'] = 'sendMessage' 311 | data['chat_id'] = cid 312 | data['parse_mode'] = 'MarkdownV2' 313 | data['disable_web_page_preview'] = True 314 | while len(text): 315 | data['text'] = text[:4096] 316 | text = text[4096:] 317 | status, res = self.makeRequest(data) 318 | # Failed to send message, try to recover. 319 | if res.get('error_code') == 400 and res.get( 320 | 'description', '').startswith('Bad Request: can\'t parse entities:'): 321 | data['text'] = unescape(data['text']) 322 | # In case of a bad markdown syntax, try with plaintext. 323 | del data['parse_mode'] 324 | status, _ = self.makeRequest(data) 325 | # Restore parse_mode for remaining blocks. 326 | data['parse_mode'] = 'MarkdownV2' 327 | 328 | if not status: 329 | break 330 | return 331 | 332 | def sendAttachment(self, cid, attachment): 333 | data = {} 334 | data['method'] = 'sendDocument' 335 | data['chat_id'] = cid 336 | data['document'] = attachment.url 337 | self.makeRequest(data) 338 | 339 | def parse(self, data): 340 | res = {} 341 | if 'message' in data: 342 | msg = data['message'] 343 | elif 'edited_message' in data: 344 | msg = data['edited_message'] 345 | if 'text' not in msg: 346 | msg['text'] = 'n n' 347 | cht = msg['chat'] 348 | frm = msg['from'] 349 | cid = cht['id'] 350 | uid = frm['id'] 351 | cmd = msg['text'].split(' ')[0] 352 | txt = msg['text'] 353 | uname = '' 354 | fname = '' 355 | lname = '' 356 | if 'username' in frm: 357 | uname = frm['username'] 358 | if 'first_name' in frm: 359 | fname = frm['first_name'] 360 | if 'last_name' in frm: 361 | lname = frm['last_name'] 362 | if len(uname) == 0: 363 | uname = fname + ' ' + lname 364 | 365 | res['msg'] = msg 366 | res['cht'] = cht 367 | res['frm'] = frm 368 | res['cid'] = cid 369 | res['uid'] = uid 370 | res['cmd'] = cmd 371 | res['txt'] = txt 372 | res['uname'] = uname 373 | return res 374 | 375 | def process(self, data): 376 | data = self.parse(data) 377 | cid = data['cid'] 378 | cmd = data['cmd'] 379 | 380 | if cmd[0] != '/': 381 | self.sendMsg(cid, "Master didn't make me a chatty bot!") 382 | return 383 | cmd = cmd[1:].lower() 384 | 385 | if cmd not in self.handlers: 386 | self.sendMsg(cid, "Master didn't implement it yet!") 387 | return 388 | 389 | if cmd != 'start': 390 | self.handlers['start'](data, False) 391 | self.handlers[cmd](data) 392 | 393 | def run(self): 394 | threading.Thread(target=self.updateTopics).start() 395 | while True: 396 | try: 397 | data = self.q.get() 398 | self.process(data) 399 | except Exception as e: 400 | logger.error('{}', e) 401 | traceback.print_exc() 402 | sys.stdout.flush() 403 | sys.stderr.flush() 404 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | {one line to give the program's name and a brief idea of what it does.} 635 | Copyright (C) {year} {name of author} 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | {project} Copyright (C) {year} {fullname} 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------