Skip to content
This repository was archived by the owner on Dec 16, 2019. It is now read-only.

Commit e636523

Browse files
committed
script to use with errbot's exec plugin
1 parent 8335376 commit e636523

File tree

3 files changed

+132
-0
lines changed

3 files changed

+132
-0
lines changed

data/config.default.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,4 +778,10 @@
778778
*/
779779
$allowUnittestMode = false;
780780

781+
/**
782+
* bookmark-bot email address mapping
783+
* Input address as key, user email as target
784+
*/
785+
$botMailMap = array();
786+
781787
?>

scripts/bookmark-bot.php

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
#!/usr/bin/env php
2+
<?php
3+
/**
4+
* CLI tool to add bookmarks to SemanticScuttle.
5+
* Intended as end point for a chat bot, e.g. "errbot-exec".
6+
*
7+
* Parameters:
8+
* 1. Message with bookmark url and tags
9+
* 2. E-Mail address of user
10+
*
11+
* You may map chat users to semanticscuttle email addresses
12+
* with the $botMailMap config variable
13+
*
14+
* @author Christian Weiske <[email protected]>
15+
* @link https://github.com/cweiske/errbot-exec
16+
*/
17+
require_once dirname(__FILE__) . '/../src/SemanticScuttle/header-standalone.php';
18+
19+
if ($argc < 3) {
20+
err('No message and user', 1);
21+
}
22+
$msg = $argv[1];
23+
$email = $argv[2];
24+
25+
if (preg_match('#(.+@.+)/.*#', $email, $matches)) {
26+
//xmpp user name with resource: [email protected]/client
27+
$email = $matches[1];
28+
}
29+
if (isset($botMailMap[$email])) {
30+
$email = $botMailMap[$email];
31+
}
32+
33+
function err($msg, $code)
34+
{
35+
echo $msg . "\n";
36+
exit($code);
37+
}
38+
39+
function getUserId($email)
40+
{
41+
$users = SemanticScuttle_Service_Factory::get('User');
42+
if (!$users->isValidEmail($email)) {
43+
err('Invalid e-mail address: ' . $email, 2);
44+
}
45+
$db = SemanticScuttle_Service_Factory::getDb();
46+
$res = $db->sql_query(
47+
'SELECT uId FROM '. $users->getTableName()
48+
. ' WHERE email = "' . $db->sql_escape($email) . '"'
49+
);
50+
$row = $db->sql_fetchrow($res);
51+
if (!is_array($row)) {
52+
err('User not found: ' . $email, 3);
53+
}
54+
return intval($row['uId']);
55+
}
56+
57+
function splitMsg($msg)
58+
{
59+
$bmUrl = $msg;
60+
$rest = '';
61+
if (strpos($msg, ' ') !== false) {
62+
list($bmUrl, $rest) = explode(' ', $msg, 2);
63+
}
64+
$parts = parse_url($bmUrl);
65+
if (!isset($parts['scheme'])) {
66+
err('Scheme missing in URL', 2);
67+
}
68+
if (!SemanticScuttle_Model_Bookmark::isValidUrl($bmUrl)) {
69+
err('Invalid bookmark URL', 2);
70+
}
71+
72+
$bmTags = array();
73+
$bmDesc = '';
74+
$rest = trim($rest);
75+
if (strlen($rest) && $rest{0} == '#') {
76+
//tags begin with '#'
77+
preg_match_all('/#([a-zA-Z0-9]+)/', $rest, $matches);
78+
$bmTags = $matches[1];
79+
foreach ($matches[0] as $tag) {
80+
if (substr($rest, 0, strlen($tag)) == $tag) {
81+
$rest = trim(substr($rest, strlen($tag)));
82+
}
83+
}
84+
$bmDesc = $rest;
85+
} else {
86+
//use rest as tags
87+
$bmTags = explode(' ', $rest);
88+
$bmTags = array_map('trim', $bmTags);
89+
}
90+
91+
return array($bmUrl, $bmTags, $bmDesc);
92+
}
93+
94+
$userId = getUserId($email);
95+
list($bmUrl, $bmTags, $bmDesc) = splitMsg($msg);
96+
97+
$bookmarks = SemanticScuttle_Service_Factory::get('Bookmark');
98+
if ($bookmarks->bookmarkExists($bmUrl)) {
99+
echo "URL already bookmarked.\n";
100+
exit(0);
101+
}
102+
103+
$urlhelper = new SemanticScuttle_UrlHelper();
104+
$bmTitle = $urlhelper->getTitle($bmUrl);
105+
106+
$id = $bookmarks->addBookmark(
107+
$bmUrl,
108+
$bmTitle,
109+
$bmDesc,
110+
null,
111+
SemanticScuttle_Model_Bookmark::SPUBLIC,
112+
$bmTags,
113+
null,
114+
null,
115+
true,
116+
false,
117+
$userId
118+
);
119+
if ($id === false) {
120+
err('Error adding bookmark', 10);
121+
} else {
122+
echo "Bookmark created.\n";
123+
exit(0);
124+
}
125+
?>

src/SemanticScuttle/header.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
require_once 'SemanticScuttle/Model/Bookmark.php';
106106
require_once 'SemanticScuttle/Model/UserArray.php';
107107
require_once 'SemanticScuttle/Model/User/SslClientCert.php';
108+
require_once 'SemanticScuttle/UrlHelper.php';
108109

109110
if (count($GLOBALS['serviceoverrides']) > 0
110111
&& !defined('UNIT_TEST_MODE')

0 commit comments

Comments
 (0)